Преглед на файлове

1.开票列表导出优化、货币金额统计 2.商品到款列表货币金额统计

hsun преди 2 години
родител
ревизия
985b9acde0
променени са 4 файла, в които са добавени 149 реда и са изтрити 81 реда
  1. 85 3
      controller/census/invoice_payment.go
  2. 51 71
      controller/contract/register.go
  3. 13 6
      models/fms/contract_invoice.go
  4. 0 1
      routers/contract.go

+ 85 - 3
controller/census/invoice_payment.go

@@ -197,7 +197,7 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 			respList = append(respList, v)
 		}
 
-		// 开票到款金额合计
+		// 开票到款金额合计(换算后)
 		amountTotalCond := `contract_register_id IN ?`
 		amountTotalPars := make([]interface{}, 0)
 		amountTotalPars = append(amountTotalPars, registerIds)
@@ -211,14 +211,66 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 			amountTotalMap[amountTotalList[i].InvoiceType] = amountTotalList[i].TotalAmount
 		}
 
+		// 分币种金额统计
+		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)
+		invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
+		paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
+		for i := range currencyList {
+			unitMap[currencyList[i].Code] = currencyList[i].UnitName
+			invoiceCurrencyTotals = append(invoiceCurrencyTotals, &fms.InvoiceListCurrencyTotal{
+				Name:     currencyList[i].Name,
+				UnitName: currencyList[i].UnitName,
+				Code:     currencyList[i].Code,
+				FlagImg:  currencyList[i].FlagImg,
+			})
+			paymentCurrencyTotals = append(paymentCurrencyTotals, &fms.InvoiceListCurrencyTotal{
+				Name:     currencyList[i].Name,
+				UnitName: currencyList[i].UnitName,
+				Code:     currencyList[i].Code,
+				FlagImg:  currencyList[i].FlagImg,
+			})
+		}
+		sumList, e := fms.GetInvoiceListCurrencySum(cond, pars, "currency_unit, invoice_type")
+		if e != nil {
+			resp.FailMsg("获取失败", "获取商品到款统计货币合计金额失败, Err: "+e.Error(), c)
+			return
+		}
+		invoiceSumMap := make(map[string]float64)
+		paymentSumMap := make(map[string]float64)
+		for i := range sumList {
+			if sumList[i].InvoiceType == fms.ContractInvoiceTypeMake {
+				invoiceSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
+				continue
+			}
+			if sumList[i].InvoiceType == fms.ContractInvoiceTypePay {
+				paymentSumMap[sumList[i].CurrencyUnit] = sumList[i].OriginAmountTotal
+			}
+		}
+		for i := range invoiceCurrencyTotals {
+			invoiceCurrencyTotals[i].Amount = invoiceSumMap[invoiceCurrencyTotals[i].Code]
+		}
+		for i := range paymentCurrencyTotals {
+			paymentCurrencyTotals[i].Amount = paymentSumMap[paymentCurrencyTotals[i].Code]
+		}
+
 		results.DataList = respList
 		results.InvoiceTotal = amountTotalMap[fms.ContractInvoiceTypeMake]
 		results.PaymentTotal = amountTotalMap[fms.ContractInvoiceTypePay]
+		results.InvoiceCurrencyTotal = invoiceCurrencyTotals
+		results.PaymentCurrencyTotal = paymentCurrencyTotals
 	}
 
 	// 是否导出
 	if req.IsExport == 1 {
-		ExportInvoicePaymentCensusList(c, results.DataList)
+		ExportInvoicePaymentCensusList(c, results)
 		return
 	}
 	page.SetTotal(total)
@@ -229,7 +281,13 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 }
 
 // ExportInvoicePaymentCensusList 导出商品到款统计列表
-func ExportInvoicePaymentCensusList(c *gin.Context, list []*fms.InvoicePaymentCensusItem) {
+func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentCensusResp) {
+	list := results.DataList
+	if len(list) == 0 {
+		resp.Fail("列表数据为空", c)
+		return
+	}
+
 	// 生成Excel文件
 	xlsxFile := xlsx.NewFile()
 	style := xlsx.NewStyle()
@@ -249,6 +307,30 @@ func ExportInvoicePaymentCensusList(c *gin.Context, list []*fms.InvoicePaymentCe
 	_ = 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()
+
+	// 四至六-到款金额合计
+	rowD := sheet.AddRow()
+	cellDA := rowD.AddCell()
+	cellDA.SetString(fmt.Sprintf("已到款合计金额(换算后):%.2f(元)", results.PaymentTotal))
+	rowEData := "已到款金额:"
+	for _, v := range results.PaymentCurrencyTotal {
+		rowEData += fmt.Sprintf("%s%.2f(%s)  ", v.Name, v.Amount, v.UnitName)
+	}
+	rowE := sheet.AddRow()
+	rowE.AddCell().SetString(rowEData)
+	sheet.AddRow()
+
 	// 表头, 套餐动态获取
 	rowTitle := []string{"序号", "客户名称", "是否新客户", "合同有效期", "开票日", "开票金额", "到款日", "到款金额", "付款方式", "销售",
 		"组别"}

+ 51 - 71
controller/contract/register.go

@@ -1269,6 +1269,7 @@ func (rg *RegisterController) Export(c *gin.Context) {
 // @Param   EndDate			query	string	false	"结束日期"
 // @Param   MinAmount		query	float64	false	"开票金额区间-最小值"
 // @Param   MaxAmount		query	float64	false	"开票金额区间-最大值"
+// @Param   IsExport		query	int		false	"是否导出: 0-否; 1-是"
 // @Success 200 {object} fms.ContractInvoiceItem
 // @router /contract/register/invoice_list [get]
 func (rg *RegisterController) InvoiceList(c *gin.Context) {
@@ -1345,6 +1346,10 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 	page.SetPageSize(pageSize)
 	page.SetCurrent(pageIndex)
 	page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
 	total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
 	if e != nil {
 		resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
@@ -1357,7 +1362,7 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 
 	// 分币种合计金额
 	var amountTotal float64
-	sumList, e := fms.GetInvoiceListCurrencySum(cond, pars)
+	sumList, e := fms.GetInvoiceListCurrencySum(cond, pars, "currency_unit")
 	if e != nil {
 		resp.FailMsg("获取失败", "获取开票/到款列表合计金额失败, Err: "+e.Error(), c)
 		return
@@ -1379,73 +1384,33 @@ func (rg *RegisterController) InvoiceList(c *gin.Context) {
 		AmountTotal:   amountTotal,
 		CurrencyTotal: currencyTotals,
 	}
+	// 是否导出
+	if req.IsExport == 1 {
+		ExportInvoiceList(c, req, respData)
+		return
+	}
 	resp.OkData("获取成功", respData, c)
 }
 
-// InvoiceExport
-// @Title 开票/到款列表-导出
-// @Description 合同登记-导出
-// @Param   InvoiceType		query	int		false	"类型: 1-开票登记; 2-到款登记"
-// @Param   ContractCode	query	string	false	"合同编号"
-// @Param   StartDate		query	string	false	"开始日期"
-// @Param   EndDate			query	string	false	"结束日期"
-// @Param   MinAmount		query	float64	false	"开票金额区间-最小值"
-// @Param   MaxAmount		query	float64	false	"开票金额区间-最大值"
-// @Success 200 string "操作成功"
-// @router /contract/register/invoice_export [get]
-func (rg *RegisterController) InvoiceExport(c *gin.Context) {
-	var req fms.ContractInvoiceListReq
-	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)
+// ExportInvoiceList 导出开票/到款列表
+func ExportInvoiceList(c *gin.Context, req fms.ContractInvoiceListReq, results *fms.InvoiceListRespData) {
+	list := make([]*fms.ContractInvoiceItem, 0)
+	if val, ok := results.List.([]*fms.ContractInvoiceItem); ok {
+		list = val
+	} else {
+		resp.Fail("列表数据有误", c)
+		return
+	}
+	if len(list) == 0 {
+		resp.Fail("列表数据为空", c)
 		return
 	}
+
 	listName := "开票"
 	if req.InvoiceType == fms.ContractInvoiceTypePay {
 		listName = "到款"
 	}
 
-	cond := `invoice_type = ?`
-	pars := make([]interface{}, 0)
-	pars = append(pars, req.InvoiceType)
-	// 合同编号
-	if req.ContractCode != "" {
-		kw := fmt.Sprint("%", req.ContractCode, "%")
-		cond += ` AND contract_code LIKE ?`
-		pars = append(pars, kw)
-	}
-	if req.StartDate != "" && req.EndDate != "" {
-		st := fmt.Sprint(req.StartDate, " 00:00:00")
-		ed := fmt.Sprint(req.EndDate, " 23:59:59")
-		cond += ` AND (invoice_time BETWEEN ? AND ?)`
-		pars = append(pars, st, ed)
-	}
-	if req.MinAmount > 0 {
-		cond += ` AND amount >= ?`
-		pars = append(pars, req.MinAmount)
-	}
-	if req.MaxAmount > 0 {
-		cond += ` AND amount <= ?`
-		pars = append(pars, req.MaxAmount)
-	}
-
-	// 获取列表数据
-	cr := new(fms.ContractInvoice)
-	orderRule := `invoice_time DESC`
-	list, e := cr.List(cond, pars, orderRule)
-	if e != nil {
-		resp.FailData(fmt.Sprintf("获取%s列表失败", listName), "Err:"+e.Error(), c)
-		return
-	}
-	if len(list) == 0 {
-		resp.Fail("无有效数据可导出", c)
-		return
-	}
-
 	// 生成Excel文件
 	xlsxFile := xlsx.NewFile()
 	style := xlsx.NewStyle()
@@ -1463,25 +1428,40 @@ func (rg *RegisterController) InvoiceExport(c *gin.Context) {
 		return
 	}
 
-	// 表头
+	// 前两行显示合计金额, 第三行空出与列表数据隔一行
+	rowA := sheet.AddRow()
+	cellAA := rowA.AddCell()
+	cellAA.SetString(fmt.Sprintf("已开票合计金额(换算后):%.2f(元)", results.AmountTotal))
+	rowBData := "已开票金额:"
+	for _, v := range results.CurrencyTotal {
+		rowBData += fmt.Sprintf("%s%.2f(%s)  ", v.Name, v.Amount, v.UnitName)
+	}
+	rowB := sheet.AddRow()
+	rowB.AddCell().SetString(rowBData)
+	sheet.AddRow()
+
+	// 列表数据表头
+	titles := []string{"合同编号", fmt.Sprintf("%s金额", listName), "金额单位", "换算金额(元)",
+		fmt.Sprintf("%s日期", listName), "销售", "备注"}
 	titleRow := sheet.AddRow()
 	titleRow.SetHeight(40)
-	cell1 := titleRow.AddCell()
-	cell1.SetString("合同编号")
-	cell1.SetStyle(style)
-	cell2 := titleRow.AddCell()
-	cell2.SetString(fmt.Sprintf("%s金额", listName))
-	cell2.SetStyle(style)
-	cell3 := titleRow.AddCell()
-	cell3.SetString(fmt.Sprintf("%s日期", listName))
-	cell3.SetStyle(style)
+	for i := range titles {
+		c := titleRow.AddCell()
+		c.SetString(titles[i])
+		c.SetStyle(style)
+	}
 
+	// 单元格赋值
 	for _, v := range list {
 		dataRow := sheet.AddRow()
 		dataRow.SetHeight(20)
-		dataRow.AddCell().SetString(v.ContractCode)
-		dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
-		dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
+		dataRow.AddCell().SetString(v.ContractCode)             // 合同编号
+		dataRow.AddCell().SetString(fmt.Sprint(v.OriginAmount)) // 开票金额
+		dataRow.AddCell().SetString(v.UnitName)                 // 金额单位
+		dataRow.AddCell().SetString(fmt.Sprint(v.Amount))       // 换算金额(元)
+		dataRow.AddCell().SetString(v.InvoiceDate)              // 开票日
+		dataRow.AddCell().SetString(v.SellerName)               // 销售
+		dataRow.AddCell().SetString(v.Remark)                   // 备注
 	}
 
 	// 输出文件

+ 13 - 6
models/fms/contract_invoice.go

@@ -204,6 +204,7 @@ type ContractInvoiceListReq struct {
 	EndDate      string  `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"结束日期"`
 	MinAmount    float64 `json:"min_amount" form:"min_amount" description:"开票金额区间-最小值"`
 	MaxAmount    float64 `json:"max_amount" form:"max_amount" description:"开票金额区间-最大值"`
+	IsExport     int     `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
 	base.PageReq
 }
 
@@ -238,6 +239,9 @@ func formatContractInvoice2Item(item *ContractInvoice) (formatItem *ContractInvo
 	formatItem.CurrencyUnit = item.CurrencyUnit
 	formatItem.InvoiceType = item.InvoiceType
 	formatItem.InvoiceDate = utils.TimeTransferString(utils.FormatDate, item.InvoiceDate)
+	formatItem.SellerId = item.SellerId
+	formatItem.SellerName = item.SellerName
+	formatItem.PayType = item.PayType
 	formatItem.Remark = item.Remark
 	formatItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
 	return
@@ -293,9 +297,11 @@ func GetInvoicePaymentCensusPageList(page base.IPage, condition string, pars []i
 
 // InvoicePaymentCensusResp 商品到款统计响应体
 type InvoicePaymentCensusResp struct {
-	DataList     []*InvoicePaymentCensusItem `json:"data_list"`
-	InvoiceTotal float64                     `json:"invoice_total" description:"开票总金额"`
-	PaymentTotal float64                     `json:"payment_total" description:"到款总金额"`
+	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:"到款-分币种总金额"`
 }
 
 // InvoicePaymentCensusItem 商品到款统计信息
@@ -456,17 +462,18 @@ type InvoiceListCurrencyTotal struct {
 // InvoiceListCurrencySum 开票/到款列表分币种总和
 type InvoiceListCurrencySum struct {
 	CurrencyUnit      string  `json:"currency_unit" description:"货币代码"`
+	InvoiceType       int     `json:"invoice_type" description:"开票类型:1-开票; 2-到款"`
 	AmountTotal       float64 `json:"amount_total" description:"换算后合计金额"`
 	OriginAmountTotal float64 `json:"origin_amount_total" description:"原合计金额"`
 }
 
 // GetInvoiceListCurrencySum 获取开票/到款分货币合计
-func GetInvoiceListCurrencySum(condition string, pars []interface{}) (results []*InvoiceListCurrencySum, err error) {
+func GetInvoiceListCurrencySum(condition string, pars []interface{}, groupRule string) (results []*InvoiceListCurrencySum, err error) {
 	query := global.DEFAULT_MYSQL.Table("contract_invoice").
-		Select("currency_unit, SUM(amount) AS amount_total, SUM(origin_amount) AS origin_amount_total").
+		Select("currency_unit, invoice_type, SUM(amount) AS amount_total, SUM(origin_amount) AS origin_amount_total").
 		Where("is_deleted = 0").
 		Where(condition, pars...).
-		Group("currency_unit")
+		Group(groupRule)
 	err = query.Find(&results).Error
 	return
 }

+ 0 - 1
routers/contract.go

@@ -20,7 +20,6 @@ func InitContract(rg *gin.RouterGroup) {
 	crGroup.POST("invoice", cr.Invoice)
 	crGroup.POST("payment", cr.Invoice) // 与开票登记用同一个func, 路由作区分划分权限
 	crGroup.GET("invoice_list", cr.InvoiceList)
-	crGroup.GET("invoice_export", cr.InvoiceExport)
 	crGroup.POST("import", cr.Import)
 	crGroup.GET("currency_list", cr.CurrencyList)