Browse Source

test 商品到款统计列表导出

hsun 2 years ago
parent
commit
3abdd38692
3 changed files with 143 additions and 2 deletions
  1. 134 2
      controller/census/invoice_payment.go
  2. 8 0
      models/fms/constants.go
  3. 1 0
      models/fms/contract_invoice.go

+ 134 - 2
controller/census/invoice_payment.go

@@ -1,16 +1,20 @@
 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/base"
 	"hongze/fms_api/models/fms"
 	fmsService "hongze/fms_api/services/fms"
 	"hongze/fms_api/utils"
+	"net/http"
 	"strings"
+	"time"
 )
 
 // InvoicePaymentController 商品到款统计
@@ -27,6 +31,7 @@ type InvoicePaymentController struct{}
 // @Param   TimeType		query	int		false	"时间类型: 1-开票时间; 2-到款时间"
 // @Param   HasInvoice		query	int		false	"已开票"
 // @Param   HasPayment		query	int		false	"已到款"
+// @Param   IsExport		query	int		false	"是否导出"
 // @Success 200 {object} fms.ContractRegisterItem
 // @router /census/invoice_payment/list [get]
 func (ct *InvoicePaymentController) List(c *gin.Context) {
@@ -87,6 +92,10 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 	page.SetPageSize(req.PageSize)
 	page.SetCurrent(req.Current)
 	page.AddOrderItem(base.OrderItem{Column: "a.create_time", Asc: false})
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
 
 	total, list, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
 	if e != nil {
@@ -153,7 +162,7 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 		// 到款套餐分配
 		serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
 		if len(paymentIds) > 0 {
-			serviceAmountCond := `contract_invoice_id IN ?`
+			serviceAmountCond := `contract_payment_id IN ?`
 			serviceAmountPars := make([]interface{}, 0)
 			serviceAmountPars = append(serviceAmountPars, paymentIds)
 			serviceAmountOB := new(fms.ContractPaymentServiceAmount)
@@ -178,7 +187,7 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 			v.CompanyName = list[i].CompanyName
 			v.NewCompany = list[i].NewCompany
 			v.StartDate = list[i].StartDate.Format(utils.FormatDate)
-			v.StartDate = list[i].StartDate.Format(utils.FormatDate)
+			v.EndDate = list[i].EndDate.Format(utils.FormatDate)
 			svList := servicesNameMap[list[i].ContractRegisterId]
 			v.ServicesName = strings.Join(svList, ",")
 			// 格式化(合并)开票到款数据
@@ -206,9 +215,132 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 		results.PaymentTotal = amountTotalMap[fms.ContractInvoiceTypePay]
 	}
 
+	// 是否导出
+	if req.IsExport == 1 {
+		ExportInvoicePaymentCensusList(c, results.DataList)
+		return
+	}
 	page.SetTotal(total)
 	baseData := new(base.BaseData)
 	baseData.SetPage(page)
 	baseData.SetList(results)
 	resp.OkData("获取成功", baseData, c)
 }
+
+// ExportInvoicePaymentCensusList 导出商品到款统计列表
+func ExportInvoicePaymentCensusList(c *gin.Context, list []*fms.InvoicePaymentCensusItem) {
+	// 生成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)
+
+	// 表头, 套餐动态获取
+	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
+	}
+	for i := range serviceTempList {
+		rowTitle = append(rowTitle, serviceTempList[i].Title)
+	}
+
+	titleRow := sheet.AddRow()
+	for i := range rowTitle {
+		v := titleRow.AddCell()
+		v.SetValue(rowTitle[i])
+		v.SetStyle(style)
+	}
+
+	newCompanyMap := map[int]string{0: "否", 1: "是"}
+	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,                            // 组别
+			}
+			// 套餐金额信息
+			for i := range serviceTempList {
+				sa := ""
+				for s2 := range v2.ServiceAmountList {
+					if v2.ServiceAmountList[s2].ServiceTemplateId == serviceTempList[i].ServiceTemplateId {
+						sa = fmt.Sprint(v2.ServiceAmountList[s2].Amount)
+						break
+					}
+				}
+				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)
+}

+ 8 - 0
models/fms/constants.go

@@ -52,3 +52,11 @@ var ContractInvoiceKeyNameMap = map[int]string{
 	ContractRegisterOpTypeInvoice: "开票",
 	ContractRegisterOpTypePayment: "到款",
 }
+
+var ContractPaymentPayTypeNameMap = map[int]string{
+	ContractPaymentPayTypeYear:     "年付",
+	ContractPaymentPayTypeHalfYear: "半年付",
+	ContractPaymentPayTypeQuarter:  "季付",
+	ContractPaymentPayTypeOther:    "次付",
+	ContractPaymentPayTypeAbnormal: "异常",
+}

+ 1 - 0
models/fms/contract_invoice.go

@@ -237,6 +237,7 @@ type InvoicePaymentCensusListReq struct {
 	TimeType    int    `json:"time_type" form:"time_type" description:"时间类型: 1-开票时间; 2-到款时间"`
 	HasInvoice  int    `json:"has_invoice" form:"has_invoice" description:"已开票"`
 	HasPayment  int    `json:"has_payment" form:"has_payment" description:"已到款"`
+	IsExport    int    `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
 	base.PageReq
 }