Contract.go 34 KB

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