|
@@ -7,6 +7,7 @@ import (
|
|
"hongze/fms_api/models/fms"
|
|
"hongze/fms_api/models/fms"
|
|
"hongze/fms_api/services/alarm_msg"
|
|
"hongze/fms_api/services/alarm_msg"
|
|
"hongze/fms_api/utils"
|
|
"hongze/fms_api/utils"
|
|
|
|
+ "strconv"
|
|
"time"
|
|
"time"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -280,12 +281,150 @@ func SummaryInvoicePaymentByContractRegisterId(registerId int) {
|
|
}
|
|
}
|
|
|
|
|
|
// CalculatePaymentServiceAmount
|
|
// CalculatePaymentServiceAmount
|
|
-func CalculatePaymentServiceAmount() {
|
|
|
|
|
|
+func CalculatePaymentServiceAmount(registerId int) (err error) {
|
|
// todo 判断是否符合均分金额的条件,如果符合,需要生成金额分配记录表
|
|
// 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
|
|
|
|
+}
|
|
|
|
+
|