|
@@ -0,0 +1,1588 @@
|
|
|
+package census
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "fmt"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "github.com/go-playground/validator/v10"
|
|
|
+ "github.com/tealeg/xlsx"
|
|
|
+ "hongze/fms_api/controller/resp"
|
|
|
+ "hongze/fms_api/global"
|
|
|
+ "hongze/fms_api/models"
|
|
|
+ "hongze/fms_api/models/fms"
|
|
|
+ "hongze/fms_api/services/alarm_msg"
|
|
|
+ "hongze/fms_api/utils"
|
|
|
+ "net/http"
|
|
|
+ "sort"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (this *InvoicePaymentController) IncomeChartList(c *gin.Context) {
|
|
|
+ var req fms.IncomeChartListReq
|
|
|
+ if e := c.BindQuery(&req); e != nil {
|
|
|
+ err, ok := e.(validator.ValidationErrors)
|
|
|
+ if !ok {
|
|
|
+ resp.FailData("参数解析失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.FailData("参数解析失败", err.Translate(global.Trans), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var incomeList models.CensusIncomeChartResp
|
|
|
+ ch := make(chan models.CensusIncomeChartResp, 1)
|
|
|
+ if req.IncomeType == 0 {
|
|
|
+ go getCensusIncomeChartList(ch, req)
|
|
|
+ } else {
|
|
|
+ go getPaymentIncomeChartList(ch, req)
|
|
|
+ }
|
|
|
+
|
|
|
+ for v := range ch {
|
|
|
+ incomeList = v
|
|
|
+ close(ch)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.IsExport == 1 {
|
|
|
+ ExportIncomeList(c, incomeList.DataList)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.OkData("获取成功", incomeList, c)
|
|
|
+}
|
|
|
+
|
|
|
+func getCensusIncomeChartList(ch chan models.CensusIncomeChartResp, req fms.IncomeChartListReq) (incomeChart models.CensusIncomeChartResp, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ global.LOG.Error(err)
|
|
|
+ if err != utils.ErrNoRow {
|
|
|
+ go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ch <- incomeChart
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ cond := ``
|
|
|
+ historyCond := ``
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ historyPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ cond += ` AND (a.seller_id in ? ) `
|
|
|
+ historyCond += ` AND (seller_id in ? ) `
|
|
|
+ pars = append(pars, sellerIds)
|
|
|
+ historyPars = append(historyPars, sellerIds)
|
|
|
+ }
|
|
|
+ if req.CompanyType == 1 {
|
|
|
+ cond += ` AND b.contract_type = 1 `
|
|
|
+ historyCond += ` AND new_company = 1 `
|
|
|
+ } else if req.CompanyType == 2 {
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ historyCond += ` AND new_company = 0 `
|
|
|
+ }
|
|
|
+
|
|
|
+ var latestTime time.Time
|
|
|
+
|
|
|
+ invoiceItem, err := fms.GetLatestIncome(cond, pars)
|
|
|
+ if err != nil && err != utils.ErrNoRow {
|
|
|
+ err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ latestTime = invoiceItem.InvoiceDate
|
|
|
+
|
|
|
+ if err == utils.ErrNoRow {
|
|
|
+ historyItem, e := fms.GetLatestHistoryIncome(historyCond, historyPars)
|
|
|
+ if e != nil && e != utils.ErrNoRow {
|
|
|
+ err = fmt.Errorf("获取最新的历史开票或到款日期, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ latestTime = historyItem.InvoiceDate
|
|
|
+ }
|
|
|
+
|
|
|
+ if latestTime.IsZero() {
|
|
|
+ latestTime = time.Now()
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.CompanyType == 3 {
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ historyCond += ` AND new_company = 0 `
|
|
|
+ }
|
|
|
+
|
|
|
+ latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
|
|
|
+ addMonth := 0
|
|
|
+ if req.ListParam == "1" {
|
|
|
+ addMonth = int(3 - latestTime.Month()%3)
|
|
|
+ }
|
|
|
+ if req.ListParam == "2" {
|
|
|
+ addMonth = int(6 - latestTime.Month()%6)
|
|
|
+ }
|
|
|
+ if req.ListParam == "3" {
|
|
|
+ addMonth = int(12 - latestTime.Month()%12)
|
|
|
+ }
|
|
|
+ latestTime = latestTime.AddDate(0, addMonth, 0)
|
|
|
+
|
|
|
+ var dateSlice []string
|
|
|
+ var totalMoneySlice, prevTotalMoneySlice []float64
|
|
|
+ var yoySlice []string
|
|
|
+ var yearNum, monthNum int
|
|
|
+ var reqStartDate, reqEndDate time.Time
|
|
|
+ historyTime, _ := time.Parse(utils.FormatDate, "2023-04-01")
|
|
|
+
|
|
|
+
|
|
|
+ if req.ServiceTypes != "" {
|
|
|
+ serviceTypes := strings.Split(req.ServiceTypes, ",")
|
|
|
+ registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(registerIds) > 0 {
|
|
|
+ cond += ` AND b.contract_register_id IN ?`
|
|
|
+ pars = append(pars, registerIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartDate == "" {
|
|
|
+ req.StartDate = "2023-04"
|
|
|
+ req.EndDate = "3023-04"
|
|
|
+ } else {
|
|
|
+ startDateTime, _ := time.Parse(utils.FormatMonth, req.StartDate)
|
|
|
+ if startDateTime.Before(historyTime) {
|
|
|
+ req.StartDate = "2023-04"
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ st := fmt.Sprint(req.StartDate, "-01 00:00:00")
|
|
|
+ ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
|
|
|
+ reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
|
|
|
+ reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
|
|
|
+ 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 = latestTime.Year() - 2020
|
|
|
+ monthNum = int(latestTime.Month() - 1)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ numMonth := yearNum*12 + monthNum
|
|
|
+ if numMonth < 0 {
|
|
|
+ numMonth = -numMonth
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ var accumulate float64
|
|
|
+ var partAccumulate float64
|
|
|
+ var historyAccumulate float64
|
|
|
+ var partHistoryAccumulate float64
|
|
|
+
|
|
|
+
|
|
|
+ fmt.Println("numMonth:", numMonth)
|
|
|
+ fmt.Println("InvoiceDate:", latestTime)
|
|
|
+
|
|
|
+ var j int
|
|
|
+ for i := 0; i <= numMonth; i++ {
|
|
|
+
|
|
|
+
|
|
|
+ var endDateTime time.Time
|
|
|
+ var prevEndDateTime time.Time
|
|
|
+ var prevStartDate, prevEndDate string
|
|
|
+ var startDate, endDate string
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
|
|
|
+ prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
|
|
|
+ } else {
|
|
|
+ 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")
|
|
|
+ startDateTime, _ := time.Parse(utils.FormatDate, startDate)
|
|
|
+ prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
|
|
|
+
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ endDateTime = reqStartDate.AddDate(0, i+1, -1)
|
|
|
+ prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
|
|
|
+ if reqEndDate.After(latestTime) {
|
|
|
+ endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
|
|
|
+ prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ 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)
|
|
|
+
|
|
|
+ cond := `1 = 1`
|
|
|
+ histrtyCond := `1 = 1`
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ historyPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ prevCond := `1 = 1`
|
|
|
+ prevHistoryCond := `1 = 1`
|
|
|
+ prevPars := make([]interface{}, 0)
|
|
|
+ prevHistoryPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if req.CompanyType == 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.contract_type IN (2,3,4) `
|
|
|
+ prevCond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ histrtyCond += ` AND new_company = 0 `
|
|
|
+ prevHistoryCond += ` AND new_company = 0 `
|
|
|
+ } else if req.CompanyType == 3 {
|
|
|
+
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+
|
|
|
+ histrtyCond += ` AND new_company = 0 `
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
|
|
|
+ pars = append(pars, sellerIds, sellerIds)
|
|
|
+ prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
|
|
|
+ prevPars = append(prevPars, sellerIds, sellerIds)
|
|
|
+ histrtyCond += ` AND seller_id in ? `
|
|
|
+ prevHistoryCond += ` AND seller_id in ? `
|
|
|
+ historyPars = append(historyPars, sellerIds)
|
|
|
+ prevHistoryPars = append(prevHistoryPars, sellerIds)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.ServiceTypes != "" {
|
|
|
+ serviceTypes := strings.Split(req.ServiceTypes, ",")
|
|
|
+ registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(registerIds) > 0 {
|
|
|
+ cond += ` AND a.register_id IN ?`
|
|
|
+ prevCond += ` AND a.register_id IN ?`
|
|
|
+ pars = append(pars, registerIds)
|
|
|
+ prevPars = append(pars, registerIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ prevCond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ st := fmt.Sprint(startDate, " 00:00:00")
|
|
|
+ ed := fmt.Sprint(endDate, " 23:59:59")
|
|
|
+
|
|
|
+ if startDateTime.After(historyTime) || startDateTime.Equal(historyTime) {
|
|
|
+
|
|
|
+ cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
|
|
|
+ pars = append(pars, st, ed, st, ed)
|
|
|
+ summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var amountTotal float64
|
|
|
+
|
|
|
+ if len(summaryIds) > 0 {
|
|
|
+ amountCond := `a.id IN ? `
|
|
|
+ amountPars := make([]interface{}, 0)
|
|
|
+ amountPars = append(amountPars, summaryIds)
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
|
|
|
+ amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
|
|
|
+ } else {
|
|
|
+ amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
|
|
|
+ amountPars = append(amountPars, st, ed, st, ed)
|
|
|
+ }
|
|
|
+ results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var amountSum float64
|
|
|
+ for _, result := range results {
|
|
|
+ incomeChart.DataList = append(incomeChart.DataList, result)
|
|
|
+ amountSum += result.Amount
|
|
|
+ fmt.Println("result.Amount:", result.Amount)
|
|
|
+ }
|
|
|
+ amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ accumulate += amountTotal
|
|
|
+ partAccumulate += amountTotal
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if endDateTime.Before(historyTime) || endDateTime.Equal(historyTime) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ histrtyCond += ` AND (invoice_time BETWEEN ? AND ? )`
|
|
|
+ historyPars = append(historyPars, st, ed)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ var amountTotal float64
|
|
|
+ results, e := fms.GetIncomeHistory(histrtyCond, historyPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var amountSum float64
|
|
|
+
|
|
|
+ for _, result := range results {
|
|
|
+ incomeChart.DataList = append(incomeChart.DataList, result)
|
|
|
+ amountSum += result.Amount
|
|
|
+ }
|
|
|
+ amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ accumulate += amountTotal
|
|
|
+ partAccumulate += amountTotal
|
|
|
+
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ prevSt := fmt.Sprint(prevStartDate, " 00:00:00")
|
|
|
+ prevEd := fmt.Sprint(prevEndDate, " 23:59:59")
|
|
|
+
|
|
|
+
|
|
|
+ if prevStartDateTime.After(historyTime) || prevStartDateTime.Equal(historyTime) {
|
|
|
+
|
|
|
+ prevCond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
|
|
|
+ prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd)
|
|
|
+ prevSummaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(prevCond, prevPars)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var prevAmountTotal float64
|
|
|
+
|
|
|
+ if len(prevSummaryIds) > 0 {
|
|
|
+ amountCond := `a.id IN ? `
|
|
|
+ amountPars := make([]interface{}, 0)
|
|
|
+ amountPars = append(amountPars, prevSummaryIds)
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ amountCond += ` AND (( b.seller_id IN ? AND a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountCond += `OR ( d.seller_id IN ? AND a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?)) `
|
|
|
+ amountPars = append(amountPars, sellerIds, prevSt, prevEd, sellerIds, prevSt, prevEd)
|
|
|
+ } else {
|
|
|
+ amountCond += ` AND ((a.invoice_id <> 0 AND b.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountCond += `OR (a.payment_id <> 0 AND a.invoice_id = 0 AND d.invoice_time BETWEEN ? AND ?))`
|
|
|
+ amountPars = append(amountPars, prevSt, prevEd, prevSt, prevEd)
|
|
|
+ }
|
|
|
+ results, e := fms.GetContractSummaryIncomeAmount(amountCond, amountPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var amountSum float64
|
|
|
+
|
|
|
+ for _, result := range results {
|
|
|
+ amountSum += result.Amount
|
|
|
+ }
|
|
|
+ prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ historyAccumulate += prevAmountTotal
|
|
|
+ partHistoryAccumulate += prevAmountTotal
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ partHistoryAccumulate = 0.0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if prevEndDateTime.Before(historyTime) || prevEndDateTime.Equal(historyTime) {
|
|
|
+
|
|
|
+ fmt.Println("prevSt:", prevSt)
|
|
|
+ fmt.Println("prevEd:", prevEd)
|
|
|
+ prevHistoryCond += ` AND (invoice_time BETWEEN ? AND ?)`
|
|
|
+ prevHistoryPars = append(prevHistoryPars, prevSt, prevEd)
|
|
|
+
|
|
|
+ var amountTotal float64
|
|
|
+ results, e := fms.GetIncomeHistory(prevHistoryCond, prevHistoryPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var amountSum float64
|
|
|
+ for _, result := range results {
|
|
|
+ amountSum += result.Amount
|
|
|
+ }
|
|
|
+ amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ historyAccumulate += amountTotal
|
|
|
+ partHistoryAccumulate += amountTotal
|
|
|
+
|
|
|
+ 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)
|
|
|
+ partHistoryAccumulate = 0.0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if req.ListParam == "1" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
|
|
|
+ j = j + 2
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
|
|
|
+ j = j + 3
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "2" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
|
|
|
+ j = j + 5
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
|
|
|
+ j = j + 6
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "3" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
|
|
|
+ j = j + 11
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
|
|
|
+ j = j + 12
|
|
|
+ }
|
|
|
+ } else if i == j {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.Format("06/01"))
|
|
|
+ j++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if req.ListParam == "1" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
|
|
|
+ j = j + 2
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
|
|
|
+ j = j + 3
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "2" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
|
|
|
+ j = j + 5
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
|
|
|
+ j = j + 6
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "3" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
|
|
|
+ j = j + 11
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
|
|
|
+ j = j + 12
|
|
|
+ }
|
|
|
+ } else if i == j {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.Format("06/01"))
|
|
|
+ j++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
|
|
|
+ fmt.Println("totalMoneySlice:", len(totalMoneySlice))
|
|
|
+
|
|
|
+ if req.CompanyType != 3 {
|
|
|
+
|
|
|
+ for i := range prevTotalMoneySlice {
|
|
|
+ var yoy float64
|
|
|
+ var yoyStr string
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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])
|
|
|
+ yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
|
|
|
+ yoyStr = fmt.Sprintf("%.4f", yoy)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ yoySlice = append(yoySlice, yoyStr)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for i := range prevTotalMoneySlice {
|
|
|
+ totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", prevTotalMoneySlice[i]-totalMoneySlice[i]), 64)
|
|
|
+
|
|
|
+ var yoy float64
|
|
|
+ var yoyStr string
|
|
|
+ if i > 1 && req.ListParam == "0" {
|
|
|
+
|
|
|
+ yoy = (totalMoneySlice[i] + totalMoneySlice[i-1] + totalMoneySlice[i-2]) / 3
|
|
|
+ yoyStr = fmt.Sprintf("%.4f", yoy)
|
|
|
+ yoySlice = append(yoySlice, yoyStr)
|
|
|
+ } else {
|
|
|
+ yoySlice = append(yoySlice, "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ incomeChart.Title = "开票到款统计图"
|
|
|
+ incomeChart.Date = dateSlice[:len(totalMoneySlice)]
|
|
|
+ incomeChart.TotalMoney = totalMoneySlice
|
|
|
+ incomeChart.PrevTotalMoney = prevTotalMoneySlice
|
|
|
+ incomeChart.Yoy = yoySlice
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func getPaymentIncomeChartList(ch chan models.CensusIncomeChartResp, req fms.IncomeChartListReq) (incomeChart models.CensusIncomeChartResp, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ global.LOG.Error(err)
|
|
|
+ if err != utils.ErrNoRow {
|
|
|
+ go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ch <- incomeChart
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ cond := ``
|
|
|
+ historyCond := ``
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ historyPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ cond += ` AND (a.seller_id in ? ) `
|
|
|
+ historyCond += ` AND (seller_id in ? ) `
|
|
|
+ pars = append(pars, sellerIds)
|
|
|
+ historyPars = append(historyPars, sellerIds)
|
|
|
+ }
|
|
|
+ if req.CompanyType == 1 {
|
|
|
+ cond += ` AND b.contract_type = 1 `
|
|
|
+ historyCond += ` AND new_company = 1 `
|
|
|
+ } else if req.CompanyType == 2 {
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ historyCond += ` AND new_company = 0 `
|
|
|
+ }
|
|
|
+
|
|
|
+ var latestTime time.Time
|
|
|
+
|
|
|
+ invoiceItem, err := fms.GetLatestIncome(cond, pars)
|
|
|
+ if err != nil && err != utils.ErrNoRow {
|
|
|
+ err = fmt.Errorf("获取最新的开票或到款日期, Err: %s", err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ latestTime = invoiceItem.InvoiceDate
|
|
|
+
|
|
|
+ if latestTime.IsZero() {
|
|
|
+ latestTime = time.Now()
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.CompanyType == 3 {
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ historyCond += ` AND new_company = 0 `
|
|
|
+ }
|
|
|
+
|
|
|
+ latestTime = latestTime.AddDate(0, 0, -latestTime.Day()+1)
|
|
|
+ addMonth := 0
|
|
|
+ if req.ListParam == "1" {
|
|
|
+ addMonth = int(3 - latestTime.Month()%3)
|
|
|
+ }
|
|
|
+ if req.ListParam == "2" {
|
|
|
+ addMonth = int(6 - latestTime.Month()%6)
|
|
|
+ }
|
|
|
+ if req.ListParam == "3" {
|
|
|
+ addMonth = int(12 - latestTime.Month()%12)
|
|
|
+ }
|
|
|
+ latestTime = latestTime.AddDate(0, addMonth, 0)
|
|
|
+
|
|
|
+ var dateSlice []string
|
|
|
+ var totalMoneySlice, prevTotalMoneySlice []float64
|
|
|
+ var yoySlice []string
|
|
|
+ var yearNum, monthNum int
|
|
|
+ var reqStartDate, reqEndDate time.Time
|
|
|
+
|
|
|
+
|
|
|
+ if req.ServiceTypes != "" {
|
|
|
+ serviceTypes := strings.Split(req.ServiceTypes, ",")
|
|
|
+ registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(registerIds) > 0 {
|
|
|
+ cond += ` AND b.contract_register_id IN ?`
|
|
|
+ pars = append(pars, registerIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ st := fmt.Sprint(req.StartDate, "-01 00:00:00")
|
|
|
+ ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
|
|
|
+ reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
|
|
|
+ reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
|
|
|
+ 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 = latestTime.Year() - 2020
|
|
|
+ monthNum = int(latestTime.Month() - 1)
|
|
|
+ }
|
|
|
+
|
|
|
+ numMonth := yearNum*12 + monthNum
|
|
|
+ if numMonth < 0 {
|
|
|
+ numMonth = -numMonth
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var accumulate float64
|
|
|
+ var partAccumulate float64
|
|
|
+ var historyAccumulate float64
|
|
|
+ var partHistoryAccumulate float64
|
|
|
+
|
|
|
+
|
|
|
+ fmt.Println("numMonth:", numMonth)
|
|
|
+ fmt.Println("InvoiceDate:", latestTime)
|
|
|
+
|
|
|
+ var j int
|
|
|
+ for i := 0; i <= numMonth; i++ {
|
|
|
+
|
|
|
+
|
|
|
+ var endDateTime time.Time
|
|
|
+ var prevEndDateTime time.Time
|
|
|
+ var prevStartDate, prevEndDate string
|
|
|
+ var startDate, endDate string
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ startDate = reqStartDate.AddDate(0, i, 0).Format("2006-01")
|
|
|
+ prevStartDate = reqStartDate.AddDate(-1, i, 0).Format("2006-01")
|
|
|
+ } else {
|
|
|
+ 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")
|
|
|
+ startDateTime, _ := time.Parse(utils.FormatDate, startDate)
|
|
|
+ prevStartDateTime, _ := time.Parse(utils.FormatDate, prevStartDate)
|
|
|
+
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ endDateTime = reqStartDate.AddDate(0, i+1, -1)
|
|
|
+ prevEndDateTime = reqStartDate.AddDate(-1, i+1, -1)
|
|
|
+ if reqEndDate.After(latestTime) {
|
|
|
+ endDateTime = latestTime.AddDate(0, i-numMonth+1, -1)
|
|
|
+ prevEndDateTime = latestTime.AddDate(-1, i-numMonth+1, -1)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ 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)
|
|
|
+
|
|
|
+ cond := `1 = 1`
|
|
|
+ histrtyCond := `1 = 1`
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ historyPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ prevCond := `1 = 1`
|
|
|
+ prevHistoryCond := `1 = 1`
|
|
|
+ prevPars := make([]interface{}, 0)
|
|
|
+ prevHistoryPars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if req.CompanyType == 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.contract_type IN (2,3,4) `
|
|
|
+ prevCond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ histrtyCond += ` AND new_company = 0 `
|
|
|
+ prevHistoryCond += ` AND new_company = 0 `
|
|
|
+ } else if req.CompanyType == 3 {
|
|
|
+
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+
|
|
|
+ histrtyCond += ` AND new_company = 0 `
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
|
|
|
+ pars = append(pars, sellerIds, sellerIds)
|
|
|
+ prevCond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
|
|
|
+ prevPars = append(prevPars, sellerIds, sellerIds)
|
|
|
+ histrtyCond += ` AND seller_id in ? `
|
|
|
+ prevHistoryCond += ` AND seller_id in ? `
|
|
|
+ historyPars = append(historyPars, sellerIds)
|
|
|
+ prevHistoryPars = append(prevHistoryPars, sellerIds)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.ServiceTypes != "" {
|
|
|
+ serviceTypes := strings.Split(req.ServiceTypes, ",")
|
|
|
+ registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(registerIds) > 0 {
|
|
|
+ cond += ` AND a.register_id IN ?`
|
|
|
+ prevCond += ` AND a.register_id IN ?`
|
|
|
+ pars = append(pars, registerIds)
|
|
|
+ prevPars = append(pars, registerIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ prevCond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ st := fmt.Sprint(startDate, " 00:00:00")
|
|
|
+ ed := fmt.Sprint(endDate, " 23:59:59")
|
|
|
+
|
|
|
+
|
|
|
+ cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ pars = append(pars, st, ed, st, ed)
|
|
|
+ summaryIds, e := fms.GetPaymentCensusSummaryDataIds(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var amountTotal float64
|
|
|
+
|
|
|
+ if len(summaryIds) > 0 {
|
|
|
+ amountCond := `a.id IN ? `
|
|
|
+ amountPars := make([]interface{}, 0)
|
|
|
+ amountPars = append(amountPars, summaryIds)
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?) `
|
|
|
+ amountPars = append(amountPars, sellerIds, st, ed, sellerIds, st, ed)
|
|
|
+ } else {
|
|
|
+ amountCond += `AND (a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountPars = append(amountPars, st, ed, st, ed)
|
|
|
+ }
|
|
|
+ results, e := fms.GetContractSummaryPaymentIncomeAmount(amountCond, amountPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ var amountSum float64
|
|
|
+ for _, result := range results {
|
|
|
+ incomeChart.DataList = append(incomeChart.DataList, result)
|
|
|
+ amountSum += result.Amount
|
|
|
+ fmt.Println("result.Amount:", result.Amount)
|
|
|
+ }
|
|
|
+ amountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ accumulate += amountTotal
|
|
|
+ partAccumulate += amountTotal
|
|
|
+ }
|
|
|
+ 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
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ prevSt := fmt.Sprint(prevStartDate, " 00:00:00")
|
|
|
+ prevEd := fmt.Sprint(prevEndDate, " 23:59:59")
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ prevCond += ` AND (d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ prevPars = append(prevPars, prevSt, prevEd, prevSt, prevEd)
|
|
|
+ prevSummaryIds, e := fms.GetPaymentCensusSummaryDataIds(prevCond, prevPars)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var prevAmountTotal float64
|
|
|
+
|
|
|
+ if len(prevSummaryIds) > 0 {
|
|
|
+ amountCond := `a.id IN ? `
|
|
|
+ amountPars := make([]interface{}, 0)
|
|
|
+ amountPars = append(amountPars, prevSummaryIds)
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?) `
|
|
|
+ amountPars = append(amountPars, sellerIds, prevSt, prevEd, sellerIds, prevSt, prevEd)
|
|
|
+ } else {
|
|
|
+ amountCond += `AND (a.payment_id <> 0 AND d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ amountPars = append(amountPars, prevSt, prevEd, prevSt, prevEd)
|
|
|
+ }
|
|
|
+ results, e := fms.GetContractSummaryPaymentIncomeAmount(amountCond, amountPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var amountSum float64
|
|
|
+
|
|
|
+ for _, result := range results {
|
|
|
+ amountSum += result.Amount
|
|
|
+ }
|
|
|
+ prevAmountTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", amountSum), 64)
|
|
|
+ historyAccumulate += prevAmountTotal
|
|
|
+ partHistoryAccumulate += prevAmountTotal
|
|
|
+ }
|
|
|
+ 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)
|
|
|
+ partHistoryAccumulate = 0.0
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.ListParam == "1" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 2, 0).Format("06/01"))
|
|
|
+ j = j + 2
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 3, 0).Format("06/01"))
|
|
|
+ j = j + 3
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "2" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 5, 0).Format("06/01"))
|
|
|
+ j = j + 5
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 6, 0).Format("06/01"))
|
|
|
+ j = j + 6
|
|
|
+ }
|
|
|
+ } else if req.ListParam == "3" && i == j {
|
|
|
+ if i == 0 {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 11, 0).Format("06/01"))
|
|
|
+ j = j + 11
|
|
|
+ } else {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.AddDate(0, 12, 0).Format("06/01"))
|
|
|
+ j = j + 12
|
|
|
+ }
|
|
|
+ } else if i == j {
|
|
|
+ dateSlice = append(dateSlice, startDateTime.Format("06/01"))
|
|
|
+ j++
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fmt.Println("prevTotalMoneySlice:", len(prevTotalMoneySlice))
|
|
|
+ fmt.Println("totalMoneySlice:", len(totalMoneySlice))
|
|
|
+
|
|
|
+ if req.CompanyType != 3 {
|
|
|
+
|
|
|
+ for i := range prevTotalMoneySlice {
|
|
|
+ var yoy float64
|
|
|
+ var yoyStr string
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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])
|
|
|
+ yoy = (totalMoneySlice[i] - totalMoneySlice[i-1]) / totalMoneySlice[i-1]
|
|
|
+ yoyStr = fmt.Sprintf("%.4f", yoy)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ yoySlice = append(yoySlice, yoyStr)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for i := range prevTotalMoneySlice {
|
|
|
+ totalMoneySlice[i], _ = strconv.ParseFloat(fmt.Sprintf("%.2f", prevTotalMoneySlice[i]-totalMoneySlice[i]), 64)
|
|
|
+
|
|
|
+ var yoy float64
|
|
|
+ var yoyStr string
|
|
|
+ if i > 1 && req.ListParam == "0" {
|
|
|
+
|
|
|
+ yoy = (totalMoneySlice[i] + totalMoneySlice[i-1] + totalMoneySlice[i-2]) / 3
|
|
|
+ yoyStr = fmt.Sprintf("%.4f", yoy)
|
|
|
+ yoySlice = append(yoySlice, yoyStr)
|
|
|
+ } else {
|
|
|
+ yoySlice = append(yoySlice, "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ incomeChart.Title = "开票到款统计图"
|
|
|
+ incomeChart.Date = dateSlice[:len(totalMoneySlice)]
|
|
|
+ incomeChart.TotalMoney = totalMoneySlice
|
|
|
+ incomeChart.PrevTotalMoney = prevTotalMoneySlice
|
|
|
+ incomeChart.Yoy = yoySlice
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func ExportIncomeList(c *gin.Context, list []*fms.IncomeSummaryItem) {
|
|
|
+
|
|
|
+ xlsxFile := xlsx.NewFile()
|
|
|
+ style := xlsx.NewStyle()
|
|
|
+ alignment := xlsx.Alignment{
|
|
|
+ Horizontal: "center",
|
|
|
+ Vertical: "center",
|
|
|
+ WrapText: true,
|
|
|
+ }
|
|
|
+ style.Alignment = alignment
|
|
|
+ style.ApplyAlignment = true
|
|
|
+
|
|
|
+ sheetName := "业务收入统计表"
|
|
|
+ sheet, err := xlsxFile.AddSheet(sheetName)
|
|
|
+ if err != nil {
|
|
|
+ resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ rowTitle := []string{"开票日期", "新客户(1)", "客户名称", "金额", "销售"}
|
|
|
+ titleRow := sheet.AddRow()
|
|
|
+ for i := range rowTitle {
|
|
|
+ v := titleRow.AddCell()
|
|
|
+ v.SetString(rowTitle[i])
|
|
|
+ v.SetStyle(style)
|
|
|
+ }
|
|
|
+ incomeSummaryItemList := make(fms.IncomeSummaryItemList, 0)
|
|
|
+ incomeSummaryItemList = list
|
|
|
+ sort.Sort(incomeSummaryItemList)
|
|
|
+
|
|
|
+ for _, v := range incomeSummaryItemList {
|
|
|
+ dataRow := sheet.AddRow()
|
|
|
+ dataRow.AddCell().SetString(v.InvoiceDate.Format(utils.FormatDate))
|
|
|
+ newCompany := 0
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ var buffer bytes.Buffer
|
|
|
+ _ = xlsxFile.Write(&buffer)
|
|
|
+ content := bytes.NewReader(buffer.Bytes())
|
|
|
+ randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
|
|
|
+ fileName := sheetName + randStr + ".xlsx"
|
|
|
+
|
|
|
+ c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
|
|
|
+ c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
|
+ http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (this *InvoicePaymentController) IncomeList(c *gin.Context) {
|
|
|
+ var req fms.IncomeListReq
|
|
|
+ if e := c.BindQuery(&req); e != nil {
|
|
|
+ err, ok := e.(validator.ValidationErrors)
|
|
|
+ if !ok {
|
|
|
+ resp.FailData("参数解析失败", "Err:"+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ resp.FailData("参数解析失败", err.Translate(global.Trans), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ incomeList := make([]*fms.IncomeSummaryRespItem, 0)
|
|
|
+ ch := make(chan []*fms.IncomeSummaryRespItem, 1)
|
|
|
+ if req.IncomeType == 0 {
|
|
|
+ go getPaymentIncomeList(ch, req)
|
|
|
+ } else {
|
|
|
+ go getPaymentIncomeList(ch, req)
|
|
|
+ }
|
|
|
+
|
|
|
+ for v := range ch {
|
|
|
+ incomeList = append(incomeList, v...)
|
|
|
+ close(ch)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ resp.OkData("获取成功", incomeList, c)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func getPaymentIncomeList(ch chan []*fms.IncomeSummaryRespItem, req fms.IncomeListReq) (resp []*fms.IncomeSummaryRespItem, err error) {
|
|
|
+ defer func() {
|
|
|
+ if err != nil {
|
|
|
+ global.LOG.Error(err)
|
|
|
+ if err != utils.ErrNoRow {
|
|
|
+ go alarm_msg.SendAlarmMsg("获取业务收入金额统计数据异常,Err:"+err.Error(), 3)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ch <- resp
|
|
|
+ }()
|
|
|
+
|
|
|
+
|
|
|
+ var reqStartDate, reqEndDate time.Time
|
|
|
+
|
|
|
+ st := fmt.Sprint(req.StartDate, "-01 00:00:00")
|
|
|
+ ed := fmt.Sprint(req.EndDate, "-01 23:59:59")
|
|
|
+ reqStartDate, _ = time.Parse(utils.FormatDateTime, st)
|
|
|
+ reqEndDate, _ = time.Parse(utils.FormatDateTime, ed)
|
|
|
+
|
|
|
+ var startDate, endDate string
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ startDate = reqStartDate.Format(utils.FormatDate)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if req.StartDate != "" && req.EndDate != "" {
|
|
|
+ endDate = reqEndDate.Format(utils.FormatDate)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ cond := `1 = 1`
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+
|
|
|
+ if req.CompanyType == 1 {
|
|
|
+ cond += ` AND b.contract_type = 1 `
|
|
|
+ } else if req.CompanyType == 2 {
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ } else if req.CompanyType == 3 {
|
|
|
+
|
|
|
+ cond += ` AND b.contract_type IN (2,3,4) `
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ cond += ` AND (c.seller_id in ? OR d.seller_id in ?)`
|
|
|
+ pars = append(pars, sellerIds, sellerIds)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if req.ServiceTypes != "" {
|
|
|
+ serviceTypes := strings.Split(req.ServiceTypes, ",")
|
|
|
+ registerIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取合同登记IDs失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(registerIds) > 0 {
|
|
|
+ cond += ` AND a.register_id IN ?`
|
|
|
+ pars = append(pars, registerIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ if startDate != "" && endDate != "" {
|
|
|
+ st := fmt.Sprint(startDate, " 00:00:00")
|
|
|
+ ed := fmt.Sprint(endDate, " 23:59:59")
|
|
|
+ cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ pars = append(pars, st, ed, st, ed)
|
|
|
+ }
|
|
|
+
|
|
|
+ summaryIds, e := fms.GetPaymentCensusSummaryDataIds(cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(summaryIds) > 0 {
|
|
|
+ amountCond := `a.id IN ? `
|
|
|
+ amountPars := make([]interface{}, 0)
|
|
|
+ amountPars = append(amountPars, summaryIds)
|
|
|
+ if req.SellerIds != "" {
|
|
|
+ sellerIds := strings.Split(req.SellerIds, ",")
|
|
|
+ amountCond += `AND ( d.seller_id IN ? AND a.payment_id <> 0 ) `
|
|
|
+ amountPars = append(amountPars, sellerIds)
|
|
|
+ } else {
|
|
|
+ amountCond += `AND (a.payment_id <> 0)`
|
|
|
+ }
|
|
|
+ results, e := fms.GetContractSummaryPaymentIncomeAmountPage(amountCond, amountPars)
|
|
|
+ if e != nil {
|
|
|
+ err = fmt.Errorf("获取汇总数据失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range results {
|
|
|
+ respItem := &fms.IncomeSummaryRespItem{
|
|
|
+ CompanyName: v.CompanyName,
|
|
|
+ ContractType: v.ContractType,
|
|
|
+ InvoiceDate: v.InvoiceDate.Format(utils.FormatDate),
|
|
|
+ Amount: v.Amount,
|
|
|
+ SellerName: v.SellerName,
|
|
|
+ FinalSellerId: v.FinalSellerId,
|
|
|
+ }
|
|
|
+ resp = append(resp, respItem)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return
|
|
|
+}
|