Procházet zdrojové kódy

Merge branch 'fms_2.8'

ziwen před 1 rokem
rodič
revize
c10f7a9e37

+ 79 - 45
controller/census/invoice_payment.go

@@ -588,7 +588,8 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 	} 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 ?))`
+		cond += ` AND ((a.invoice_id <> 0 AND c.invoice_time BETWEEN ? AND ?)`
+		cond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
 		pars = append(pars, st, ed, st, ed)
 	}
 
@@ -996,7 +997,7 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 	defer func() {
 		if err != nil {
 			global.LOG.Error(err)
-			go alarm_msg.SendAlarmMsg("获取近两年的收入统计数据异常,Err:"+err.Error(), 3)
+			go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
 			//go utils.SendEmail(utils.APPNAME+"获取近12个月的收入统计数据异常:"+time.Now().Format("2006-01-02 15:04:05"), err.Error(), utils.EmailSendToUsers)
 		}
 		ch <- incomeChart
@@ -1009,24 +1010,44 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 
 	//获取最新的开票到款日期
 	cond := ``
+	historyCond := ``
 	pars := make([]interface{}, 0)
+	historyPars := make([]interface{}, 0)
 
 	if req.SellerIds != "" {
 		sellerIds := strings.Split(req.SellerIds, ",")
 		cond += ` AND (a.seller_id in ? )`
-		pars = append(pars, sellerIds, sellerIds)
+		historyCond += ` AND (seller_id in ? )`
+		pars = append(pars, sellerIds)
+		historyPars = append(historyPars, sellerIds)
 	}
 	if req.CompanyType == 1 {
-		cond += ` AND b.new_company = 1 `
+		cond += ` AND b.contract_type = 1 `
+		historyCond += ` AND new_company = 1`
 	} else if req.CompanyType == 2 {
-		cond += ` AND b.new_company = 0 `
+		cond += ` AND b.contract_type = 2 `
+		historyCond += ` AND new_company = 0`
 	}
+
+	var latestTime time.Time
+
 	invoiceItem, err := fms.GetLatestIncome(cond, pars)
-	if err != nil {
+	if err != nil && err != utils.ErrNoRow {
 		err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
 		return
 	}
-	invoiceItem.InvoiceDate = invoiceItem.InvoiceDate.AddDate(0,0,-invoiceItem.InvoiceDate.Day()+1)
+	latestTime = invoiceItem.InvoiceDate
+
+	if err == utils.ErrNoRow {
+		historyItem, e := fms.GetLatestHistoryIncome(historyCond, historyPars)
+		if e != nil && err != utils.ErrNoRow {
+			err = fmt.Errorf("获取最新的历史开票或到款日期, Err: %s", err.Error())
+			return
+		}
+		latestTime = historyItem.InvoiceDate
+	}
+
+	latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
 
 	var dateSlice []string
 	var totalMoneySlice, prevTotalMoneySlice []float64
@@ -1040,16 +1061,16 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 		ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
 		reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
 		reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
-		if reqEndDate.After(invoiceItem.InvoiceDate) {
-			yearNum = invoiceItem.InvoiceDate.Year() - reqStartDate.Year()
-			monthNum = int(invoiceItem.InvoiceDate.Month() - reqStartDate.Month())
+		if reqEndDate.After(latestTime) {
+			yearNum = latestTime.Year() - reqStartDate.Year()
+			monthNum = int(latestTime.Month() - reqStartDate.Month())
 		} else {
 			yearNum = reqEndDate.Year() - reqStartDate.Year()
 			monthNum = int(reqEndDate.Month() - reqStartDate.Month())
 		}
 	} else {
-		yearNum = invoiceItem.InvoiceDate.Year() - 2020
-		monthNum = int(invoiceItem.InvoiceDate.Month() - 1)
+		yearNum = latestTime.Year() - 2020
+		monthNum = int(latestTime.Month() - 1)
 	}
 
 	if yearNum < 0 {
@@ -1074,7 +1095,7 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 	//dataList := make([]*fms.IncomeSummaryItem, 0)
 	//historydataList := make([]*fms.IncomeSummaryItem, 0)
 	fmt.Println("numMonth:", numMonth)
-	fmt.Println("InvoiceDate:", invoiceItem.InvoiceDate)
+	fmt.Println("InvoiceDate:", latestTime)
 
 	var j int
 	for i := 0; i <= numMonth; i++ {
@@ -1089,8 +1110,8 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 			startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
 			prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
 		} else {
-			startDate = invoiceItem.InvoiceDate.AddDate(0, i-numMonth, 0).Format("2006-01")
-			prevStartDate = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth, 0).Format("2006-01")
+			startDate = latestTime.AddDate(0, i-numMonth, 0).Format("2006-01")
+			prevStartDate = latestTime.AddDate(-1, i-numMonth, 0).Format("2006-01")
 		}
 		startDate = fmt.Sprint(startDate, "-01")
 		prevStartDate = fmt.Sprint(prevStartDate, "-01")
@@ -1101,13 +1122,13 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 		if req.StartDate != "" && req.EndDate != "" {
 			endDateTime = reqStartDate.AddDate(0, i+1, -1)
 			prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
-			if reqEndDate.After(invoiceItem.InvoiceDate) {
-				endDateTime = invoiceItem.InvoiceDate.AddDate(0, i-numMonth+1, -1)
-				prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth+1, -1)
+			if reqEndDate.After(latestTime) {
+				endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
+				prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
 			}
 		} else {
-			endDateTime = invoiceItem.InvoiceDate.AddDate(0, i-numMonth+1, -1)
-			prevEndDateTime = invoiceItem.InvoiceDate.AddDate(-1, i-numMonth+1, -1)
+			endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
+			prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
 		}
 		endDate = endDateTime.Format(utils.FormatDate)
 		prevEndDate = prevEndDateTime.Format(utils.FormatDate)
@@ -1123,13 +1144,13 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 		prevHistoryPars := make([]interface{}, 0)
 
 		if req.CompanyType == 1 {
-			cond += ` AND b.new_company = 1 `
-			prevCond += ` AND b.new_company = 1 `
+			cond += ` AND b.contract_type = 1 `
+			prevCond += ` AND b.contract_type = 1 `
 			histrtyCond += ` AND new_company = 1 `
 			prevHistoryCond += ` AND new_company = 1 `
 		} else if req.CompanyType == 2 {
-			cond += ` AND b.new_company = 0 `
-			prevCond += ` AND b.new_company = 0 `
+			cond += ` AND b.contract_type = 2 `
+			prevCond += ` AND b.contract_type = 2 `
 			histrtyCond += ` AND new_company = 0 `
 			prevHistoryCond += ` AND new_company = 0 `
 		}
@@ -1145,8 +1166,8 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 			historyPars = append(historyPars, sellerIds)
 			prevHistoryPars = append(prevHistoryPars, sellerIds)
 		}
-//fmt.Println("i:",i)
-//fmt.Println("j:",j)
+		//fmt.Println("i:",i)
+		//fmt.Println("j:",j)
 		{
 			//本期
 			st := fmt.Sprint(startDate, " 00:00:00")
@@ -1182,7 +1203,7 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 					for _, result := range results {
 						incomeChart.DataList = append(incomeChart.DataList, result)
 						amountSum += result.Amount
-						fmt.Println("result.Amount:",result.Amount)
+						fmt.Println("result.Amount:", result.Amount)
 					}
 					amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
 					accumulate += amountTotal
@@ -1191,19 +1212,22 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 				if i == j || i == numMonth {
 					if req.ListParam == "4" {
 						totalMoneySlice = append(totalMoneySlice, accumulate)
+						if startDateTime.Month() == 12 {
+							accumulate = 0
+						}
 					} else if req.ListParam == "0" {
 						totalMoneySlice = append(totalMoneySlice, amountTotal)
 					} else if i > 0 || i == numMonth {
 						totalMoneySlice = append(totalMoneySlice, partAccumulate)
-						fmt.Println("partAccumulate:",partAccumulate)
-						partAccumulate  = 0.0
+						fmt.Println("partAccumulate:", partAccumulate)
+						partAccumulate = 0.0
 					}
 				}
 			} else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
 				//全部走旧查询
 				//fmt.Println("旧查询")
-				fmt.Println("st:",st)
-				fmt.Println("ed:",ed)
+				//fmt.Println("st:",st)
+				//fmt.Println("ed:",ed)
 				histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
 				historyPars = append(historyPars, st, ed)
 				//fmt.Println("st:",st)
@@ -1228,11 +1252,14 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 				if i == j || i == numMonth {
 					if req.ListParam == "4" {
 						totalMoneySlice = append(totalMoneySlice, accumulate)
+						if startDateTime.Month() == 12 {
+							accumulate = 0
+						}
 					} else if req.ListParam == "0" {
 						totalMoneySlice = append(totalMoneySlice, amountTotal)
 					} else if i > 0 || i == numMonth {
 						totalMoneySlice = append(totalMoneySlice, partAccumulate)
-						partAccumulate  = 0.0
+						partAccumulate = 0.0
 					}
 				}
 
@@ -1282,18 +1309,21 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 				if i == j || i == numMonth {
 					if req.ListParam == "4" {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
+						if prevStartDateTime.Month() == 12 {
+							historyAccumulate = 0
+						}
 					} else if req.ListParam == "0" {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, prevAmountTotal)
 					} else if i > 0 || i == numMonth {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
-						fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
+						fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
 						partHistoryAccumulate = 0.0
 					}
 				}
 			} else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
 				//全部走旧查询
-				fmt.Println("prevSt:",prevSt)
-				fmt.Println("prevEd:",prevEd)
+				fmt.Println("prevSt:", prevSt)
+				fmt.Println("prevEd:", prevEd)
 				prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)`
 				prevHistoryPars = append(prevHistoryPars, prevSt, prevEd)
 				// 开票到款金额合计(换算后)
@@ -1315,18 +1345,21 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 				if i == j || i == numMonth {
 					if req.ListParam == "4" {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, historyAccumulate)
+						if prevStartDateTime.Month() == 12 {
+							historyAccumulate = 0
+						}
 					} else if req.ListParam == "0" {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, amountTotal)
 					} else if i > 0 || i == numMonth {
 						prevTotalMoneySlice = append(prevTotalMoneySlice, partHistoryAccumulate)
-						fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
+						fmt.Println("partHistoryAccumulate:", partHistoryAccumulate)
 						partHistoryAccumulate = 0.0
 					}
 				}
 				//fmt.Println("partHistoryAccumulate:",partHistoryAccumulate)
 			}
 			if req.ListParam == "1" && i == j {
-				if i == 0{
+				if i == 0 {
 					dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
 					j = j + 2
 				} else {
@@ -1334,7 +1367,7 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 					j = j + 3
 				}
 			} else if req.ListParam == "2" && i == j {
-				if i == 0{
+				if i == 0 {
 					dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
 					j = j + 5
 				} else {
@@ -1342,7 +1375,7 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 					j = j + 6
 				}
 			} else if req.ListParam == "3" && i == j {
-				if i == 0{
+				if i == 0 {
 					dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
 					j = j + 11
 				} else {
@@ -1366,12 +1399,13 @@ func getCensusIncomeListV2(ch chan models.CensusIncomeChartResp, req fms.IncomeL
 		//fmt.Println("1:", prevTotalMoneySlice[i])
 		//fmt.Println("2:", totalMoneySlice[i])
 		//fmt.Println("3:", totalMoneySlice[i]-prevTotalMoneySlice[i])
+		totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", totalMoneySlice[i]), 64)
 		if prevTotalMoneySlice[i] != 0 && totalMoneySlice[i] != 0 {
 			yoy = (totalMoneySlice[i] - prevTotalMoneySlice[i]) / prevTotalMoneySlice[i]
 			yoyStr = fmt.Sprintf("%.4f", yoy)
 
-			if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3"{
-				fmt.Println("totalMoneySlice[i-1]:",totalMoneySlice[i-1])
+			if i == len(prevTotalMoneySlice)-1 && i > 0 && req.ListParam == "3" {
+				fmt.Println("totalMoneySlice[i-1]:", totalMoneySlice[i-1])
 				yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
 				yoyStr = fmt.Sprintf("%.4f", yoy)
 			}
@@ -1438,10 +1472,10 @@ func ExportIncomeList(c *gin.Context, list []*fms.IncomeSummaryItem) {
 		if v.ContractType == 1 {
 			newCompany = 1
 		}
-		dataRow.AddCell().SetString(strconv.Itoa(newCompany))             // 新客户
-		dataRow.AddCell().SetString(v.CompanyName)                          // 客户名称
-		dataRow.AddCell().SetString(fmt.Sprint(v.Amount))                   // 金额
-		dataRow.AddCell().SetString(v.SellerName)                           // 销售员
+		dataRow.AddCell().SetString(strconv.Itoa(newCompany)) // 新客户
+		dataRow.AddCell().SetString(v.CompanyName)            // 客户名称
+		dataRow.AddCell().SetString(fmt.Sprint(v.Amount))     // 金额
+		dataRow.AddCell().SetString(v.SellerName)             // 销售员
 	}
 
 	// 输出文件

+ 12 - 1
models/fms/contract_income_history.go

@@ -38,7 +38,7 @@ func GetIncomeHistory(condition string, pars []interface{}) (results []*IncomeSu
 
 // GetInvoicePaymentSummaryByRegisterIdAndInvoiceId 根据合同登记ID和开票ID获取汇总数据
 func GetIncomeHistoryList() (results []*ContractIncomeHistory, err error) {
-	sql := `SELECT * FROM contract_income_history `
+	sql := `SELECT * FROM contract_income_history WHERE seller_id=0 `
 	err = global.DEFAULT_MYSQL.Raw(sql).Find(&results).Error
 	return
 }
@@ -47,3 +47,14 @@ func (c *ContractIncomeHistory) Update(updateCols []string) (err error) {
 	err = global.DEFAULT_MYSQL.Model(c).Select(updateCols).Updates(c).Error
 	return
 }
+
+func GetLatestHistoryIncome(condition string, pars []interface{}) (result *ContractInvoice, err error) {
+	sql := `SELECT
+	* 
+FROM
+	contract_income_history WHERE 1=1 `
+	sql += condition
+	sql += `ORDER BY invoice_time DESC  `
+	err = global.DEFAULT_MYSQL.Raw(sql, pars...).First(&result).Error
+	return
+}