contract_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * @description: 合同数据相关操作
  3. * @Author: CP
  4. * @Date: 2020-10-27 11:28:24
  5. * @FilePath: \construction_management\services\contract_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "fmt"
  11. "html"
  12. "log"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/kataras/iris/v12"
  17. "github.com/shopspring/decimal"
  18. "go.mod/comm"
  19. "go.mod/conf"
  20. "go.mod/dao"
  21. "go.mod/datasource"
  22. "go.mod/models"
  23. "go.mod/web/viewmodels"
  24. )
  25. //定义项目用户Service接口
  26. type ContractService interface {
  27. ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  28. ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  29. ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  30. ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  31. ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  32. ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  33. ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error)
  34. ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error)
  35. ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error)
  36. ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error)
  37. ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error)
  38. ValidRuleContractRetrunAdd(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  39. ValidRuleContractRetrun(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  40. ValidRuleContractRetrunDel(ctx iris.Context) (*viewmodels.ContractsReturn, error)
  41. ValidRuleContractPaidAdd(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  42. ValidRuleContractPaid(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  43. ValidRuleContractPaidDel(ctx iris.Context) (*viewmodels.ContractsPaid, error)
  44. // 项目节
  45. Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  46. GetSectionTreeContract(attribution string, bidsectionId int, projectId int, treeType int) []*viewmodels.Contracts
  47. GetSecionTree(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  48. SetSection(templateNumber int, bidsectionId int, projectIdInt int, treeType int) error
  49. SectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  50. ContractSectionAdd(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) (*models.CmTreeContracts, error)
  51. SectionSave(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  52. UpdateSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  53. SectionDelete(treeId int, bidsectionId int, projectId int, treeType int) error
  54. MoveDepth(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  55. MoveSerial(sectionData *viewmodels.TreeSectionContract, bidsectionId int, projectId int, treeType int) error
  56. GetSecionTreeNotContract(bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract
  57. GetContract(contractId int) *viewmodels.Contracts
  58. Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  59. Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  60. Delete(projectId int, bidsectionId int, treeId int, id int) error
  61. Close(projectId int, bidsectionId int, treeId int, id int, treeType int) error
  62. Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error
  63. GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{}
  64. ReturnCreate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  65. ReturnUpdate(returnData *viewmodels.ContractsReturn, projectId int, bidsectionId int, contractsId int, id int) error
  66. ReturnAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsReturn
  67. ReturnDelete(projectId int, bidsectionId int, contractsId int, id int) error
  68. //支出合同
  69. AddExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  70. UpdateExpenditure(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error
  71. DeleteExpenditure(projectId int, bidsectionId int, treeId int, id int) error
  72. // 已支付
  73. PaidAll(projectId int, bidsectionId int, contractsId int, page int) []*viewmodels.ContractsPaid
  74. PaidCreate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, projectAccountId int) error
  75. PaidUpdate(returnData *viewmodels.ContractsPaid, projectId int, bidsectionId int, contractsId int, id int) error
  76. PaidDelete(projectId int, bidsectionId int, contractsId int, id int) error
  77. }
  78. //返回service操作类
  79. type contractService struct {
  80. treeContractDao *dao.TreeContractDao
  81. contractDao *dao.ContractDao
  82. contractReturnDao *dao.ContractReturnDao
  83. contractPaidDao *dao.ContractPaidDao
  84. treeDao *dao.TreeDao
  85. annexDao *dao.AnnexDao
  86. }
  87. //创建项目用户service
  88. func NewContractService() ContractService {
  89. return &contractService{
  90. treeContractDao: dao.NewTreeContractDao(datasource.InstanceDbMaster()),
  91. contractDao: dao.NewContractDao(datasource.InstanceDbMaster()),
  92. contractReturnDao: dao.NewContractReturnDao(datasource.InstanceDbMaster()),
  93. contractPaidDao: dao.NewContractPaidDao(datasource.InstanceDbMaster()),
  94. treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  95. annexDao: dao.NewAnnexDao(datasource.InstanceDbMaster()),
  96. }
  97. }
  98. // 升级降级规则验证
  99. func (s *contractService) ValidRuleDepth(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  100. treeSectionVaild := &viewmodels.TreeSectionContract{}
  101. err := ctx.ReadJSON(treeSectionVaild)
  102. if err != nil {
  103. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  104. return treeSectionVaild, err
  105. }
  106. err = treeSectionVaild.ValidateDepth()
  107. if err != nil {
  108. log.Println("参数验证错误, error=", err)
  109. return treeSectionVaild, err
  110. }
  111. return treeSectionVaild, nil
  112. }
  113. // 模板规则验证
  114. func (s *contractService) ValidRuleTemplate(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  115. treeSectionVaild := &viewmodels.TreeSectionContract{}
  116. err := ctx.ReadJSON(treeSectionVaild)
  117. if err != nil {
  118. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  119. return treeSectionVaild, err
  120. }
  121. err = treeSectionVaild.ValidateTemplate()
  122. if err != nil {
  123. log.Println("参数验证错误, error=", err)
  124. return treeSectionVaild, err
  125. }
  126. return treeSectionVaild, nil
  127. }
  128. // 模板规则新增项目节
  129. func (s *contractService) ValidRuleSectionAdd(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  130. treeSectionVaild := &viewmodels.TreeSectionContract{}
  131. err := ctx.ReadJSON(treeSectionVaild)
  132. if err != nil {
  133. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  134. return treeSectionVaild, err
  135. }
  136. err = treeSectionVaild.ValidateSectionAdd()
  137. if err != nil {
  138. log.Println("参数验证错误, error=", err)
  139. return treeSectionVaild, err
  140. }
  141. return treeSectionVaild, nil
  142. }
  143. // 模板规则新增项目节
  144. func (s *contractService) ValidRuleSectionDelete(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  145. treeSectionVaild := &viewmodels.TreeSectionContract{}
  146. err := ctx.ReadForm(treeSectionVaild)
  147. if err != nil {
  148. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  149. return treeSectionVaild, err
  150. }
  151. err = treeSectionVaild.ValidateSectionDelete()
  152. if err != nil {
  153. log.Println("参数验证错误, error=", err)
  154. return treeSectionVaild, err
  155. }
  156. return treeSectionVaild, nil
  157. }
  158. func (s *contractService) ValidRuleSectionNot(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  159. treeSectionVaild := &viewmodels.TreeSectionContract{}
  160. err := ctx.ReadForm(treeSectionVaild)
  161. if err != nil {
  162. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  163. return treeSectionVaild, err
  164. }
  165. err = treeSectionVaild.ValidateSectionNot()
  166. if err != nil {
  167. log.Println("参数验证错误, error=", err)
  168. return treeSectionVaild, err
  169. }
  170. return treeSectionVaild, nil
  171. }
  172. func (s *contractService) ValidRuleGet(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  173. treeSectionVaild := &viewmodels.TreeSectionContract{}
  174. err := ctx.ReadForm(treeSectionVaild)
  175. if err != nil {
  176. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  177. return treeSectionVaild, err
  178. }
  179. err = treeSectionVaild.ValidateSectionDelete()
  180. if err != nil {
  181. log.Println("参数验证错误, error=", err)
  182. return treeSectionVaild, err
  183. }
  184. return treeSectionVaild, nil
  185. }
  186. // 验证序号相关
  187. func (s *contractService) ValidRuleSerial(ctx iris.Context) (*viewmodels.TreeSectionContract, error) {
  188. treeSectionVaild := &viewmodels.TreeSectionContract{}
  189. err := ctx.ReadJSON(treeSectionVaild)
  190. if err != nil {
  191. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  192. return treeSectionVaild, err
  193. }
  194. err = treeSectionVaild.ValidateSectionSerial()
  195. if err != nil {
  196. log.Println("参数验证错误, error=", err)
  197. return treeSectionVaild, err
  198. }
  199. return treeSectionVaild, nil
  200. }
  201. // 新增合同参数验证
  202. func (s *contractService) ValidRuleContractAdd(ctx iris.Context) (*viewmodels.Contracts, error) {
  203. // 创建一个存放前端传过来参数
  204. contractsVaild := &viewmodels.Contracts{}
  205. // 存放raw的值,放入到contractsVaild
  206. err := ctx.ReadJSON(contractsVaild)
  207. if err != nil {
  208. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  209. return contractsVaild, err
  210. }
  211. // 验证合同传参
  212. err = contractsVaild.ValidateAdd()
  213. if err != nil {
  214. log.Println("参数验证错误, error=", err)
  215. return contractsVaild, err
  216. }
  217. // xss
  218. contractsVaild.Code = html.EscapeString(contractsVaild.Code)
  219. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  220. contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  221. return contractsVaild, nil
  222. }
  223. // 校验编辑接口
  224. func (s *contractService) ValidRuleContractEdi(ctx iris.Context) (*viewmodels.Contracts, error) {
  225. // 创建一个存放前端传过来参数
  226. contractsVaild := &viewmodels.Contracts{}
  227. // 存放raw的值,放入到contractsVaild
  228. err := ctx.ReadJSON(contractsVaild)
  229. if err != nil {
  230. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  231. return contractsVaild, err
  232. }
  233. // 验证合同传参
  234. err = contractsVaild.ValidateEdi()
  235. if err != nil {
  236. log.Println("参数验证错误, error=", err)
  237. return contractsVaild, err
  238. }
  239. contractsVaild.Content = html.EscapeString(contractsVaild.Content)
  240. contractsVaild.Name = html.EscapeString(contractsVaild.Name)
  241. contractsVaild.Price = html.EscapeString(contractsVaild.Price)
  242. contractsVaild.PartyA = html.EscapeString(contractsVaild.PartyA)
  243. contractsVaild.PartyASigner = html.EscapeString(contractsVaild.PartyASigner)
  244. contractsVaild.PartyB = html.EscapeString(contractsVaild.PartyB)
  245. contractsVaild.PartyBSigner = html.EscapeString(contractsVaild.PartyBSigner)
  246. return contractsVaild, nil
  247. }
  248. // 校验删除合同参数
  249. func (s *contractService) ValidRuleContractDel(ctx iris.Context) (*viewmodels.Contracts, error) {
  250. // 创建一个存放前端传过来参数
  251. contractsVaild := &viewmodels.Contracts{}
  252. // 存放raw的值,放入到contractsVaild
  253. err := ctx.ReadForm(contractsVaild)
  254. if err != nil {
  255. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  256. return contractsVaild, err
  257. }
  258. // 验证合同传参
  259. err = contractsVaild.ValidateDel()
  260. if err != nil {
  261. log.Println("参数验证错误, error=", err)
  262. return contractsVaild, err
  263. }
  264. return contractsVaild, nil
  265. }
  266. // 校验关闭合同参数
  267. func (s *contractService) ValidRuleContractClose(ctx iris.Context) (*viewmodels.Contracts, error) {
  268. // 创建一个存放前端传过来参数
  269. contractsVaild := &viewmodels.Contracts{}
  270. // 存放raw的值,放入到contractsVaild
  271. err := ctx.ReadJSON(contractsVaild)
  272. if err != nil {
  273. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  274. return contractsVaild, err
  275. }
  276. // 验证合同传参
  277. err = contractsVaild.ValidateDel()
  278. if err != nil {
  279. log.Println("参数验证错误, error=", err)
  280. return contractsVaild, err
  281. }
  282. return contractsVaild, nil
  283. }
  284. //------------------------------------------------------------
  285. // 获得项目节
  286. func (s *contractService) Get(treeId int, bidsectionId int, projectId int, treeType int) *viewmodels.TreeSectionContract {
  287. // 1.获得项目节
  288. section := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  289. // 2.构造数据
  290. sectionVM := s.makeSectionView(section)
  291. // 3.更新 上移和下一的限制
  292. youngerBrotherList := s.treeContractDao.GetYoungerBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  293. if len(youngerBrotherList) == 0 {
  294. sectionVM.IsEnd = true
  295. }
  296. sectionVM.ElderBrother = true
  297. elderBrotherList := s.treeContractDao.GetElderBrother(section.Serial, section.Depth, section.ParentId, bidsectionId, projectId, treeType)
  298. if len(elderBrotherList) == 0 {
  299. sectionVM.ElderBrother = false
  300. }
  301. return sectionVM
  302. }
  303. // 获得合同详情
  304. func (s *contractService) GetContract(contractId int) *viewmodels.Contracts {
  305. contract := s.contractDao.Get(contractId)
  306. contractsVM := s.makeContractVM(contract)
  307. return &contractsVM
  308. }
  309. // 新增合同
  310. func (s *contractService) Add(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  311. // 1. 项目节存在
  312. contracts := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  313. if contracts.Id == 0 {
  314. return errors.New("未找到项目节")
  315. }
  316. // 2.项目节是没有合同
  317. if contracts.ContractId != 0 {
  318. return errors.New("该项目节上已经存在合同")
  319. }
  320. // 2-1.查找合同编号是否存在
  321. codeData := s.contractDao.GetByCode(projectId, bidsectionId, contractData.Code, 1)
  322. if len(codeData) != 0 {
  323. return errors.New("该合同编号已经存在")
  324. }
  325. // 3.新增合同 --合计标段上的金额
  326. contractsCm := &models.CmContracts{}
  327. contractsCm.Code = contractData.Code
  328. contractsCm.Name = contractData.Name
  329. contractsCm.ContractsType = 1
  330. contractsCm.Price = contractData.Price
  331. contractsCm.Returned = "0"
  332. contractsCm.Paid = "0"
  333. contractsCm.TreeId = treeId
  334. contractsCm.ProjectId = projectId
  335. contractsCm.BidsectionId = bidsectionId
  336. contractsCm.Status = 0
  337. contractsCm.CreateTime = time.Now()
  338. contractsCm.UpdateTime = time.Now()
  339. err := s.contractDao.Add(contractsCm)
  340. if err != nil {
  341. return err
  342. }
  343. // 3.获得该标段下合同总数 - 总收入金额
  344. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  345. // 更新标段目录上合同金额和总数
  346. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  347. if err != nil {
  348. return err
  349. }
  350. return nil
  351. }
  352. // 更新合同
  353. func (s *contractService) Update(contractData *viewmodels.Contracts, projectId int, bidsectionId int, treeId int) error {
  354. // 1. 项目节存在
  355. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  356. if contractsTree.Id == 0 {
  357. return errors.New("未找到项目节")
  358. }
  359. // 2.项目节是没有合同
  360. if contractsTree.ContractId == 0 {
  361. return errors.New("该项目节上没有找到合同")
  362. }
  363. // 3.合同锁定 不能删除
  364. if contractsTree.ContractLocking == 1 {
  365. return errors.New("该合同已锁定")
  366. }
  367. // 4.合同金额不能小于回款金额
  368. returnedPrice, _ := decimal.NewFromString(contractsTree.ContractReturned)
  369. price, err := decimal.NewFromString(contractData.Price)
  370. if err != nil {
  371. return errors.New("金额填写有误")
  372. }
  373. if returnedPrice.GreaterThan(price) {
  374. return errors.New(fmt.Sprintf("合同金额不能低于%s", contractsTree.ContractReturned))
  375. }
  376. contractsCm := &models.CmContracts{}
  377. contractsCm.Id = contractsTree.ContractId
  378. contractsCm.Content = contractData.Content
  379. contractsCm.Name = contractData.Name
  380. contractsCm.Price = contractData.Price
  381. contractsCm.PartyA = contractData.PartyA
  382. contractsCm.PartyASigner = contractData.PartyASigner
  383. contractsCm.PartyB = contractData.PartyB
  384. contractsCm.PartyBSigner = contractData.PartyBSigner
  385. loc, _ := time.LoadLocation("Local")
  386. SignerTime, err := time.ParseInLocation(conf.SysTimeform, contractData.SignerTime, loc)
  387. if err != nil {
  388. return errors.New("签约时间填写异常")
  389. }
  390. contractsCm.SignerTime = SignerTime
  391. contractsCm.Remarks = contractData.Remarks
  392. columns := []string{"Content", "Name", "Price", "PartyA", "PartyASigner", "PartyB", "PartyBSigner"}
  393. err = s.contractDao.Update(contractsCm, columns, projectId, bidsectionId, treeId)
  394. if err != nil {
  395. return err
  396. }
  397. // 3.获得该标段下合同总数 - 总收入金额
  398. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  399. // 更新标段目录上合同金额和总数
  400. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  401. if err != nil {
  402. return err
  403. }
  404. return nil
  405. }
  406. // 删除合同
  407. func (s *contractService) Delete(projectId int, bidsectionId int, treeId int, id int) error {
  408. // 1. 项目节存在
  409. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, 0)
  410. if contractsTree.Id == 0 {
  411. return errors.New("未找到项目节")
  412. }
  413. // 2.项目节是没有合同
  414. if contractsTree.ContractId == 0 {
  415. return errors.New("该项目节上没有找到合同")
  416. }
  417. // 3.合同锁定 不能删除
  418. if contractsTree.ContractLocking == 1 {
  419. return errors.New("该合同已锁定")
  420. }
  421. // 删除合同
  422. err := s.contractDao.Delete(projectId, bidsectionId, treeId, id)
  423. if err != nil {
  424. return err
  425. }
  426. // 3.获得该标段下合同总数 - 总收入金额
  427. contractTotal, priceTotal := s.getContractTotalAndPrice(bidsectionId, projectId, 0)
  428. // 更新标段目录上合同金额和总数
  429. err = s.treeDao.UpdateContractsAndIncomePrice(projectId, bidsectionId, contractTotal, priceTotal)
  430. if err != nil {
  431. return err
  432. }
  433. // 4.更新回款总金额
  434. err = s.contractReturnDao.UpdateTotalPrice(projectId, bidsectionId, id)
  435. if err != nil {
  436. return err
  437. }
  438. return nil
  439. }
  440. // 关闭合同
  441. func (s *contractService) Close(projectId int, bidsectionId int, treeId int, id int, treeType int) error {
  442. // 1. 项目节存在
  443. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  444. if contractsTree.Id == 0 {
  445. return errors.New("未找到项目节")
  446. }
  447. // 2.项目节是没有合同
  448. if contractsTree.ContractId == 0 {
  449. return errors.New("该项目节上没有找到合同")
  450. }
  451. // 3.合同锁定 不能删除
  452. if contractsTree.ContractLocking == 1 {
  453. return errors.New("该合同已锁定")
  454. }
  455. // 关闭合同
  456. err := s.contractDao.Close(projectId, bidsectionId, treeId, id)
  457. if err != nil {
  458. return err
  459. }
  460. return nil
  461. }
  462. // 解锁合同
  463. func (s *contractService) Unlock(projectId int, bidsectionId int, treeId int, id int, treeType int) error {
  464. // 1. 项目节存在
  465. contractsTree := s.treeContractDao.Get(treeId, bidsectionId, projectId, treeType)
  466. if contractsTree.Id == 0 {
  467. return errors.New("未找到项目节")
  468. }
  469. // 2.项目节是没有合同
  470. if contractsTree.ContractId == 0 {
  471. return errors.New("该项目节上没有找到合同")
  472. }
  473. // 解锁合同
  474. err := s.contractDao.Unlock(projectId, bidsectionId, treeId, id)
  475. if err != nil {
  476. return err
  477. }
  478. return nil
  479. }
  480. //获得合同收入概况
  481. func (s *contractService) GetSurvey(bidsectionId int, projectId int, contractsType int) map[string]interface{} {
  482. // 1.获得收入合同
  483. year := time.Now().Year()
  484. incomeList := s.contractDao.GetTypeYear(bidsectionId, projectId, contractsType, year)
  485. // 2.初始化
  486. totalContractPrice := 0.00
  487. totalTypePrice := 0.00
  488. performNumber := 0
  489. closeNumber := 0
  490. uncloseNumber := 0
  491. // 3.当年数据初始化
  492. returnDate := map[string]float64{
  493. fmt.Sprintf("%d-01", year): 0.00,
  494. fmt.Sprintf("%d-02", year): 0.00,
  495. fmt.Sprintf("%d-03", year): 0.00,
  496. fmt.Sprintf("%d-04", year): 0.00,
  497. fmt.Sprintf("%d-05", year): 0.00,
  498. fmt.Sprintf("%d-06", year): 0.00,
  499. fmt.Sprintf("%d-07", year): 0.00,
  500. fmt.Sprintf("%d-08", year): 0.00,
  501. fmt.Sprintf("%d-09", year): 0.00,
  502. fmt.Sprintf("%d-10", year): 0.00,
  503. fmt.Sprintf("%d-11", year): 0.00,
  504. fmt.Sprintf("%d-12", year): 0.00,
  505. }
  506. contractIds := []string{}
  507. for _, item := range incomeList {
  508. contractPrice, _ := strconv.ParseFloat(item.Price, 64)
  509. totalContractPrice = totalContractPrice + contractPrice
  510. // 回款金额或已支付金额合计
  511. contractIds = append(contractIds, strconv.Itoa(item.Id))
  512. // typePrice := 0.00
  513. // if contractsType == 1 {
  514. // typePrice, _ = strconv.ParseFloat(item.Returned, 64)
  515. // totalTypePrice = totalTypePrice + typePrice
  516. // } else {
  517. // typePrice, _ = strconv.ParseFloat(item.Paid, 64)
  518. // totalTypePrice = totalTypePrice + typePrice
  519. // }
  520. if item.Status == 0 {
  521. performNumber = performNumber + 1
  522. } else if item.Status == 1 {
  523. uncloseNumber = uncloseNumber + 1
  524. } else if item.Status == 2 {
  525. closeNumber = closeNumber + 1
  526. }
  527. // returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] = returnDate[item.CreateTime.Format(conf.SysTimeformMonth)] + typePrice
  528. }
  529. contractInId := strings.Join(contractIds, ",")
  530. if contractsType == 1 {
  531. list := s.contractReturnDao.GetInContractsIds(contractInId)
  532. for _, item := range list {
  533. typePrice := 0.00
  534. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  535. totalTypePrice = totalTypePrice + typePrice
  536. // :=decimal.NewFromFloat(typePrice)
  537. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  538. }
  539. } else {
  540. list := s.contractPaidDao.GetInContractsIds(contractInId)
  541. for _, item := range list {
  542. typePrice := 0.00
  543. typePrice, _ = strconv.ParseFloat(item.Price, 64)
  544. totalTypePrice = totalTypePrice + typePrice
  545. returnDate[item.Time.Format(conf.SysTimeformMonth)], _ = decimal.NewFromFloat(returnDate[item.Time.Format(conf.SysTimeformMonth)]).Add(decimal.NewFromFloat(typePrice)).Float64()
  546. }
  547. }
  548. totalContractPrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalContractPrice), 64)
  549. totalTypePrice, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalTypePrice), 64)
  550. // ac := accounting.Accounting{Symbol: "", Precision: 2}
  551. surveryData := map[string]interface{}{
  552. "totalContractPrice": totalContractPrice,
  553. "totalContractPriceShow": totalContractPrice,
  554. // "totalReturnPrice": ac.FormatMoney(totalReturnPrice),
  555. "performNumber": performNumber,
  556. "closeNumber": closeNumber,
  557. "uncloseNumber": uncloseNumber,
  558. "returnDate": returnDate,
  559. }
  560. if contractsType == 1 {
  561. surveryData["totalReturnPriceShow"] = totalTypePrice
  562. } else {
  563. surveryData["totalPaidPriceShow"] = totalTypePrice
  564. }
  565. return surveryData
  566. }
  567. // 获得合同总数量和总金额
  568. func (s *contractService) getContractTotalAndPrice(bidsectionId int, projectId int, treeType int) (contractTotal int, priceTotal float64) {
  569. contractListAll := s.treeContractDao.GetContractAll(bidsectionId, projectId)
  570. // 获得收入合同
  571. contractList := s.treeContractDao.GetContract(bidsectionId, projectId, treeType)
  572. priceTotal = 0.00
  573. for _, item := range contractList {
  574. contractPrice, _ := strconv.ParseFloat(item.ContractPrice, 64)
  575. priceTotal = priceTotal + contractPrice
  576. }
  577. // 合同总数
  578. // contractTotal = len(contractList) + 1
  579. contractTotal = len(contractListAll)
  580. // 合同收入总金额
  581. // price, _ := strconv.ParseFloat(priceString, 64)
  582. // priceTotal = priceTotal + price
  583. // 保留2位小数
  584. priceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", priceTotal), 64)
  585. return contractTotal, priceTotal
  586. }
  587. // 构建合同详情视图
  588. func (s *contractService) makeContractVM(contract *models.CmContracts) viewmodels.Contracts {
  589. contractsVM := viewmodels.Contracts{}
  590. if contract.Id == 0 {
  591. return contractsVM
  592. }
  593. id, _ := comm.AesEncrypt(strconv.Itoa(contract.Id), conf.SignSecret)
  594. treeId, _ := comm.AesEncrypt(strconv.Itoa(contract.TreeId), conf.SignSecret)
  595. bidsectionId, _ := comm.AesEncrypt(strconv.Itoa(contract.BidsectionId), conf.SignSecret)
  596. contractsVM.Id = id
  597. contractsVM.TreeId = treeId
  598. contractsVM.ContractsType = contract.ContractsType
  599. contractsVM.BidsectionId = bidsectionId
  600. contractsVM.Name = contract.Name
  601. contractsVM.Content = contract.Content
  602. contractsVM.Code = contract.Code
  603. contractsVM.PartyA = contract.PartyA
  604. contractsVM.PartyASigner = contract.PartyASigner
  605. contractsVM.PartyB = contract.PartyB
  606. contractsVM.PartyBSigner = contract.PartyBSigner
  607. contractsVM.Remarks = contract.Remarks
  608. price, _ := strconv.ParseFloat(contract.Price, 64)
  609. returned, _ := strconv.ParseFloat(contract.Returned, 64)
  610. paid, _ := strconv.ParseFloat(contract.Paid, 64)
  611. contractsVM.Price = fmt.Sprintf("%G", price)
  612. contractsVM.Returned = fmt.Sprintf("%G", returned)
  613. contractsVM.Paid = fmt.Sprintf("%G", paid)
  614. contractsVM.Status = contract.Status
  615. contractsVM.Locking = contract.Locking
  616. contractsVM.CreateTime = contract.CreateTime.Format(conf.SysTimeform)
  617. contractsVM.UpdateTime = contract.UpdateTime.Format(conf.SysTimeform)
  618. // nilTime := time.Time{}
  619. // contract.SignerTime != nilTime
  620. if !contract.SignerTime.IsZero() {
  621. contractsVM.SignerTime = contract.SignerTime.Format(conf.SysTimeform)
  622. }
  623. return contractsVM
  624. }