ziwen 1 year ago
parent
commit
146b4ddf11
2 changed files with 82 additions and 108 deletions
  1. 63 108
      controller/census/invoice_payment.go
  2. 19 0
      models/fms/invoice_payment_summary.go

+ 63 - 108
controller/census/invoice_payment.go

@@ -1592,42 +1592,6 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		noSummaryCond += ` AND b.product_ids = '2' `
 	}
 
-	// 开票到款日期
-	//if req.TimeType != 0 {
-	//	if req.TimeType == 1 &&  req.StartDate != "" && req.EndDate != "" {
-	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
-	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
-	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
-	//		pars = append(pars, st, ed)
-	//	}else if req.TimeType == 2 &&  req.StartDate != "" && req.EndDate != "" {
-	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
-	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
-	//		cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
-	//		pars = append(pars, st, ed)
-	//	}else if req.TimeType == 3 &&  req.StartDate != "" && req.EndDate != "" {
-	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
-	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
-	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
-	//		pars = append(pars, st, ed, st, ed)
-	//	}
-	//} else if req.StartDate != "" && req.EndDate != "" {
-	//	st := fmt.Sprint(req.StartDate, " 00:00:00")
-	//	ed := fmt.Sprint(req.EndDate, " 23:59:59")
-	//	cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
-	//	pars = append(pars, st, ed, st, ed)
-	//}
-
-	//if req.HasInvoice == "1" {
-	//	cond += ` AND a.invoice_id > 0 `
-	//}else if req.HasInvoice == "0" {
-	//	cond += ` AND a.invoice_id = 0 `
-	//}
-	//
-	//if req.HasPayment == "1" {
-	//	cond += ` AND a.payment_id > 0 `
-	//}else if req.HasPayment == "0" {
-	//	cond += ` AND a.payment_id = 0 `
-	//}
 
 	page := new(base.Page)
 	page.SetPageSize(req.PageSize)
@@ -1693,21 +1657,21 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		}
 
 		// 获取汇总数据IDs, 用于查询合计数据
-		summaryIdsCond := cond
-		summaryIdsPars := pars
-		if req.SellerIds != "" {
-			sellerIds := strings.Split(req.SellerIds, ",")
-			summaryIdsCond += ` AND c.seller_id in ?  AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
-			summaryIdsPars = append(summaryIdsPars, sellerIds)
-		} else {
-			summaryIdsCond += `  AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
-			summaryIdsPars = append(summaryIdsPars)
-		}
-		summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
-		if e != nil {
-			resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
-			return
-		}
+		//summaryIdsCond := cond
+		//summaryIdsPars := pars
+		//if req.SellerIds != "" {
+		//	sellerIds := strings.Split(req.SellerIds, ",")
+		//	summaryIdsCond += ` AND c.seller_id in ?  AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+		//	summaryIdsPars = append(summaryIdsPars, sellerIds)
+		//} else {
+		//	summaryIdsCond += `  AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+		//	summaryIdsPars = append(summaryIdsPars)
+		//}
+		//summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
+		//if e != nil {
+		//	resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
+		//	return
+		//}
 
 		//fmt.Println("summaryIds:",summaryIds)
 		var listErr, totalErr, totalGroupErr error
@@ -1931,29 +1895,52 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		}()
 
 		// 开票到款金额合计(换算后)
-		var invoiceTotal, paymentTotal, contractAmount float64
+		//var invoiceTotal, paymentTotal, contractAmount float64
+		//wg.Add(1)
+		//go func() {
+		//	defer wg.Done()
+		//
+		//	if len(summaryIds) == 0 {
+		//		return
+		//	}
+		//	amountTotalCond := `a.id IN ?`
+		//	amountTotalPars := make([]interface{}, 0)
+		//	amountTotalPars = append(amountTotalPars, summaryIds)
+		//	invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
+		//	if e != nil {
+		//		totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
+		//		return
+		//	}
+		//	invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
+		//	paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
+		//	if e != nil {
+		//		totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
+		//		return
+		//	}
+		//	paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
+		//}()
+
+		// 开票到款金额合计(换算后)V2
+		var invoiceTotal, contractAmount float64
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
 
-			if len(summaryIds) == 0 {
-				return
-			}
-			amountTotalCond := `a.id IN ?`
-			amountTotalPars := make([]interface{}, 0)
-			amountTotalPars = append(amountTotalPars, summaryIds)
-			invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
+			registerList, e := fms.GetInvoiceCensusList(cond, pars)
 			if e != nil {
-				totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
+				resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
 				return
 			}
-			invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
-			paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
-			if e != nil {
-				totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
-				return
+
+			for _, v := range registerList {
+				if v.InvoicedAmountCount >= v.PaymentAmountCount {
+					invoiceTotal += v.InvoicedAmountCount
+				} else if v.InvoicedAmountCount == 0 {
+					invoiceTotal += v.PaymentAmountCount
+				}
+				contractAmount += v.ContractAmount
 			}
-			paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
+
 		}()
 
 		// 分币种金额统计
@@ -1995,56 +1982,25 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 				})
 			}
 
-			if len(summaryIds) == 0 {
-				return
-			}
-			totalGroupCond := `a.id IN ?`
-			totalGroupPars := make([]interface{}, 0)
-			totalGroupPars = append(totalGroupPars, summaryIds)
-			invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
-			if e != nil {
-				totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
-				return
-			}
-			paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
+			registerList, e := fms.GetInvoiceCensusList(cond, pars)
 			if e != nil {
-				totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
+				resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
 				return
 			}
+
 			invoiceSumMap := make(map[string]float64)
-			paymentSumMap := make(map[string]float64)
 			contractSumMap := make(map[string]float64)
 
-			for i := range invoiceSumGroup {
-				invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
-				continue
-			}
-			for i := range paymentSumGroup {
-				paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
-				continue
-			}
-			for i := range invoiceCurrencyTotals {
-				a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
-				invoiceCurrencyTotals[i].Amount = a
-			}
-			for i := range paymentCurrencyTotals {
-				a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
-				paymentCurrencyTotals[i].Amount = a
-			}
 
-			contractRegisters, err := fms.GetContractInfoByRegisterIds(queryRegisterIds)
-			if err != nil {
-				resp.FailMsg("查询合同信息失败", "查询合同信息失败, Err: "+err.Error(), c)
-				return
-			}
-			for _, v := range contractRegisters {
-				if v.RMBRate == 0.0 {
-					v.RMBRate = 1
+			for _, v := range registerList {
+				if v.InvoicedAmount >= v.PaymentAmount {
+					invoiceSumMap[v.CurrencyUnit] += v.InvoicedAmount
+				} else if v.InvoicedAmount == 0 {
+					invoiceSumMap[v.CurrencyUnit] += v.PaymentAmount
 				}
-				amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
-				contractAmount += amount
-				contractSumMap[v.CurrencyUnit] = v.ContractAmount
+				contractSumMap[v.CurrencyUnit] += v.ContractAmount
 			}
+
 			for _, v := range contractAmountCurrencyTotals {
 				v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
 			}
@@ -2067,7 +2023,6 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		}
 		results.DataList = respList
 		results.InvoiceTotal = invoiceTotal
-		results.PaymentTotal = paymentTotal
 		results.NotInvoiceTotal = contractAmount - invoiceTotal
 		results.InvoiceCurrencyTotal = invoiceCurrencyTotals
 		results.PaymentCurrencyTotal = paymentCurrencyTotals

+ 19 - 0
models/fms/invoice_payment_summary.go

@@ -342,4 +342,23 @@ func GetNoInvoicePaymentCensusData(condition string, pars []interface{}) (result
 		Where(condition, pars...).Group("contract_register_id")
 	query.Find(&results)
 	return
+}
+
+// GetInvoiceCensusList 获取未开票统计列表
+func GetInvoiceCensusList(condition string, pars []interface{}) (results []*InvoiceSummary, err error) {
+	query := global.DEFAULT_MYSQL.Table("contract_register AS b").
+		Select("b.*, a.id as summary_id, IF(a.invoice_id >0, 1,0) as sort_invoice_id, IF(a.payment_id >0, 1,0) as sort_payment_id, COALESCE(SUM( c.amount ),0) AS invoiced_amount_count,COALESCE(SUM( d.amount ),0) AS payment_amount_count ").
+		Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
+		Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
+		Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
+		Where(condition, pars...).Group("contract_register_id")
+
+	nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
+		Select("*").Where(" e.invoiced_amount_count <>  e.contract_amount ")
+
+	err = nq.Find(&results).Error
+	if err != nil {
+		return
+	}
+	return
 }