contract_approval.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. package contract
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_mobile_admin/models/custom"
  7. "hongze/hongze_mobile_admin/models/tables/admin"
  8. "hongze/hongze_mobile_admin/models/tables/approval_flow"
  9. "hongze/hongze_mobile_admin/models/tables/approval_flow_node"
  10. "hongze/hongze_mobile_admin/models/tables/company"
  11. "hongze/hongze_mobile_admin/models/tables/company_approval_message"
  12. "hongze/hongze_mobile_admin/models/tables/company_log"
  13. "hongze/hongze_mobile_admin/models/tables/contract"
  14. "hongze/hongze_mobile_admin/models/tables/contract_approval"
  15. "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
  16. "hongze/hongze_mobile_admin/models/tables/contract_operation_record"
  17. "hongze/hongze_mobile_admin/models/tables/contract_relation"
  18. "hongze/hongze_mobile_admin/services"
  19. "hongze/hongze_mobile_admin/services/alarm_msg"
  20. "hongze/hongze_mobile_admin/services/flow"
  21. "hongze/hongze_mobile_admin/utils"
  22. "math"
  23. "os"
  24. "strings"
  25. "time"
  26. )
  27. //提交审批
  28. func Apply(contractId int) (err error) {
  29. //获取合同详情
  30. contractInfo, err := GetContractDetail(contractId)
  31. if err != nil {
  32. err = errors.New("获取合同信息失败,Err:" + err.Error())
  33. return
  34. }
  35. if contractInfo == nil {
  36. err = errors.New("合同异常,没有合同信息")
  37. return
  38. }
  39. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  40. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  41. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  42. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  43. //合同状态判断
  44. ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
  45. if !strings.Contains(strings.Join(ignoreStatus, ","), contractInfo.Status) {
  46. err = errors.New("合同状态异常,不允许提交审批,当前合同状态:" + contractInfo.Status)
  47. return
  48. }
  49. //待审内容
  50. applyContent := "生成新模板"
  51. //if contractInfo.SourceId > 0 {
  52. // applyContent = "作废重生成"
  53. //}
  54. if contractInfo.Status == "已驳回" {
  55. applyContent = "驳回重申请"
  56. }
  57. //if contractInfo.Status == "已撤回" {
  58. // applyContent = "撤回重申请"
  59. //}
  60. contractDetailByte, err := json.Marshal(contractInfo)
  61. if err != nil {
  62. err = errors.New(fmt.Sprint("合同状态异常,当前合同数据格式化失败:", err))
  63. return
  64. }
  65. //获取基础审批流
  66. var flowItemInfo *approval_flow.ApprovalFlowItem
  67. if contractInfo.ProductId == 1 {
  68. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(3)
  69. if tmpErr != nil {
  70. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  71. return
  72. }
  73. flowItemInfo = tmpFlowItemInfo
  74. } else {
  75. tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(4)
  76. if tmpErr != nil {
  77. err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
  78. return
  79. }
  80. flowItemInfo = tmpFlowItemInfo
  81. }
  82. //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
  83. contractApproval := &contract_approval.ContractApproval{
  84. ContractId: contractInfo.ContractId,
  85. Status: "待审批",
  86. ApplyContent: applyContent,
  87. ContractDetail: string(contractDetailByte),
  88. ApplyUserId: contractInfo.SellerId,
  89. ApplyUserName: contractInfo.SellerName,
  90. FlowId: flowItemInfo.FlowId,
  91. FlowVersion: flowItemInfo.CurrVersion,
  92. CurrNodeId: flowItemInfo.NodeList[0].NodeId,
  93. ModifyTime: time.Now(),
  94. CreateTime: time.Now(),
  95. }
  96. has, err := contractApproval.CheckPendingByContractId(contractInfo.ContractId, "contract")
  97. if err != nil {
  98. return
  99. }
  100. if has {
  101. err = errors.New("合同异常,不允许提交审批,存在待审核的审批单")
  102. return
  103. }
  104. contractApprovalRecordList := make([]*contract_approval_record.ContractApprovalRecord, 0)
  105. ApproveUserMap := make(map[int]approval_flow_node.User)
  106. for _, nodeItem := range flowItemInfo.NodeList {
  107. //审批流记录
  108. if len(nodeItem.UserList) <= 0 {
  109. err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
  110. return
  111. }
  112. for _, userItem := range nodeItem.UserList {
  113. contractApprovalRecord := &contract_approval_record.ContractApprovalRecord{
  114. Status: "待审批",
  115. ApproveRemark: "",
  116. ApproveUserId: userItem.AdminId,
  117. ApproveUserName: userItem.Name,
  118. ApproveRoleTypeCode: userItem.RoleTypeCode,
  119. NodeId: nodeItem.NodeId,
  120. NodeType: nodeItem.NodeType,
  121. NextNodeId: nodeItem.NextNodeId,
  122. CreateTime: time.Now(),
  123. ModifyTime: time.Now(),
  124. }
  125. contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
  126. ApproveUserMap[userItem.AdminId] = userItem
  127. }
  128. }
  129. err = contractApproval.Apply(contractApproval, contractApprovalRecordList)
  130. if err != nil {
  131. return
  132. }
  133. //添加操作日志
  134. remark := "提交审批"
  135. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecordList[0].ContractApprovalRecordId, "apply", contractInfo.SellerName, remark)
  136. //待办通知
  137. {
  138. //发送消息下级审批人
  139. go messageToNodeUser(contractApprovalRecordList[0].NodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, applyContent)
  140. //发送消息给所有的抄送人
  141. //go messageToAllCcUser(contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, applyContent)
  142. }
  143. return
  144. }
  145. //撤销合同
  146. func CancelApplyByContract(contractInfo *contract.Contract, opUserId int) (err error) {
  147. if contractInfo == nil {
  148. err = errors.New("审批流异常,没有审批流信息")
  149. return
  150. }
  151. //合同状态判断
  152. if contractInfo.Status != "待审批" {
  153. err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
  154. return
  155. }
  156. //校验操作人与合同申请人是否同一人
  157. if contractInfo.SellerId != opUserId {
  158. err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
  159. return
  160. }
  161. //获取最近一次待审批的审批单信息
  162. contractApproval, err := contract_approval.GetLastContractApprovalByContractId(contractInfo.ContractId, "contract")
  163. if err != nil {
  164. return
  165. }
  166. //获取当前待审批的审批流信息
  167. contractApprovalRecord, err := contract_approval_record.GetCurrContractApprovalRecordByContractId(contractApproval.ContractApprovalId)
  168. if err != nil {
  169. return
  170. }
  171. //判断当前节点是否存在上级节点,如果存在,那么说明
  172. if contractApprovalRecord.PrevNodeId > 0 {
  173. err = errors.New("合同已存在审批操作,不允许撤回申请")
  174. return
  175. }
  176. //获取当前审批单中所有待审批的流程流
  177. contractApprovalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
  178. if err != nil {
  179. return
  180. }
  181. //撤回审批流
  182. err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
  183. if err != nil {
  184. return
  185. }
  186. //添加操作日志
  187. remark := "撤回申请"
  188. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
  189. //作废原有消息
  190. for _, contractApprovalRecord := range contractApprovalRecordList {
  191. go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
  192. }
  193. return
  194. }
  195. //撤回审批单
  196. func CancelApply(contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUserId int) (err error) {
  197. if contractApprovalRecord == nil {
  198. err = errors.New("审批流异常,没有审批流信息")
  199. return
  200. }
  201. //审批流状态判断
  202. if contractApprovalRecord.Status != "待审批" {
  203. err = errors.New("审批流状态异常,不允许撤回申请,当前审批流状态:" + contractApprovalRecord.Status)
  204. return
  205. }
  206. //获取审批单详情
  207. contractApproval, err := contract_approval.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
  208. if err != nil {
  209. return
  210. }
  211. //审批单状态判断
  212. if contractApproval.Status != "待审批" {
  213. err = errors.New("审批单状态异常,不允许撤回申请,当前审批单状态:" + contractApproval.Status)
  214. return
  215. }
  216. //获取合同信息
  217. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  218. if err != nil {
  219. return
  220. }
  221. //合同状态判断
  222. if contractInfo.Status != "待审批" {
  223. err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
  224. return
  225. }
  226. //校验操作人与合同申请人是否同一人
  227. if contractInfo.SellerId != opUserId {
  228. err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
  229. return
  230. }
  231. //获取当前审批单中所有待审批的流程流
  232. contractApprovalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
  233. if err != nil {
  234. return
  235. }
  236. err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
  237. if err != nil {
  238. return
  239. }
  240. //添加操作日志
  241. remark := "撤回申请"
  242. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
  243. //作废原有消息
  244. go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
  245. return
  246. }
  247. //校验审批操作权限
  248. func CheckApproveAuth(contractId int, opUser *custom.AdminWx) (contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, err error) {
  249. //合同详情
  250. contractInfo, err = contract.GetContractById(contractId)
  251. if err != nil {
  252. err = errors.New("获取合同失败,Err:" + err.Error())
  253. return
  254. }
  255. //合同状态判断
  256. if contractInfo.Status != "待审批" {
  257. err = errors.New("合同状态异常,不允许驳回申请,当前合同状态:" + contractInfo.Status)
  258. return
  259. }
  260. //获取该最近一条审批单详情
  261. approvalInfo, err = contract_approval.GetLastContractApprovalByContractId(contractInfo.ContractId, "contract")
  262. if err != nil {
  263. err = errors.New("获取审批单失败,Err:" + err.Error())
  264. return
  265. }
  266. if approvalInfo.Status != "待审批" {
  267. err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
  268. return
  269. }
  270. //审批流
  271. approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
  272. if err != nil {
  273. err = errors.New("获取审批失败,Err:" + err.Error())
  274. return
  275. }
  276. for _, tmpApprovalRecord := range approvalRecordList {
  277. if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId {
  278. approvalRecord = tmpApprovalRecord
  279. }
  280. }
  281. if approvalRecord == nil {
  282. err = errors.New("当前账号没有审批权限")
  283. return
  284. }
  285. return
  286. }
  287. //审批驳回
  288. func Reject(contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) {
  289. if contractApprovalRecord == nil {
  290. err = errors.New("审批流异常,没有审批流信息")
  291. return
  292. }
  293. //审批流状态判断
  294. if contractApprovalRecord.Status != "待审批" {
  295. err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + contractApprovalRecord.Status)
  296. return
  297. }
  298. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  299. if contractApprovalRecord.NodeType != "check" {
  300. err = errors.New("当前账号没有审批权限")
  301. return
  302. }
  303. //操作人
  304. opUserId := opUser.AdminId
  305. opUserName := opUser.RealName
  306. //操作权限校验
  307. if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
  308. err = errors.New("当前账号没有审批权限")
  309. return
  310. }
  311. if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
  312. err = errors.New("当前账号没有审批权限,需要指定人操作")
  313. return
  314. }
  315. //审批单状态判断
  316. if approvalInfo.Status != "待审批" {
  317. err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approvalInfo.Status)
  318. return
  319. }
  320. //驳回操作
  321. err = approvalInfo.Reject(approvalInfo, contractApprovalRecord, opUserId, opUserName, rejectRemark)
  322. if err != nil {
  323. return
  324. }
  325. //添加操作日志
  326. remark := "驳回申请"
  327. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "reject", opUserName, remark)
  328. //待办通知
  329. {
  330. content := contractInfo.CompanyName + " 合同模板已驳回"
  331. approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
  332. go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 3, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  333. //发送消息给所有的抄送人
  334. //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 3, contractInfo.CompanyName, content)
  335. }
  336. return
  337. }
  338. //审批通过
  339. func Approved(contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
  340. if contractApprovalRecord == nil {
  341. err = errors.New("审批流异常,没有审批流信息")
  342. return
  343. }
  344. //审批流状态判断
  345. if contractApprovalRecord.Status != "待审批" {
  346. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  347. return
  348. }
  349. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  350. if contractApprovalRecord.NodeType != "check" {
  351. err = errors.New("当前账号没有审批权限")
  352. return
  353. }
  354. //操作人
  355. opUserId := opUser.AdminId
  356. opUserName := opUser.RealName
  357. //操作权限校验
  358. if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
  359. err = errors.New("当前账号没有审批权限")
  360. return
  361. }
  362. if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
  363. err = errors.New("当前账号没有审批权限,需要指定人操作")
  364. return
  365. }
  366. //审批单状态判断
  367. if approvalInfo.Status != "待审批" {
  368. err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approvalInfo.Status)
  369. return
  370. }
  371. //合同状态判断
  372. if contractInfo.Status != "待审批" {
  373. err = errors.New("合同状态异常,不允许审批,当前合同状态:" + contractInfo.Status)
  374. return
  375. }
  376. //if contractApprovalRecord
  377. err = approvalInfo.Approved(approvalInfo, contractApprovalRecord, approvedRemark)
  378. if err != nil {
  379. return
  380. }
  381. //添加操作日志
  382. remark := "审批通过"
  383. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
  384. //发送信息给所有抄送人
  385. content := contractInfo.CompanyName + " 合同模板已审核"
  386. //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
  387. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  388. if contractApprovalRecord.NextNodeId == 0 {
  389. //待办通知(通知销售已经审核通过了)
  390. {
  391. content := contractInfo.CompanyName + " 合同模板已审核"
  392. approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
  393. go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  394. }
  395. //审核通过后的异步操作(pdf生成、修改数据等)
  396. go AfterApproved(contractInfo.ContractId)
  397. } else {
  398. //获取下级节点信息
  399. flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(contractApprovalRecord.NextNodeId)
  400. if tmpErr != nil {
  401. err = tmpErr
  402. return
  403. }
  404. //如果该级节点是抄送类型,那么需要将该节点给处理掉
  405. if flowNodeInfo.NodeType == "cc" {
  406. go approvedByCc(approvedRemark, contractApprovalRecord)
  407. } else {
  408. //发送消息下级审批人
  409. go messageToNodeUser(contractApprovalRecord.NextNodeId, contractInfo.SellerId, approvalInfo.ContractApprovalId, 1, 1, contractInfo.CompanyName, content)
  410. }
  411. }
  412. return
  413. }
  414. //审批通过(实际操作)
  415. func approved(contractInfo *contract.Contract, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
  416. if contractApprovalRecord == nil {
  417. err = errors.New("审批流异常,没有审批流信息")
  418. return
  419. }
  420. //审批流状态判断
  421. if contractApprovalRecord.Status != "待审批" {
  422. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  423. return
  424. }
  425. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  426. if contractApprovalRecord.NodeType != "check" {
  427. err = errors.New("当前账号没有审批权限")
  428. return
  429. }
  430. //操作人
  431. opUserId := opUser.AdminId
  432. opUserName := opUser.RealName
  433. //操作权限校验
  434. if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
  435. err = errors.New("当前账号没有审批权限")
  436. return
  437. }
  438. if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
  439. err = errors.New("当前账号没有审批权限,需要指定人操作")
  440. return
  441. }
  442. //获取审批单详情
  443. contractApproval, err := contract_approval.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
  444. if err != nil {
  445. return
  446. }
  447. //审批单状态判断
  448. if contractApproval.Status != "待审批" {
  449. err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + contractApproval.Status)
  450. return
  451. }
  452. //if contractApprovalRecord
  453. err = contractApproval.Approved(contractApproval, contractApprovalRecord, approvedRemark)
  454. if err != nil {
  455. return
  456. }
  457. //添加操作日志
  458. remark := "审批通过"
  459. _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
  460. //发送信息给所有抄送人
  461. content := contractInfo.CompanyName + " 合同模板已审核"
  462. //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
  463. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  464. if contractApprovalRecord.NextNodeId == 0 {
  465. //待办通知(通知销售已经审核通过了)
  466. {
  467. content := contractInfo.CompanyName + " 合同模板已审核"
  468. approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
  469. go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  470. }
  471. //审核通过后的异步操作(pdf生成、修改数据等)
  472. go AfterApproved(contractInfo.ContractId)
  473. } else {
  474. //获取下级节点信息
  475. flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(contractApprovalRecord.NextNodeId)
  476. if tmpErr != nil {
  477. err = tmpErr
  478. return
  479. }
  480. //如果该级节点是抄送类型,那么需要将该节点给处理掉
  481. if flowNodeInfo.NodeType == "cc" {
  482. go approvedByCc(approvedRemark, contractApprovalRecord)
  483. } else {
  484. //发送消息下级审批人
  485. go messageToNodeUser(contractApprovalRecord.NextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, content)
  486. }
  487. }
  488. return
  489. }
  490. //审批通过(抄送节点)
  491. func approvedByCc(approvedRemark string, sourceContractApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
  492. //下个流程节点id
  493. nextNodeId := 0
  494. //获取审批单中抄送节点的所有数据列表
  495. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(sourceContractApprovalRecord.ContractApprovalId, sourceContractApprovalRecord.NextNodeId)
  496. if err != nil {
  497. return
  498. }
  499. //遍历所有的抄送单
  500. for _, contractApprovalRecord := range contractApprovalRecordList {
  501. nextNodeId = contractApprovalRecord.NextNodeId
  502. //审批流状态判断
  503. if contractApprovalRecord.Status != "待审批" {
  504. err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
  505. return
  506. }
  507. //判断是否审批类型,如果不是审批类型,那么就没有审批权限
  508. if contractApprovalRecord.NodeType != "cc" {
  509. err = errors.New("当前账号不是抄送权限")
  510. return
  511. }
  512. }
  513. //获取审批单详情
  514. contractApproval, err := contract_approval.GetContractApprovalById(sourceContractApprovalRecord.ContractApprovalId)
  515. if err != nil {
  516. return
  517. }
  518. //获取合同信息
  519. contractInfo, err := contract.GetContractById(contractApproval.ContractId)
  520. if err != nil {
  521. return
  522. }
  523. //if contractApprovalRecord
  524. err = contractApproval.ApprovedByCc(contractApproval, contractApprovalRecordList, approvedRemark, nextNodeId)
  525. if err != nil {
  526. return
  527. }
  528. content := contractInfo.CompanyName + " 合同模板已审核"
  529. //发送消息给抄送人
  530. go messageToNodeUser(contractApprovalRecordList[0].NodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
  531. //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
  532. if nextNodeId == 0 {
  533. //待办通知
  534. {
  535. approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
  536. go services.AddCompanyApprovalMessage(sourceContractApprovalRecord.ApproveUserId, contractInfo.SellerId, 0, sourceContractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
  537. }
  538. //审核通过后的异步操作(pdf生成、修改数据等)
  539. go AfterApproved(contractInfo.ContractId)
  540. } else {
  541. //获取下级节点信息
  542. flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(nextNodeId)
  543. if tmpErr != nil {
  544. err = tmpErr
  545. return
  546. }
  547. //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
  548. if flowNodeInfo.NodeType == "cc" {
  549. go approvedByCc(approvedRemark, sourceContractApprovalRecord)
  550. } else {
  551. //如果下级级节点是审批类型
  552. //发送消息下级审批人
  553. go messageToNodeUser(nextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, contractApproval.ApplyContent)
  554. }
  555. }
  556. return
  557. }
  558. //抄送信息给用户(根据节点来推送)
  559. func messageToNodeUser(flowNodeId, createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  560. //获取审批单中抄送节点的所有数据列表
  561. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
  562. if err != nil {
  563. return
  564. }
  565. err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
  566. return
  567. }
  568. //发送信息给所有抄送人
  569. // @param createUserId 消息发起人
  570. // @param contractApprovalId 审批单id
  571. // @param messageType 消息类型:1:申请消息,2:审批结果
  572. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  573. // @param companyName 企业客户名称
  574. // @param content 消息内容
  575. func messageToAllCcUser(createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
  576. //获取审批单中抄送节点的所有数据列表
  577. contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
  578. if err != nil {
  579. return
  580. }
  581. err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
  582. return
  583. }
  584. //抄送信息给用户(发送给所有抄送人)
  585. // @param createUserId 待审批、抄送记录
  586. // @param createUserId 消息发起人
  587. // @param contractApprovalId 审批单id
  588. // @param messageType 消息类型:1:申请消息,2:审批结果
  589. // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
  590. // @param companyName 企业客户名称
  591. // @param content 消息内容
  592. func messageToUser(contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, createUserId, messageType, approvalStatus int, companyName, content string) (err error) {
  593. for _, contractApprovalRecord := range contractApprovalRecordList {
  594. sysUserMobile := ""
  595. sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId)
  596. if sysUser != nil {
  597. sysUserMobile = sysUser.Mobile
  598. }
  599. //接收人,就是审批记录里面的待审核人(抄送人)
  600. receiveUserId := contractApprovalRecord.ApproveUserId
  601. go services.AddCompanyApprovalMessage(createUserId, receiveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, messageType, 2, approvalStatus, companyName, content, content, sysUserMobile, "")
  602. }
  603. return
  604. }
  605. //审批完成后操作
  606. func AfterApproved(contractId int) (err error) {
  607. defer func() {
  608. if err != nil {
  609. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  610. go alarm_msg.SendAlarmMsg("合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", 3)
  611. }
  612. }()
  613. contractDetail, err := GetContractDetail(contractId)
  614. if err != nil {
  615. return
  616. }
  617. //fmt.Println("contractDetail:", contractDetail)
  618. //pdf生成并保存
  619. {
  620. ////获取合同的html模板信息
  621. //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractDetail, "pdf")
  622. //if tmpErr != nil {
  623. // err = tmpErr
  624. // return
  625. //}
  626. //
  627. ////生成pdf
  628. //pdfPath := fmt.Sprint("./static/系统生成合同_", contractId, ".pdf")
  629. //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
  630. //if tmpErr != nil {
  631. // err = tmpErr
  632. // return
  633. //}
  634. //
  635. //defer os.Remove(pdfPath)
  636. //生成word
  637. wordPath := fmt.Sprint("./static/系统生成合同_", contractDetail.ContractId, ".docx")
  638. tmpErr := services.GenerateWordV2(contractDetail, wordPath)
  639. if tmpErr != nil {
  640. err = tmpErr
  641. return
  642. }
  643. defer os.Remove(wordPath)
  644. ////word转pdf
  645. //converterType := "pdf"
  646. //pdfPath, tmpErr := services.FuncDocs2Pdf(utils.LibreOfficePath, wordPath, "./static", converterType)
  647. //if tmpErr != nil {
  648. // err = tmpErr
  649. // return
  650. //}
  651. //defer os.Remove(pdfPath)
  652. //
  653. ////randStr := utils.GetRandStringNoSpecialChar(28)
  654. ////fileName := contractDetail.ContractCode + ".pdf"
  655. //fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".pdf"
  656. ////非正式环境下,文件名上面还是加上随机数
  657. //if utils.RunMode != "release" {
  658. // //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  659. // fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  660. //}
  661. ////上传到阿里云
  662. //resourceUrl, tmpErr := services.UploadAliyun(fileName, pdfPath)
  663. fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".docx"
  664. //非正式环境下,文件名上面还是加上随机数
  665. if utils.RunMode != "release" {
  666. //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  667. fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".docx"
  668. }
  669. //上传到阿里云
  670. resourceUrl, tmpErr := services.UploadAliyun(fileName, wordPath)
  671. if tmpErr != nil {
  672. err = tmpErr
  673. return
  674. }
  675. tmpErr = contract.AddContractPdf(contractId, resourceUrl)
  676. if tmpErr != nil {
  677. err = tmpErr
  678. return
  679. }
  680. }
  681. //修改客户名称
  682. {
  683. //根据社会信用码获取客户信息
  684. companyInfo, tmpErr := company.GetCompanyByCreditCode(contractDetail.CreditCode)
  685. //如果查询异常,且并不是在系统中找不到该社会信用码,那么就异常返回
  686. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  687. err = tmpErr
  688. return
  689. }
  690. //如果客户信息不为空的情况下,那么去校验客户名称
  691. if companyInfo != nil {
  692. if companyInfo.CompanyName != contractDetail.CompanyName || companyInfo.Province != contractDetail.Province || companyInfo.City != contractDetail.City {
  693. companyInfo.CompanyName = contractDetail.CompanyName
  694. companyInfo.Province = contractDetail.Province
  695. companyInfo.City = contractDetail.City
  696. companyInfo.Address = contractDetail.Address
  697. tmpErr = company.ModifyCompany(companyInfo)
  698. if tmpErr != nil {
  699. err = tmpErr
  700. return
  701. }
  702. //新增客户日志
  703. {
  704. companyLog := new(company_log.CompanyLog)
  705. companyLog.CompanyId = companyInfo.CompanyId
  706. companyLog.CompanyName = companyInfo.CompanyName
  707. companyLog.CreditCode = companyInfo.CreditCode
  708. companyLog.Address = companyInfo.Address
  709. companyLog.City = companyInfo.City
  710. companyLog.Province = companyInfo.Province
  711. companyLog.LastUpdatedTime = companyInfo.LastUpdatedTime
  712. companyLog.AdminId = 0
  713. companyLog.LogType = "modify"
  714. companyLog.LogCreateTime = time.Now()
  715. go company_log.AddCompanyLog(companyLog)
  716. }
  717. }
  718. }
  719. }
  720. //修改业务合同的已支付金额
  721. if contractDetail.ContractBusinessType == "代付合同" {
  722. go updateContractPaidPriceByPaymentOnBehalfContractId(contractDetail.ContractId, contractDetail.Price)
  723. }
  724. return
  725. }
  726. //审批完成后操作
  727. func FixPdf(contractId int) (err error) {
  728. defer func() {
  729. if err != nil {
  730. //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
  731. go alarm_msg.SendAlarmMsg("合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", 3)
  732. }
  733. }()
  734. contractDetail, err := GetContractDetail(contractId)
  735. if err != nil {
  736. return
  737. }
  738. //fmt.Println("contractDetail:", contractDetail)
  739. //pdf生成并保存
  740. {
  741. ////获取合同的html模板信息
  742. //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractDetail, "pdf")
  743. //if tmpErr != nil {
  744. // err = tmpErr
  745. // return
  746. //}
  747. //
  748. ////生成pdf
  749. //pdfPath := fmt.Sprint("./static/系统生成合同_", contractId, ".pdf")
  750. //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
  751. //if tmpErr != nil {
  752. // err = tmpErr
  753. // return
  754. //}
  755. //
  756. //defer os.Remove(pdfPath)
  757. //生成word
  758. wordPath := fmt.Sprint("./static/系统生成合同_", contractDetail.ContractId, ".docx")
  759. tmpErr := services.GenerateWordV2(contractDetail, wordPath)
  760. if tmpErr != nil {
  761. err = tmpErr
  762. return
  763. }
  764. defer os.Remove(wordPath)
  765. ////word转pdf
  766. //converterType := "pdf"
  767. //pdfPath, tmpErr := services.FuncDocs2Pdf(utils.LibreOfficePath, wordPath, "./static", converterType)
  768. //if tmpErr != nil {
  769. // err = tmpErr
  770. // return
  771. //}
  772. //defer os.Remove(pdfPath)
  773. //
  774. ////randStr := utils.GetRandStringNoSpecialChar(28)
  775. ////fileName := contractDetail.ContractCode + ".pdf"
  776. //fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".pdf"
  777. ////非正式环境下,文件名上面还是加上随机数
  778. //if utils.RunMode != "release" {
  779. // //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  780. // fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  781. //}
  782. ////上传到阿里云
  783. //resourceUrl, tmpErr := services.UploadAliyun(fileName, pdfPath)
  784. fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".docx"
  785. //非正式环境下,文件名上面还是加上随机数
  786. if utils.RunMode != "release" {
  787. //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
  788. fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".docx"
  789. }
  790. //上传到阿里云
  791. resourceUrl, tmpErr := services.UploadAliyun(fileName, wordPath)
  792. if tmpErr != nil {
  793. err = tmpErr
  794. return
  795. }
  796. tmpErr = contract.AddContractPdf(contractId, resourceUrl)
  797. if tmpErr != nil {
  798. err = tmpErr
  799. return
  800. }
  801. fmt.Println(resourceUrl)
  802. }
  803. return
  804. }
  805. // updateContractPaidPriceByPaymentOnBehalfContractId 根据代付合同id来修改业务合同的支付金额
  806. func updateContractPaidPriceByPaymentOnBehalfContractId(contractId int, price float64) (err error) {
  807. tmpList, tmpErr := contract_relation.GetContractRelationListByPaymentOnBehalfContractId(contractId)
  808. if tmpErr != nil {
  809. return
  810. }
  811. if len(tmpList) < 0 {
  812. err = errors.New("未关联业务合同")
  813. return
  814. }
  815. updatePriceStr := ``
  816. if price > 0 {
  817. updatePriceStr = `+` + fmt.Sprint(price)
  818. } else {
  819. updatePriceStr = `-` + fmt.Sprint(math.Abs(price))
  820. }
  821. err = contract.UpdateBusinessContractPaidPrice(tmpList[0].ContractId, updatePriceStr)
  822. return
  823. }