Contract.go 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  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. Id, _ := c.GetInt("Id")
  645. T_number := c.GetString("T_number")
  646. T_money, _ := c.GetFloat("T_money")
  647. T_discount, _ := c.GetFloat("T_discount")
  648. T_date := c.GetString("T_date")
  649. T_product := c.GetString("T_product")
  650. T_remark := c.GetString("T_remark")
  651. T_pdf := c.GetString("T_pdf")
  652. T_project := c.GetString("T_project")
  653. T_recoveries := c.GetString("T_recoveries")
  654. T_invoice := c.GetString("T_invoice")
  655. T_start_date := c.GetString("T_start_date")
  656. T_end_date := c.GetString("T_end_date")
  657. T_submit := c.GetString("T_submit")
  658. o := orm.NewOrm()
  659. o.Begin()
  660. ContractDao := Contract.NewContract(o)
  661. ContractProductDao := Contract.NewContractProduct(o)
  662. contract, err := ContractDao.Read_Contract_ById(Id)
  663. if err != nil || contract.T_type == 1 {
  664. o.Rollback()
  665. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  666. c.ServeJSON()
  667. return
  668. }
  669. oldT_number := contract.T_number
  670. if T_number != oldT_number {
  671. _, err1 := ContractDao.Read_Contract_ByT_number(T_number)
  672. if err1 == nil {
  673. o.Rollback()
  674. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("合同编号%s已存在!", T_number)}
  675. c.ServeJSON()
  676. return
  677. }
  678. contract.T_number = T_number
  679. }
  680. if T_money > 0 {
  681. contract.T_money = float32(T_money)
  682. }
  683. if T_discount > 0 {
  684. contract.T_discount = float32(T_discount)
  685. }
  686. if len(T_date) > 0 {
  687. contract.T_date = T_date
  688. }
  689. if len(T_remark) > 0 {
  690. contract.T_remark = T_remark
  691. }
  692. if len(T_pdf) > 0 {
  693. contract.T_pdf = T_pdf
  694. }
  695. if len(T_project) > 0 {
  696. contract.T_project = T_project
  697. }
  698. if len(T_recoveries) > 0 {
  699. contract.T_recoveries = T_recoveries
  700. _, contract.T_recoveries_money = Contract.ContractToContractFinance_R(T_recoveries)
  701. }
  702. if len(T_invoice) > 0 {
  703. contract.T_invoice = T_invoice
  704. _, contract.T_invoice_money = Contract.ContractToContractFinance_R(T_invoice)
  705. }
  706. if len(T_start_date) > 0 {
  707. contract.T_start_date = T_start_date
  708. }
  709. if len(T_end_date) > 0 {
  710. contract.T_end_date = T_end_date
  711. }
  712. if len(T_submit) > 0 {
  713. contract.T_submit = T_submit
  714. }
  715. err = ContractDao.Update_Contract(contract, "T_number", "T_money", "T_discount", "T_date", "T_remark", "T_pdf", "T_State", "T_project",
  716. "T_recoveries", "T_recoveries_money", "T_invoice", "T_invoice_money", "T_start_date", "T_end_date", "T_submit")
  717. if err != nil {
  718. o.Rollback()
  719. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  720. c.ServeJSON()
  721. return
  722. }
  723. if len(T_product) == 0 {
  724. o.Rollback()
  725. c.Data["json"] = lib.JSONS{Code: 202, Msg: "产品明细不能为空"}
  726. c.ServeJSON()
  727. return
  728. }
  729. productList := lib.SplitString(T_product, "|")
  730. cpl, _ := ContractProductDao.Read_ContractProductList_ByT_number(oldT_number)
  731. needToDeleteList, allList := Check_NeedToDelete_Product_List(productList, cpl)
  732. // 检查编辑时需要修改的产品
  733. for k, v := range needToDeleteList {
  734. id, _ := strconv.Atoi(strings.Split(k, "-")[0])
  735. productId, _ := strconv.Atoi(strings.Split(k, "-")[1])
  736. if v > 0 {
  737. o.Rollback()
  738. product, _ := Basic.Read_Product_ById(productId)
  739. c.Data["json"] = lib.JSONS{Code: 202, Msg: fmt.Sprintf("%s已(部分)出库,禁止删除!", product.T_name)}
  740. c.ServeJSON()
  741. return
  742. }
  743. cp := Contract.ContractProduct{Id: id}
  744. err = ContractProductDao.Delete_ContractProduct(cp)
  745. if err != nil {
  746. o.Rollback()
  747. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  748. c.ServeJSON()
  749. return
  750. }
  751. }
  752. for _, v := range productList {
  753. product_id, _ := strconv.Atoi(strings.Split(v, ",")[0])
  754. num, _ := strconv.Atoi(strings.Split(v, ",")[1])
  755. price := lib.StringToFloat64TwoDecimal(strings.Split(v, ",")[2])
  756. oldContractProduct, ok := allList[product_id]
  757. if !ok {
  758. contractProduct := Contract.ContractProduct{
  759. T_contract_number: T_number,
  760. T_product_id: product_id,
  761. T_product_total: num,
  762. T_product_out: 0, // 已出库数量
  763. T_State: 1, // 1-未出库 2-已部分出库 3-已全部出库
  764. }
  765. _, err = ContractProductDao.Add_ContractProduct(contractProduct)
  766. if err != nil {
  767. o.Rollback()
  768. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  769. c.ServeJSON()
  770. return
  771. } else {
  772. continue
  773. }
  774. }
  775. // 修改产品总数量
  776. contractProductId, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[0])
  777. oldNum, _ := strconv.Atoi(strings.Split(oldContractProduct, "-")[1])
  778. if oldNum == num && T_number == oldT_number {
  779. continue
  780. }
  781. cp := Contract.ContractProduct{
  782. Id: contractProductId,
  783. T_contract_number: T_number,
  784. T_product_total: num,
  785. T_price: float32(price), // 价格
  786. }
  787. err = ContractProductDao.Update_ContractProduct(cp, "T_product_total", "T_contract_number")
  788. if err != nil {
  789. o.Rollback()
  790. c.Data["json"] = lib.JSONS{Code: 203, Msg: "修改失败"}
  791. c.ServeJSON()
  792. return
  793. }
  794. }
  795. o.Commit()
  796. err = Update_VerifyContract_State(contract.T_customer_id, "UPDATE")
  797. if err != nil {
  798. c.Data["json"] = lib.JSONS{Code: 202, Msg: "更新状态失败"}
  799. c.ServeJSON()
  800. return
  801. }
  802. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "修改", contract)
  803. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  804. c.ServeJSON()
  805. return
  806. }
  807. func (c *ContractController) VerifyContract_Del() {
  808. T_number := c.GetString("T_number")
  809. o := orm.NewOrm()
  810. o.Begin()
  811. ContractDao := Contract.NewContract(o)
  812. ContractProductDao := Contract.NewContractProduct(o)
  813. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  814. if err != nil {
  815. o.Rollback()
  816. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  817. c.ServeJSON()
  818. return
  819. }
  820. err = ContractDao.Delete_Contract(contract)
  821. if err != nil {
  822. o.Rollback()
  823. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  824. c.ServeJSON()
  825. return
  826. }
  827. cpList := ContractProductDao.Read_ContractProduct_List(T_number)
  828. for _, v := range cpList {
  829. err = ContractProductDao.Delete_ContractProduct(v)
  830. if err != nil {
  831. o.Rollback()
  832. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  833. c.ServeJSON()
  834. return
  835. }
  836. }
  837. o.Commit()
  838. err = Update_VerifyContract_State(contract.T_customer_id, "DELETE")
  839. if err != nil {
  840. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改状态失败"}
  841. c.ServeJSON()
  842. return
  843. }
  844. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "删除", T_number)
  845. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  846. c.ServeJSON()
  847. return
  848. }
  849. func Update_VerifyContract_State(T_customer_id, flag string) error {
  850. o := orm.NewOrm()
  851. ContractDao := Contract.NewContract(o)
  852. VerifyContractDao := Contract.NewVerifyContract(o)
  853. vcn := ContractDao.Read_VerifyContract_Newest(T_customer_id)
  854. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  855. if err != nil {
  856. return err
  857. }
  858. if vcn.Id == 0 {
  859. vc.T_sign_times = 0
  860. vc.T_start_date = ""
  861. vc.T_end_date = ""
  862. vc.T_State = 1
  863. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  864. return err
  865. }
  866. if flag == "ADD" {
  867. vc.T_sign_times += 1
  868. } else if flag == "DELETE" {
  869. vc.T_sign_times -= 1
  870. }
  871. if len(vc.T_start_date) == 0 {
  872. vc.T_start_date = vcn.T_start_date
  873. }
  874. if vc.T_end_date != vcn.T_end_date {
  875. vc.T_end_date = vcn.T_end_date
  876. }
  877. nowTime := time.Now()
  878. now := time.Date(nowTime.Year(), nowTime.Month(), nowTime.Day(), 0, 0, 0, 0, time.Local)
  879. end, _ := lib.DateStrToTime(vcn.T_end_date)
  880. now2Month := now.AddDate(0, 2, 0)
  881. // 1-未签约 2-已作废 3-已签约 4-即将到期
  882. state := 1
  883. // 结束时间<今天 已过期
  884. if end.Before(now) {
  885. state = 2
  886. } else {
  887. // 结束时间<今天+2月 即将到期 && 结束时间>今天
  888. if end.Before(now2Month) || end.Equal(now) {
  889. state = 4
  890. NatsServer.AddNews(vcn.T_approver, fmt.Sprintf("【合同即将过期提醒】%s的验证合同将于%s过期!", vc.T_customer, vc.T_end_date), conf.VerifyContractUrl)
  891. }
  892. // 结束时间>今天+2月 已签约
  893. if end.After(now2Month) || end.Equal(now2Month) {
  894. state = 3
  895. }
  896. }
  897. vc.T_State = state
  898. err = VerifyContractDao.Update_VerifyContract(vc, "T_start_date", "T_end_date", "T_sign_times", "T_State")
  899. return err
  900. }
  901. // 验证合同-添加客户
  902. func (c *ContractController) VerifyContract_Add_Customer() {
  903. T_customer := c.GetString("T_customer")
  904. o := orm.NewOrm()
  905. VerifyContractDao := Contract.NewVerifyContract(o)
  906. T_number := ""
  907. for true {
  908. rand_x := 1
  909. T_number = fmt.Sprintf("YZKH-%s", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", int64(rand_x)))
  910. _, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_number)
  911. if err != nil && err.Error() == orm.ErrNoRows.Error() {
  912. break
  913. }
  914. rand_x += 1
  915. }
  916. var_ := Contract.VerifyContract{
  917. T_customer: T_customer,
  918. T_customer_id: T_number,
  919. T_State: 1,
  920. }
  921. _, err := VerifyContractDao.Add_VerifyContract(var_)
  922. if err != nil {
  923. o.Rollback()
  924. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  925. c.ServeJSON()
  926. return
  927. }
  928. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "添加客户", var_)
  929. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_number}
  930. c.ServeJSON()
  931. return
  932. }
  933. // 验证合同-编辑客户
  934. func (c *ContractController) VerifyContract_Update_Customer() {
  935. T_customer_id := c.GetString("T_customer_id")
  936. T_customer := c.GetString("T_customer")
  937. o := orm.NewOrm()
  938. VerifyContractDao := Contract.NewVerifyContract(o)
  939. vc, err := VerifyContractDao.Read_VerifyContract_ByT_customer_id(T_customer_id)
  940. vc.T_customer = T_customer
  941. err = VerifyContractDao.Update_VerifyContract(vc)
  942. if err != nil {
  943. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  944. c.ServeJSON()
  945. return
  946. }
  947. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "编辑客户", vc)
  948. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: T_customer_id}
  949. c.ServeJSON()
  950. return
  951. }
  952. // 验证合同-客户列表
  953. func (c *ContractController) VerifyContract_Customer_List() {
  954. // 分页参数 初始化
  955. page, _ := c.GetInt("page")
  956. if page < 1 {
  957. page = 1
  958. }
  959. page_z, _ := c.GetInt("page_z")
  960. if page_z < 1 {
  961. page_z = conf.Page_size
  962. }
  963. // 查询
  964. T_name := c.GetString("T_name")
  965. T_state, _ := c.GetInt("T_state")
  966. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  967. R_List, R_cnt := VerifyContractDao.Read_VerifyContract_List(T_name, T_state, page, page_z)
  968. var r_jsons lib.R_JSONS
  969. r_jsons.Num = R_cnt
  970. r_jsons.Data = R_List
  971. r_jsons.Page = page
  972. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  973. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  974. c.ServeJSON()
  975. return
  976. }
  977. // 合同回款明细列表
  978. func (c *ContractController) Contract_List_RecoveriesMoney() {
  979. // 分页参数 初始化
  980. page, _ := c.GetInt("page")
  981. if page < 1 {
  982. page = 1
  983. }
  984. page_z, _ := c.GetInt("page_z")
  985. if page_z < 1 {
  986. page_z = conf.Page_size
  987. }
  988. // 查询
  989. T_submit := c.GetString("T_submit")
  990. T_name := c.GetString("T_name")
  991. T_start_date := c.GetString("T_start_date") // 回款开始日期
  992. T_end_date := c.GetString("T_end_date") // 回款结束日期
  993. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  994. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  995. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  996. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  997. userList, _ := NatsServer.Read_User_List_All()
  998. Account.Read_User_All_Map(userList)
  999. Contract.Read_VerifyContract_All_Map()
  1000. ContractDao := Contract.NewContract(orm.NewOrm())
  1001. R_List, R_cnt := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, page, page_z)
  1002. var r_jsons lib.R_JSONS
  1003. r_jsons.Num = R_cnt
  1004. r_jsons.Data = R_List
  1005. r_jsons.Page = page
  1006. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1007. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1008. c.ServeJSON()
  1009. return
  1010. }
  1011. // 导出合同回款明细列表
  1012. func (c *ContractController) Contract_List_RecoveriesMoney_Export() {
  1013. // 查询
  1014. T_submit := c.GetString("T_submit")
  1015. T_name := c.GetString("T_name")
  1016. T_start_date := c.GetString("T_start_date") // 回款开始日期
  1017. T_end_date := c.GetString("T_end_date") // 回款结束日期
  1018. T_recoveries_state, _ := c.GetInt("T_recoveries_state") // 回款状态 1 未回款 2 部分回款 3 全部回款
  1019. T_invoice_state, _ := c.GetInt("T_invoice_state") // 开票状态 1 未开票 2 部分开票 3 全部开票
  1020. T_percentage_state, _ := c.GetInt("T_percentage_state") // 提成状态 1-未打款 2-已打款
  1021. T_mine, _ := c.GetInt("T_mine") // 我的 用于合同提成显示
  1022. userList, _ := NatsServer.Read_User_List_All()
  1023. Account.Read_User_All_Map(userList)
  1024. Contract.Read_VerifyContract_All_Map()
  1025. ContractDao := Contract.NewContract(orm.NewOrm())
  1026. R_List, _ := ContractDao.Read_Contract_List_For_RecoveriesAndInvoice(c.User.T_uuid, T_submit, T_mine, T_name, T_start_date, T_end_date, T_recoveries_state, T_invoice_state, T_percentage_state, 0, 9999)
  1027. title := "销售提成明细表"
  1028. if T_mine == 1 {
  1029. title = fmt.Sprintf("%s-销售提成明细表", c.User.T_name)
  1030. }
  1031. filename := fmt.Sprintf("%s(%s)", title, lib.GetRandstring(6, "0123456789", 0))
  1032. f := excelize.NewFile() // 设置单元格的值
  1033. Style1, _ := f.NewStyle(
  1034. &excelize.Style{
  1035. Font: &excelize.Font{Bold: true, Size: 12, Family: "宋体"},
  1036. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1037. })
  1038. Style2, _ := f.NewStyle(
  1039. &excelize.Style{
  1040. Font: &excelize.Font{Bold: true, Size: 11, Family: "宋体"},
  1041. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  1042. Border: []excelize.Border{
  1043. {Type: "left", Color: "000000", Style: 1},
  1044. {Type: "top", Color: "000000", Style: 1},
  1045. {Type: "bottom", Color: "000000", Style: 1},
  1046. {Type: "right", Color: "000000", Style: 1},
  1047. },
  1048. })
  1049. Style3, _ := f.NewStyle(
  1050. &excelize.Style{
  1051. Font: &excelize.Font{Size: 11, Family: "宋体"},
  1052. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  1053. Border: []excelize.Border{
  1054. {Type: "left", Color: "000000", Style: 1},
  1055. {Type: "top", Color: "000000", Style: 1},
  1056. {Type: "bottom", Color: "000000", Style: 1},
  1057. {Type: "right", Color: "000000", Style: 1},
  1058. },
  1059. })
  1060. f.MergeCell("Sheet1", "A1", "J1")
  1061. f.SetRowStyle("Sheet1", 1, 1, Style1)
  1062. f.SetCellValue("Sheet1", "A1", title)
  1063. f.SetRowHeight("Sheet1", 1, 30)
  1064. // 这里设置表头
  1065. f.SetCellStyle("Sheet1", "A2", "J2", Style2)
  1066. f.SetRowHeight("Sheet1", 2, 20)
  1067. f.SetCellValue("Sheet1", "A2", "公司名称")
  1068. f.SetCellValue("Sheet1", "B2", "合同编号")
  1069. f.SetCellValue("Sheet1", "C2", "签约时间")
  1070. f.SetCellValue("Sheet1", "D2", "合同金额")
  1071. f.SetCellValue("Sheet1", "E2", "回款金额")
  1072. f.SetCellValue("Sheet1", "F2", "回款比例")
  1073. f.SetCellValue("Sheet1", "G2", "销售类型")
  1074. f.SetCellValue("Sheet1", "H2", "销售提成5%")
  1075. f.SetCellValue("Sheet1", "I2", "提成金额")
  1076. f.SetCellValue("Sheet1", "J2", "提成状态")
  1077. // 设置列宽
  1078. f.SetColWidth("Sheet1", "A", "A", 25)
  1079. f.SetColWidth("Sheet1", "B", "I", 15)
  1080. line := 2
  1081. for _, contract := range R_List {
  1082. line += 1
  1083. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), line-2)
  1084. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), contract.T_customer)
  1085. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), contract.T_number)
  1086. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), contract.T_date)
  1087. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), contract.T_discount)
  1088. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), contract.T_recoveries_money)
  1089. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), fmt.Sprintf("%.02f%%", contract.T_recoveries_money/contract.T_discount*100))
  1090. T_type_str := "验证合同"
  1091. if contract.T_type == 1 {
  1092. T_type_str = "销售合同"
  1093. }
  1094. f.SetCellValue("Sheet1", fmt.Sprintf("G%d", line), T_type_str)
  1095. f.SetCellValue("Sheet1", fmt.Sprintf("H%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1096. f.SetCellValue("Sheet1", fmt.Sprintf("I%d", line), fmt.Sprintf("%.02f", contract.T_discount*0.05))
  1097. T_percentage_str := "未打款"
  1098. if contract.T_percentage_state == 2 {
  1099. T_percentage_str = "已打款"
  1100. }
  1101. f.SetCellValue("Sheet1", fmt.Sprintf("J%d", line), T_percentage_str)
  1102. }
  1103. f.SetCellStyle("Sheet1", "A3", fmt.Sprintf("J%d", line), Style3)
  1104. // 保存文件
  1105. if err := f.SaveAs("ofile/" + filename + ".xlsx"); err != nil {
  1106. fmt.Println(err)
  1107. }
  1108. var url string
  1109. //// 上传 OSS
  1110. nats := natslibs.NewNats(Nats.Nats, conf.NatsSubj_Prefix)
  1111. url, is := nats.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+filename+".xlsx", "ofile/"+filename+".xlsx")
  1112. if !is {
  1113. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  1114. c.ServeJSON()
  1115. return
  1116. }
  1117. //删除目录
  1118. err := os.Remove("ofile/" + filename + ".xlsx")
  1119. if err != nil {
  1120. logs.Error(lib.FuncName(), err)
  1121. }
  1122. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  1123. c.ServeJSON()
  1124. return
  1125. }
  1126. func (c *ContractController) Contract_Stat() {
  1127. T_type, _ := c.GetInt("T_type")
  1128. T_year, _ := c.GetInt("T_year")
  1129. type R_JSONS struct {
  1130. ContractMoneyByYear interface{}
  1131. TotalMoneyByYear float32
  1132. RecoveriesMoneyByYear float32
  1133. ContractNumByYear interface{}
  1134. TotalMumByYear int64
  1135. ContractMoneyByMonth interface{}
  1136. TotalMoneyByMonth float32
  1137. RecoveriesMoneyByMonth float32
  1138. ContractNumByMonth interface{}
  1139. TotalMumByMonth int64
  1140. TotalMoneyByThisYear float32 // 今年合同总金额
  1141. RecoveriesMoneyByThisYear float32 // 今年回款总金额
  1142. TotalMumByThisYear int64 // 今年合同总数
  1143. TotalMoneyByLastYear float32 // 去年合同总金额
  1144. RecoveriesMoneyByLastYear float32 // 去年回款总金额
  1145. TotalMumByLastYear int64 // 去年合同总数
  1146. }
  1147. var r_jsons R_JSONS
  1148. thisYear := time.Now().Year()
  1149. if T_year == 0 {
  1150. T_year = thisYear
  1151. }
  1152. ContractDao := Contract.NewContract(orm.NewOrm())
  1153. monryStat, moneyTotal, moneyRecoveries := ContractDao.Stat_ContractMoney_ByYear(T_type)
  1154. numStat, numTotal := ContractDao.Stat_ContractNum_ByYear(T_type)
  1155. monryMonthStat, moneyMonthTotal, moneyMonthRecoveries := ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year)
  1156. numMonthStat, numMonthTotal := ContractDao.Stat_ContractNum_ByMonth(T_type, T_year)
  1157. // 去年
  1158. _, r_jsons.TotalMoneyByLastYear, r_jsons.RecoveriesMoneyByLastYear = ContractDao.Stat_ContractMoney_ByMonth(T_type, T_year-1)
  1159. _, r_jsons.TotalMumByLastYear = ContractDao.Stat_ContractNum_ByMonth(T_type, T_year-1)
  1160. r_jsons.ContractMoneyByYear = monryStat
  1161. r_jsons.TotalMoneyByYear = moneyTotal
  1162. r_jsons.RecoveriesMoneyByYear = moneyRecoveries
  1163. r_jsons.ContractNumByYear = numStat
  1164. r_jsons.TotalMumByYear = numTotal
  1165. r_jsons.ContractMoneyByMonth = monryMonthStat
  1166. r_jsons.TotalMoneyByMonth = moneyMonthTotal
  1167. r_jsons.RecoveriesMoneyByMonth = moneyMonthRecoveries
  1168. r_jsons.ContractNumByMonth = numMonthStat
  1169. r_jsons.TotalMumByMonth = numMonthTotal
  1170. r_jsons.TotalMoneyByThisYear = moneyMonthTotal
  1171. r_jsons.RecoveriesMoneyByThisYear = moneyMonthRecoveries
  1172. r_jsons.TotalMumByThisYear = numMonthTotal
  1173. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1174. c.ServeJSON()
  1175. return
  1176. }
  1177. func Cron_VerifyContract() {
  1178. //创建一个定时任务对象
  1179. c := cron.New(cron.WithSeconds())
  1180. //给对象增加定时任务
  1181. //c.AddFunc("0 */1 * * * ?", Cron_VerifyContract_ChangeState)
  1182. c.AddFunc("@daily", Cron_VerifyContract_ChangeState)
  1183. //启动定时任务
  1184. c.Start()
  1185. defer c.Stop()
  1186. //查询语句,阻塞,让main函数不退出,保持程序运行
  1187. select {}
  1188. }
  1189. // 修改合同状态
  1190. func Cron_VerifyContract_ChangeState() {
  1191. T_date := time.Now().Format("2006-01-02")
  1192. logs.Info("开始处理" + T_date + "验证合同状态")
  1193. VerifyContractDao := Contract.NewVerifyContract(orm.NewOrm())
  1194. R_List, _ := VerifyContractDao.Read_VerifyContract_List("", 0, 0, 999)
  1195. for _, v := range R_List {
  1196. err := Update_VerifyContract_State(v.T_customer_id, "UPDATE")
  1197. if err != nil {
  1198. logs.Error("修改合同状态失败")
  1199. }
  1200. }
  1201. }
  1202. // 修改回款金额及开票金额
  1203. func UpdateVerifyContract_RecoveriesInvoice_Money() {
  1204. ContractDao := Contract.NewContract(orm.NewOrm())
  1205. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1206. for _, verify := range R_List {
  1207. R := Contract.Contract{Id: verify.Id}
  1208. _, R.T_recoveries_money = Contract.ContractToContractFinance_R(verify.T_recoveries)
  1209. _, R.T_invoice_money = Contract.ContractToContractFinance_R(verify.T_invoice)
  1210. ContractDao.Update_Contract(R, "T_recoveries_money", "T_invoice_money")
  1211. }
  1212. }
  1213. // 修改回款时间
  1214. func UpdateVerifyContract_RecoveriesInvoice_Time() {
  1215. ContractDao := Contract.NewContract(orm.NewOrm())
  1216. R_List, _ := ContractDao.Read_VerifyContract_List("", "", 0, 9999)
  1217. for _, verify := range R_List {
  1218. R := Contract.Contract{Id: verify.Id}
  1219. if verify.T_discount == verify.T_recoveries_money {
  1220. recoveriesList, _ := Contract.ContractToContractFinance_R(verify.T_recoveries)
  1221. if len(recoveriesList) > 0 {
  1222. R.T_recoveries_time = recoveriesList[len(recoveriesList)-1].T_date
  1223. ContractDao.Update_Contract(R, "T_recoveries_time")
  1224. }
  1225. }
  1226. }
  1227. }
  1228. // 合同提成打款
  1229. func (c *ContractController) Contract_Percentage_Remit() {
  1230. T_number := c.GetString("T_number")
  1231. o := orm.NewOrm()
  1232. ContractDao := Contract.NewContract(o)
  1233. contract, err := ContractDao.Read_Contract_ByT_number(T_number)
  1234. if err != nil {
  1235. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_number Err!"}
  1236. c.ServeJSON()
  1237. return
  1238. }
  1239. if contract.T_recoveries_money != contract.T_discount {
  1240. c.Data["json"] = lib.JSONS{Code: 202, Msg: "合同未回款,不可打款!"}
  1241. c.ServeJSON()
  1242. return
  1243. }
  1244. contract.T_percentage_state = 2 // 已打款
  1245. err = ContractDao.Update_Contract(contract, "T_percentage_state")
  1246. if err != nil {
  1247. c.Data["json"] = lib.JSONS{Code: 203, Msg: "添加失败"}
  1248. c.ServeJSON()
  1249. return
  1250. }
  1251. NatsServer.AddUserLogs(c.User.T_uuid, "验证合同", "打款", contract)
  1252. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: contract.Id}
  1253. c.ServeJSON()
  1254. return
  1255. }
  1256. func (c *ContractController) User_List() {
  1257. var r_jsons lib.R_JSONS
  1258. // 分页参数 初始化
  1259. page, _ := c.GetInt("page")
  1260. if page < 1 {
  1261. page = 1
  1262. }
  1263. page_z, _ := c.GetInt("page_z")
  1264. if page_z < 1 {
  1265. page_z = conf.Page_size
  1266. }
  1267. T_name := c.GetString("T_name")
  1268. ContractDao := Contract.NewContract(orm.NewOrm())
  1269. uuidList := ContractDao.Read_T_submit_List()
  1270. if len(uuidList) == 0 {
  1271. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  1272. c.ServeJSON()
  1273. return
  1274. }
  1275. R_List, R_cnt, err := NatsServer.Read_User_List_T_uuid(T_name, uuidList, page, page_z)
  1276. if err != nil {
  1277. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败"}
  1278. c.ServeJSON()
  1279. return
  1280. }
  1281. var U_List []userlibs.User
  1282. for _, user := range R_List {
  1283. U_List = append(U_List, user)
  1284. }
  1285. r_jsons.Num = R_cnt
  1286. r_jsons.Data = U_List
  1287. r_jsons.Page = page
  1288. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  1289. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  1290. c.ServeJSON()
  1291. return
  1292. }