| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- package service
- import (
- "context"
- "database/sql"
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- dao "dashoo.cn/micro/app/dao/contract"
- customerDao "dashoo.cn/micro/app/dao/cust"
- model "dashoo.cn/micro/app/model/contract"
- workflowModel "dashoo.cn/micro/app/model/workflow"
- workflowService "dashoo.cn/micro/app/service/workflow"
- "dashoo.cn/opms_libary/utils"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/myerrors"
- "dashoo.cn/opms_libary/plugin/dingtalk/message"
- "dashoo.cn/opms_libary/plugin/dingtalk/workflow"
- "dashoo.cn/opms_libary/request"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gvalid"
- )
- type CtrContractInvoiceService struct {
- Dao *dao.CtrContractInvoiceDao
- ContractDao *dao.CtrContractDao
- CustomerDao *customerDao.CustCustomerDao
- ctrSrv *CtrContractService
- Tenant string
- userInfo request.UserInfo
- }
- func NewCtrContractInvoiceService(ctx context.Context) (*CtrContractInvoiceService, error) {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, myerrors.TipsError(fmt.Sprintf("获取租户码异常:%s", err.Error())) //fmt.Errorf("获取租户码异常:%s", err.Error())
- }
- // 获取用户信息
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, myerrors.TipsError(fmt.Sprintf("获取用户信息异常:%s", err.Error())) //fmt.Errorf("获取用户信息异常:%s", err.Error())
- }
- ctrSrv, err := NewCtrContractService(ctx)
- if err != nil {
- return nil, err
- }
- return &CtrContractInvoiceService{
- Dao: dao.NewCtrContractInvoiceDao(tenant),
- ContractDao: dao.NewCtrContractDao(tenant),
- CustomerDao: customerDao.NewCustCustomerDao(tenant),
- ctrSrv: ctrSrv,
- Tenant: tenant,
- userInfo: userInfo,
- }, nil
- }
- func (s CtrContractInvoiceService) List(ctx context.Context, req *model.CtrContractInvoiceListReq) (int, []*model.CtrContractInvoice, error) {
- dao := &s.Dao.CtrContractInvoiceDao
- if req.SearchText != "" {
- likestr := fmt.Sprintf("%%%s%%", req.SearchText)
- dao = dao.Where("(cust_name LIKE ? || contract_code LIKE ?)", likestr, likestr)
- }
- if req.CustId != 0 {
- dao = dao.Where("cust_id = ?", req.CustId)
- }
- if req.CustName != "" {
- likestr := fmt.Sprintf("%%%s%%", req.CustName)
- dao = dao.Where("cust_name like ?", likestr)
- }
- if req.ContractId != 0 {
- dao = dao.Where("contract_id = ?", req.ContractId)
- }
- if req.ContractCode != "" {
- likestr := fmt.Sprintf("%%%s%%", req.ContractCode)
- dao = dao.Where("contract_code like ?", likestr)
- }
- if req.InvoiceType != "" {
- dao = dao.Where("invoice_type = ?", req.InvoiceType)
- }
- if req.ApproStatus != "" {
- dao = dao.Where("appro_status = ?", req.ApproStatus)
- }
- if req.InvoiceCode != "" {
- dao = dao.Where("invoice_code = ?", req.InvoiceCode)
- }
- if req.CourierCode != "" {
- dao = dao.Where("courier_code = ?", req.CourierCode)
- }
- if req.BeginTime != "" {
- dao = dao.Where("created_time > ?", req.BeginTime)
- }
- if req.EndTime != "" {
- dao = dao.Where("created_time < ?", req.EndTime)
- }
- total, err := dao.Count()
- if err != nil {
- return 0, nil, err
- }
- if req.PageNum != 0 {
- dao = dao.Page(req.GetPage())
- }
- orderby := "created_time desc"
- if req.OrderBy != "" {
- orderby = req.OrderBy
- }
- dao = dao.Order(orderby)
- ents := []*model.CtrContractInvoice{}
- err = dao.Structs(&ents)
- if err != nil && err != sql.ErrNoRows {
- return 0, nil, err
- }
- return total, ents, err
- }
- func (s CtrContractInvoiceService) Add(ctx context.Context, req *model.CtrContractInvoiceAddReq) (int, error) {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return 0, myerrors.TipsError(validErr.Current().Error())
- }
- c, err := s.ContractDao.Where("id = ?", req.ContractId).One()
- if err != nil {
- return 0, err
- }
- if c == nil {
- return 0, myerrors.TipsError(fmt.Sprintf("合同:%d 不存在", req.ContractId))
- }
- if req.InvoiceCode != "" {
- ent, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One()
- if err != nil {
- return 0, err
- }
- if ent != nil {
- return 0, myerrors.TipsError(fmt.Sprintf("发票号码:%s 已存在", req.InvoiceCode))
- }
- }
- if req.CourierCode != "" {
- ent, err := s.Dao.Where("courier_code = ?", req.CourierCode).One()
- if err != nil {
- return 0, err
- }
- if ent != nil {
- return 0, myerrors.TipsError(fmt.Sprintf("快递单号:%s 已存在", req.CourierCode))
- }
- }
- ent := model.CtrContractInvoice{
- CustId: c.CustId,
- CustName: c.CustName,
- ContractId: req.ContractId,
- ContractCode: c.ContractCode,
- ContractAmount: c.ContractAmount,
- InvoiceAmount: req.InvoiceAmount,
- InvoiceDate: req.InvoiceDate,
- InvoiceType: req.InvoiceType,
- ApproStatus: "10",
- InvoiceCode: req.InvoiceCode,
- ActualInvoiceDate: req.ActualInvoiceDate,
- CourierCode: req.CourierCode,
- Remark: req.Remark,
- CreatedBy: int(s.userInfo.Id),
- CreatedName: s.userInfo.NickName,
- CreatedTime: gtime.Now(),
- UpdatedBy: int(s.userInfo.Id),
- UpdatedName: s.userInfo.NickName,
- UpdatedTime: gtime.Now(),
- }
- var id int
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- invoiceId, err := tx.InsertAndGetId("ctr_contract_invoice", ent)
- if err != nil {
- return err
- }
- err = s.ctrSrv.AddDynamicsByCurrentUser(tx, req.ContractId, "创建发票", map[string]interface{}{
- "id": invoiceId,
- "invoiceAmount": req.InvoiceAmount,
- "invoiceDate": req.InvoiceDate,
- "invoiceType": req.InvoiceType,
- "invoiceCode": req.InvoiceCode,
- "actualInvoiceDate": req.ActualInvoiceDate,
- "courierCode": req.CourierCode,
- })
- if err != nil {
- return err
- }
- id = int(invoiceId)
- return nil
- })
- return id, txerr
- }
- func (s CtrContractInvoiceService) Update(ctx context.Context, req *model.CtrContractInvoiceUpdateReq) error {
- validErr := gvalid.CheckStruct(ctx, req, nil)
- if validErr != nil {
- return myerrors.TipsError(validErr.Current().Error())
- }
- ent, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if ent == nil {
- return myerrors.TipsError(fmt.Sprintf("发票不存在: %d", req.Id))
- }
- if req.InvoiceCode != "" {
- exist, err := s.Dao.Where("invoice_code = ?", req.InvoiceCode).One()
- if err != nil {
- return err
- }
- if exist != nil && exist.Id != req.Id {
- return myerrors.TipsError(fmt.Sprintf("发票号码: %s 已存在", req.InvoiceCode))
- }
- }
- if req.CourierCode != "" {
- exist, err := s.Dao.Where("courier_code = ?", req.CourierCode).One()
- if err != nil {
- return err
- }
- if exist != nil && exist.Id != req.Id {
- return myerrors.TipsError(fmt.Sprintf("快递单号: %s 已存在", req.CourierCode))
- }
- }
- toupdate := map[string]interface{}{}
- // if req.CustId != 0 {
- // toupdate["cust_id"] = req.CustId
- // }
- // if req.CustName != 0 {
- // toupdate["cust_name"] = req.CustName
- // }
- // if req.ContractId != 0 {
- // toupdate["contract_id"] = req.ContractId
- // }
- // if req.ContractCode != 0 {
- // toupdate["contract_code"] = req.ContractCode
- // }
- // if req.ContractAmount != 0 {
- // toupdate["contract_amount"] = req.ContractAmount
- // }
- if req.InvoiceAmount != nil {
- toupdate["invoice_amount"] = *req.InvoiceAmount
- }
- if req.InvoiceDate != nil {
- toupdate["invoice_date"] = req.InvoiceDate
- }
- if req.InvoiceType != "" {
- toupdate["invoice_type"] = req.InvoiceType
- }
- if req.ApproStatus != "" {
- toupdate["appro_status"] = req.ApproStatus
- }
- if req.InvoiceCode != "" {
- toupdate["invoice_code"] = req.InvoiceCode
- }
- if req.ActualInvoiceDate != nil {
- toupdate["actual_invoice_date"] = req.ActualInvoiceDate
- }
- if req.CourierCode != "" {
- toupdate["courier_code"] = req.CourierCode
- }
- if req.Remark != nil {
- toupdate["remark"] = *req.Remark
- }
- if len(toupdate) != 0 {
- toupdate["updated_by"] = int(s.userInfo.Id)
- toupdate["updated_name"] = s.userInfo.NickName
- toupdate["updated_time"] = gtime.Now()
- txerr := s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err = tx.Update("ctr_contract_invoice", toupdate, "id= ?", req.Id)
- if err != nil {
- return err
- }
- if req.InvoiceAmount != nil || req.ApproStatus != "" {
- err = s.ctrSrv.UpdateInvoiceAmount(tx, ent.ContractId)
- if err != nil {
- return err
- }
- }
- return nil
- })
- if txerr != nil {
- return txerr
- }
- }
- return nil
- }
- var InvoiceApplyProcessCode = "PROC-D2F97FDE-6277-404D-89B5-91123FEBF1B8" // 申请发票
- func (s CtrContractInvoiceService) InvoiceApply(ctx context.Context, req *model.CtrContractInvoiceInvoiceApplyReq) error {
- invoice, err := s.Dao.Where("id = ?", req.Id).One()
- if err != nil {
- return err
- }
- if invoice == nil {
- return myerrors.TipsError("发票不存在")
- }
- if invoice.ApproStatus != "10" {
- return myerrors.TipsError("发票当前状态不可提交审核")
- }
- contract, err := s.ContractDao.Where("id = ?", invoice.ContractId).One()
- if err != nil {
- return err
- }
- if contract == nil {
- return myerrors.TipsError("发票所属合同不存在")
- }
- cust, err := s.CustomerDao.Where("id = ?", invoice.CustId).One()
- if err != nil {
- return err
- }
- if cust == nil {
- return myerrors.TipsError("发票所属合同客户不存在")
- }
- allReceive := "未回全款"
- if req.AllReceive {
- allReceive = "已回全款"
- }
- product, err := s.ctrSrv.CtrProductDao.Where("contract_id = ?", contract.Id).All()
- if err != nil {
- return err
- }
- // productForm := []*workflow.StartProcessInstanceRequestFormComponentValuesDetails{}
- // for _, p := range product {
- // productForm = append(productForm, &workflow.StartProcessInstanceRequestFormComponentValuesDetails{
- // Details: []*workflow.StartProcessInstanceRequestFormComponentValuesDetailsDetails{
- // {
- // Id: utils.String("TextField_R967EJ9XVWW0"),
- // Name: utils.String("产品名称"),
- // Value: utils.String(p.ProdName),
- // },
- // {
- // Id: utils.String("TextField_18AAHJYNVUBG0"),
- // Name: utils.String("产品型号"),
- // Value: utils.String(p.ProdCode),
- // },
- // {
- // Id: utils.String("NumberField_1RFPF86Y699C0"),
- // Name: utils.String("产品数量"),
- // Value: utils.String(strconv.Itoa(p.ProdNum)),
- // },
- // },
- // })
- // }
- productForm := []interface{}{}
- for _, p := range product {
- productForm = append(productForm, []*workflow.StartProcessInstanceRequestFormComponentValuesDetailsDetails{
- {
- Id: utils.String("TextField_R967EJ9XVWW0"),
- Name: utils.String("产品名称"),
- Value: utils.String(p.ProdName),
- },
- {
- Id: utils.String("TextField_18AAHJYNVUBG0"),
- Name: utils.String("产品型号"),
- Value: utils.String(p.ProdCode),
- },
- {
- Id: utils.String("NumberField_1RFPF86Y699C0"),
- Name: utils.String("产品数量"),
- Value: utils.String(strconv.Itoa(p.ProdNum)),
- },
- })
- }
- productFormByte, err := json.Marshal(productForm)
- if err != nil {
- return err
- }
- workflowSrv, err := workflowService.NewFlowService(ctx)
- if err != nil {
- return err
- }
- bizCode := strings.Join([]string{
- strconv.Itoa(invoice.Id),
- strconv.Itoa(s.userInfo.Id),
- }, ":")
- _, err = workflowSrv.StartProcessInstance(bizCode, "31", "", &workflow.StartProcessInstanceRequest{
- ProcessCode: &InvoiceApplyProcessCode,
- FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
- {
- Id: utils.String("DDSelectField_FSWX8IG61HC0"),
- Name: utils.String("单选框"),
- Value: utils.String(allReceive),
- },
- {
- Id: utils.String("TextField_LUZ9EQ3IJB40"),
- Name: utils.String("合同编号"),
- Value: utils.String(contract.ContractCode),
- },
- {
- Id: utils.String("TextField_A0V5RQK1BC80"),
- Name: utils.String("客户编号"),
- Value: utils.String(cust.CustCode),
- },
- {
- Id: utils.String("TextField_1E2J1LS6H9HC0"),
- Name: utils.String("客户名称"),
- Value: utils.String(cust.CustName),
- },
- {
- Id: utils.String("TableField_1IGTG9NX08U80"),
- Name: utils.String("表格"),
- // Details: productForm,
- Value: utils.String(string(productFormByte)),
- },
- {
- Id: utils.String("MoneyField_1D8481FCI5FK0"),
- Name: utils.String("合同金额(元)"),
- Value: utils.String(strconv.FormatFloat(contract.ContractAmount, 'f', 2, 64)),
- },
- {
- Id: utils.String("MoneyField_BQ2E2JGXHGO0"),
- Name: utils.String("回款金额(元)"),
- Value: utils.String(strconv.FormatFloat(req.ReceiveAmount, 'f', 2, 64)),
- },
- {
- Id: utils.String("MoneyField_V47BSZICHIO0"),
- Name: utils.String("开票金额(元)"),
- Value: utils.String(strconv.FormatFloat(invoice.InvoiceAmount, 'f', 2, 64)),
- },
- },
- })
- if err != nil {
- return err
- }
- _, err = s.Dao.Where("id = ?", invoice.Id).Data(map[string]interface{}{
- "appro_status": 20,
- }).Update()
- return err
- }
- func InvoiceApplyApproval(ctx context.Context, flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return fmt.Errorf("获取租户码异常:%s", err.Error())
- }
- invoiceDao := dao.NewCtrContractInvoiceDao(tenant)
- bizCode := strings.Split(flow.BizCode, ":")
- if len(bizCode) != 2 {
- return fmt.Errorf("申请发票审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
- }
- invoiceId, err := strconv.Atoi(bizCode[0])
- if err != nil {
- return fmt.Errorf("申请发票审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
- }
- invoice, err := invoiceDao.Where("id = ?", invoiceId).One()
- if err != nil {
- return err
- }
- if invoice == nil {
- return fmt.Errorf("发票不存在:%s Id: %d", flow.BizCode, flow.Id)
- }
- if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
- return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
- }
- if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
- return fmt.Errorf("无法识别的 Result :%s", msg.Result)
- }
- if msg.ProcessType == "terminate" {
- _, err = invoiceDao.Where("id = ?", invoiceId).Data(map[string]interface{}{
- "appro_status": "50",
- }).Update()
- return err
- }
- pass := msg.Result == "agree"
- if !pass {
- _, err = invoiceDao.Where("id = ?", invoiceId).Data(map[string]interface{}{
- "appro_status": "40",
- }).Update()
- return err
- }
- _, err = invoiceDao.Where("id = ?", invoiceId).Data(map[string]interface{}{
- "appro_status": "30",
- }).Update()
- return err
- }
- func (s CtrContractInvoiceService) Delete(ctx context.Context, id []int) error {
- if len(id) == 0 {
- return nil
- }
- return s.Dao.DB.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- contractId := map[int]bool{}
- for _, i := range id {
- ent := model.CtrContractInvoice{}
- err := tx.GetStruct(&ent, "select * from ctr_contract_invoice where id = ?", i)
- if err == sql.ErrNoRows {
- continue
- }
- if err != nil {
- return err
- }
- if ent.ApproStatus == "30" {
- contractId[ent.ContractId] = true
- }
- }
- _, err := tx.Delete("ctr_contract_invoice", "id in (?)", id)
- if err != nil {
- return err
- }
- for cid := range contractId {
- err = s.ctrSrv.UpdateInvoiceAmount(tx, cid)
- if err != nil {
- return err
- }
- }
- return nil
- })
- }
|