Contract.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. package controllers
  2. import (
  3. "ERP_storage/Nats/NatsServer"
  4. "ERP_storage/conf"
  5. "ERP_storage/logs"
  6. "ERP_storage/models/Account"
  7. "ERP_storage/models/Basic"
  8. "ERP_storage/models/Contract"
  9. "ERP_storage/models/Stock"
  10. "fmt"
  11. userlibs "git.baozhida.cn/ERP_libs/User"
  12. "git.baozhida.cn/ERP_libs/lib"
  13. "github.com/beego/beego/v2/adapter/orm"
  14. beego "github.com/beego/beego/v2/server/web"
  15. "github.com/robfig/cron/v3"
  16. "math"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. type ContractController struct {
  22. beego.Controller
  23. User userlibs.User
  24. }
  25. func (c *ContractController) Prepare() {
  26. c.User = *Account.User_r
  27. }
  28. func (c *ContractController) Contract_GenT_number() {
  29. o := orm.NewOrm()
  30. ContractDao := Contract.NewContract(o)
  31. T_number := ""
  32. rand_x := 1
  33. for true {
  34. T_number = fmt.Sprintf("GZBZD-%s%03d", time.Now().Format("20060102"), rand_x)
  35. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  36. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  37. break
  38. }
  39. rand_x += 1
  40. }
  41. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  42. c.ServeJSON()
  43. return
  44. }
  45. func (c *ContractController) Contract_List() {
  46. // 分页参数 初始化
  47. page, _ := c.GetInt("page")
  48. if page < 1 {
  49. page = 1
  50. }
  51. page_z, _ := c.GetInt("page_z")
  52. if page_z < 1 {
  53. page_z = conf.Page_size
  54. }
  55. // 查询
  56. T_name := c.GetString("T_name")
  57. T_state, _ := c.GetInt("T_state")
  58. userList, _ := NatsServer.Read_User_List_All()
  59. Account.Read_User_All_Map(userList)
  60. ContractDao := Contract.NewContract(orm.NewOrm())
  61. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, T_state, 0, page, page_z)
  62. var r_jsons lib.R_JSONS
  63. r_jsons.Num = R_cnt
  64. r_jsons.Data = R_List
  65. r_jsons.Page = page
  66. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  67. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  68. c.ServeJSON()
  69. return
  70. }
  71. func (c *ContractController) Contract_User_List() {
  72. // 分页参数 初始化
  73. page, _ := c.GetInt("page")
  74. if page < 1 {
  75. page = 1
  76. }
  77. page_z, _ := c.GetInt("page_z")
  78. if page_z < 1 {
  79. page_z = conf.Page_size
  80. }
  81. // 查询
  82. T_name := c.GetString("T_name")
  83. T_state, _ := c.GetInt("T_state")
  84. userList, _ := NatsServer.Read_User_List_All()
  85. Account.Read_User_All_Map(userList)
  86. ContractDao := Contract.NewContract(orm.NewOrm())
  87. R_List, R_cnt := ContractDao.Read_Contract_List(c.User.T_uuid, T_name, T_state, 0, page, page_z)
  88. var r_jsons lib.R_JSONS
  89. r_jsons.Num = R_cnt
  90. r_jsons.Data = R_List
  91. r_jsons.Page = page
  92. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  93. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  94. c.ServeJSON()
  95. return
  96. }
  97. func (c *ContractController) Contract_Get() {
  98. // 查询
  99. T_number := c.GetString("T_number")
  100. o := orm.NewOrm()
  101. ContractDao := Contract.NewContract(o)
  102. DeviceDao := Stock.NewDevice(o)
  103. ContractProductDao := Contract.NewContractProduct(o)
  104. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  105. if err != nil {
  106. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  107. c.ServeJSON()
  108. return
  109. }
  110. productList := ContractProductDao.Read_ContractProduct_List(contract.T_number)
  111. var pList []Contract.ContractProduct_R
  112. for _, v := range productList {
  113. p := Contract.ContractProductToContractProduct_R(v)
  114. p.T_device_list, _ = DeviceDao.Read_DeviceSn_List(T_number, v.T_product_id, "", "")
  115. pList = append(pList, p)
  116. }
  117. userList, _ := NatsServer.Read_User_List_All()
  118. Account.Read_User_All_Map(userList)
  119. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Contract.ContractToContract_Detail(contract, pList)}
  120. c.ServeJSON()
  121. return
  122. }
  123. func (c *ContractController) Contract_Add() {
  124. T_number := c.GetString("T_number")
  125. T_customer := c.GetString("T_customer")
  126. T_money, _ := c.GetFloat("T_money") // 总价
  127. T_discount, _ := c.GetFloat("T_discount") // 优惠价
  128. T_date := c.GetString("T_date")
  129. T_product := c.GetString("T_product")
  130. T_remark := c.GetString("T_remark")
  131. T_pdf := c.GetString("T_pdf")
  132. T_project := c.GetString("T_project")
  133. T_recoveries := c.GetString("T_recoveries")
  134. T_invoice := c.GetString("T_invoice")
  135. T_submit := c.GetString("T_submit")
  136. if len(T_submit) == 0 {
  137. T_submit = c.User.T_uuid
  138. }
  139. var_ := Contract.Contract{
  140. T_number: T_number,
  141. T_customer: T_customer,
  142. T_money: float32(T_money),
  143. T_discount: float32(T_discount),
  144. T_project: T_project,
  145. T_type: 1, //合同类型 1-销售合同 2-验证合同
  146. T_date: T_date,
  147. T_out: 1,
  148. T_State: 3,
  149. T_remark: T_remark,
  150. T_pdf: T_pdf,
  151. T_submit: T_submit,
  152. T_recoveries: T_recoveries,
  153. T_invoice: T_invoice,
  154. }
  155. o := orm.NewOrm()
  156. o.Begin()
  157. ContractDao := Contract.NewContract(o)
  158. ContractProductDao := Contract.NewContractProduct(o)
  159. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  160. if err == nil {
  161. o.Rollback()
  162. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  163. c.ServeJSON()
  164. return
  165. }
  166. _, err = ContractDao.Add_Contract(var_)
  167. if err != nil {
  168. o.Rollback()
  169. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  170. c.ServeJSON()
  171. return
  172. }
  173. if len(T_product) == 0 {
  174. o.Rollback()
  175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "销售合同产品明细不能为空"}
  176. c.ServeJSON()
  177. return
  178. }
  179. productList := lib.SplitString(T_product, "|")
  180. for _, v := range productList {
  181. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  182. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  183. contractProduct := Contract.ContractProduct{
  184. T_contract_number: T_number,
  185. T_product_id: product_id,
  186. T_product_total: num,
  187. T_product_out: 0, // 已出库数量
  188. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  189. }
  190. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  191. if err != nil {
  192. o.Rollback()
  193. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  194. c.ServeJSON()
  195. return
  196. }
  197. }
  198. o.Commit()
  199. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "添加", var_)
  200. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  201. c.ServeJSON()
  202. return
  203. }
  204. func (c *ContractController) Contract_Approval() {
  205. T_number := c.GetString("T_number")
  206. T_state, _ := c.GetInt("T_state")
  207. o := orm.NewOrm()
  208. ContractDao := Contract.NewContract(o)
  209. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  210. if err != nil {
  211. o.Rollback()
  212. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  213. c.ServeJSON()
  214. return
  215. }
  216. // 1-已通过 2-未通过
  217. contract.T_State = T_state
  218. contract.T_approver = c.User.T_uuid
  219. err = ContractDao.Update_Contract(contract, "T_State", "T_approver")
  220. if err != nil {
  221. o.Rollback()
  222. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  223. c.ServeJSON()
  224. return
  225. }
  226. if T_state == 1 {
  227. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核已通过", T_number), conf.ContractApprovalUrl)
  228. }
  229. if T_state == 2 {
  230. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核未通过", T_number), conf.ContractApprovalUrl)
  231. }
  232. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "审核", contract)
  233. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  234. c.ServeJSON()
  235. return
  236. }
  237. func (c *ContractController) Contract_Edit() {
  238. T_number := c.GetString("T_number")
  239. T_customer := c.GetString("T_customer")
  240. T_money, _ := c.GetFloat("T_money")
  241. T_date := c.GetString("T_date")
  242. T_product := c.GetString("T_product")
  243. T_remark := c.GetString("T_remark")
  244. T_pdf := c.GetString("T_pdf")
  245. T_project := c.GetString("T_project")
  246. T_recoveries := c.GetString("T_recoveries")
  247. T_invoice := c.GetString("T_invoice")
  248. T_submit := c.GetString("T_submit")
  249. o := orm.NewOrm()
  250. o.Begin()
  251. ContractDao := Contract.NewContract(o)
  252. ContractProductDao := Contract.NewContractProduct(o)
  253. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  254. if err != nil {
  255. o.Rollback()
  256. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  257. c.ServeJSON()
  258. return
  259. }
  260. if len(T_customer) > 0 {
  261. contract.T_customer = T_customer
  262. }
  263. if T_money > 0 {
  264. contract.T_money = float32(T_money)
  265. }
  266. if len(T_date) > 0 {
  267. contract.T_date = T_date
  268. }
  269. if len(T_remark) > 0 {
  270. contract.T_remark = T_remark
  271. }
  272. if len(T_pdf) > 0 {
  273. contract.T_pdf = T_pdf
  274. }
  275. if len(T_project) > 0 {
  276. contract.T_project = T_project
  277. }
  278. if len(T_recoveries) > 0 {
  279. contract.T_recoveries = T_recoveries
  280. }
  281. if len(T_invoice) > 0 {
  282. contract.T_invoice = T_invoice
  283. }
  284. if len(T_submit) > 0 {
  285. contract.T_submit = T_submit
  286. }
  287. //1-已通过 2-未通过 3-待审核 合同状态为未通过,修改之后将状态更改为待审核
  288. if contract.T_State == 2 {
  289. contract.T_State = 3
  290. }
  291. err = ContractDao.Update_Contract(contract, "T_customer", "T_money", "T_date", "T_remark", "T_pdf", "T_State", "T_project", "T_recoveries", "T_invoice", "T_submit")
  292. if err != nil {
  293. o.Rollback()
  294. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  295. c.ServeJSON()
  296. return
  297. }
  298. //合同类型 1-销售合同 2-验证合同
  299. if contract.T_type == 2 {
  300. o.Commit()
  301. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "修改", contract)
  302. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  303. c.ServeJSON()
  304. return
  305. }
  306. if len(T_product) == 0 {
  307. o.Rollback()
  308. c.Data["json"] = lib.JSONS{Code: 202, Msg: "销售合同产品明细不能为空"}
  309. c.ServeJSON()
  310. return
  311. }
  312. productList := lib.SplitString(T_product, "|")
  313. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  314. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  315. // 检查编辑时需要修改的产品
  316. for k, v := range needToDeleteList {
  317. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  318. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  319. if v > 0 {
  320. o.Rollback()
  321. product, _ := Basic.Read_Product_ById(productId)
  322. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  323. c.ServeJSON()
  324. return
  325. }
  326. cp := Contract.ContractProduct{Id: id}
  327. err = ContractProductDao.Delete_ContractProduct(cp)
  328. if err != nil {
  329. o.Rollback()
  330. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  331. c.ServeJSON()
  332. return
  333. }
  334. }
  335. //for _, v := range productList {
  336. // product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  337. // num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  338. //
  339. // var cp Contract.ContractProduct
  340. // cp, err = ContractProductDao.Read_ContractProduct_ByT_number_T_product_id(T_number, product_id)
  341. // if err != nil {
  342. // if err.Error() == orm.ErrNoRows.Error() {
  343. // contractProduct := Contract.ContractProduct{
  344. // T_contract_number: T_number,
  345. // T_product_id: product_id,
  346. // T_product_total: num,
  347. // T_product_out: 0, // 已出库数量
  348. // T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  349. // }
  350. // _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  351. // if err != nil {
  352. // o.Rollback()
  353. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  354. // c.ServeJSON()
  355. // return
  356. // }
  357. //
  358. // } else {
  359. // o.Rollback()
  360. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  361. // c.ServeJSON()
  362. // return
  363. // }
  364. // }
  365. // // 修改产品总数量
  366. // if cp.T_product_total == num {
  367. // continue
  368. // }
  369. // cp.T_product_total = num
  370. // err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  371. // if err != nil {
  372. // o.Rollback()
  373. // c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  374. // c.ServeJSON()
  375. // return
  376. // }
  377. //
  378. //}
  379. for _, v := range productList {
  380. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  381. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  382. oldContractProduct, ok := allList[product_id]
  383. if !ok {
  384. contractProduct := Contract.ContractProduct{
  385. T_contract_number: T_number,
  386. T_product_id: product_id,
  387. T_product_total: num,
  388. T_product_out: 0, // 已出库数量
  389. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  390. }
  391. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  392. if err != nil {
  393. o.Rollback()
  394. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  395. c.ServeJSON()
  396. return
  397. } else {
  398. continue
  399. }
  400. }
  401. // 修改产品总数量
  402. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  403. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  404. if oldNum == num {
  405. continue
  406. }
  407. cp := Contract.ContractProduct{Id: contractProductId, T_product_total: num}
  408. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  409. if err != nil {
  410. o.Rollback()
  411. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  412. c.ServeJSON()
  413. return
  414. }
  415. }
  416. o.Commit()
  417. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "修改", contract)
  418. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  419. c.ServeJSON()
  420. return
  421. }
  422. func (c *ContractController) Contract_Del() {
  423. T_number := c.GetString("T_number")
  424. o := orm.NewOrm()
  425. o.Begin()
  426. ContractDao := Contract.NewContract(o)
  427. ContractProductDao := Contract.NewContractProduct(o)
  428. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  429. if err != nil {
  430. o.Rollback()
  431. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  432. c.ServeJSON()
  433. return
  434. }
  435. //1-已通过 2-未通过 3-待审核 审核状态
  436. if contract.T_State == 1 {
  437. o.Rollback()
  438. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同已审核通过,禁止删除!"}
  439. c.ServeJSON()
  440. return
  441. }
  442. err = ContractDao.Delete_Contract(contract)
  443. if err != nil {
  444. o.Rollback()
  445. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  446. c.ServeJSON()
  447. return
  448. }
  449. //合同类型 1-销售合同 2-验证合同
  450. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  451. for _, v := range cpList {
  452. err = ContractProductDao.Delete_ContractProduct(v)
  453. if err != nil {
  454. o.Rollback()
  455. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  456. c.ServeJSON()
  457. return
  458. }
  459. }
  460. o.Commit()
  461. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "删除", T_number)
  462. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  463. c.ServeJSON()
  464. return
  465. }
  466. func (c *ContractController) Contract_List_For_Out() {
  467. // 分页参数 初始化
  468. page, _ := c.GetInt("page")
  469. if page < 1 {
  470. page = 1
  471. }
  472. page_z, _ := c.GetInt("page_z")
  473. if page_z < 1 {
  474. page_z = conf.Page_size
  475. }
  476. // 查询
  477. T_name := c.GetString("T_name")
  478. userList, _ := NatsServer.Read_User_List_All()
  479. Account.Read_User_All_Map(userList)
  480. ContractDao := Contract.NewContract(orm.NewOrm())
  481. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, 1, 1, page, page_z)
  482. var r_jsons lib.R_JSONS
  483. r_jsons.Num = R_cnt
  484. r_jsons.Data = R_List
  485. r_jsons.Page = page
  486. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  487. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  488. c.ServeJSON()
  489. return
  490. }
  491. // 合同下的产品列表 出库时使用
  492. func (c *ContractController) Contract_Product_List() {
  493. // 查询
  494. T_number := c.GetString("T_number")
  495. T_depot_id, _ := c.GetInt("T_depot_id")
  496. o := orm.NewOrm()
  497. ContractDao := Contract.NewContract(o)
  498. ContractProductDao := Contract.NewContractProduct(o)
  499. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  500. if err != nil {
  501. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  502. c.ServeJSON()
  503. return
  504. }
  505. productList := ContractProductDao.Read_ContractProduct_OutList(contract.T_number)
  506. Stock.Read_Stock_All_Map(T_depot_id)
  507. var pList []Contract.ContractProduct_Out
  508. for _, v := range productList {
  509. p := Contract.ContractProductToContractProduct_Out(v)
  510. p.T_stock_total = Stock.Read_Stock_Get(T_depot_id, p.T_product_id)
  511. pList = append(pList, p)
  512. }
  513. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pList}
  514. c.ServeJSON()
  515. return
  516. }
  517. // 检查需要删除的合同产品列表
  518. func Check_NeedToDelete_Product_List(list []string, newList []Contract.ContractProduct) (needToDelete map[string]int, all map[int]string) {
  519. var productMap = map[int]int{}
  520. needToDelete = make(map[string]int)
  521. all = make(map[int]string)
  522. for _, v := range list {
  523. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  524. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  525. productMap[product_id] = num
  526. }
  527. for _, product := range newList {
  528. all[product.T_product_id] = fmt.Sprintf("%d-%d", product.Id, product.T_product_total)
  529. _, ok := productMap[product.T_product_id]
  530. if !ok {
  531. id := fmt.Sprintf("%d-%d", product.Id, product.T_product_id)
  532. needToDelete[id] = product.T_product_out
  533. }
  534. }
  535. return
  536. }
  537. // 验证合同
  538. func (c *ContractController) VerifyContract_List() {
  539. // 分页参数 初始化
  540. page, _ := c.GetInt("page")
  541. if page < 1 {
  542. page = 1
  543. }
  544. page_z, _ := c.GetInt("page_z")
  545. if page_z < 1 {
  546. page_z = conf.Page_size
  547. }
  548. T_customer_id := c.GetString("T_customer_id")
  549. // 查询
  550. T_name := c.GetString("T_name")
  551. userList, _ := NatsServer.Read_User_List_All()
  552. Account.Read_User_All_Map(userList)
  553. ContractDao := Contract.NewContract(orm.NewOrm())
  554. R_List, R_cnt := ContractDao.Read_VerifyContract_List(T_name, T_customer_id, page, page_z)
  555. var r_jsons lib.R_JSONS
  556. r_jsons.Num = R_cnt
  557. r_jsons.Data = R_List
  558. r_jsons.Page = page
  559. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  560. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  561. c.ServeJSON()
  562. return
  563. }
  564. // 验证合同呢添加
  565. func (c *ContractController) VerifyContract_Add() {
  566. T_number := c.GetString("T_number")
  567. T_customer_id := c.GetString("T_customer_id")
  568. T_money, _ := c.GetFloat("T_money")
  569. T_discount, _ := c.GetFloat("T_discount")
  570. T_date := c.GetString("T_date")
  571. T_product := c.GetString("T_product")
  572. T_submit := c.GetString("T_submit")
  573. T_remark := c.GetString("T_remark")
  574. T_pdf := c.GetString("T_pdf")
  575. T_project := c.GetString("T_project")
  576. T_recoveries := c.GetString("T_recoveries")
  577. T_invoice := c.GetString("T_invoice")
  578. T_start_date := c.GetString("T_start_date")
  579. T_end_date := c.GetString("T_end_date")
  580. var_ := Contract.Contract{
  581. T_number: T_number,
  582. T_customer_id: T_customer_id,
  583. T_money: float32(T_money),
  584. T_discount: float32(T_discount),
  585. T_project: T_project,
  586. T_type: 2, //合同类型 1-销售合同 2-验证合同
  587. T_date: T_date,
  588. T_State: 1,
  589. T_remark: T_remark,
  590. T_pdf: T_pdf,
  591. T_submit: T_submit,
  592. T_recoveries: T_recoveries,
  593. T_invoice: T_invoice,
  594. T_start_date: T_start_date,
  595. T_end_date: T_end_date,
  596. }
  597. o := orm.NewOrm()
  598. o.Begin()
  599. ContractDao := Contract.NewContract(o)
  600. ContractProductDao := Contract.NewContractProduct(o)
  601. _, err := ContractDao.Read_Contract_ByT_date(T_customer_id, T_start_date, T_end_date)
  602. if err == nil {
  603. o.Rollback()
  604. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同签约时间重复!"}
  605. c.ServeJSON()
  606. return
  607. }
  608. _, err = ContractDao.Read_Contract_ByT_number(T_number)
  609. if err == nil {
  610. o.Rollback()
  611. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  612. c.ServeJSON()
  613. return
  614. }
  615. ContractID, err := ContractDao.Add_Contract(var_)
  616. if err != nil {
  617. o.Rollback()
  618. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  619. c.ServeJSON()
  620. return
  621. }
  622. if len(T_product) == 0 {
  623. o.Rollback()
  624. c.Data["json"] = lib.JSONS{Code: 202, Msg: "验证合同明细不能为空"}
  625. c.ServeJSON()
  626. return
  627. }
  628. productList := lib.SplitString(T_product, "|")
  629. ContractOut := 0
  630. for _, v := range productList {
  631. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  632. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  633. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  634. product, _ := Basic.Read_Product_ById(product_id)
  635. contractProduct := Contract.ContractProduct{
  636. T_contract_number: T_number,
  637. T_product_id: product_id,
  638. T_product_total: num,
  639. T_product_out: 0, // 已出库数量
  640. T_price: float32(price), // 已出库数量
  641. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  642. }
  643. if product.T_class < 0 {
  644. contractProduct.T_State = 3
  645. }
  646. if product.T_class == 0 {
  647. ContractOut = 1
  648. }
  649. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  650. if err != nil {
  651. o.Rollback()
  652. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  653. c.ServeJSON()
  654. return
  655. }
  656. }
  657. o.Commit()
  658. o2 := orm.NewOrm()
  659. ContractDao2 := Contract.NewContract(o2)
  660. if ContractOut == 1 {
  661. err = ContractDao2.Update_Contract(Contract.Contract{Id: int(ContractID), T_out: ContractOut}, "T_out")
  662. if err != nil {
  663. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  664. c.ServeJSON()
  665. return
  666. }
  667. }
  668. err = Update_VerifyContract_State(T_customer_id, "ADD")
  669. if err != nil {
  670. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  671. c.ServeJSON()
  672. return
  673. }
  674. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加", var_)
  675. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  676. c.ServeJSON()
  677. return
  678. }
  679. // 验证合同呢修改
  680. func (c *ContractController) VerifyContract_Edit() {
  681. T_number := c.GetString("T_number")
  682. T_money, _ := c.GetFloat("T_money")
  683. T_date := c.GetString("T_date")
  684. T_product := c.GetString("T_product")
  685. T_remark := c.GetString("T_remark")
  686. T_pdf := c.GetString("T_pdf")
  687. T_project := c.GetString("T_project")
  688. T_recoveries := c.GetString("T_recoveries")
  689. T_invoice := c.GetString("T_invoice")
  690. T_start_date := c.GetString("T_start_date")
  691. T_end_date := c.GetString("T_end_date")
  692. T_submit := c.GetString("T_submit")
  693. o := orm.NewOrm()
  694. o.Begin()
  695. ContractDao := Contract.NewContract(o)
  696. ContractProductDao := Contract.NewContractProduct(o)
  697. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  698. if err != nil || contract.T_type == 1 {
  699. o.Rollback()
  700. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  701. c.ServeJSON()
  702. return
  703. }
  704. if T_money > 0 {
  705. contract.T_money = float32(T_money)
  706. }
  707. if len(T_date) > 0 {
  708. contract.T_date = T_date
  709. }
  710. if len(T_remark) > 0 {
  711. contract.T_remark = T_remark
  712. }
  713. if len(T_pdf) > 0 {
  714. contract.T_pdf = T_pdf
  715. }
  716. if len(T_project) > 0 {
  717. contract.T_project = T_project
  718. }
  719. if len(T_recoveries) > 0 {
  720. contract.T_recoveries = T_recoveries
  721. }
  722. if len(T_invoice) > 0 {
  723. contract.T_invoice = T_invoice
  724. }
  725. if len(T_start_date) > 0 {
  726. contract.T_start_date = T_start_date
  727. }
  728. if len(T_end_date) > 0 {
  729. contract.T_end_date = T_end_date
  730. }
  731. if len(T_submit) > 0 {
  732. contract.T_submit = T_submit
  733. }
  734. err = ContractDao.Update_Contract(contract, "T_money", "T_date", "T_remark", "T_pdf", "T_State", "T_project", "T_recoveries", "T_invoice", "T_start_date", "T_end_date", "T_submit")
  735. if err != nil {
  736. o.Rollback()
  737. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  738. c.ServeJSON()
  739. return
  740. }
  741. if len(T_product) == 0 {
  742. o.Rollback()
  743. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  744. c.ServeJSON()
  745. return
  746. }
  747. productList := lib.SplitString(T_product, "|")
  748. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  749. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  750. // 检查编辑时需要修改的产品
  751. for k, v := range needToDeleteList {
  752. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  753. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  754. if v > 0 {
  755. o.Rollback()
  756. product, _ := Basic.Read_Product_ById(productId)
  757. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  758. c.ServeJSON()
  759. return
  760. }
  761. cp := Contract.ContractProduct{Id: id}
  762. err = ContractProductDao.Delete_ContractProduct(cp)
  763. if err != nil {
  764. o.Rollback()
  765. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  766. c.ServeJSON()
  767. return
  768. }
  769. }
  770. for _, v := range productList {
  771. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  772. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  773. oldContractProduct, ok := allList[product_id]
  774. if !ok {
  775. contractProduct := Contract.ContractProduct{
  776. T_contract_number: T_number,
  777. T_product_id: product_id,
  778. T_product_total: num,
  779. T_product_out: 0, // 已出库数量
  780. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  781. }
  782. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  783. if err != nil {
  784. o.Rollback()
  785. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  786. c.ServeJSON()
  787. return
  788. } else {
  789. continue
  790. }
  791. }
  792. // 修改产品总数量
  793. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  794. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  795. if oldNum == num {
  796. continue
  797. }
  798. cp := Contract.ContractProduct{Id: contractProductId, T_product_total: num}
  799. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  800. if err != nil {
  801. o.Rollback()
  802. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  803. c.ServeJSON()
  804. return
  805. }
  806. }
  807. o.Commit()
  808. err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
  809. if err != nil {
  810. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  811. c.ServeJSON()
  812. return
  813. }
  814. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
  815. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  816. c.ServeJSON()
  817. return
  818. }
  819. func (c *ContractController) VerifyContract_Del() {
  820. T_number := c.GetString("T_number")
  821. o := orm.NewOrm()
  822. o.Begin()
  823. ContractDao := Contract.NewContract(o)
  824. ContractProductDao := Contract.NewContractProduct(o)
  825. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  826. if err != nil {
  827. o.Rollback()
  828. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  829. c.ServeJSON()
  830. return
  831. }
  832. err = ContractDao.Delete_Contract(contract)
  833. if err != nil {
  834. o.Rollback()
  835. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  836. c.ServeJSON()
  837. return
  838. }
  839. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  840. for _, v := range cpList {
  841. err = ContractProductDao.Delete_ContractProduct(v)
  842. if err != nil {
  843. o.Rollback()
  844. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  845. c.ServeJSON()
  846. return
  847. }
  848. }
  849. o.Commit()
  850. err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
  851. if err != nil {
  852. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
  853. c.ServeJSON()
  854. return
  855. }
  856. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
  857. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  858. c.ServeJSON()
  859. return
  860. }
  861. func Update_VerifyContract_State(T_customer_id, flag string) error {
  862. o := orm.NewOrm()
  863. ContractDao := Contract.NewContract(o)
  864. VerifyContractDao := Contract.NewVerifyContract(o)
  865. vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
  866. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  867. if err != nil {
  868. return err
  869. }
  870. if vcn.Id == 0 {
  871. vc.T_sign_times = 0
  872. vc.T_start_date = ""
  873. vc.T_end_date = ""
  874. vc.T_State = 1
  875. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  876. return err
  877. }
  878. if flag == "ADD" {
  879. vc.T_sign_times += 1
  880. } else if flag == "DELETE" {
  881. vc.T_sign_times -= 1
  882. }
  883. if len(vc.T_start_date) == 0 {
  884. vc.T_start_date = vcn.T_start_date
  885. }
  886. if vc.T_end_date != vcn.T_end_date {
  887. vc.T_end_date = vcn.T_end_date
  888. }
  889. nowTime := time.Now()
  890. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  891. end, _ := lib.DateStrToTime(vcn.T_end_date)
  892. now2Month := now.AddDate(0, 2, 0)
  893. // 1-未签约 2-已作废 3-已签约 4-即将到期
  894. state := 1
  895. // 结束时间<今天 已过期
  896. if end.Before(now) {
  897. state = 2
  898. } else {
  899. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  900. if end.Before(now2Month) || end.Equal(now) {
  901. state = 4
  902. NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
  903. }
  904. // 结束时间>今天+2月 已签约
  905. if end.After(now2Month) || end.Equal(now2Month) {
  906. state = 3
  907. }
  908. }
  909. vc.T_State = state
  910. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  911. return err
  912. }
  913. // 验证合同-添加客户
  914. func (c *ContractController) VerifyContract_Add_Customer() {
  915. T_customer := c.GetString("T_customer")
  916. o := orm.NewOrm()
  917. VerifyContractDao := Contract.NewVerifyContract(o)
  918. T_number := ""
  919. for true {
  920. rand_x := 1
  921. T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
  922. _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
  923. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  924. break
  925. }
  926. rand_x += 1
  927. }
  928. var_ := Contract.VerifyContract{
  929. T_customer: T_customer,
  930. T_customer_id: T_number,
  931. T_State: 1,
  932. }
  933. _, err := VerifyContractDao.Add_VerifyContract(var_)
  934. if err != nil {
  935. o.Rollback()
  936. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  937. c.ServeJSON()
  938. return
  939. }
  940. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
  941. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  942. c.ServeJSON()
  943. return
  944. }
  945. // 验证合同-编辑客户
  946. func (c *ContractController) VerifyContract_Update_Customer() {
  947. T_customer_id := c.GetString("T_customer_id")
  948. T_customer := c.GetString("T_customer")
  949. o := orm.NewOrm()
  950. VerifyContractDao := Contract.NewVerifyContract(o)
  951. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  952. vc.T_customer = T_customer
  953. err = VerifyContractDao.Update_VerifyContract(vc)
  954. if err != nil {
  955. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  956. c.ServeJSON()
  957. return
  958. }
  959. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
  960. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
  961. c.ServeJSON()
  962. return
  963. }
  964. // 验证合同-客户列表
  965. func (c *ContractController) VerifyContract_Customer_List() {
  966. // 分页参数 初始化
  967. page, _ := c.GetInt("page")
  968. if page < 1 {
  969. page = 1
  970. }
  971. page_z, _ := c.GetInt("page_z")
  972. if page_z < 1 {
  973. page_z = conf.Page_size
  974. }
  975. // 查询
  976. T_name := c.GetString("T_name")
  977. T_state, _ := c.GetInt("T_state")
  978. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  979. R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
  980. var r_jsons lib.R_JSONS
  981. r_jsons.Num = R_cnt
  982. r_jsons.Data = R_List
  983. r_jsons.Page = page
  984. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  985. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  986. c.ServeJSON()
  987. return
  988. }
  989. func Cron_VerifyContract() {
  990. //创建一个定时任务对象
  991. c := cron.New(cron.WithSeconds())
  992. //给对象增加定时任务
  993. //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
  994. c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
  995. //启动定时任务
  996. c.Start()
  997. defer c.Stop()
  998. //查询语句,阻塞,让main函数不退出,保持程序运行
  999. select {}
  1000. }
  1001. // 修改合同状态
  1002. func Cron_VerifyContract_ChangeState() {
  1003. T_date := time.Now().Format("2006-01-02")
  1004. logs.Info("开始处理" + T_date + "验证合同状态")
  1005. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1006. R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
  1007. for _, v := range R_List {
  1008. err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
  1009. if err != nil {
  1010. logs.Error("修改合同状态失败")
  1011. }
  1012. }
  1013. }