123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925 |
- package contract
- import (
- "encoding/json"
- "errors"
- "fmt"
- "hongze/hongze_mobile_admin/models/custom"
- "hongze/hongze_mobile_admin/models/tables/admin"
- "hongze/hongze_mobile_admin/models/tables/approval_flow"
- "hongze/hongze_mobile_admin/models/tables/approval_flow_node"
- "hongze/hongze_mobile_admin/models/tables/company"
- "hongze/hongze_mobile_admin/models/tables/company_approval_message"
- "hongze/hongze_mobile_admin/models/tables/company_log"
- "hongze/hongze_mobile_admin/models/tables/contract"
- "hongze/hongze_mobile_admin/models/tables/contract_approval"
- "hongze/hongze_mobile_admin/models/tables/contract_approval_record"
- "hongze/hongze_mobile_admin/models/tables/contract_operation_record"
- "hongze/hongze_mobile_admin/models/tables/contract_relation"
- "hongze/hongze_mobile_admin/services"
- "hongze/hongze_mobile_admin/services/alarm_msg"
- "hongze/hongze_mobile_admin/services/flow"
- "hongze/hongze_mobile_admin/utils"
- "math"
- "os"
- "strings"
- "time"
- )
- //提交审批
- func Apply(contractId int) (err error) {
- //获取合同详情
- contractInfo, err := GetContractDetail(contractId)
- if err != nil {
- err = errors.New("获取合同信息失败,Err:" + err.Error())
- return
- }
- if contractInfo == nil {
- err = errors.New("合同异常,没有合同信息")
- return
- }
- contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
- contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
- contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
- contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
- //合同状态判断
- ignoreStatus := []string{"待提交", "已撤回", "已驳回"}
- if !strings.Contains(strings.Join(ignoreStatus, ","), contractInfo.Status) {
- err = errors.New("合同状态异常,不允许提交审批,当前合同状态:" + contractInfo.Status)
- return
- }
- //待审内容
- applyContent := "生成新模板"
- //if contractInfo.SourceId > 0 {
- // applyContent = "作废重生成"
- //}
- if contractInfo.Status == "已驳回" {
- applyContent = "驳回重申请"
- }
- //if contractInfo.Status == "已撤回" {
- // applyContent = "撤回重申请"
- //}
- contractDetailByte, err := json.Marshal(contractInfo)
- if err != nil {
- err = errors.New(fmt.Sprint("合同状态异常,当前合同数据格式化失败:", err))
- return
- }
- //获取基础审批流
- var flowItemInfo *approval_flow.ApprovalFlowItem
- if contractInfo.ProductId == 1 {
- tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(3)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
- return
- }
- flowItemInfo = tmpFlowItemInfo
- } else {
- tmpFlowItemInfo, tmpErr := flow.GetApprovalFlow(4)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("查找审批流程配置失败:", tmpErr))
- return
- }
- flowItemInfo = tmpFlowItemInfo
- }
- //校验是否存在待审批的审批单(其实没有多大意义,只是为了 异常数据校验)
- contractApproval := &contract_approval.ContractApproval{
- ContractId: contractInfo.ContractId,
- Status: "待审批",
- ApplyContent: applyContent,
- ContractDetail: string(contractDetailByte),
- ApplyUserId: contractInfo.SellerId,
- ApplyUserName: contractInfo.SellerName,
- FlowId: flowItemInfo.FlowId,
- FlowVersion: flowItemInfo.CurrVersion,
- CurrNodeId: flowItemInfo.NodeList[0].NodeId,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- has, err := contractApproval.CheckPendingByContractId(contractInfo.ContractId, "contract")
- if err != nil {
- return
- }
- if has {
- err = errors.New("合同异常,不允许提交审批,存在待审核的审批单")
- return
- }
- contractApprovalRecordList := make([]*contract_approval_record.ContractApprovalRecord, 0)
- ApproveUserMap := make(map[int]approval_flow_node.User)
- for _, nodeItem := range flowItemInfo.NodeList {
- //审批流记录
- if len(nodeItem.UserList) <= 0 {
- err = errors.New(fmt.Sprint("审批流程异常,没有可审批的人员,Err:", err))
- return
- }
- for _, userItem := range nodeItem.UserList {
- contractApprovalRecord := &contract_approval_record.ContractApprovalRecord{
- Status: "待审批",
- ApproveRemark: "",
- ApproveUserId: userItem.AdminId,
- ApproveUserName: userItem.Name,
- ApproveRoleTypeCode: userItem.RoleTypeCode,
- NodeId: nodeItem.NodeId,
- NodeType: nodeItem.NodeType,
- NextNodeId: nodeItem.NextNodeId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- contractApprovalRecordList = append(contractApprovalRecordList, contractApprovalRecord)
- ApproveUserMap[userItem.AdminId] = userItem
- }
- }
- err = contractApproval.Apply(contractApproval, contractApprovalRecordList)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "提交审批"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecordList[0].ContractApprovalRecordId, "apply", contractInfo.SellerName, remark)
- //待办通知
- {
- //发送消息下级审批人
- go messageToNodeUser(contractApprovalRecordList[0].NodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, applyContent)
- //发送消息给所有的抄送人
- //go messageToAllCcUser(contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, applyContent)
- }
- return
- }
- //撤销合同
- func CancelApplyByContract(contractInfo *contract.Contract, opUserId int) (err error) {
- if contractInfo == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //合同状态判断
- if contractInfo.Status != "待审批" {
- err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
- return
- }
- //校验操作人与合同申请人是否同一人
- if contractInfo.SellerId != opUserId {
- err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
- return
- }
- //获取最近一次待审批的审批单信息
- contractApproval, err := contract_approval.GetLastContractApprovalByContractId(contractInfo.ContractId, "contract")
- if err != nil {
- return
- }
- //获取当前待审批的审批流信息
- contractApprovalRecord, err := contract_approval_record.GetCurrContractApprovalRecordByContractId(contractApproval.ContractApprovalId)
- if err != nil {
- return
- }
- //判断当前节点是否存在上级节点,如果存在,那么说明
- if contractApprovalRecord.PrevNodeId > 0 {
- err = errors.New("合同已存在审批操作,不允许撤回申请")
- return
- }
- //获取当前审批单中所有待审批的流程流
- contractApprovalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
- if err != nil {
- return
- }
- //撤回审批流
- err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "撤回申请"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
- //作废原有消息
- for _, contractApprovalRecord := range contractApprovalRecordList {
- go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
- }
- return
- }
- //撤回审批单
- func CancelApply(contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUserId int) (err error) {
- if contractApprovalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许撤回申请,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //获取审批单详情
- contractApproval, err := contract_approval.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //审批单状态判断
- if contractApproval.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许撤回申请,当前审批单状态:" + contractApproval.Status)
- return
- }
- //获取合同信息
- contractInfo, err := contract.GetContractById(contractApproval.ContractId)
- if err != nil {
- return
- }
- //合同状态判断
- if contractInfo.Status != "待审批" {
- err = errors.New("合同状态异常,不允许撤回申请,当前合同状态:" + contractInfo.Status)
- return
- }
- //校验操作人与合同申请人是否同一人
- if contractInfo.SellerId != opUserId {
- err = errors.New("操作人异常,不允许撤回非本人提交的申请:" + contractInfo.Status)
- return
- }
- //获取当前审批单中所有待审批的流程流
- contractApprovalRecordList, err := contract_approval_record.GetPendingContractApprovalRecordListByContractId(contractApproval.ContractApprovalId, contractApprovalRecord.NodeId)
- if err != nil {
- return
- }
- err = contractApproval.Cancel(contractApproval, contractApprovalRecordList)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "撤回申请"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, contractInfo.SellerId, contractApprovalRecord.ContractApprovalRecordId, "cancel_apply", contractInfo.SellerName, remark)
- //作废原有消息
- go company_approval_message.CancelCompanyApprovalMessage(contractApprovalRecord.ContractApprovalRecordId, 2)
- return
- }
- //校验审批操作权限
- func CheckApproveAuth(contractId int, opUser *custom.AdminWx) (contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, approvalRecord *contract_approval_record.ContractApprovalRecord, err error) {
- //合同详情
- contractInfo, err = contract.GetContractById(contractId)
- if err != nil {
- err = errors.New("获取合同失败,Err:" + err.Error())
- return
- }
- //合同状态判断
- if contractInfo.Status != "待审批" {
- err = errors.New("合同状态异常,不允许驳回申请,当前合同状态:" + contractInfo.Status)
- return
- }
- //获取该最近一条审批单详情
- approvalInfo, err = contract_approval.GetLastContractApprovalByContractId(contractInfo.ContractId, "contract")
- if err != nil {
- err = errors.New("获取审批单失败,Err:" + err.Error())
- return
- }
- if approvalInfo.Status != "待审批" {
- err = errors.New("当前审批单状态异常,不允许审批,审批单状态:" + approvalInfo.Status)
- return
- }
- //审批流
- approvalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalIdAndNodeId(approvalInfo.ContractApprovalId, approvalInfo.CurrNodeId)
- if err != nil {
- err = errors.New("获取审批失败,Err:" + err.Error())
- return
- }
- for _, tmpApprovalRecord := range approvalRecordList {
- if tmpApprovalRecord.NodeType == "check" && tmpApprovalRecord.ApproveUserId == opUser.AdminId {
- approvalRecord = tmpApprovalRecord
- }
- }
- if approvalRecord == nil {
- err = errors.New("当前账号没有审批权限")
- return
- }
- return
- }
- //审批驳回
- func Reject(contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, rejectRemark string) (err error) {
- if contractApprovalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许驳回申请,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if contractApprovalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
- //操作人
- opUserId := opUser.AdminId
- opUserName := opUser.RealName
- //操作权限校验
- if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- //审批单状态判断
- if approvalInfo.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许驳回申请,当前审批单状态:" + approvalInfo.Status)
- return
- }
- //驳回操作
- err = approvalInfo.Reject(approvalInfo, contractApprovalRecord, opUserId, opUserName, rejectRemark)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "驳回申请"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "reject", opUserName, remark)
- //待办通知
- {
- content := contractInfo.CompanyName + " 合同模板已驳回"
- approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
- go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 3, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- //发送消息给所有的抄送人
- //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 3, contractInfo.CompanyName, content)
- }
- return
- }
- //审批通过
- func Approved(contractInfo *contract.Contract, approvalInfo *contract_approval.ContractApproval, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
- if contractApprovalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if contractApprovalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
- //操作人
- opUserId := opUser.AdminId
- opUserName := opUser.RealName
- //操作权限校验
- if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- //审批单状态判断
- if approvalInfo.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + approvalInfo.Status)
- return
- }
- //合同状态判断
- if contractInfo.Status != "待审批" {
- err = errors.New("合同状态异常,不允许审批,当前合同状态:" + contractInfo.Status)
- return
- }
- //if contractApprovalRecord
- err = approvalInfo.Approved(approvalInfo, contractApprovalRecord, approvedRemark)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "审批通过"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
- //发送信息给所有抄送人
- content := contractInfo.CompanyName + " 合同模板已审核"
- //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
- //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
- if contractApprovalRecord.NextNodeId == 0 {
- //待办通知(通知销售已经审核通过了)
- {
- content := contractInfo.CompanyName + " 合同模板已审核"
- approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
- go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- //审核通过后的异步操作(pdf生成、修改数据等)
- go AfterApproved(contractInfo.ContractId)
- } else {
- //获取下级节点信息
- flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(contractApprovalRecord.NextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //如果该级节点是抄送类型,那么需要将该节点给处理掉
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, contractApprovalRecord)
- } else {
- //发送消息下级审批人
- go messageToNodeUser(contractApprovalRecord.NextNodeId, contractInfo.SellerId, approvalInfo.ContractApprovalId, 1, 1, contractInfo.CompanyName, content)
- }
- }
- return
- }
- //审批通过(实际操作)
- func approved(contractInfo *contract.Contract, contractApprovalRecord *contract_approval_record.ContractApprovalRecord, opUser *custom.AdminWx, approvedRemark string) (err error) {
- if contractApprovalRecord == nil {
- err = errors.New("审批流异常,没有审批流信息")
- return
- }
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if contractApprovalRecord.NodeType != "check" {
- err = errors.New("当前账号没有审批权限")
- return
- }
- //操作人
- opUserId := opUser.AdminId
- opUserName := opUser.RealName
- //操作权限校验
- if opUser.RoleTypeCode != contractApprovalRecord.ApproveRoleTypeCode {
- err = errors.New("当前账号没有审批权限")
- return
- }
- if contractApprovalRecord.ApproveUserId > 0 && contractApprovalRecord.ApproveUserId != opUserId {
- err = errors.New("当前账号没有审批权限,需要指定人操作")
- return
- }
- //获取审批单详情
- contractApproval, err := contract_approval.GetContractApprovalById(contractApprovalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //审批单状态判断
- if contractApproval.Status != "待审批" {
- err = errors.New("审批单状态异常,不允许审批,当前审批单状态:" + contractApproval.Status)
- return
- }
- //if contractApprovalRecord
- err = contractApproval.Approved(contractApproval, contractApprovalRecord, approvedRemark)
- if err != nil {
- return
- }
- //添加操作日志
- remark := "审批通过"
- _ = contract_operation_record.AddContractOperationRecord(contractInfo.ContractId, opUserId, contractApprovalRecord.ContractApprovalRecordId, "approval", opUserName, remark)
- //发送信息给所有抄送人
- content := contractInfo.CompanyName + " 合同模板已审核"
- //go messageToAllCcUser(opUserId, contractApprovalRecord.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
- //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
- if contractApprovalRecord.NextNodeId == 0 {
- //待办通知(通知销售已经审核通过了)
- {
- content := contractInfo.CompanyName + " 合同模板已审核"
- approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
- go services.AddCompanyApprovalMessage(opUserId, contractInfo.SellerId, 0, contractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- //审核通过后的异步操作(pdf生成、修改数据等)
- go AfterApproved(contractInfo.ContractId)
- } else {
- //获取下级节点信息
- flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(contractApprovalRecord.NextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //如果该级节点是抄送类型,那么需要将该节点给处理掉
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, contractApprovalRecord)
- } else {
- //发送消息下级审批人
- go messageToNodeUser(contractApprovalRecord.NextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, content)
- }
- }
- return
- }
- //审批通过(抄送节点)
- func approvedByCc(approvedRemark string, sourceContractApprovalRecord *contract_approval_record.ContractApprovalRecord) (err error) {
- //下个流程节点id
- nextNodeId := 0
- //获取审批单中抄送节点的所有数据列表
- contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(sourceContractApprovalRecord.ContractApprovalId, sourceContractApprovalRecord.NextNodeId)
- if err != nil {
- return
- }
- //遍历所有的抄送单
- for _, contractApprovalRecord := range contractApprovalRecordList {
- nextNodeId = contractApprovalRecord.NextNodeId
- //审批流状态判断
- if contractApprovalRecord.Status != "待审批" {
- err = errors.New("审批流状态异常,不允许审批,当前审批流状态:" + contractApprovalRecord.Status)
- return
- }
- //判断是否审批类型,如果不是审批类型,那么就没有审批权限
- if contractApprovalRecord.NodeType != "cc" {
- err = errors.New("当前账号不是抄送权限")
- return
- }
- }
- //获取审批单详情
- contractApproval, err := contract_approval.GetContractApprovalById(sourceContractApprovalRecord.ContractApprovalId)
- if err != nil {
- return
- }
- //获取合同信息
- contractInfo, err := contract.GetContractById(contractApproval.ContractId)
- if err != nil {
- return
- }
- //if contractApprovalRecord
- err = contractApproval.ApprovedByCc(contractApproval, contractApprovalRecordList, approvedRemark, nextNodeId)
- if err != nil {
- return
- }
- content := contractInfo.CompanyName + " 合同模板已审核"
- //发送消息给抄送人
- go messageToNodeUser(contractApprovalRecordList[0].NodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 2, 2, contractInfo.CompanyName, content)
- //如果下一个节点属于结束节点,那么通知对应的销售,同时,异步生成合同pdf
- if nextNodeId == 0 {
- //待办通知
- {
- approvalSysUser, _ := admin.GetAdminById(contractInfo.SellerId)
- go services.AddCompanyApprovalMessage(sourceContractApprovalRecord.ApproveUserId, contractInfo.SellerId, 0, sourceContractApprovalRecord.ContractApprovalRecordId, 2, 2, 2, contractInfo.CompanyName, content, content, approvalSysUser.Mobile, "")
- }
- //审核通过后的异步操作(pdf生成、修改数据等)
- go AfterApproved(contractInfo.ContractId)
- } else {
- //获取下级节点信息
- flowNodeInfo, tmpErr := approval_flow_node.GetByNodeId(nextNodeId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //如果下级节点是抄送类型,那么还是需要处理抄送节点逻辑
- if flowNodeInfo.NodeType == "cc" {
- go approvedByCc(approvedRemark, sourceContractApprovalRecord)
- } else {
- //如果下级级节点是审批类型
- //发送消息下级审批人
- go messageToNodeUser(nextNodeId, contractInfo.SellerId, contractApproval.ContractApprovalId, 1, 1, contractInfo.CompanyName, contractApproval.ApplyContent)
- }
- }
- return
- }
- //抄送信息给用户(根据节点来推送)
- func messageToNodeUser(flowNodeId, createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
- //获取审批单中抄送节点的所有数据列表
- contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractIdAndNode(contractApprovalId, flowNodeId)
- if err != nil {
- return
- }
- err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
- return
- }
- //发送信息给所有抄送人
- // @param createUserId 消息发起人
- // @param contractApprovalId 审批单id
- // @param messageType 消息类型:1:申请消息,2:审批结果
- // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
- // @param companyName 企业客户名称
- // @param content 消息内容
- func messageToAllCcUser(createUserId, contractApprovalId, messageType, approvalStatus int, companyName, content string) (err error) {
- //获取审批单中抄送节点的所有数据列表
- contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordListByContractApprovalId(contractApprovalId)
- if err != nil {
- return
- }
- err = messageToUser(contractApprovalRecordList, createUserId, messageType, approvalStatus, companyName, content)
- return
- }
- //抄送信息给用户(发送给所有抄送人)
- // @param createUserId 待审批、抄送记录
- // @param createUserId 消息发起人
- // @param contractApprovalId 审批单id
- // @param messageType 消息类型:1:申请消息,2:审批结果
- // @param approvalStatus 审批状态:1:待审批,2:已审批,3:已驳回"
- // @param companyName 企业客户名称
- // @param content 消息内容
- func messageToUser(contractApprovalRecordList []*contract_approval_record.ContractApprovalRecord, createUserId, messageType, approvalStatus int, companyName, content string) (err error) {
- for _, contractApprovalRecord := range contractApprovalRecordList {
- sysUserMobile := ""
- sysUser, _ := admin.GetAdminById(contractApprovalRecord.ApproveUserId)
- if sysUser != nil {
- sysUserMobile = sysUser.Mobile
- }
- //接收人,就是审批记录里面的待审核人(抄送人)
- receiveUserId := contractApprovalRecord.ApproveUserId
- go services.AddCompanyApprovalMessage(createUserId, receiveUserId, 0, contractApprovalRecord.ContractApprovalRecordId, messageType, 2, approvalStatus, companyName, content, content, sysUserMobile, "")
- }
- return
- }
- //审批完成后操作
- func AfterApproved(contractId int) (err error) {
- defer func() {
- if err != nil {
- //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", 3)
- }
- }()
- contractDetail, err := GetContractDetail(contractId)
- if err != nil {
- return
- }
- //fmt.Println("contractDetail:", contractDetail)
- //pdf生成并保存
- {
- ////获取合同的html模板信息
- //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractDetail, "pdf")
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //
- ////生成pdf
- //pdfPath := fmt.Sprint("./static/系统生成合同_", contractId, ".pdf")
- //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //
- //defer os.Remove(pdfPath)
- //生成word
- wordPath := fmt.Sprint("./static/系统生成合同_", contractDetail.ContractId, ".docx")
- tmpErr := services.GenerateWordV2(contractDetail, wordPath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- defer os.Remove(wordPath)
- ////word转pdf
- //converterType := "pdf"
- //pdfPath, tmpErr := services.FuncDocs2Pdf(utils.LibreOfficePath, wordPath, "./static", converterType)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //defer os.Remove(pdfPath)
- //
- ////randStr := utils.GetRandStringNoSpecialChar(28)
- ////fileName := contractDetail.ContractCode + ".pdf"
- //fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".pdf"
- ////非正式环境下,文件名上面还是加上随机数
- //if utils.RunMode != "release" {
- // //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- // fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- //}
- ////上传到阿里云
- //resourceUrl, tmpErr := services.UploadAliyun(fileName, pdfPath)
- fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".docx"
- //非正式环境下,文件名上面还是加上随机数
- if utils.RunMode != "release" {
- //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".docx"
- }
- //上传到阿里云
- resourceUrl, tmpErr := services.UploadAliyun(fileName, wordPath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpErr = contract.AddContractPdf(contractId, resourceUrl)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- }
- //修改客户名称
- {
- //根据社会信用码获取客户信息
- companyInfo, tmpErr := company.GetCompanyByCreditCode(contractDetail.CreditCode)
- //如果查询异常,且并不是在系统中找不到该社会信用码,那么就异常返回
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- err = tmpErr
- return
- }
- //如果客户信息不为空的情况下,那么去校验客户名称
- if companyInfo != nil {
- if companyInfo.CompanyName != contractDetail.CompanyName || companyInfo.Province != contractDetail.Province || companyInfo.City != contractDetail.City {
- companyInfo.CompanyName = contractDetail.CompanyName
- companyInfo.Province = contractDetail.Province
- companyInfo.City = contractDetail.City
- companyInfo.Address = contractDetail.Address
- tmpErr = company.ModifyCompany(companyInfo)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //新增客户日志
- {
- companyLog := new(company_log.CompanyLog)
- companyLog.CompanyId = companyInfo.CompanyId
- companyLog.CompanyName = companyInfo.CompanyName
- companyLog.CreditCode = companyInfo.CreditCode
- companyLog.Address = companyInfo.Address
- companyLog.City = companyInfo.City
- companyLog.Province = companyInfo.Province
- companyLog.LastUpdatedTime = companyInfo.LastUpdatedTime
- companyLog.AdminId = 0
- companyLog.LogType = "modify"
- companyLog.LogCreateTime = time.Now()
- go company_log.AddCompanyLog(companyLog)
- }
- }
- }
- }
- //修改业务合同的已支付金额
- if contractDetail.ContractBusinessType == "代付合同" {
- go updateContractPaidPriceByPaymentOnBehalfContractId(contractDetail.ContractId, contractDetail.Price)
- }
- return
- }
- //审批完成后操作
- func FixPdf(contractId int) (err error) {
- defer func() {
- if err != nil {
- //go utils.SendEmail("异常提醒:"+utils.RunMode, "合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", utils.EmailSendToUsers)
- go alarm_msg.SendAlarmMsg("合同审批通过后,生成pdf失败,ERR:"+err.Error()+"", 3)
- }
- }()
- contractDetail, err := GetContractDetail(contractId)
- if err != nil {
- return
- }
- //fmt.Println("contractDetail:", contractDetail)
- //pdf生成并保存
- {
- ////获取合同的html模板信息
- //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractDetail, "pdf")
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //
- ////生成pdf
- //pdfPath := fmt.Sprint("./static/系统生成合同_", contractId, ".pdf")
- //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //
- //defer os.Remove(pdfPath)
- //生成word
- wordPath := fmt.Sprint("./static/系统生成合同_", contractDetail.ContractId, ".docx")
- tmpErr := services.GenerateWordV2(contractDetail, wordPath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- defer os.Remove(wordPath)
- ////word转pdf
- //converterType := "pdf"
- //pdfPath, tmpErr := services.FuncDocs2Pdf(utils.LibreOfficePath, wordPath, "./static", converterType)
- //if tmpErr != nil {
- // err = tmpErr
- // return
- //}
- //defer os.Remove(pdfPath)
- //
- ////randStr := utils.GetRandStringNoSpecialChar(28)
- ////fileName := contractDetail.ContractCode + ".pdf"
- //fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".pdf"
- ////非正式环境下,文件名上面还是加上随机数
- //if utils.RunMode != "release" {
- // //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- // fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- //}
- ////上传到阿里云
- //resourceUrl, tmpErr := services.UploadAliyun(fileName, pdfPath)
- fileName := contractDetail.CompanyName + "_" + contractDetail.ContractCode + ".docx"
- //非正式环境下,文件名上面还是加上随机数
- if utils.RunMode != "release" {
- //fileName = contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".pdf"
- fileName = contractDetail.CompanyName + "_" + contractDetail.ContractCode + "_" + utils.GetRandStringNoSpecialChar(10) + ".docx"
- }
- //上传到阿里云
- resourceUrl, tmpErr := services.UploadAliyun(fileName, wordPath)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpErr = contract.AddContractPdf(contractId, resourceUrl)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- fmt.Println(resourceUrl)
- }
- return
- }
- // updateContractPaidPriceByPaymentOnBehalfContractId 根据代付合同id来修改业务合同的支付金额
- func updateContractPaidPriceByPaymentOnBehalfContractId(contractId int, price float64) (err error) {
- tmpList, tmpErr := contract_relation.GetContractRelationListByPaymentOnBehalfContractId(contractId)
- if tmpErr != nil {
- return
- }
- if len(tmpList) < 0 {
- err = errors.New("未关联业务合同")
- return
- }
- updatePriceStr := ``
- if price > 0 {
- updatePriceStr = `+` + fmt.Sprint(price)
- } else {
- updatePriceStr = `-` + fmt.Sprint(math.Abs(price))
- }
- err = contract.UpdateBusinessContractPaidPrice(tmpList[0].ContractId, updatePriceStr)
- return
- }
|