Contract.go 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. package controllers
  2. import (
  3. "ERP_storage/Nats"
  4. "ERP_storage/Nats/NatsServer"
  5. "ERP_storage/conf"
  6. "ERP_storage/logs"
  7. "ERP_storage/models/Account"
  8. "ERP_storage/models/Basic"
  9. "ERP_storage/models/Contract"
  10. "ERP_storage/models/Stock"
  11. "fmt"
  12. "github.com/beego/beego/v2/adapter/orm"
  13. beego "github.com/beego/beego/v2/server/web"
  14. "github.com/robfig/cron/v3"
  15. "github.com/xuri/excelize/v2"
  16. natslibs "gogs.baozhida.cn/zoie/ERP_libs/Nats"
  17. userlibs "gogs.baozhida.cn/zoie/ERP_libs/User"
  18. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  19. "math"
  20. "os"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. type ContractController struct {
  26. beego.Controller
  27. User userlibs.User
  28. }
  29. func (c *ContractController) Prepare() {
  30. c.User = *Account.User_r
  31. }
  32. func (c *ContractController) Contract_GenT_number() {
  33. o := orm.NewOrm()
  34. ContractDao := Contract.NewContract(o)
  35. T_number := ""
  36. rand_x := 1
  37. for true {
  38. T_number = fmt.Sprintf("GZBZD-%s%03d", time.Now().Format("20060102"), rand_x)
  39. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  40. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  41. break
  42. }
  43. rand_x += 1
  44. }
  45. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  46. c.ServeJSON()
  47. return
  48. }
  49. func (c *ContractController) Contract_List() {
  50. // 分页参数 初始化
  51. page, _ := c.GetInt("page")
  52. if page < 1 {
  53. page = 1
  54. }
  55. page_z, _ := c.GetInt("page_z")
  56. if page_z < 1 {
  57. page_z = conf.Page_size
  58. }
  59. // 查询
  60. T_name := c.GetString("T_name")
  61. T_state, _ := c.GetInt("T_state")
  62. userList, _ := NatsServer.Read_User_List_All()
  63. Account.Read_User_All_Map(userList)
  64. ContractDao := Contract.NewContract(orm.NewOrm())
  65. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, T_state, 0, page, page_z)
  66. var r_jsons lib.R_JSONS
  67. r_jsons.Num = R_cnt
  68. r_jsons.Data = R_List
  69. r_jsons.Page = page
  70. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  71. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  72. c.ServeJSON()
  73. return
  74. }
  75. func (c *ContractController) Contract_User_List() {
  76. // 分页参数 初始化
  77. page, _ := c.GetInt("page")
  78. if page < 1 {
  79. page = 1
  80. }
  81. page_z, _ := c.GetInt("page_z")
  82. if page_z < 1 {
  83. page_z = conf.Page_size
  84. }
  85. // 查询
  86. T_name := c.GetString("T_name")
  87. T_state, _ := c.GetInt("T_state")
  88. userList, _ := NatsServer.Read_User_List_All()
  89. Account.Read_User_All_Map(userList)
  90. ContractDao := Contract.NewContract(orm.NewOrm())
  91. R_List, R_cnt := ContractDao.Read_Contract_List(c.User.T_uuid, T_name, T_state, 0, page, page_z)
  92. var r_jsons lib.R_JSONS
  93. r_jsons.Num = R_cnt
  94. r_jsons.Data = R_List
  95. r_jsons.Page = page
  96. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  97. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  98. c.ServeJSON()
  99. return
  100. }
  101. func (c *ContractController) Contract_Get() {
  102. // 查询
  103. T_number := c.GetString("T_number")
  104. o := orm.NewOrm()
  105. ContractDao := Contract.NewContract(o)
  106. DeviceDao := Stock.NewDevice(o)
  107. ContractProductDao := Contract.NewContractProduct(o)
  108. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  109. if err != nil {
  110. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  111. c.ServeJSON()
  112. return
  113. }
  114. productList := ContractProductDao.Read_ContractProduct_List(contract.T_number)
  115. var pList []Contract.ContractProduct_R
  116. for _, v := range productList {
  117. p := Contract.ContractProductToContractProduct_R(v)
  118. p.T_device_list, _ = DeviceDao.Read_DeviceSn_List(T_number, v.T_product_id, "", "")
  119. pList = append(pList, p)
  120. }
  121. userList, _ := NatsServer.Read_User_List_All()
  122. Account.Read_User_All_Map(userList)
  123. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Contract.ContractToContract_Detail(contract, pList)}
  124. c.ServeJSON()
  125. return
  126. }
  127. func (c *ContractController) Contract_Add() {
  128. T_number := c.GetString("T_number")
  129. T_customer := c.GetString("T_customer")
  130. T_money, _ := c.GetFloat("T_money") // 总价
  131. T_discount, _ := c.GetFloat("T_discount") // 优惠价
  132. T_date := c.GetString("T_date")
  133. T_product := c.GetString("T_product")
  134. T_remark := c.GetString("T_remark")
  135. T_pdf := c.GetString("T_pdf")
  136. T_project := c.GetString("T_project")
  137. T_recoveries := c.GetString("T_recoveries")
  138. T_invoice := c.GetString("T_invoice")
  139. T_submit := c.GetString("T_submit")
  140. if len(T_submit) == 0 {
  141. T_submit = c.User.T_uuid
  142. }
  143. var_ := Contract.Contract{
  144. T_number: T_number,
  145. T_customer: T_customer,
  146. T_money: float32(T_money),
  147. T_discount: float32(T_discount),
  148. T_project: T_project,
  149. T_type: 1, //合同类型 1-销售合同 2-验证合同
  150. T_date: T_date,
  151. T_out: 1,
  152. T_State: 3,
  153. T_remark: T_remark,
  154. T_pdf: T_pdf,
  155. T_submit: T_submit,
  156. T_recoveries: T_recoveries,
  157. T_invoice: T_invoice,
  158. }
  159. _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  160. _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  161. o := orm.NewOrm()
  162. o.Begin()
  163. ContractDao := Contract.NewContract(o)
  164. ContractProductDao := Contract.NewContractProduct(o)
  165. _, err := ContractDao.Read_Contract_ByT_number(T_number)
  166. if err == nil {
  167. o.Rollback()
  168. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  169. c.ServeJSON()
  170. return
  171. }
  172. _, err = ContractDao.Add_Contract(var_)
  173. if err != nil {
  174. o.Rollback()
  175. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  176. c.ServeJSON()
  177. return
  178. }
  179. if len(T_product) == 0 {
  180. o.Rollback()
  181. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
  182. c.ServeJSON()
  183. return
  184. }
  185. productList := lib.SplitString(T_product, "|")
  186. for _, v := range productList {
  187. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  188. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  189. contractProduct := Contract.ContractProduct{
  190. T_contract_number: T_number,
  191. T_product_id: product_id,
  192. T_product_total: num,
  193. T_product_out: 0, // 已出库数量
  194. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  195. }
  196. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  197. if err != nil {
  198. o.Rollback()
  199. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  200. c.ServeJSON()
  201. return
  202. }
  203. }
  204. o.Commit()
  205. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "添加", var_)
  206. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  207. c.ServeJSON()
  208. return
  209. }
  210. func (c *ContractController) Contract_Approval() {
  211. T_number := c.GetString("T_number")
  212. T_state, _ := c.GetInt("T_state")
  213. o := orm.NewOrm()
  214. ContractDao := Contract.NewContract(o)
  215. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  216. if err != nil {
  217. o.Rollback()
  218. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  219. c.ServeJSON()
  220. return
  221. }
  222. // 1-已通过 2-未通过
  223. contract.T_State = T_state
  224. contract.T_approver = c.User.T_uuid
  225. err = ContractDao.Update_Contract(contract, "T_State", "T_approver")
  226. if err != nil {
  227. o.Rollback()
  228. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  229. c.ServeJSON()
  230. return
  231. }
  232. if T_state == 1 {
  233. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核已通过", T_number), conf.ContractApprovalUrl)
  234. }
  235. if T_state == 2 {
  236. NatsServer.AddNews(contract.T_submit, fmt.Sprintf("【合同审核】您提交的合同(%s)审核未通过", T_number), conf.ContractApprovalUrl)
  237. }
  238. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "审核", contract)
  239. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  240. c.ServeJSON()
  241. return
  242. }
  243. func (c *ContractController) Contract_Edit() {
  244. T_number := c.GetString("T_number")
  245. T_customer := c.GetString("T_customer")
  246. T_money, _ := c.GetFloat("T_money")
  247. T_discount, _ := c.GetFloat("T_discount") // 优惠价
  248. T_date := c.GetString("T_date")
  249. T_product := c.GetString("T_product")
  250. T_remark := c.GetString("T_remark")
  251. T_pdf := c.GetString("T_pdf")
  252. T_project := c.GetString("T_project")
  253. T_recoveries := c.GetString("T_recoveries")
  254. T_invoice := c.GetString("T_invoice")
  255. T_submit := c.GetString("T_submit")
  256. o := orm.NewOrm()
  257. o.Begin()
  258. ContractDao := Contract.NewContract(o)
  259. ContractProductDao := Contract.NewContractProduct(o)
  260. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  261. if err != nil {
  262. o.Rollback()
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. if len(T_customer) > 0 {
  268. contract.T_customer = T_customer
  269. }
  270. if T_money > 0 {
  271. contract.T_money = float32(T_money)
  272. }
  273. if T_discount > 0 {
  274. contract.T_discount = float32(T_discount)
  275. }
  276. if len(T_date) > 0 {
  277. contract.T_date = T_date
  278. }
  279. if len(T_remark) > 0 {
  280. contract.T_remark = T_remark
  281. }
  282. if len(T_pdf) > 0 {
  283. contract.T_pdf = T_pdf
  284. }
  285. if len(T_project) > 0 {
  286. contract.T_project = T_project
  287. }
  288. if len(T_recoveries) > 0 {
  289. contract.T_recoveries = T_recoveries
  290. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  291. }
  292. if len(T_invoice) > 0 {
  293. contract.T_invoice = T_invoice
  294. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  295. }
  296. if len(T_submit) > 0 {
  297. contract.T_submit = T_submit
  298. }
  299. //1-已通过 2-未通过 3-待审核 合同状态为未通过,修改之后将状态更改为待审核
  300. if contract.T_State == 2 {
  301. contract.T_State = 3
  302. }
  303. err = ContractDao.Update_Contract(contract, "T_customer", "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State",
  304. "T_project", "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_submit")
  305. if err != nil {
  306. o.Rollback()
  307. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  308. c.ServeJSON()
  309. return
  310. }
  311. if len(T_product) == 0 {
  312. o.Rollback()
  313. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同产品明细不能为空"}
  314. c.ServeJSON()
  315. return
  316. }
  317. productList := lib.SplitString(T_product, "|")
  318. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  319. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  320. // 检查编辑时需要修改的产品
  321. for k, v := range needToDeleteList {
  322. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  323. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  324. if v > 0 {
  325. o.Rollback()
  326. product, _ := Basic.Read_Product_ById(productId)
  327. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  328. c.ServeJSON()
  329. return
  330. }
  331. cp := Contract.ContractProduct{Id: id}
  332. err = ContractProductDao.Delete_ContractProduct(cp)
  333. if err != nil {
  334. o.Rollback()
  335. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  336. c.ServeJSON()
  337. return
  338. }
  339. }
  340. for _, v := range productList {
  341. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  342. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  343. oldContractProduct, ok := allList[product_id]
  344. if !ok {
  345. contractProduct := Contract.ContractProduct{
  346. T_contract_number: T_number,
  347. T_product_id: product_id,
  348. T_product_total: num,
  349. T_product_out: 0, // 已出库数量
  350. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  351. }
  352. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  353. if err != nil {
  354. o.Rollback()
  355. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  356. c.ServeJSON()
  357. return
  358. } else {
  359. continue
  360. }
  361. }
  362. // 修改产品总数量
  363. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  364. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  365. if oldNum == num {
  366. continue
  367. }
  368. cp := Contract.ContractProduct{Id: contractProductId, T_product_total: num}
  369. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  370. if err != nil {
  371. o.Rollback()
  372. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  373. c.ServeJSON()
  374. return
  375. }
  376. }
  377. o.Commit()
  378. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "修改", contract)
  379. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  380. c.ServeJSON()
  381. return
  382. }
  383. func (c *ContractController) Contract_Del() {
  384. T_number := c.GetString("T_number")
  385. o := orm.NewOrm()
  386. o.Begin()
  387. ContractDao := Contract.NewContract(o)
  388. ContractProductDao := Contract.NewContractProduct(o)
  389. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  390. if err != nil {
  391. o.Rollback()
  392. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  393. c.ServeJSON()
  394. return
  395. }
  396. //1-已通过 2-未通过 3-待审核 审核状态
  397. if contract.T_State == 1 {
  398. o.Rollback()
  399. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同已审核通过,禁止删除!"}
  400. c.ServeJSON()
  401. return
  402. }
  403. err = ContractDao.Delete_Contract(contract)
  404. if err != nil {
  405. o.Rollback()
  406. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  407. c.ServeJSON()
  408. return
  409. }
  410. //合同类型 1-销售合同 2-验证合同
  411. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  412. for _, v := range cpList {
  413. err = ContractProductDao.Delete_ContractProduct(v)
  414. if err != nil {
  415. o.Rollback()
  416. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  417. c.ServeJSON()
  418. return
  419. }
  420. }
  421. o.Commit()
  422. NatsServer.AddUserLogs(c.User.T_uuid, "合同", "删除", T_number)
  423. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  424. c.ServeJSON()
  425. return
  426. }
  427. func (c *ContractController) Contract_List_For_Out() {
  428. // 分页参数 初始化
  429. page, _ := c.GetInt("page")
  430. if page < 1 {
  431. page = 1
  432. }
  433. page_z, _ := c.GetInt("page_z")
  434. if page_z < 1 {
  435. page_z = conf.Page_size
  436. }
  437. // 查询
  438. T_name := c.GetString("T_name")
  439. userList, _ := NatsServer.Read_User_List_All()
  440. Account.Read_User_All_Map(userList)
  441. ContractDao := Contract.NewContract(orm.NewOrm())
  442. R_List, R_cnt := ContractDao.Read_Contract_List("", T_name, 1, 1, page, page_z)
  443. var r_jsons lib.R_JSONS
  444. r_jsons.Num = R_cnt
  445. r_jsons.Data = R_List
  446. r_jsons.Page = page
  447. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  448. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  449. c.ServeJSON()
  450. return
  451. }
  452. // 合同下的产品列表 出库时使用
  453. func (c *ContractController) Contract_Product_List() {
  454. // 查询
  455. T_number := c.GetString("T_number")
  456. T_depot_id, _ := c.GetInt("T_depot_id")
  457. o := orm.NewOrm()
  458. ContractDao := Contract.NewContract(o)
  459. ContractProductDao := Contract.NewContractProduct(o)
  460. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  461. if err != nil {
  462. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  463. c.ServeJSON()
  464. return
  465. }
  466. productList := ContractProductDao.Read_ContractProduct_OutList(contract.T_number)
  467. Stock.Read_Stock_All_Map(T_depot_id)
  468. var pList []Contract.ContractProduct_Out
  469. for _, v := range productList {
  470. p := Contract.ContractProductToContractProduct_Out(v)
  471. p.T_stock_total = Stock.Read_Stock_Get(T_depot_id, p.T_product_id)
  472. pList = append(pList, p)
  473. }
  474. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: pList}
  475. c.ServeJSON()
  476. return
  477. }
  478. // 检查需要删除的合同产品列表
  479. func Check_NeedToDelete_Product_List(list []string, newList []Contract.ContractProduct) (needToDelete map[string]int, all map[int]string) {
  480. var productMap = map[int]int{}
  481. needToDelete = make(map[string]int)
  482. all = make(map[int]string)
  483. for _, v := range list {
  484. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  485. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  486. productMap[product_id] = num
  487. }
  488. for _, product := range newList {
  489. all[product.T_product_id] = fmt.Sprintf("%d-%d", product.Id, product.T_product_total)
  490. _, ok := productMap[product.T_product_id]
  491. if !ok {
  492. id := fmt.Sprintf("%d-%d", product.Id, product.T_product_id)
  493. needToDelete[id] = product.T_product_out
  494. }
  495. }
  496. return
  497. }
  498. // 验证合同
  499. func (c *ContractController) VerifyContract_List() {
  500. // 分页参数 初始化
  501. page, _ := c.GetInt("page")
  502. if page < 1 {
  503. page = 1
  504. }
  505. page_z, _ := c.GetInt("page_z")
  506. if page_z < 1 {
  507. page_z = conf.Page_size
  508. }
  509. T_customer_id := c.GetString("T_customer_id")
  510. // 查询
  511. T_name := c.GetString("T_name")
  512. userList, _ := NatsServer.Read_User_List_All()
  513. Account.Read_User_All_Map(userList)
  514. ContractDao := Contract.NewContract(orm.NewOrm())
  515. R_List, R_cnt := ContractDao.Read_VerifyContract_List(T_name, T_customer_id, page, page_z)
  516. var r_jsons lib.R_JSONS
  517. r_jsons.Num = R_cnt
  518. r_jsons.Data = R_List
  519. r_jsons.Page = page
  520. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  521. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  522. c.ServeJSON()
  523. return
  524. }
  525. // 验证合同呢添加
  526. func (c *ContractController) VerifyContract_Add() {
  527. T_number := c.GetString("T_number")
  528. T_customer_id := c.GetString("T_customer_id")
  529. T_money, _ := c.GetFloat("T_money")
  530. T_discount, _ := c.GetFloat("T_discount")
  531. T_date := c.GetString("T_date")
  532. T_product := c.GetString("T_product")
  533. T_submit := c.GetString("T_submit")
  534. T_remark := c.GetString("T_remark")
  535. T_pdf := c.GetString("T_pdf")
  536. T_project := c.GetString("T_project")
  537. T_recoveries := c.GetString("T_recoveries")
  538. T_invoice := c.GetString("T_invoice")
  539. T_start_date := c.GetString("T_start_date")
  540. T_end_date := c.GetString("T_end_date")
  541. var_ := Contract.Contract{
  542. T_number: T_number,
  543. T_customer_id: T_customer_id,
  544. T_money: float32(T_money),
  545. T_discount: float32(T_discount),
  546. T_project: T_project,
  547. T_type: 2, //合同类型 1-销售合同 2-验证合同
  548. T_date: T_date,
  549. T_State: 1,
  550. T_remark: T_remark,
  551. T_pdf: T_pdf,
  552. T_submit: T_submit,
  553. T_recoveries: T_recoveries,
  554. T_invoice: T_invoice,
  555. T_start_date: T_start_date,
  556. T_end_date: T_end_date,
  557. }
  558. _, var_.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  559. _, var_.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  560. o := orm.NewOrm()
  561. o.Begin()
  562. ContractDao := Contract.NewContract(o)
  563. ContractProductDao := Contract.NewContractProduct(o)
  564. _, err := ContractDao.Read_Contract_ByT_date(T_customer_id, T_start_date, T_end_date)
  565. if err == nil {
  566. o.Rollback()
  567. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同签约时间重复!"}
  568. c.ServeJSON()
  569. return
  570. }
  571. _, err = ContractDao.Read_Contract_ByT_number(T_number)
  572. if err == nil {
  573. o.Rollback()
  574. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同编号重复!"}
  575. c.ServeJSON()
  576. return
  577. }
  578. ContractID, err := ContractDao.Add_Contract(var_)
  579. if err != nil {
  580. o.Rollback()
  581. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  582. c.ServeJSON()
  583. return
  584. }
  585. if len(T_product) == 0 {
  586. o.Rollback()
  587. c.Data["json"] = lib.JSONS{Code: 202, Msg: "验证合同明细不能为空"}
  588. c.ServeJSON()
  589. return
  590. }
  591. productList := lib.SplitString(T_product, "|")
  592. ContractOut := 0
  593. for _, v := range productList {
  594. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  595. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  596. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  597. product, _ := Basic.Read_Product_ById(product_id)
  598. contractProduct := Contract.ContractProduct{
  599. T_contract_number: T_number,
  600. T_product_id: product_id,
  601. T_product_total: num,
  602. T_product_out: 0, // 已出库数量
  603. T_price: float32(price), // 价格
  604. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  605. }
  606. if product.T_class < 0 {
  607. contractProduct.T_State = 3
  608. }
  609. if product.T_class == 0 {
  610. ContractOut = 1
  611. }
  612. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  613. if err != nil {
  614. o.Rollback()
  615. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败"}
  616. c.ServeJSON()
  617. return
  618. }
  619. }
  620. o.Commit()
  621. o2 := orm.NewOrm()
  622. ContractDao2 := Contract.NewContract(o2)
  623. if ContractOut == 1 {
  624. err = ContractDao2.Update_Contract(Contract.Contract{Id: int(ContractID), T_out: ContractOut}, "T_out")
  625. if err != nil {
  626. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新合同出库状态失败"}
  627. c.ServeJSON()
  628. return
  629. }
  630. }
  631. err = Update_VerifyContract_State(T_customer_id, "ADD")
  632. if err != nil {
  633. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  634. c.ServeJSON()
  635. return
  636. }
  637. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加", var_)
  638. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  639. c.ServeJSON()
  640. return
  641. }
  642. // 验证合同呢修改
  643. func (c *ContractController) VerifyContract_Edit() {
  644. T_number := c.GetString("T_number")
  645. T_money, _ := c.GetFloat("T_money")
  646. T_discount, _ := c.GetFloat("T_discount")
  647. T_date := c.GetString("T_date")
  648. T_product := c.GetString("T_product")
  649. T_remark := c.GetString("T_remark")
  650. T_pdf := c.GetString("T_pdf")
  651. T_project := c.GetString("T_project")
  652. T_recoveries := c.GetString("T_recoveries")
  653. T_invoice := c.GetString("T_invoice")
  654. T_start_date := c.GetString("T_start_date")
  655. T_end_date := c.GetString("T_end_date")
  656. T_submit := c.GetString("T_submit")
  657. o := orm.NewOrm()
  658. o.Begin()
  659. ContractDao := Contract.NewContract(o)
  660. ContractProductDao := Contract.NewContractProduct(o)
  661. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  662. if err != nil || contract.T_type == 1 {
  663. o.Rollback()
  664. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  665. c.ServeJSON()
  666. return
  667. }
  668. if T_money > 0 {
  669. contract.T_money = float32(T_money)
  670. }
  671. if T_discount > 0 {
  672. contract.T_discount = float32(T_discount)
  673. }
  674. if len(T_date) > 0 {
  675. contract.T_date = T_date
  676. }
  677. if len(T_remark) > 0 {
  678. contract.T_remark = T_remark
  679. }
  680. if len(T_pdf) > 0 {
  681. contract.T_pdf = T_pdf
  682. }
  683. if len(T_project) > 0 {
  684. contract.T_project = T_project
  685. }
  686. if len(T_recoveries) > 0 {
  687. contract.T_recoveries = T_recoveries
  688. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  689. }
  690. if len(T_invoice) > 0 {
  691. contract.T_invoice = T_invoice
  692. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  693. }
  694. if len(T_start_date) > 0 {
  695. contract.T_start_date = T_start_date
  696. }
  697. if len(T_end_date) > 0 {
  698. contract.T_end_date = T_end_date
  699. }
  700. if len(T_submit) > 0 {
  701. contract.T_submit = T_submit
  702. }
  703. err = ContractDao.Update_Contract(contract, "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State", "T_project",
  704. "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_start_date", "T_end_date", "T_submit")
  705. if err != nil {
  706. o.Rollback()
  707. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  708. c.ServeJSON()
  709. return
  710. }
  711. if len(T_product) == 0 {
  712. o.Rollback()
  713. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  714. c.ServeJSON()
  715. return
  716. }
  717. productList := lib.SplitString(T_product, "|")
  718. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(T_number)
  719. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  720. // 检查编辑时需要修改的产品
  721. for k, v := range needToDeleteList {
  722. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  723. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  724. if v > 0 {
  725. o.Rollback()
  726. product, _ := Basic.Read_Product_ById(productId)
  727. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  728. c.ServeJSON()
  729. return
  730. }
  731. cp := Contract.ContractProduct{Id: id}
  732. err = ContractProductDao.Delete_ContractProduct(cp)
  733. if err != nil {
  734. o.Rollback()
  735. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  736. c.ServeJSON()
  737. return
  738. }
  739. }
  740. for _, v := range productList {
  741. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  742. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  743. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  744. oldContractProduct, ok := allList[product_id]
  745. if !ok {
  746. contractProduct := Contract.ContractProduct{
  747. T_contract_number: T_number,
  748. T_product_id: product_id,
  749. T_product_total: num,
  750. T_product_out: 0, // 已出库数量
  751. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  752. }
  753. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  754. if err != nil {
  755. o.Rollback()
  756. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  757. c.ServeJSON()
  758. return
  759. } else {
  760. continue
  761. }
  762. }
  763. // 修改产品总数量
  764. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  765. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  766. if oldNum == num {
  767. continue
  768. }
  769. cp := Contract.ContractProduct{
  770. Id: contractProductId,
  771. T_product_total: num,
  772. T_price: float32(price), // 价格
  773. }
  774. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total")
  775. if err != nil {
  776. o.Rollback()
  777. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  778. c.ServeJSON()
  779. return
  780. }
  781. }
  782. o.Commit()
  783. err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
  784. if err != nil {
  785. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  786. c.ServeJSON()
  787. return
  788. }
  789. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
  790. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  791. c.ServeJSON()
  792. return
  793. }
  794. func (c *ContractController) VerifyContract_Del() {
  795. T_number := c.GetString("T_number")
  796. o := orm.NewOrm()
  797. o.Begin()
  798. ContractDao := Contract.NewContract(o)
  799. ContractProductDao := Contract.NewContractProduct(o)
  800. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  801. if err != nil {
  802. o.Rollback()
  803. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  804. c.ServeJSON()
  805. return
  806. }
  807. err = ContractDao.Delete_Contract(contract)
  808. if err != nil {
  809. o.Rollback()
  810. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  811. c.ServeJSON()
  812. return
  813. }
  814. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  815. for _, v := range cpList {
  816. err = ContractProductDao.Delete_ContractProduct(v)
  817. if err != nil {
  818. o.Rollback()
  819. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  820. c.ServeJSON()
  821. return
  822. }
  823. }
  824. o.Commit()
  825. err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
  826. if err != nil {
  827. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
  828. c.ServeJSON()
  829. return
  830. }
  831. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
  832. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  833. c.ServeJSON()
  834. return
  835. }
  836. func Update_VerifyContract_State(T_customer_id, flag string) error {
  837. o := orm.NewOrm()
  838. ContractDao := Contract.NewContract(o)
  839. VerifyContractDao := Contract.NewVerifyContract(o)
  840. vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
  841. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  842. if err != nil {
  843. return err
  844. }
  845. if vcn.Id == 0 {
  846. vc.T_sign_times = 0
  847. vc.T_start_date = ""
  848. vc.T_end_date = ""
  849. vc.T_State = 1
  850. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  851. return err
  852. }
  853. if flag == "ADD" {
  854. vc.T_sign_times += 1
  855. } else if flag == "DELETE" {
  856. vc.T_sign_times -= 1
  857. }
  858. if len(vc.T_start_date) == 0 {
  859. vc.T_start_date = vcn.T_start_date
  860. }
  861. if vc.T_end_date != vcn.T_end_date {
  862. vc.T_end_date = vcn.T_end_date
  863. }
  864. nowTime := time.Now()
  865. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  866. end, _ := lib.DateStrToTime(vcn.T_end_date)
  867. now2Month := now.AddDate(0, 2, 0)
  868. // 1-未签约 2-已作废 3-已签约 4-即将到期
  869. state := 1
  870. // 结束时间<今天 已过期
  871. if end.Before(now) {
  872. state = 2
  873. } else {
  874. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  875. if end.Before(now2Month) || end.Equal(now) {
  876. state = 4
  877. NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
  878. }
  879. // 结束时间>今天+2月 已签约
  880. if end.After(now2Month) || end.Equal(now2Month) {
  881. state = 3
  882. }
  883. }
  884. vc.T_State = state
  885. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  886. return err
  887. }
  888. // 验证合同-添加客户
  889. func (c *ContractController) VerifyContract_Add_Customer() {
  890. T_customer := c.GetString("T_customer")
  891. o := orm.NewOrm()
  892. VerifyContractDao := Contract.NewVerifyContract(o)
  893. T_number := ""
  894. for true {
  895. rand_x := 1
  896. T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
  897. _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
  898. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  899. break
  900. }
  901. rand_x += 1
  902. }
  903. var_ := Contract.VerifyContract{
  904. T_customer: T_customer,
  905. T_customer_id: T_number,
  906. T_State: 1,
  907. }
  908. _, err := VerifyContractDao.Add_VerifyContract(var_)
  909. if err != nil {
  910. o.Rollback()
  911. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  912. c.ServeJSON()
  913. return
  914. }
  915. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
  916. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  917. c.ServeJSON()
  918. return
  919. }
  920. // 验证合同-编辑客户
  921. func (c *ContractController) VerifyContract_Update_Customer() {
  922. T_customer_id := c.GetString("T_customer_id")
  923. T_customer := c.GetString("T_customer")
  924. o := orm.NewOrm()
  925. VerifyContractDao := Contract.NewVerifyContract(o)
  926. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  927. vc.T_customer = T_customer
  928. err = VerifyContractDao.Update_VerifyContract(vc)
  929. if err != nil {
  930. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  931. c.ServeJSON()
  932. return
  933. }
  934. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
  935. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
  936. c.ServeJSON()
  937. return
  938. }
  939. // 验证合同-客户列表
  940. func (c *ContractController) VerifyContract_Customer_List() {
  941. // 分页参数 初始化
  942. page, _ := c.GetInt("page")
  943. if page < 1 {
  944. page = 1
  945. }
  946. page_z, _ := c.GetInt("page_z")
  947. if page_z < 1 {
  948. page_z = conf.Page_size
  949. }
  950. // 查询
  951. T_name := c.GetString("T_name")
  952. T_state, _ := c.GetInt("T_state")
  953. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  954. R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
  955. var r_jsons lib.R_JSONS
  956. r_jsons.Num = R_cnt
  957. r_jsons.Data = R_List
  958. r_jsons.Page = page
  959. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  960. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  961. c.ServeJSON()
  962. return
  963. }
  964. // 合同回款明细列表
  965. func (c *ContractController) Contract_List_RecoveriesMoney() {
  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_start_date := c.GetString("T_start_date") // 回款开始日期
  978. T_end_date := c.GetString("T_end_date") // 回款结束日期
  979. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  980. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  981. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  982. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  983. userList, _ := NatsServer.Read_User_List_All()
  984. Account.Read_User_All_Map(userList)
  985. Contract.Read_VerifyContract_All_Map()
  986. ContractDao := Contract.NewContract(orm.NewOrm())
  987. R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, page, page_z)
  988. var r_jsons lib.R_JSONS
  989. r_jsons.Num = R_cnt
  990. r_jsons.Data = R_List
  991. r_jsons.Page = page
  992. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  993. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  994. c.ServeJSON()
  995. return
  996. }
  997. // 导出合同回款明细列表
  998. func (c *ContractController) Contract_List_RecoveriesMoney_Export() {
  999. // 查询
  1000. T_name := c.GetString("T_name")
  1001. T_start_date := c.GetString("T_start_date") // 回款开始日期
  1002. T_end_date := c.GetString("T_end_date") // 回款结束日期
  1003. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  1004. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  1005. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  1006. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  1007. userList, _ := NatsServer.Read_User_List_All()
  1008. Account.Read_User_All_Map(userList)
  1009. Contract.Read_VerifyContract_All_Map()
  1010. ContractDao := Contract.NewContract(orm.NewOrm())
  1011. R_List, _ := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, 0, 9999)
  1012. title := "销售提成明细表"
  1013. if T_mine == 1 {
  1014. title = fmt.Sprintf("%s-销售提成明细表", c.User.T_name)
  1015. }
  1016. filename := fmt.Sprintf("%s(%s)", title, lib.GetRandstring(6, "0123456789", 0))
  1017. f := excelize.NewFile() // 设置单元格的值
  1018. Style1, _ := f.NewStyle(
  1019. &excelize.Style{
  1020. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1021. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1022. })
  1023. Style2, _ := f.NewStyle(
  1024. &excelize.Style{
  1025. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1026. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1027. Border: []excelize.Border{
  1028. {Type: "left", Color: "000000", Style: 1},
  1029. {Type: "top", Color: "000000", Style: 1},
  1030. {Type: "bottom", Color: "000000", Style: 1},
  1031. {Type: "right", Color: "000000", Style: 1},
  1032. },
  1033. })
  1034. Style3, _ := f.NewStyle(
  1035. &excelize.Style{
  1036. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1037. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1038. Border: []excelize.Border{
  1039. {Type: "left", Color: "000000", Style: 1},
  1040. {Type: "top", Color: "000000", Style: 1},
  1041. {Type: "bottom", Color: "000000", Style: 1},
  1042. {Type: "right", Color: "000000", Style: 1},
  1043. },
  1044. })
  1045. f.MergeCell("Sheet1", "A1", "J1")
  1046. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1047. f.SetCellValue("Sheet1", "A1", title)
  1048. f.SetRowHeight("Sheet1", 1, 30)
  1049. // 这里设置表头
  1050. f.SetCellStyle("Sheet1", "A2", "J2", Style2)
  1051. f.SetRowHeight("Sheet1", 2, 20)
  1052. f.SetCellValue("Sheet1", "A2", "公司名称")
  1053. f.SetCellValue("Sheet1", "B2", "合同编号")
  1054. f.SetCellValue("Sheet1", "C2", "签约时间")
  1055. f.SetCellValue("Sheet1", "D2", "合同金额")
  1056. f.SetCellValue("Sheet1", "E2", "回款金额")
  1057. f.SetCellValue("Sheet1", "F2", "回款比例")
  1058. f.SetCellValue("Sheet1", "G2", "销售类型")
  1059. f.SetCellValue("Sheet1", "H2", "销售提成5%")
  1060. f.SetCellValue("Sheet1", "I2", "提成金额")
  1061. f.SetCellValue("Sheet1", "J2", "提成状态")
  1062. // 设置列宽
  1063. f.SetColWidth("Sheet1", "A", "A", 25)
  1064. f.SetColWidth("Sheet1", "B", "I", 15)
  1065. line := 2
  1066. for _, contract := range R_List {
  1067. line += 1
  1068. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1069. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), contract.T_customer)
  1070. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), contract.T_number)
  1071. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), contract.T_date)
  1072. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), contract.T_discount)
  1073. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), contract.T_recoveries_money)
  1074. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("%.02f%%", contract.T_recoveries_money/contract.T_discount*100))
  1075. T_type_str := "验证合同"
  1076. if contract.T_type == 1 {
  1077. T_type_str = "销售合同"
  1078. }
  1079. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), T_type_str)
  1080. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1081. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1082. T_percentage_str := "未打款"
  1083. if contract.T_percentage_state == 2 {
  1084. T_percentage_str = "已打款"
  1085. }
  1086. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), T_percentage_str)
  1087. }
  1088. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("J%d", line), Style3)
  1089. // 保存文件
  1090. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1091. fmt.Println(err)
  1092. }
  1093. var url string
  1094. //// 上传 OSS
  1095. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1096. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1097. if !is {
  1098. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1099. c.ServeJSON()
  1100. return
  1101. }
  1102. //删除目录
  1103. err := os.Remove("ofile/" + filename + ".xlsx")
  1104. if err != nil {
  1105. logs.Error(lib.FuncName(), err)
  1106. }
  1107. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1108. c.ServeJSON()
  1109. return
  1110. }
  1111. func (c *ContractController) Contract_Stat() {
  1112. T_type, _ := c.GetInt("T_type")
  1113. T_year, _ := c.GetInt("T_year")
  1114. type R_JSONS struct {
  1115. ContractMoneyByYear interface{}
  1116. TotalMoneyByYear float32
  1117. RecoveriesMoneyByYear float32
  1118. ContractNumByYear interface{}
  1119. TotalMumByYear int64
  1120. ContractMoneyByMonth interface{}
  1121. TotalMoneyByMonth float32
  1122. RecoveriesMoneyByMonth float32
  1123. ContractNumByMonth interface{}
  1124. TotalMumByMonth int64
  1125. TotalMoneyByThisYear float32 // 今年合同总金额
  1126. RecoveriesMoneyByThisYear float32 // 今年回款总金额
  1127. TotalMumByThisYear int64 // 今年合同总数
  1128. TotalMoneyByLastYear float32 // 去年合同总金额
  1129. RecoveriesMoneyByLastYear float32 // 去年回款总金额
  1130. TotalMumByLastYear int64 // 去年合同总数
  1131. }
  1132. var r_jsons R_JSONS
  1133. thisYear := time.Now().Year()
  1134. if T_year == 0 {
  1135. T_year = thisYear
  1136. }
  1137. ContractDao := Contract.NewContract(orm.NewOrm())
  1138. monryStat, moneyTotal, moneyRecoveries := ContractDao.Stat_ContractMoney_ByYear(T_type)
  1139. numStat, numTotal := ContractDao.Stat_ContractNum_ByYear(T_type)
  1140. monryMonthStat, moneyMonthTotal, moneyMonthRecoveries := ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year)
  1141. numMonthStat, numMonthTotal := ContractDao.Stat_ContractNum_ByMonth(T_type, T_year)
  1142. // 去年
  1143. _, r_jsons.TotalMoneyByLastYear, r_jsons.RecoveriesMoneyByLastYear = ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year-1)
  1144. _, r_jsons.TotalMumByLastYear = ContractDao.Stat_ContractNum_ByMonth(T_type, T_year-1)
  1145. r_jsons.ContractMoneyByYear = monryStat
  1146. r_jsons.TotalMoneyByYear = moneyTotal
  1147. r_jsons.RecoveriesMoneyByYear = moneyRecoveries
  1148. r_jsons.ContractNumByYear = numStat
  1149. r_jsons.TotalMumByYear = numTotal
  1150. r_jsons.ContractMoneyByMonth = monryMonthStat
  1151. r_jsons.TotalMoneyByMonth = moneyMonthTotal
  1152. r_jsons.RecoveriesMoneyByMonth = moneyMonthRecoveries
  1153. r_jsons.ContractNumByMonth = numMonthStat
  1154. r_jsons.TotalMumByMonth = numMonthTotal
  1155. r_jsons.TotalMoneyByThisYear = moneyMonthTotal
  1156. r_jsons.RecoveriesMoneyByThisYear = moneyMonthRecoveries
  1157. r_jsons.TotalMumByThisYear = numMonthTotal
  1158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1159. c.ServeJSON()
  1160. return
  1161. }
  1162. func Cron_VerifyContract() {
  1163. //创建一个定时任务对象
  1164. c := cron.New(cron.WithSeconds())
  1165. //给对象增加定时任务
  1166. //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
  1167. c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
  1168. //启动定时任务
  1169. c.Start()
  1170. defer c.Stop()
  1171. //查询语句,阻塞,让main函数不退出,保持程序运行
  1172. select {}
  1173. }
  1174. // 修改合同状态
  1175. func Cron_VerifyContract_ChangeState() {
  1176. T_date := time.Now().Format("2006-01-02")
  1177. logs.Info("开始处理" + T_date + "验证合同状态")
  1178. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1179. R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
  1180. for _, v := range R_List {
  1181. err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
  1182. if err != nil {
  1183. logs.Error("修改合同状态失败")
  1184. }
  1185. }
  1186. }
  1187. // 修改回款金额及开票金额
  1188. func UpdateVerifyContract_RecoveriesInvoice_Money() {
  1189. ContractDao := Contract.NewContract(orm.NewOrm())
  1190. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1191. for _, verify := range R_List {
  1192. R := Contract.Contract{Id: verify.Id}
  1193. _, R.T_recoveries_money = Contract.ContractToContractFinance_R(verify.T_recoveries)
  1194. _, R.T_invoice_money = Contract.ContractToContractFinance_R(verify.T_invoice)
  1195. ContractDao.Update_Contract(R, "T_recoveries_money", "T_invoice_money")
  1196. }
  1197. }
  1198. // 修改回款时间
  1199. func UpdateVerifyContract_RecoveriesInvoice_Time() {
  1200. ContractDao := Contract.NewContract(orm.NewOrm())
  1201. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1202. for _, verify := range R_List {
  1203. R := Contract.Contract{Id: verify.Id}
  1204. if verify.T_discount == verify.T_recoveries_money {
  1205. recoveriesList, _ := Contract.ContractToContractFinance_R(verify.T_recoveries)
  1206. if len(recoveriesList) > 0 {
  1207. R.T_recoveries_time = recoveriesList[len(recoveriesList)-1].T_date
  1208. ContractDao.Update_Contract(R, "T_recoveries_time")
  1209. }
  1210. }
  1211. }
  1212. }
  1213. // 合同提成打款
  1214. func (c *ContractController) Contract_Percentage_Remit() {
  1215. T_number := c.GetString("T_number")
  1216. o := orm.NewOrm()
  1217. ContractDao := Contract.NewContract(o)
  1218. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  1219. if err != nil {
  1220. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  1221. c.ServeJSON()
  1222. return
  1223. }
  1224. if contract.T_recoveries_money != contract.T_discount {
  1225. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同未回款,不可打款!"}
  1226. c.ServeJSON()
  1227. return
  1228. }
  1229. contract.T_percentage_state = 2 // 已打款
  1230. err = ContractDao.Update_Contract(contract, "T_percentage_state")
  1231. if err != nil {
  1232. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1233. c.ServeJSON()
  1234. return
  1235. }
  1236. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "打款", contract)
  1237. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  1238. c.ServeJSON()
  1239. return
  1240. }