Contract.go 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. _, 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_date := c.GetString("T_date")
  643. T_product := c.GetString("T_product")
  644. T_remark := c.GetString("T_remark")
  645. T_pdf := c.GetString("T_pdf")
  646. T_project := c.GetString("T_project")
  647. T_recoveries := c.GetString("T_recoveries")
  648. T_invoice := c.GetString("T_invoice")
  649. T_start_date := c.GetString("T_start_date")
  650. T_end_date := c.GetString("T_end_date")
  651. T_submit := c.GetString("T_submit")
  652. o := orm.NewOrm()
  653. o.Begin()
  654. ContractDao := Contract.NewContract(o)
  655. ContractProductDao := Contract.NewContractProduct(o)
  656. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  657. if err != nil || contract.T_type == 1 {
  658. o.Rollback()
  659. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  660. c.ServeJSON()
  661. return
  662. }
  663. if T_money > 0 {
  664. contract.T_money = float32(T_money)
  665. }
  666. if len(T_date) > 0 {
  667. contract.T_date = T_date
  668. }
  669. if len(T_remark) > 0 {
  670. contract.T_remark = T_remark
  671. }
  672. if len(T_pdf) > 0 {
  673. contract.T_pdf = T_pdf
  674. }
  675. if len(T_project) > 0 {
  676. contract.T_project = T_project
  677. }
  678. if len(T_recoveries) > 0 {
  679. contract.T_recoveries = T_recoveries
  680. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  681. }
  682. if len(T_invoice) > 0 {
  683. contract.T_invoice = T_invoice
  684. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  685. }
  686. if len(T_start_date) > 0 {
  687. contract.T_start_date = T_start_date
  688. }
  689. if len(T_end_date) > 0 {
  690. contract.T_end_date = T_end_date
  691. }
  692. if len(T_submit) > 0 {
  693. contract.T_submit = T_submit
  694. }
  695. err = ContractDao.Update_Contract(contract, "T_money", "T_date", "T_remark", "T_pdf", "T_State", "T_project",
  696. "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_start_date", "T_end_date", "T_submit")
  697. if err != nil {
  698. o.Rollback()
  699. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  700. c.ServeJSON()
  701. return
  702. }
  703. if len(T_product) == 0 {
  704. o.Rollback()
  705. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  706. c.ServeJSON()
  707. return
  708. }
  709. productList := lib.SplitString(T_product, "|")
  710. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  711. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  712. // 检查编辑时需要修改的产品
  713. for k, v := range needToDeleteList {
  714. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  715. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  716. if v > 0 {
  717. o.Rollback()
  718. product, _ := Basic.Read_Product_ById(productId)
  719. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  720. c.ServeJSON()
  721. return
  722. }
  723. cp := Contract.ContractProduct{Id: id}
  724. err = ContractProductDao.Delete_ContractProduct(cp)
  725. if err != nil {
  726. o.Rollback()
  727. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  728. c.ServeJSON()
  729. return
  730. }
  731. }
  732. for _, v := range productList {
  733. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  734. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  735. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  736. oldContractProduct, ok := allList[product_id]
  737. if !ok {
  738. contractProduct := Contract.ContractProduct{
  739. T_contract_number: T_number,
  740. T_product_id: product_id,
  741. T_product_total: num,
  742. T_product_out: 0, // 已出库数量
  743. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  744. }
  745. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  746. if err != nil {
  747. o.Rollback()
  748. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  749. c.ServeJSON()
  750. return
  751. } else {
  752. continue
  753. }
  754. }
  755. // 修改产品总数量
  756. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  757. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  758. if oldNum == num {
  759. continue
  760. }
  761. cp := Contract.ContractProduct{
  762. Id: contractProductId,
  763. T_product_total: num,
  764. T_price: float32(price), // 价格
  765. }
  766. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  767. if err != nil {
  768. o.Rollback()
  769. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  770. c.ServeJSON()
  771. return
  772. }
  773. }
  774. o.Commit()
  775. err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
  776. if err != nil {
  777. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  778. c.ServeJSON()
  779. return
  780. }
  781. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
  782. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  783. c.ServeJSON()
  784. return
  785. }
  786. func (c *ContractController) VerifyContract_Del() {
  787. T_number := c.GetString("T_number")
  788. o := orm.NewOrm()
  789. o.Begin()
  790. ContractDao := Contract.NewContract(o)
  791. ContractProductDao := Contract.NewContractProduct(o)
  792. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  793. if err != nil {
  794. o.Rollback()
  795. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  796. c.ServeJSON()
  797. return
  798. }
  799. err = ContractDao.Delete_Contract(contract)
  800. if err != nil {
  801. o.Rollback()
  802. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  803. c.ServeJSON()
  804. return
  805. }
  806. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  807. for _, v := range cpList {
  808. err = ContractProductDao.Delete_ContractProduct(v)
  809. if err != nil {
  810. o.Rollback()
  811. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  812. c.ServeJSON()
  813. return
  814. }
  815. }
  816. o.Commit()
  817. err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
  818. if err != nil {
  819. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
  820. c.ServeJSON()
  821. return
  822. }
  823. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
  824. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  825. c.ServeJSON()
  826. return
  827. }
  828. func Update_VerifyContract_State(T_customer_id, flag string) error {
  829. o := orm.NewOrm()
  830. ContractDao := Contract.NewContract(o)
  831. VerifyContractDao := Contract.NewVerifyContract(o)
  832. vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
  833. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  834. if err != nil {
  835. return err
  836. }
  837. if vcn.Id == 0 {
  838. vc.T_sign_times = 0
  839. vc.T_start_date = ""
  840. vc.T_end_date = ""
  841. vc.T_State = 1
  842. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  843. return err
  844. }
  845. if flag == "ADD" {
  846. vc.T_sign_times += 1
  847. } else if flag == "DELETE" {
  848. vc.T_sign_times -= 1
  849. }
  850. if len(vc.T_start_date) == 0 {
  851. vc.T_start_date = vcn.T_start_date
  852. }
  853. if vc.T_end_date != vcn.T_end_date {
  854. vc.T_end_date = vcn.T_end_date
  855. }
  856. nowTime := time.Now()
  857. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  858. end, _ := lib.DateStrToTime(vcn.T_end_date)
  859. now2Month := now.AddDate(0, 2, 0)
  860. // 1-未签约 2-已作废 3-已签约 4-即将到期
  861. state := 1
  862. // 结束时间<今天 已过期
  863. if end.Before(now) {
  864. state = 2
  865. } else {
  866. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  867. if end.Before(now2Month) || end.Equal(now) {
  868. state = 4
  869. NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
  870. }
  871. // 结束时间>今天+2月 已签约
  872. if end.After(now2Month) || end.Equal(now2Month) {
  873. state = 3
  874. }
  875. }
  876. vc.T_State = state
  877. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  878. return err
  879. }
  880. // 验证合同-添加客户
  881. func (c *ContractController) VerifyContract_Add_Customer() {
  882. T_customer := c.GetString("T_customer")
  883. o := orm.NewOrm()
  884. VerifyContractDao := Contract.NewVerifyContract(o)
  885. T_number := ""
  886. for true {
  887. rand_x := 1
  888. T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
  889. _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
  890. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  891. break
  892. }
  893. rand_x += 1
  894. }
  895. var_ := Contract.VerifyContract{
  896. T_customer: T_customer,
  897. T_customer_id: T_number,
  898. T_State: 1,
  899. }
  900. _, err := VerifyContractDao.Add_VerifyContract(var_)
  901. if err != nil {
  902. o.Rollback()
  903. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  904. c.ServeJSON()
  905. return
  906. }
  907. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
  908. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  909. c.ServeJSON()
  910. return
  911. }
  912. // 验证合同-编辑客户
  913. func (c *ContractController) VerifyContract_Update_Customer() {
  914. T_customer_id := c.GetString("T_customer_id")
  915. T_customer := c.GetString("T_customer")
  916. o := orm.NewOrm()
  917. VerifyContractDao := Contract.NewVerifyContract(o)
  918. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  919. vc.T_customer = T_customer
  920. err = VerifyContractDao.Update_VerifyContract(vc)
  921. if err != nil {
  922. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  923. c.ServeJSON()
  924. return
  925. }
  926. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
  927. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
  928. c.ServeJSON()
  929. return
  930. }
  931. // 验证合同-客户列表
  932. func (c *ContractController) VerifyContract_Customer_List() {
  933. // 分页参数 初始化
  934. page, _ := c.GetInt("page")
  935. if page < 1 {
  936. page = 1
  937. }
  938. page_z, _ := c.GetInt("page_z")
  939. if page_z < 1 {
  940. page_z = conf.Page_size
  941. }
  942. // 查询
  943. T_name := c.GetString("T_name")
  944. T_state, _ := c.GetInt("T_state")
  945. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  946. R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
  947. var r_jsons lib.R_JSONS
  948. r_jsons.Num = R_cnt
  949. r_jsons.Data = R_List
  950. r_jsons.Page = page
  951. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  952. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  953. c.ServeJSON()
  954. return
  955. }
  956. // 合同回款明细列表
  957. func (c *ContractController) Contract_List_RecoveriesMoney() {
  958. // 分页参数 初始化
  959. page, _ := c.GetInt("page")
  960. if page < 1 {
  961. page = 1
  962. }
  963. page_z, _ := c.GetInt("page_z")
  964. if page_z < 1 {
  965. page_z = conf.Page_size
  966. }
  967. // 查询
  968. T_name := c.GetString("T_name")
  969. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  970. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  971. userList, _ := NatsServer.Read_User_List_All()
  972. Account.Read_User_All_Map(userList)
  973. ContractDao := Contract.NewContract(orm.NewOrm())
  974. R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(T_name, T_recoveries_state, T_invoice_state, page, page_z)
  975. var r_jsons lib.R_JSONS
  976. r_jsons.Num = R_cnt
  977. r_jsons.Data = R_List
  978. r_jsons.Page = page
  979. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  980. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  981. c.ServeJSON()
  982. return
  983. }
  984. func (c *ContractController) Contract_Stat() {
  985. T_type, _ := c.GetInt("T_type")
  986. T_year, _ := c.GetInt("T_year")
  987. type R_JSONS struct {
  988. ContractMoneyByYear interface{}
  989. TotalMoneyByYear float32
  990. RecoveriesMoneyByYear float32
  991. ContractNumByYear interface{}
  992. TotalMumByYear int64
  993. ContractMoneyByMonth interface{}
  994. TotalMoneyByMonth float32
  995. RecoveriesMoneyByMonth float32
  996. ContractNumByMonth interface{}
  997. TotalMumByMonth int64
  998. TotalMoneyByThisYear float32 // 今年合同总金额
  999. RecoveriesMoneyByThisYear float32 // 今年回款总金额
  1000. TotalMumByThisYear int64 // 今年合同总数
  1001. TotalMoneyByLastYear float32 // 去年合同总金额
  1002. RecoveriesMoneyByLastYear float32 // 去年回款总金额
  1003. TotalMumByLastYear int64 // 去年合同总数
  1004. }
  1005. var r_jsons R_JSONS
  1006. thisYear := time.Now().Year()
  1007. if T_year == 0 {
  1008. T_year = thisYear
  1009. }
  1010. ContractDao := Contract.NewContract(orm.NewOrm())
  1011. monryStat, moneyTotal, moneyRecoveries := ContractDao.Stat_ContractMoney_ByYear(T_type)
  1012. numStat, numTotal := ContractDao.Stat_ContractNum_ByYear(T_type)
  1013. monryMonthStat, moneyMonthTotal, moneyMonthRecoveries := ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year)
  1014. numMonthStat, numMonthTotal := ContractDao.Stat_ContractNum_ByMonth(T_type, T_year)
  1015. // 去年
  1016. _, r_jsons.TotalMoneyByLastYear, r_jsons.RecoveriesMoneyByLastYear = ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year-1)
  1017. _, r_jsons.TotalMumByLastYear = ContractDao.Stat_ContractNum_ByMonth(T_type, T_year-1)
  1018. r_jsons.ContractMoneyByYear = monryStat
  1019. r_jsons.TotalMoneyByYear = moneyTotal
  1020. r_jsons.RecoveriesMoneyByYear = moneyRecoveries
  1021. r_jsons.ContractNumByYear = numStat
  1022. r_jsons.TotalMumByYear = numTotal
  1023. r_jsons.ContractMoneyByMonth = monryMonthStat
  1024. r_jsons.TotalMoneyByMonth = moneyMonthTotal
  1025. r_jsons.RecoveriesMoneyByMonth = moneyMonthRecoveries
  1026. r_jsons.ContractNumByMonth = numMonthStat
  1027. r_jsons.TotalMumByMonth = numMonthTotal
  1028. r_jsons.TotalMoneyByThisYear = moneyMonthTotal
  1029. r_jsons.RecoveriesMoneyByThisYear = moneyMonthRecoveries
  1030. r_jsons.TotalMumByThisYear = numMonthTotal
  1031. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1032. c.ServeJSON()
  1033. return
  1034. }
  1035. func Cron_VerifyContract() {
  1036. //创建一个定时任务对象
  1037. c := cron.New(cron.WithSeconds())
  1038. //给对象增加定时任务
  1039. //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
  1040. c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
  1041. //启动定时任务
  1042. c.Start()
  1043. defer c.Stop()
  1044. //查询语句,阻塞,让main函数不退出,保持程序运行
  1045. select {}
  1046. }
  1047. // 修改合同状态
  1048. func Cron_VerifyContract_ChangeState() {
  1049. T_date := time.Now().Format("2006-01-02")
  1050. logs.Info("开始处理" + T_date + "验证合同状态")
  1051. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1052. R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
  1053. for _, v := range R_List {
  1054. err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
  1055. if err != nil {
  1056. logs.Error("修改合同状态失败")
  1057. }
  1058. }
  1059. }
  1060. // 修改回款金额及开票金额
  1061. func UpdateVerifyContract_RecoveriesInvoice_Money() {
  1062. ContractDao := Contract.NewContract(orm.NewOrm())
  1063. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1064. for _, verify := range R_List {
  1065. R := Contract.Contract{Id: verify.Id}
  1066. _, R.T_recoveries_money = Contract.ContractToContractFinance_R(verify.T_recoveries)
  1067. _, R.T_invoice_money = Contract.ContractToContractFinance_R(verify.T_invoice)
  1068. ContractDao.Update_Contract(R, "T_recoveries_money", "T_invoice_money")
  1069. }
  1070. }