ziwen 1 year ago
parent
commit
95ff7f5ba7

+ 10 - 8
controller/contract/pre_register.go

@@ -829,14 +829,16 @@ func (rg *PreRegisterController) Detail(c *gin.Context) {
 		return
 	}
 
-	dupList, e := fms.GetDuplicateContractInvoiceDetailItemListWithType(nItme.CompanyName, nItme.StartDate, nItme.EndDate, req.InvoiceType)
-	if e != nil {
-		err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
-		return
-	}
-	for _, v := range dupList {
-		if v.ContractRegisterId != req.ContractRegisterId {
-			list = append(list, v)
+	if req.DetailType == 1 {
+		dupList, e := fms.GetDuplicateContractInvoiceDetailItemListWithType(nItme.CompanyName, nItme.StartDate, nItme.EndDate, req.InvoiceType)
+		if e != nil {
+			err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
+			return
+		}
+		for _, v := range dupList {
+			if v.ContractRegisterId != req.ContractRegisterId {
+				list = append(list, v)
+			}
 		}
 	}
 

+ 211 - 7
controller/contract/register.go

@@ -385,6 +385,9 @@ func (rg *RegisterController) Add(c *gin.Context) {
 	dayDiff := ob.EndDate.Sub(ob.StartDate).Hours() / 24
 	// 新增合同及套餐
 	logList := make([]*fms.ContractRegisterLog, 0)
+
+	//是否补录合同
+	sellerItemMap := make(map[int]*crm.SellerAdminWithGroupTeam)
 	if req.Supplement == 1 {
 		//新增到款信息
 		if ob.HasInvoice == 0 {
@@ -419,6 +422,7 @@ func (rg *RegisterController) Add(c *gin.Context) {
 			sellerMap[sellerList[i].SellerId] = sellerList[i]
 		}
 		for _, ppItem := range invoiceList {
+			j := 0
 			v := &fms.ContractInvoice{
 				ContractRegisterId: ob.ContractRegisterId,
 				ContractCode:       ob.ContractCode,
@@ -440,6 +444,19 @@ func (rg *RegisterController) Add(c *gin.Context) {
 				StartDate:          ppItem.StartDate,
 				EndDate:            ppItem.EndDate,
 			}
+			if ppItem.InvoiceType == 3 {
+				sellerItemMap[j] = &crm.SellerAdminWithGroupTeam{
+					SellerId:   ppItem.SellerId,
+					SellerName: ppItem.SellerName,
+					GroupId:    ppItem.SellerGroupId,
+					GroupName:  ppItem.SellerGroupName,
+					TeamId:     ppItem.SellerTeamId,
+					TeamName:   ppItem.SellerTeamName,
+				}
+				j++
+			}
+
+
 			if ppItem.InvoiceType == 3 {
 				v.InvoiceType = 1
 			} else if ppItem.InvoiceType == 4 {
@@ -490,8 +507,26 @@ func (rg *RegisterController) Add(c *gin.Context) {
 			err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
 			return
 		}
+		//先遍历一遍拿到开票销售信息
+		for _, ppItem := range dupList {
+			if ppItem.ContractRegisterId != req.ContractRegisterId {
+				j := 0
+				if ppItem.InvoiceType == 3 {
+					sellerItemMap[j] = &crm.SellerAdminWithGroupTeam{
+						SellerId:   ppItem.SellerId,
+						SellerName: ppItem.SellerName,
+						GroupId:    ppItem.SellerGroupId,
+						GroupName:  ppItem.SellerGroupName,
+						TeamId:     ppItem.SellerTeamId,
+						TeamName:   ppItem.SellerTeamName,
+					}
+					j++
+				}
+			}
+		}
 		for _, ppItem := range dupList {
-			if ppItem.ContractRegisterId != req.ContractRegisterId{
+			if ppItem.ContractRegisterId != req.ContractRegisterId {
+				j := 0
 				v := &fms.ContractInvoice{
 					ContractRegisterId: ob.ContractRegisterId,
 					ContractCode:       ob.ContractCode,
@@ -517,6 +552,15 @@ func (rg *RegisterController) Add(c *gin.Context) {
 					v.InvoiceType = 1
 				} else if ppItem.InvoiceType == 4 {
 					v.InvoiceType = 2
+					if sellerItem, ok := sellerItemMap[j]; ok {
+						v.SellerId = sellerItem.SellerId
+						v.SellerName = sellerItem.SellerName
+						v.SellerGroupId = sellerItem.GroupId
+						v.SellerGroupName = sellerItem.GroupName
+						v.SellerTeamId = sellerItem.TeamId
+						v.SellerTeamName = sellerItem.TeamName
+					}
+					j++
 				}
 				v.Set()
 				// 到款登记-付款方式
@@ -576,7 +620,6 @@ func (rg *RegisterController) Add(c *gin.Context) {
 			}
 		}
 
-
 		ppOB := new(fms.ContractPreRegister)
 		items, e := ppOB.FetchByRegisterId(req.ContractRegisterId)
 		if e != nil {
@@ -625,8 +668,26 @@ func (rg *RegisterController) Add(c *gin.Context) {
 			err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
 			return
 		}
+		//先遍历一遍拿到开票销售信息
 		for _, ppItem := range dupList {
-			if ppItem.ContractRegisterId != req.ContractRegisterId{
+			j := 0
+			if ppItem.ContractRegisterId != req.ContractRegisterId {
+				if ppItem.InvoiceType == 3 {
+					sellerItemMap[j] = &crm.SellerAdminWithGroupTeam{
+						SellerId:   ppItem.SellerId,
+						SellerName: ppItem.SellerName,
+						GroupId:    ppItem.SellerGroupId,
+						GroupName:  ppItem.SellerGroupName,
+						TeamId:     ppItem.SellerTeamId,
+						TeamName:   ppItem.SellerTeamName,
+					}
+				}
+				j++
+			}
+		}
+		for _, ppItem := range dupList {
+			j := 0
+			if ppItem.ContractRegisterId != req.ContractRegisterId {
 				v := &fms.ContractInvoice{
 					ContractRegisterId: ob.ContractRegisterId,
 					ContractCode:       ob.ContractCode,
@@ -652,6 +713,16 @@ func (rg *RegisterController) Add(c *gin.Context) {
 					v.InvoiceType = 1
 				} else if ppItem.InvoiceType == 4 {
 					v.InvoiceType = 2
+					if sellerItem, ok := sellerItemMap[j]; ok{
+						v.SellerId = sellerItem.SellerId
+						v.SellerName = sellerItem.SellerName
+						v.SellerGroupId = sellerItem.GroupId
+						v.SellerGroupName = sellerItem.GroupName
+						v.SellerTeamId = sellerItem.TeamId
+						v.SellerTeamName = sellerItem.TeamName
+
+						j++
+					}
 				}
 				v.Set()
 				// 到款登记-付款方式
@@ -1465,7 +1536,7 @@ func (rg *RegisterController) Invoice(c *gin.Context) {
 			invoiceItem.SellerGroupName = sellerItem.GroupName
 			invoiceItem.SellerTeamId = sellerItem.TeamId
 			invoiceItem.SellerTeamName = sellerItem.TeamName
-			e = invoiceItem.Update([]string{"SellerId","SellerName","SellerGroupId","SellerGroupName","SellerTeamId","SellerTeamName"})
+			e = invoiceItem.Update([]string{"SellerId", "SellerName", "SellerGroupId", "SellerGroupName", "SellerTeamId", "SellerTeamName"})
 			if e != nil {
 				resp.FailMsg("更新到款销售失败", "更新到款销售失败, Err: "+e.Error(), c)
 				return
@@ -2452,7 +2523,8 @@ func (rg *RegisterController) Import(c *gin.Context) {
 		resp.Fail("请上传Excel文件", c)
 		return
 	}
-
+	claims, _ := c.Get("adminInfo")
+	adminInfo := claims.(*system.SysAdmin)
 	uploadDir := "static/xls"
 	err = os.MkdirAll(uploadDir, 766)
 	if err != nil {
@@ -2473,7 +2545,7 @@ func (rg *RegisterController) Import(c *gin.Context) {
 		resp.FailData("打开文件失败", "Err:"+err.Error(), c)
 		return
 	}
-
+	logList := make([]*fms.ContractRegisterLog, 0)
 	// 获取所有已登记,根据合同编号去重
 	contractCodeArr := make([]string, 0)
 	registerOB := new(fms.ContractRegister)
@@ -2601,6 +2673,7 @@ func (rg *RegisterController) Import(c *gin.Context) {
 		// 遍历行读取
 		maxRow := sheet.MaxRow
 		fmt.Println("maxRow", maxRow)
+		var dayDiff float64
 		for i := 0; i < maxRow; i++ {
 			// 第三行开始读取表头
 			if i == 2 {
@@ -3268,7 +3341,7 @@ func (rg *RegisterController) Import(c *gin.Context) {
 									return
 								}
 								// 付款方式
-								dayDiff := rowRegister.EndDate.Sub(rowRegister.StartDate).Hours() / 24
+								dayDiff = rowRegister.EndDate.Sub(rowRegister.StartDate).Hours() / 24
 								contractAmount := rowRegister.ContractAmount
 								payType := fmsService.CalculateContractPaymentType(amount, contractAmount, dayDiff)
 								rowPayments[ir].PayType = payType
@@ -3388,13 +3461,144 @@ func (rg *RegisterController) Import(c *gin.Context) {
 					resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
 					return
 				}
+
+				//处理重复公司名日期等重复的开票到款记录
+				sellerItemMap := make(map[int]*crm.SellerAdminWithGroupTeam)
+				dupList, e := fms.GetDuplicateContractInvoiceDetailItemList(rowRegister.CompanyName, rowRegister.StartDate.Format(utils.FormatDate), rowRegister.EndDate.Format(utils.FormatDate))
+				if e != nil {
+					err = fmt.Errorf("获取开票到款列表失败, Err: %s", e.Error())
+					return
+				}
+				//先遍历一遍拿到开票销售信息
+				for _, ppItem := range dupList {
+					j := 0
+					if ppItem.ContractRegisterId != newId {
+						if ppItem.InvoiceType == 3 {
+							sellerItemMap[j] = &crm.SellerAdminWithGroupTeam{
+								SellerId:   ppItem.SellerId,
+								SellerName: ppItem.SellerName,
+								GroupId:    ppItem.SellerGroupId,
+								GroupName:  ppItem.SellerGroupName,
+								TeamId:     ppItem.SellerTeamId,
+								TeamName:   ppItem.SellerTeamName,
+							}
+						}
+						j++
+					}
+				}
+				for _, ppItem := range dupList {
+					j := 0
+					if ppItem.ContractRegisterId != newId {
+						v := &fms.ContractInvoice{
+							ContractRegisterId: newId,
+							ContractCode:       rowRegister.ContractCode,
+							Amount:             ppItem.Amount,
+							OriginAmount:       ppItem.OriginAmount,
+							CurrencyUnit:       ppItem.CurrencyUnit,
+							InvoiceDate:        ppItem.InvoiceDate,
+							SellerId:           ppItem.SellerId,
+							SellerName:         ppItem.SellerName,
+							SellerGroupId:      ppItem.SellerGroupId,
+							SellerGroupName:    ppItem.SellerGroupName,
+							SellerTeamId:       ppItem.SellerTeamId,
+							SellerTeamName:     ppItem.SellerTeamName,
+							AdminId:            int(adminInfo.AdminId),
+							AdminName:          adminInfo.RealName,
+							Remark:             ppItem.Remark,
+							ServiceProductId:   ppItem.ServiceProductId,
+							IsPrePay:           1,
+							StartDate:          ppItem.StartDate,
+							EndDate:            ppItem.EndDate,
+						}
+						if ppItem.InvoiceType == 3 {
+							v.InvoiceType = 1
+						} else if ppItem.InvoiceType == 4 {
+							v.InvoiceType = 2
+							if sellerItem, ok := sellerItemMap[j]; ok{
+								v.SellerId = sellerItem.SellerId
+								v.SellerName = sellerItem.SellerName
+								v.SellerGroupId = sellerItem.GroupId
+								v.SellerGroupName = sellerItem.GroupName
+								v.SellerTeamId = sellerItem.TeamId
+								v.SellerTeamName = sellerItem.TeamName
+
+								j++
+							}
+						}
+						v.Set()
+						// 到款登记-付款方式
+						v.PayType = fmsService.CalculateContractPaymentType(ppItem.Amount, rowRegister.ContractAmount, dayDiff)
+
+						// 新增的记录
+						opData := ""
+						//opDataByte, e := json.Marshal(req)
+						if e != nil {
+							return
+						}
+						//opData = string(opDataByte)
+						opType := fms.ContractRegisterOpTypePreInvoice
+						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: newId,
+							AdminId:            int(adminInfo.AdminId),
+							AdminName:          adminInfo.RealName,
+							OpData:             opData,
+							OpType:             opType,
+							CreateTime:         time.Now().Local(),
+							AmountRemark:       fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
+						})
+
+						if e := v.Create(); e != nil {
+							resp.FailData("日期格式有误", "Err:"+e.Error(), c)
+							return
+						}
+
+						//最后删除预到款记录
+						e = ppItem.Delete()
+						if e != nil {
+							resp.FailMsg("删除预到款记录失败", "Err:"+e.Error(), c)
+							return
+						}
+
+						ppOB := new(fms.ContractPreRegister)
+						items, e := ppOB.FetchByRegisterId(ppItem.ContractRegisterId)
+						if e != nil {
+							if e == utils.ErrNoRow {
+								resp.Fail("预登记记录不存在或已被删除", c)
+								return
+							}
+							resp.FailMsg("获取预登记失败", "Err:"+e.Error(), c)
+							return
+						}
+						for _, item := range items {
+							e = item.Delete()
+							if e != nil {
+								resp.FailMsg("删除预登记失败", "Err:"+e.Error(), c)
+								return
+							}
+						}
+					}
+				}
 				// 自动分配套餐金额
 				go fmsService.CalculatePaymentServiceAmount(newId)
 				newIds = append(newIds, newId)
+
+
 			}
 		}
 	}
 
+	// 操作日志
+	go func() {
+		logOB := new(fms.ContractRegisterLog)
+		if e := logOB.AddInBatches(logList); e != nil {
+			return
+		}
+	}()
+
 	resp.Ok("操作成功", c)
 }
 

+ 2 - 1
models/fms/contract_pre_register.go

@@ -111,7 +111,7 @@ func formatContractPreRegister2Item(item *ContractPreRegister) (formatItem *Cont
 	formatItem.StartDate = item.StartDate.Format(utils.FormatDate)
 	formatItem.EndDate = item.EndDate.Format(utils.FormatDate)
 	formatItem.PaymentSellerId = item.PaymentSellerId
-	formatItem.PaymentSellerName  = item.PaymentSellerName
+	formatItem.PaymentSellerName = item.PaymentSellerName
 	formatItem.PaymentSellerGroupId = item.PaymentSellerGroupId
 	formatItem.PaymentSellerGroupName = item.PaymentSellerGroupName
 	formatItem.PaymentSellerTeamId = item.PaymentSellerTeamId
@@ -219,6 +219,7 @@ type PreRegisterDetailReq struct {
 	PreRegisterId      int `gorm:"column:pre_register_id" json:"pre_register_id" description:"预登记ID"`
 	InvoiceId          int `gorm:"column:invoice_id" json:"invoice_id" description:"开票ID"`
 	ArriveId           int `gorm:"column:arrive_id" json:"arrive_id" description:"到款ID"`
+	DetailType         int `json:"detail_type" description:"请求类型"`
 }
 
 // ContractPreRegisterDetail 预登记详情