|
@@ -274,9 +274,98 @@ func (rg *RegisterController) Add(c *gin.Context) {
|
|
|
|
|
|
|
|
|
if req.Supplement == 1 {
|
|
|
-
|
|
|
-
|
|
|
- }else {
|
|
|
+
|
|
|
+ if e = fms.CreateContractRegisterAndServicesAndPayMent(ob, serviceList); e != nil {
|
|
|
+ resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if ob.HasPayment == 1 {
|
|
|
+ resp.Fail("合同存在代付不允许添加开票/到款登记", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ prePayOB := new(fms.ContractPrePayment)
|
|
|
+ ppItem, e := prePayOB.Fetch(req.PrePayId)
|
|
|
+ if e != nil {
|
|
|
+ if e == utils.ErrNoRow {
|
|
|
+ resp.Fail("预到款不存在或已被删除", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.FailMsg("获取预到款记录失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dayDiff := ob.EndDate.Sub(ob.StartDate).Hours() / 24
|
|
|
+
|
|
|
+
|
|
|
+ sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
|
|
|
+ if e != nil {
|
|
|
+ resp.FailData("获取销售失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
|
|
|
+ for i := range sellerList {
|
|
|
+ sellerMap[sellerList[i].SellerId] = sellerList[i]
|
|
|
+ }
|
|
|
+
|
|
|
+ v := &fms.ContractInvoice{
|
|
|
+ ContractRegisterId: ob.ContractRegisterId,
|
|
|
+ ContractCode: ob.ContractCode,
|
|
|
+ Amount: ppItem.Amount,
|
|
|
+ OriginAmount: ppItem.OriginAmount,
|
|
|
+ CurrencyUnit: ppItem.CurrencyUnit,
|
|
|
+ InvoiceType: fms.ContractInvoiceTypePrePay,
|
|
|
+ InvoiceDate: ppItem.CreateTime,
|
|
|
+ AdminId: int(adminInfo.AdminId),
|
|
|
+ AdminName: adminInfo.RealName,
|
|
|
+ Remark: ppItem.Remark,
|
|
|
+ }
|
|
|
+ v.Set()
|
|
|
+
|
|
|
+ v.PayType = fmsService.CalculateContractPaymentType(ppItem.Amount, ob.ContractAmount, dayDiff)
|
|
|
+
|
|
|
+
|
|
|
+ logList := make([]*fms.ContractRegisterLog, 0)
|
|
|
+ opData := ""
|
|
|
+ opDataByte, e := json.Marshal(req)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ opData = string(opDataByte)
|
|
|
+ opType := fms.ContractInvoiceTypePrePay
|
|
|
+ newAmount := decimal.NewFromFloat(0).Round(2)
|
|
|
+ a := decimal.NewFromFloat(v.Amount).Round(2)
|
|
|
+ newAmount = newAmount.Add(a)
|
|
|
+ ia, _ := newAmount.Round(2).Float64()
|
|
|
+ logList = append(logList, &fms.ContractRegisterLog{
|
|
|
+ ContractRegisterId: ob.ContractRegisterId,
|
|
|
+ AdminId: int(adminInfo.AdminId),
|
|
|
+ AdminName: adminInfo.RealName,
|
|
|
+ OpData: opData,
|
|
|
+ OpType: opType,
|
|
|
+ CreateTime: nowTime,
|
|
|
+ AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
|
|
|
+ })
|
|
|
+
|
|
|
+
|
|
|
+ ob := new(fms.ContractInvoice)
|
|
|
+ if e := v.Create(); e != nil {
|
|
|
+ resp.FailData("日期格式有误", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ go fmsService.SummaryInvoicePaymentByContractRegisterId(ob.ContractRegisterId)
|
|
|
+
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ logOB := new(fms.ContractRegisterLog)
|
|
|
+ if e := logOB.AddInBatches(logList); e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }()
|
|
|
+ } else {
|
|
|
if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
|
|
|
resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
|
|
|
return
|