Przeglądaj źródła

自动分配金额逻辑

xiexiaoyuan 2 lat temu
rodzic
commit
72830a998f

+ 21 - 12
controller/census/invoice_payment.go

@@ -346,14 +346,20 @@ func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentC
 			rowTitle = append(rowTitle, serviceTempList[i].Title)
 			serviceTempShow = append(serviceTempShow, serviceTempList[i])
 		}else if serviceTempList[i].ProductId == 2{
-			if serviceTempList[i].Title == "行业套餐" ||  serviceTempList[i].Title == "45万" ||  serviceTempList[i].Title == "70万"{
+			if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "权益大套餐" || (serviceTempList[i].Title == "策略" && serviceTempListMap[serviceTempList[i].Pid].Pid==0){
+			}else if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" || serviceTempList[i].Title == "科技" || serviceTempList[i].Title == "智造" {
+			}else {
+				serviceTempShow = append(serviceTempShow, serviceTempList[i])
+			}
+			if serviceTempList[i].Title == "行业套餐" || serviceTempList[i].Title == "45万" ||  serviceTempList[i].Title == "70万" {
 				continue
 			}
+
 			if serviceTempList[i].Pid > 0 &&  serviceTempListMap[serviceTempList[i].Pid].Pid > 0 {
 				continue
 			}
+
 			rowTitle = append(rowTitle, serviceTempList[i].Title)
-			serviceTempShow = append(serviceTempShow, serviceTempList[i])
 		}
 	}
 
@@ -440,21 +446,24 @@ func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentC
 				sellerTypeMap[v2.SellerType],                  // 销售类型
 			}
 			// 套餐金额信息
+			serviceTempShowAmount := make(map[int]string)
 			for i := range serviceTempShow {
-				sa := ""
 				for s2 := range v2.ServiceAmountList {
 					item := v2.ServiceAmountList[s2]
-					if item.ServiceTemplateId == serviceTempList[i].ServiceTemplateId {
-						if serviceTempList[i].Title == "医药" || serviceTempList[i].Title == "消费" {
-							// todo 则需要查询对应的主观和客观的金额
-
-							break
-						}else{
-							sa = fmt.Sprint(v2.ServiceAmountList[s2].Amount)
-							break
-						}
+					if item.ServiceTemplateId == serviceTempShow[i].ServiceTemplateId {
+						serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount)
+						break
+					}else if serviceTempShow[i].Pid == item.ServiceTemplateId {
+						serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId] = fmt.Sprint(item.Amount/2)
+						break
 					}
 				}
+			}
+			for i := range serviceTempShow {
+				sa := ""
+				if am, ok :=  serviceTempShowAmount[serviceTempShow[i].ServiceTemplateId]; ok {
+					sa = am
+				}
 				rowData = append(rowData, sa)
 			}
 			// 首行开票到款

+ 0 - 1
controller/contract/payment.go

@@ -73,7 +73,6 @@ func (ct *PaymentController) DistributePaymentServiceAmount(c *gin.Context) {
 	}
 
 	addList := make([]*fms.ContractPaymentServiceAmount, 0)
-	// todo 判断是否符合均分金额的条件
 	for i := range req.List {
 		if req.List[i].Amount <= 0 {
 			continue

+ 14 - 1
controller/contract/register.go

@@ -656,6 +656,9 @@ func (rg *RegisterController) Edit(c *gin.Context) {
 		return
 	}
 
+	// 自动分配套餐金额
+	go fmsService.CalculatePaymentServiceAmount(item.ContractRegisterId)
+
 	// 校验金额-是否修改状态
 	go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
 
@@ -978,6 +981,10 @@ func (rg *RegisterController) Invoice(c *gin.Context) {
 				resp.Fail("请选择日期", c)
 				return
 			}
+			if req.AmountList[i].ServiceProductId != 1 &&  req.AmountList[i].ServiceProductId !=2 {
+				resp.Fail("请选择套餐类型", c)
+				return
+			}
 			// 开票登记销售必填
 			if req.InvoiceType == fms.ContractInvoiceTypeMake && req.AmountList[i].SellerId <= 0 {
 				resp.Fail("请选择销售", c)
@@ -1079,7 +1086,11 @@ func (rg *RegisterController) Invoice(c *gin.Context) {
 
 	// todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
 	if req.InvoiceType == fms.ContractInvoiceTypePay {
-
+		err = fmsService.CalculatePaymentServiceAmount(req.ContractRegisterId)
+		if err != nil {
+			resp.FailMsg("自动分配金额失败", "自动分配金额失败, Err: "+err.Error(), c)
+			return
+		}
 	}
 	// 新增的记录
 	if len(newInvoice) > 0 {
@@ -2800,6 +2811,8 @@ func (rg *RegisterController) Import(c *gin.Context) {
 					resp.FailData(fmt.Sprintf("第%d行导入失败", i+1), "新增导入登记失败, Err: "+e.Error(), c)
 					return
 				}
+				// 自动分配套餐金额
+				go fmsService.CalculatePaymentServiceAmount(newId)
 				newIds = append(newIds, newId)
 			}
 		}

+ 6 - 6
models/fms/contract_payment_service_amount.go

@@ -14,6 +14,7 @@ type ContractPaymentServiceAmount struct {
 	ServiceTemplatePid             int     `gorm:"column:service_template_pid" json:"service_template_pid" description:"套餐父级ID"`
 	Amount                         float64 `gorm:"column:amount" json:"amount" description:"分配金额"`
 	IsDeleted                      int     `gorm:"column:is_deleted" json:"is_deleted" description:"是否已删除: 0-正常; 1-已删除"`
+	InitType                       int     `gorm:"column:init_type" json:"init_type" description:"初始分配类型:0人为分配,1自动分配"`
 	base.TimeBase
 }
 
@@ -98,11 +99,10 @@ func CreatePaymentServiceAmount(registerId, payId int, addList []*ContractPaymen
 	return
 }
 
-// GetIndustryServiceTempAmount 获取对应的主客观分配的金额
-func GetIndustryServiceTempAmount(contractPaymentIds []int) (list []*ContractPaymentServiceAmount, err error) {
-	err = global.DEFAULT_MYSQL.Model(ContractPaymentServiceAmount{}).
-		Where("is_deleted = 0").
-		Where("service_template_pid > 0").
-		Find(&list).Error
+func DeletePaymentServiceAmountByRegisterId(registerId int) (err error) {
+	// 删除原自动分配信息
+	err = global.DEFAULT_MYSQL.
+		Where("contract_register_id = ? and init_type=1", registerId).
+		Delete(ContractPaymentServiceAmount{}).Error
 	return
 }

+ 147 - 8
services/fms/invoice_payment.go

@@ -7,6 +7,7 @@ import (
 	"hongze/fms_api/models/fms"
 	"hongze/fms_api/services/alarm_msg"
 	"hongze/fms_api/utils"
+	"strconv"
 	"time"
 )
 
@@ -280,12 +281,150 @@ func SummaryInvoicePaymentByContractRegisterId(registerId int) {
 }
 
 // CalculatePaymentServiceAmount
-func CalculatePaymentServiceAmount()  {
+func CalculatePaymentServiceAmount(registerId int) (err error)  {
 	// todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
-	// 如果用户去修改了套餐信息,需要删除对应的自动分配记录吗
-	// 如果有被删的到款登记,则批量删除对应的到款登记下的金额分配记录
-	// 从新创建的到款登记里重新分配金额
-	// 先判断这个合同的到款登记是新创建的还是被编辑的,
-	// 如果该合同的到款登记是新的,则直接创建分配金额记录
-	// 如果该合同的到款登记是已经存在的,则判断金额是否被修改,如果被修改,是否需要重新分配
-}
+	// 查询所有的到款记录,如果没有到款记录则无需分配到款金额
+	cond := `contract_register_id = ? and invoice_type=2 and service_product_id=2`
+	pars := make([]interface{}, 0)
+	pars = append(pars, registerId)
+	paymentList, err := fms.GetContractInvoiceItemList(cond, pars)
+	if err != nil {
+		err = fmt.Errorf("获取开票到款列表失败, Err: %s", err.Error())
+		return
+	}
+	if len(paymentList) == 0 {
+		return
+	}
+	var contractPaymentIds []int
+	for _, v:= range paymentList{
+		contractPaymentIds = append(contractPaymentIds, v.ContractInvoiceId)
+	}
+	// 获取服务套餐
+	servicesList, err := fms.GetContractServiceAndDetailList(registerId)
+	if err != nil {
+		err = fmt.Errorf("获取服务套餐失败, Err: "+err.Error())
+		return
+	}
+	industrySlice := []string{
+		"行业套餐",
+		"医药",
+		"消费",
+		"科技",
+		"智造",
+		"策略",
+		"主观",
+		"客观",
+	}
+	unNeedAsign := false
+	industryFlag := false
+	servicesListMap := make(map[int]*fms.ContractServiceAndDetail)
+	// 判断当前套餐是否符合分配规则
+	for _, v :=range servicesList {
+		if v.ProductId == crm.CompanyProductRai {
+			industryFlag = true
+		}
+		servicesListMap[v.ServiceTemplateId] = v
+		if v.ProductId==crm.CompanyProductRai && !utils.InArray(v.Title, industrySlice){
+			unNeedAsign = true
+		}
+	}
+	if unNeedAsign || !industryFlag{
+		//不符合自动分配规则,则直接删除之前的自动分配记录
+		err =fms.DeletePaymentServiceAmountByRegisterId(registerId)
+		if err !=nil {
+			return
+		}
+		return
+	} else {
+		//遍历所有的到款记录,筛选出需要删除并新增的到款记录ID
+		_, serviceFormatMap, e := GetContractServiceNameFormat([]int{registerId})
+		if e != nil {
+			err = fmt.Errorf("获取合同套餐失败, Err: %s", e.Error())
+			return
+		}
+		servicesFormatList, ok := serviceFormatMap[registerId]
+		if !ok {
+			err = fmt.Errorf("查不到合同套餐")
+			return
+		}
+
+		servicesFormatStr := ""
+		length :=0
+		for _, v :=range servicesFormatList {
+			if  servicesListMap[v.ServiceTemplateId].ProductId==crm.CompanyProductRai{
+				length ++
+				servicesFormatStr += fmt.Sprintf("%d_", v.ServiceTemplateId)
+			}
+		}
+		if length ==0 {
+			return
+		}
+		payAverageAmountMap := make(map[int]float64)
+		for _, v := range paymentList{
+			payAverageAmountMap[v.ContractInvoiceId] = v.Amount/float64(length)
+			payAverageAmountMap[v.ContractInvoiceId], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", payAverageAmountMap[v.ContractInvoiceId]), 64)
+		}
+
+		//查询原来的金额分配记录
+		serviceAmountCond := `contract_payment_id = ? and contract_payment_id in ?`
+		serviceAmountPars := make([]interface{}, 0)
+		serviceAmountPars = append(serviceAmountPars, registerId, contractPaymentIds)
+		serviceAmountOB := new(fms.ContractPaymentServiceAmount)
+		serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
+		if e != nil {
+			err = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
+			return
+		}
+
+		oldPaymentServiceMap := make(map[int]string)
+		oldPaymentAmountMap := make(map[int]float64)
+		for _, v := range serviceAmountList{
+			oldPaymentServiceMap[v.ContractPaymentId] += fmt.Sprintf("%d_", v.ServiceTemplateId)
+			oldPaymentAmountMap[v.ContractPaymentId] = v.Amount
+		}
+
+		var newPaymentIds []int
+		for _, v := range paymentList{
+			if _, ok1 := oldPaymentServiceMap[v.ContractInvoiceId]; !ok1{
+				newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
+				continue
+			}
+			if oldPaymentServiceMap[v.ContractInvoiceId] != servicesFormatStr {
+				newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
+				continue
+			}
+			if oldPaymentAmountMap[v.ContractInvoiceId] != payAverageAmountMap[v.ContractInvoiceId] {
+				newPaymentIds = append(newPaymentIds, v.ContractInvoiceId)
+				continue
+			}
+		}
+
+		// 删除旧的金额分配记录并生成新的自动分配记录
+		if len(newPaymentIds) == 0{
+			return
+		}
+		for _, v := range newPaymentIds {
+			addList := make([]*fms.ContractPaymentServiceAmount, 0)
+			for _, f := range servicesFormatList {
+				if  servicesListMap[f.ServiceTemplateId].ProductId==crm.CompanyProductRai {
+					t := &fms.ContractPaymentServiceAmount{
+						ContractRegisterId: registerId,
+						ContractPaymentId:  v,
+						ServiceTemplatePid: f.ServiceTemplatePid,
+						ServiceTemplateId:  f.ServiceTemplateId,
+						Amount:             payAverageAmountMap[v],
+						InitType:           1,
+					}
+					t.Set()
+					addList = append(addList, t)
+				}
+			}
+			if e := fms.CreatePaymentServiceAmount(registerId, v, addList); e != nil {
+				err = fmt.Errorf( "新增到款套餐金额失败, Err: "+e.Error())
+				return
+			}
+		}
+	}
+	return
+}
+