ziwen 1 year ago
parent
commit
ab920781e8

+ 541 - 26
controller/census/invoice_payment.go

@@ -945,8 +945,8 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 
 
 // List
-// @Title 商品到款统计列表
-// @Description 商品到款统计列表
+// @Title 未开票统计列表
+// @Description 未开票统计列表
 // @Param   Keyword			query	string	false	"关键词"
 // @Param   SellGroupId		query	int		false	"销售组别ID"
 // @Param   ServiceType		query	int		false	"套餐类型"
@@ -957,7 +957,7 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 // @Param   HasPayment		query	string		false	"是否已到款: 0-否; 1-是; 空-全部"
 // @Param   IsExport		query	int		false	"是否导出: 0-否; 1-是"
 // @Param   ListParam		query	int		false	"套餐类型: 0-全部; 1-ficc; 2-权益"
-// @Param   SortParam       query   string  false      "排序字段参数,用来排序的字段, 枚举值:'invoice_time':开票日 、 'payment_date':到款日"
+// @Param   SortParam       query   string  false      "排序字段参数,用来排序的字段, 枚举值:'start_date':开票日 、 'end_date':到款日"
 // @Param   SortType        query   string  true       "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
 // @Success 200 {object} fms.ContractRegisterItem
 // @router /census/invoice_payment/not_invoice/list [get]
@@ -975,11 +975,11 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 
 	cond := `1 = 1`
 	pars := make([]interface{}, 0)
-	// 客户姓名/销售
+	// 客户姓名/合同编号
 	if req.Keyword != "" {
 		kw := "%" + req.Keyword + "%"
-		cond += ` AND b.company_name LIKE ?`
-		pars = append(pars, kw)
+		cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
+		pars = append(pars, kw, kw)
 	}
 	if req.SellerIds != "" {
 		sellerIds := strings.Split(req.SellerIds, ",")
@@ -1060,18 +1060,18 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		return
 	}
 	if req.SortParam == "" {
-		page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
+		page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
 	}else if req.SortParam == "start_date" {
 		if req.SortType == "asc" {
-			page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: true})
+			page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
 		}else{
-			page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
+			page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: false})
 		}
 	}else if req.SortParam == "end_date" {
 		if req.SortType == "asc" {
-			page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: true})
+			page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: true})
 		}else{
-			page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: false})
+			page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
 		}
 	}else{
 		resp.Fail("排序字段不正确", c)
@@ -1095,7 +1095,7 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 	}
 
 	//fmt.Println("queryRegisterIds:",queryRegisterIds)
-	results := new(fms.InvoicePaymentCensusResp)
+	results := new(fms.NotInvoicePaymentCensusResp)
 	if len(queryRegisterIds) > 0 {
 		// 获取汇总数据IDs, 用于查询合计数据
 		summaryIdsCond := cond
@@ -1231,6 +1231,20 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 				summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
 			}
 
+			// 货币列表
+			currencyOB := new(fms.CurrencyUnit)
+			currencyCond := `enable = 1`
+			currencyPars := make([]interface{}, 0)
+			currencyList, e := currencyOB.List(currencyCond, currencyPars)
+			if e != nil {
+				resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
+				return
+			}
+			unitMap := make(map[string]string)
+			for i := range currencyList {
+				unitMap[currencyList[i].Code] = currencyList[i].UnitName
+			}
+
 			// 响应列表
 			for i := range registerList {
 				v := new(fms.InvoicePaymentCensusItem)
@@ -1243,12 +1257,17 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 				v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
 				v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
 				v.ContractType = registerList[i].ContractType
+				v.ContractCode = registerList[i].ContractCode
+				v.ContractAmount = registerList[i].ContractAmount
+				v.UnitName = unitMap[registerList[i].CurrencyUnit]
+				v.ProductIds = registerList[i].ProductIds
+				v.NotInvoiceTotal = registerList[i].ContractAmount - registerList[i].InvoicedAmount
 				respList = append(respList, v)
 			}
 		}()
 
 		// 开票到款金额合计(换算后)
-		var invoiceTotal, paymentTotal float64
+		var invoiceTotal, paymentTotal, contractAmount float64
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
@@ -1276,6 +1295,7 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 		// 分币种金额统计
 		invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
 		paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
+		contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
@@ -1303,6 +1323,12 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 					Code:     currencyList[i].Code,
 					FlagImg:  currencyList[i].FlagImg,
 				})
+				contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
+					Name:     currencyList[i].Name,
+					UnitName: currencyList[i].UnitName,
+					Code:     currencyList[i].Code,
+					FlagImg:  currencyList[i].FlagImg,
+				})
 			}
 
 			if len(summaryIds) == 0 {
@@ -1323,6 +1349,8 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 			}
 			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
@@ -1339,6 +1367,24 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 				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
+				}
+				amount,_ := strconv.ParseFloat(fmt.Sprintf("%.2f",v.ContractAmount / v.RMBRate), 64)
+				contractAmount += amount
+				contractSumMap[v.CurrencyUnit] = v.ContractAmount
+			}
+			for _, v := range contractAmountCurrencyTotals {
+				v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
+			}
+
 		}()
 
 		wg.Wait()
@@ -1355,17 +1401,18 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 			resp.FailMsg("获取失败", totalGroupErr.Error(), c)
 			return
 		}
-
 		results.DataList = respList
 		results.InvoiceTotal = invoiceTotal
 		results.PaymentTotal = paymentTotal
+		results.NotInvoiceTotal = contractAmount-invoiceTotal
 		results.InvoiceCurrencyTotal = invoiceCurrencyTotals
 		results.PaymentCurrencyTotal = paymentCurrencyTotals
+		results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
 	}
 
 	// 是否导出
 	if req.IsExport == 1 {
-		ExportInvoicePaymentCensusList(c, results)
+		ExportNotInvoiceCensusList(c, results)
 		return
 	}
 	page.SetTotal(total)
@@ -1376,8 +1423,8 @@ func (ct *InvoicePaymentController) NotInvoiceList(c *gin.Context) {
 }
 
 // List
-// @Title 商品到款统计列表
-// @Description 商品到款统计列表
+// @Title 开票未到款统计列表
+// @Description 开票未到款统计列表
 // @Param   Keyword			query	string	false	"关键词"
 // @Param   SellGroupId		query	int		false	"销售组别ID"
 // @Param   ServiceType		query	int		false	"套餐类型"
@@ -1409,8 +1456,8 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 	// 客户姓名/销售
 	if req.Keyword != "" {
 		kw := "%" + req.Keyword + "%"
-		cond += ` AND b.company_name LIKE ?`
-		pars = append(pars, kw)
+		cond += ` AND (b.company_name LIKE ? OR b.contract_code LIKE ?)`
+		pars = append(pars, kw, kw)
 	}
 	if req.SellerIds != "" {
 		sellerIds := strings.Split(req.SellerIds, ",")
@@ -1491,9 +1538,9 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 		return
 	}
 	if req.SortType == "asc" {
-		page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
+		page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: true})
 	}else{
-		page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
+		page.AddOrderItem(base.OrderItem{Column: "e.invoice_time", Asc: false})
 	}
 
 
@@ -1513,7 +1560,7 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 	}
 
 	//fmt.Println("queryRegisterIds:",queryRegisterIds)
-	results := new(fms.InvoicePaymentCensusResp)
+	results := new(fms.NotInvoicePaymentCensusResp)
 	if len(queryRegisterIds) > 0 {
 		// 获取汇总数据IDs, 用于查询合计数据
 		summaryIdsCond := cond
@@ -1617,6 +1664,7 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 				v.PaymentDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].PaymentDate)
 				v.PaymentAmount = summaryList[i].PaymentAmount
 				v.PayType = summaryList[i].PayType
+
 				// 套餐到款分配
 				svaList := make([]*fms.ContractPaymentServiceAmountItem, 0)
 				amountList := serviceAmountMap[summaryList[i].PaymentId]
@@ -1649,6 +1697,20 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 				summaryMap[summaryList[i].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
 			}
 
+			// 货币列表
+			currencyOB := new(fms.CurrencyUnit)
+			currencyCond := `enable = 1`
+			currencyPars := make([]interface{}, 0)
+			currencyList, e := currencyOB.List(currencyCond, currencyPars)
+			if e != nil {
+				resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
+				return
+			}
+			unitMap := make(map[string]string)
+			for i := range currencyList {
+				unitMap[currencyList[i].Code] = currencyList[i].UnitName
+			}
+
 			// 响应列表
 			for i := range registerList {
 				v := new(fms.InvoicePaymentCensusItem)
@@ -1661,12 +1723,17 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 				v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
 				v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
 				v.ContractType = registerList[i].ContractType
+				v.ContractCode = registerList[i].ContractCode
+				v.ContractAmount = registerList[i].ContractAmount
+				v.UnitName = unitMap[registerList[i].CurrencyUnit]
+				v.ProductIds = registerList[i].ProductIds
 				respList = append(respList, v)
 			}
 		}()
 
 		// 开票到款金额合计(换算后)
-		var invoiceTotal, paymentTotal float64
+		// 开票到款金额合计(换算后)
+		var invoiceTotal, paymentTotal, contractAmount float64
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
@@ -1694,6 +1761,7 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 		// 分币种金额统计
 		invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
 		paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
+		contractAmountCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
 		wg.Add(1)
 		go func() {
 			defer wg.Done()
@@ -1721,6 +1789,12 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 					Code:     currencyList[i].Code,
 					FlagImg:  currencyList[i].FlagImg,
 				})
+				contractAmountCurrencyTotals = append(contractAmountCurrencyTotals, &fms.InvoiceListCurrencyTotal{
+					Name:     currencyList[i].Name,
+					UnitName: currencyList[i].UnitName,
+					Code:     currencyList[i].Code,
+					FlagImg:  currencyList[i].FlagImg,
+				})
 			}
 
 			if len(summaryIds) == 0 {
@@ -1741,6 +1815,8 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 			}
 			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
@@ -1757,6 +1833,24 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 				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
+				}
+				amount, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", v.ContractAmount/v.RMBRate), 64)
+				contractAmount += amount
+				contractSumMap[v.CurrencyUnit] = v.ContractAmount
+			}
+			for _, v := range contractAmountCurrencyTotals {
+				v.Amount = contractSumMap[v.Code] - invoiceSumMap[v.Code]
+			}
+
 		}()
 
 		wg.Wait()
@@ -1773,17 +1867,17 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 			resp.FailMsg("获取失败", totalGroupErr.Error(), c)
 			return
 		}
-
 		results.DataList = respList
 		results.InvoiceTotal = invoiceTotal
 		results.PaymentTotal = paymentTotal
+		results.NotInvoiceTotal = contractAmount - invoiceTotal
 		results.InvoiceCurrencyTotal = invoiceCurrencyTotals
 		results.PaymentCurrencyTotal = paymentCurrencyTotals
+		results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
 	}
-
 	// 是否导出
 	if req.IsExport == 1 {
-		ExportInvoicePaymentCensusList(c, results)
+		ExportNotPaymentCensusList(c, results)
 		return
 	}
 	page.SetTotal(total)
@@ -1791,4 +1885,425 @@ func (ct *InvoicePaymentController) NotPaymentList(c *gin.Context) {
 	baseData.SetPage(page)
 	baseData.SetList(results)
 	resp.OkData("获取成功", baseData, c)
+}
+
+// ExportNotInvoiceCensusList 导出未开票统计列表
+func ExportNotInvoiceCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
+	list := results.DataList
+	if len(list) == 0 {
+		resp.Fail("列表数据为空", c)
+		return
+	}
+
+	// 生成Excel文件
+	xlsxFile := xlsx.NewFile()
+	style := xlsx.NewStyle()
+	alignment := xlsx.Alignment{
+		Horizontal: "center",
+		Vertical:   "center",
+		WrapText:   true,
+	}
+	style.Alignment = alignment
+	style.ApplyAlignment = true
+
+	sheet, err := xlsxFile.AddSheet("未开票统计")
+	if err != nil {
+		resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
+		return
+	}
+	_ = sheet.SetColWidth(1, 1, 30)
+	_ = sheet.SetColWidth(3, 3, 30)
+
+	// 前三行-开票金额合计
+	rowA := sheet.AddRow()
+	cellAA := rowA.AddCell()
+	cellAA.SetString(fmt.Sprintf("未开票合计金额(换算后):%.2f(元)", results.NotInvoiceTotal))
+	rowBData := "未开票金额:"
+	for _, v := range results.NotInvoiceCurrencyTotal {
+		rowBData += fmt.Sprintf("%s%.2f(%s)  ", v.Name, v.Amount, v.UnitName)
+	}
+	rowB := sheet.AddRow()
+	rowB.AddCell().SetString(rowBData)
+	sheet.AddRow()
+
+	// 表头, 套餐动态获取
+	rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
+		"销售组别", "销售类型"}
+	serviceTempCond := ``
+	serviceTempPars := make([]interface{}, 0)
+	serviceTempOB := new(fms.ContractServiceTemplate)
+	serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
+	if e != nil {
+		resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
+		return
+	}
+	serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
+	var serviceTempShow [] *fms.ContractServiceTemplate
+	for i := range serviceTempList {
+		serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
+	}
+	for i := range serviceTempList {
+		if serviceTempList[i].ProductId == 1 {
+			rowTitle = append(rowTitle, serviceTempList[i].Title)
+			serviceTempShow = append(serviceTempShow, serviceTempList[i])
+		}else if serviceTempList[i].ProductId == 2{
+			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)
+		}
+	}
+
+	hi := 0
+	insertHi := 0
+	titleRow := sheet.AddRow()
+	for i := range rowTitle {
+		v := titleRow.AddCell()
+		v.SetString(rowTitle[i])
+		v.SetStyle(style)
+		if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
+			v.VMerge = 1
+			hi ++
+		}else{
+			if insertHi == 0 {
+				insertHi = hi
+			}
+			v.HMerge = 1
+			titleRow.AddCell().SetString("")
+		}
+	}
+
+	// 新增一行放主观和客观
+	specialRow := sheet.AddRow()
+	for i:=0; i< hi; i++ {
+		if i == insertHi {
+			v1 := specialRow.AddCell()
+			v1.SetString("45万")
+			v1.SetStyle(style)
+			v1 = specialRow.AddCell()
+			v1.SetString("70万")
+			v1.SetStyle(style)
+			for j:=0; j<8;j++ {
+				if j%2 ==0 {
+					v1 = specialRow.AddCell()
+					v1.SetString("主观")
+					v1.SetStyle(style)
+				}else{
+					v1 = specialRow.AddCell()
+					v1.SetString("客观")
+					v1.SetStyle(style)
+				}
+			}
+		}else{
+			v := specialRow.AddCell()
+			v.SetString("")
+		}
+	}
+
+	newCompanyMap := map[int]string{0: "否", 1: "是"}
+	sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
+	for k, v := range list {
+		dataRow := sheet.AddRow()
+		// 前四个单元格根据每行开票到款条数向下合并
+		l := len(v.InvoicePaymentList)
+		mergeRowNum := l - 1
+		// 序号
+		sortNum := k + 1
+		colA := dataRow.AddCell()
+		colA.VMerge = mergeRowNum
+		colA.SetString(fmt.Sprint(sortNum))
+		// 客户名称
+		colB := dataRow.AddCell()
+		colB.VMerge = mergeRowNum
+		colB.SetString(v.CompanyName)
+		// 是否新客户
+		colC := dataRow.AddCell()
+		colC.VMerge = mergeRowNum
+		colC.SetString(newCompanyMap[v.NewCompany])
+		// 合同有效期
+		colD := dataRow.AddCell()
+		colD.VMerge = mergeRowNum
+		colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
+		// 开票到款信息
+		for k2, v2 := range v.InvoicePaymentList {
+			rowData := []string{
+				v2.InvoiceDate,                                // 开票日
+				fmt.Sprint(v2.InvoiceAmount),                  // 开票金额
+				v2.PaymentDate,                                // 到款日
+				fmt.Sprint(v2.PaymentAmount),                  // 到款金额
+				fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
+				v2.SellerName,                                 // 销售
+				v2.SellerGroupName,                            // 组别
+				sellerTypeMap[v2.SellerType],                  // 销售类型
+			}
+			// 套餐金额信息
+			serviceTempShowAmount := make(map[int]string)
+			for i := range serviceTempShow {
+				for s2 := range v2.ServiceAmountList {
+					item := v2.ServiceAmountList[s2]
+					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)
+			}
+			// 首行开票到款
+			if k2 == 0 {
+				for i := range rowData {
+					dataRow.AddCell().SetString(rowData[i])
+				}
+				continue
+			}
+			// 其他行开票到款, 加四列空的单元格用于合并
+			dataRowExtra := sheet.AddRow()
+			for i := 0; i < 4; i++ {
+				dataRowExtra.AddCell()
+			}
+			for i := range rowData {
+				dataRowExtra.AddCell().SetString(rowData[i])
+			}
+		}
+	}
+
+	// 输出文件
+	var buffer bytes.Buffer
+	_ = xlsxFile.Write(&buffer)
+	content := bytes.NewReader(buffer.Bytes())
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	fileName := "商品到款统计_" + 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)
+}
+
+
+// ExportNotPaymentCensusList 导出开票未到款统计列表
+func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCensusResp) {
+	list := results.DataList
+	if len(list) == 0 {
+		resp.Fail("列表数据为空", c)
+		return
+	}
+
+	// 生成Excel文件
+	xlsxFile := xlsx.NewFile()
+	style := xlsx.NewStyle()
+	alignment := xlsx.Alignment{
+		Horizontal: "center",
+		Vertical:   "center",
+		WrapText:   true,
+	}
+	style.Alignment = alignment
+	style.ApplyAlignment = true
+
+	sheet, err := xlsxFile.AddSheet("开票未到款统计")
+	if err != nil {
+		resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
+		return
+	}
+	_ = sheet.SetColWidth(1, 1, 30)
+	_ = sheet.SetColWidth(3, 3, 30)
+
+	// 前三行-开票金额合计
+	rowA := sheet.AddRow()
+	cellAA := rowA.AddCell()
+	cellAA.SetString(fmt.Sprintf("开票未到款合计金额(换算后):%.2f(元)", results.InvoiceTotal))
+	rowBData := "已开票金额:"
+	for _, v := range results.InvoiceCurrencyTotal {
+		rowBData += fmt.Sprintf("%s%.2f(%s)  ", v.Name, v.Amount, v.UnitName)
+	}
+	rowB := sheet.AddRow()
+	rowB.AddCell().SetString(rowBData)
+	sheet.AddRow()
+
+	// 表头, 套餐动态获取
+	rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
+		"销售组别", "销售类型"}
+	serviceTempCond := ``
+	serviceTempPars := make([]interface{}, 0)
+	serviceTempOB := new(fms.ContractServiceTemplate)
+	serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
+	if e != nil {
+		resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
+		return
+	}
+	serviceTempListMap := make(map[int]*fms.ContractServiceTemplate)
+	var serviceTempShow [] *fms.ContractServiceTemplate
+	for i := range serviceTempList {
+		serviceTempListMap[serviceTempList[i].ServiceTemplateId] = serviceTempList[i]
+	}
+	for i := range serviceTempList {
+		if serviceTempList[i].ProductId == 1 {
+			rowTitle = append(rowTitle, serviceTempList[i].Title)
+			serviceTempShow = append(serviceTempShow, serviceTempList[i])
+		}else if serviceTempList[i].ProductId == 2{
+			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)
+		}
+	}
+
+	hi := 0
+	insertHi := 0
+	titleRow := sheet.AddRow()
+	for i := range rowTitle {
+		v := titleRow.AddCell()
+		v.SetString(rowTitle[i])
+		v.SetStyle(style)
+		if rowTitle[i] != "权益大套餐" && rowTitle[i] != "医药" && rowTitle[i] != "消费" && rowTitle[i] != "科技" && rowTitle[i] != "智造" {
+			v.VMerge = 1
+			hi ++
+		}else{
+			if insertHi == 0 {
+				insertHi = hi
+			}
+			v.HMerge = 1
+			titleRow.AddCell().SetString("")
+		}
+	}
+
+	// 新增一行放主观和客观
+	specialRow := sheet.AddRow()
+	for i:=0; i< hi; i++ {
+		if i == insertHi {
+			v1 := specialRow.AddCell()
+			v1.SetString("45万")
+			v1.SetStyle(style)
+			v1 = specialRow.AddCell()
+			v1.SetString("70万")
+			v1.SetStyle(style)
+			for j:=0; j<8;j++ {
+				if j%2 ==0 {
+					v1 = specialRow.AddCell()
+					v1.SetString("主观")
+					v1.SetStyle(style)
+				}else{
+					v1 = specialRow.AddCell()
+					v1.SetString("客观")
+					v1.SetStyle(style)
+				}
+			}
+		}else{
+			v := specialRow.AddCell()
+			v.SetString("")
+		}
+	}
+
+	newCompanyMap := map[int]string{0: "否", 1: "是"}
+	sellerTypeMap := map[int]string{1:"FICC销售", 2:"权益销售"}
+	for k, v := range list {
+		dataRow := sheet.AddRow()
+		// 前四个单元格根据每行开票到款条数向下合并
+		l := len(v.InvoicePaymentList)
+		mergeRowNum := l - 1
+		// 序号
+		sortNum := k + 1
+		colA := dataRow.AddCell()
+		colA.VMerge = mergeRowNum
+		colA.SetString(fmt.Sprint(sortNum))
+		// 客户名称
+		colB := dataRow.AddCell()
+		colB.VMerge = mergeRowNum
+		colB.SetString(v.CompanyName)
+		// 是否新客户
+		colC := dataRow.AddCell()
+		colC.VMerge = mergeRowNum
+		colC.SetString(newCompanyMap[v.NewCompany])
+		// 合同有效期
+		colD := dataRow.AddCell()
+		colD.VMerge = mergeRowNum
+		colD.SetString(fmt.Sprint(v.StartDate, "至", v.EndDate))
+		// 开票到款信息
+		for k2, v2 := range v.InvoicePaymentList {
+			rowData := []string{
+				v2.InvoiceDate,                                // 开票日
+				fmt.Sprint(v2.InvoiceAmount),                  // 开票金额
+				v2.PaymentDate,                                // 到款日
+				fmt.Sprint(v2.PaymentAmount),                  // 到款金额
+				fms.ContractPaymentPayTypeNameMap[v2.PayType], // 付款方式
+				v2.SellerName,                                 // 销售
+				v2.SellerGroupName,                            // 组别
+				sellerTypeMap[v2.SellerType],                  // 销售类型
+			}
+			// 套餐金额信息
+			serviceTempShowAmount := make(map[int]string)
+			for i := range serviceTempShow {
+				for s2 := range v2.ServiceAmountList {
+					item := v2.ServiceAmountList[s2]
+					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)
+			}
+			// 首行开票到款
+			if k2 == 0 {
+				for i := range rowData {
+					dataRow.AddCell().SetString(rowData[i])
+				}
+				continue
+			}
+			// 其他行开票到款, 加四列空的单元格用于合并
+			dataRowExtra := sheet.AddRow()
+			for i := 0; i < 4; i++ {
+				dataRowExtra.AddCell()
+			}
+			for i := range rowData {
+				dataRowExtra.AddCell().SetString(rowData[i])
+			}
+		}
+	}
+
+	// 输出文件
+	var buffer bytes.Buffer
+	_ = xlsxFile.Write(&buffer)
+	content := bytes.NewReader(buffer.Bytes())
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	fileName := "商品到款统计_" + 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)
 }

+ 5 - 0
models/fms/contract_invoice.go

@@ -346,6 +346,11 @@ type InvoicePaymentCensusItem struct {
 	ServicesName       string                      `json:"services_name" description:"套餐信息字符串拼接"`
 	ContractType       int                         `json:"contract_type" description:"合同类型: 0-无 1-新签; 2-续约"`
 	InvoicePaymentList []*InvoicePaymentCensusInfo `json:"invoice_payment_list" description:"开票到款列表"`
+	ContractCode       string                      `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
+	ContractAmount     float64                     `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
+	UnitName           string                      `gorm:"unit_name" json:"unit_name" description:"单位名称"`
+	ProductIds         string                      `gorm:"column:product_ids" json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
+	NotInvoiceTotal    float64                     `json:"not_invoice_total" description:"未开票总金额(换算后)"`
 }
 
 // InvoicePaymentCensusInfo 开票到款统计信息

+ 26 - 10
models/fms/invoice_payment_summary.go

@@ -92,6 +92,10 @@ type InvoicePaymentSummaryItem struct {
 	PaymentAmount    float64   `json:"payment_amount" description:"到款金额"`
 	PayType          int       `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
 	ServiceProductId int       `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
+	ContractAmount   float64   `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
+	InvoicedAmount   float64   `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
+	CurrencyUnit     string    `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
+	RMBRate          float64   `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
 }
 
 // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
@@ -180,13 +184,13 @@ func GetInvoiceCensusPageList(page base.IPage, condition string, pars []interfac
 		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("b.contract_register_id")
-	query.Count(&count)
-	if len(page.GetOrderItemsString()) > 0 {
-		query = query.Order(page.GetOrderItemsString())
-	}
 
 	nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
 		Select("*").Where(" e.invoiced_amount_count <>  e.contract_amount ")
+	nq.Count(&count)
+	if len(page.GetOrderItemsString()) > 0 {
+		nq = nq.Order(page.GetOrderItemsString())
+	}
 	err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
 	if err != nil {
 		return
@@ -194,25 +198,37 @@ func GetInvoiceCensusPageList(page base.IPage, condition string, pars []interfac
 	return
 }
 
-
 // GetNoPaymentCensusPageList 获取开票未到款统计计列表-总数
 func GetNoPaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
 	query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
-		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, SUM( c.amount ) AS invoiced_amount_count,SUM( d.amount ) AS payment_amount_count ").
+		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, SUM( c.amount ) AS invoiced_amount_count,SUM( d.amount ) AS payment_amount_count " +
+			" ,c.invoice_time ").
 		Joins("JOIN contract_register AS b 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("b.contract_register_id")
-	query.Count(&count)
-	if len(page.GetOrderItemsString()) > 0 {
-		query = query.Order(page.GetOrderItemsString())
-	}
+
 
 	nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
 		Select("*").Where(" e.invoiced_amount_count <>  e.contract_amount AND e.payment_amount = 0 ")
+	nq.Count(&count)
+	if len(page.GetOrderItemsString()) > 0 {
+		nq = nq.Order(page.GetOrderItemsString())
+	}
 	err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
 	if err != nil {
 		return
 	}
 	return
 }
+
+// NotInvoicePaymentCensusResp 未开票到款到款统计响应体
+type NotInvoicePaymentCensusResp struct {
+	DataList                []*InvoicePaymentCensusItem `json:"data_list"`
+	InvoiceTotal            float64                     `json:"invoice_total" description:"开票总金额(换算后)"`
+	PaymentTotal            float64                     `json:"payment_total" description:"到款总金额(换算后)"`
+	InvoiceCurrencyTotal    []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
+	PaymentCurrencyTotal    []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
+	NotInvoiceTotal         float64                     `json:"not_invoice_total" description:"未开票总金额(换算后)"`
+	NotInvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_invoice_currency_total" description:"未开票-分币种总金额"`
+}

+ 1 - 0
routers/census.go

@@ -12,6 +12,7 @@ func InitCensus(rg *gin.RouterGroup) {
 	invGroup := rg.Group("invoice_payment/").Use(middleware.Token())
 	invGroup.GET("list", inv.List)
 	invGroup.GET("not_invoice/list", inv.NotInvoiceList)
+	invGroup.GET("not_payment/list", inv.NotPaymentList)
 
 
 	// 销售