|
@@ -15,27 +15,15 @@ import (
|
|
|
"net/http"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
// InvoicePaymentController 商品到款统计
|
|
|
type InvoicePaymentController struct{}
|
|
|
|
|
|
-// List
|
|
|
-// @Title 商品到款统计列表
|
|
|
-// @Description 商品到款统计列表
|
|
|
-// @Param Keyword query string false "关键词"
|
|
|
-// @Param SellGroupId query int false "销售组别ID"
|
|
|
-// @Param ServiceType query int false "套餐类型"
|
|
|
-// @Param StartDate query string false "合同开始日期"
|
|
|
-// @Param EndDate query string false "合同结束日期"
|
|
|
-// @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
|
|
|
-// @Param HasInvoice query int false "是否已开票: 0-否; 1-是"
|
|
|
-// @Param HasPayment query int false "是否已到款: 0-否; 1-是"
|
|
|
-// @Param IsExport query int false "是否导出: 0-否; 1-是"
|
|
|
-// @Success 200 {object} fms.ContractRegisterItem
|
|
|
-// @router /census/invoice_payment/list [get]
|
|
|
-func (ct *InvoicePaymentController) List(c *gin.Context) {
|
|
|
+// List2 原版(已废弃)
|
|
|
+func (ct *InvoicePaymentController) List2(c *gin.Context) {
|
|
|
var req fms.InvoicePaymentCensusListReq
|
|
|
if e := c.BindQuery(&req); e != nil {
|
|
|
err, ok := e.(validator.ValidationErrors)
|
|
@@ -433,3 +421,358 @@ func ExportInvoicePaymentCensusList(c *gin.Context, results *fms.InvoicePaymentC
|
|
|
c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
|
|
http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
|
|
|
}
|
|
|
+
|
|
|
+// List
|
|
|
+// @Title 商品到款统计列表
|
|
|
+// @Description 商品到款统计列表
|
|
|
+// @Param Keyword query string false "关键词"
|
|
|
+// @Param SellGroupId query int false "销售组别ID"
|
|
|
+// @Param ServiceType query int false "套餐类型"
|
|
|
+// @Param StartDate query string false "合同开始日期"
|
|
|
+// @Param EndDate query string false "合同结束日期"
|
|
|
+// @Param TimeType query int false "时间类型: 1-开票时间; 2-到款时间"
|
|
|
+// @Param HasInvoice query int false "是否已开票: 0-否; 1-是"
|
|
|
+// @Param HasPayment query int false "是否已到款: 0-否; 1-是"
|
|
|
+// @Param IsExport query int false "是否导出: 0-否; 1-是"
|
|
|
+// @Success 200 {object} fms.ContractRegisterItem
|
|
|
+// @router /census/invoice_payment/list [get]
|
|
|
+func (ct *InvoicePaymentController) List(c *gin.Context) {
|
|
|
+ var req fms.InvoicePaymentCensusListReq
|
|
|
+ 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
|
|
|
+ }
|
|
|
+
|
|
|
+ cond := `1 = 1`
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ // 客户姓名/销售
|
|
|
+ if req.Keyword != "" {
|
|
|
+ kw := "%" + req.Keyword + "%"
|
|
|
+ cond += ` AND b.company_name LIKE ? OR c.seller_name LIKE ?`
|
|
|
+ pars = append(pars, kw, kw)
|
|
|
+ }
|
|
|
+ if req.SellGroupId > 0 {
|
|
|
+ cond += ` AND a.seller_group_id = ?`
|
|
|
+ pars = append(pars, req.SellGroupId)
|
|
|
+ }
|
|
|
+ // 套餐筛选
|
|
|
+ if req.ServiceType != 0 {
|
|
|
+ tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(tempRegisterIds) > 0 {
|
|
|
+ cond += ` AND a.register_id IN ?`
|
|
|
+ pars = append(pars, tempRegisterIds)
|
|
|
+ } else {
|
|
|
+ cond += ` AND 1 = 2`
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 开票到款日期
|
|
|
+ if req.TimeType > 0 && req.StartDate != "" && req.EndDate != "" {
|
|
|
+ st := fmt.Sprint(req.StartDate, " 00:00:00")
|
|
|
+ ed := fmt.Sprint(req.EndDate, " 23:59:59")
|
|
|
+ if req.TimeType == 1 {
|
|
|
+ cond += ` AND (c.invoice_time BETWEEN ? AND ?)`
|
|
|
+ }
|
|
|
+ if req.TimeType == 2 {
|
|
|
+ cond += ` AND (d.invoice_time BETWEEN ? AND ?)`
|
|
|
+ }
|
|
|
+ pars = append(pars, req.TimeType, st, ed)
|
|
|
+ }
|
|
|
+ // 已开票
|
|
|
+ if req.HasInvoice == 1 {
|
|
|
+ cond += ` AND a.invoice_id > 0`
|
|
|
+ }
|
|
|
+ // 已到款
|
|
|
+ if req.HasPayment == 1 {
|
|
|
+ cond += ` AND a.payment_id > 0`
|
|
|
+ }
|
|
|
+
|
|
|
+ page := new(base.Page)
|
|
|
+ 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)
|
|
|
+ }
|
|
|
+
|
|
|
+ registerList, registerIds, e := fms.GetInvoicePaymentCensusPageList(page, cond, pars)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ total := int64(len(registerIds))
|
|
|
+ queryRegisterIds := make([]int, 0)
|
|
|
+ for i := range registerList {
|
|
|
+ queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
|
|
|
+ }
|
|
|
+
|
|
|
+ results := new(fms.InvoicePaymentCensusResp)
|
|
|
+ if len(queryRegisterIds) > 0 {
|
|
|
+ // 获取汇总数据
|
|
|
+ summaryCond := `a.register_id IN ?`
|
|
|
+ summaryPars := make([]interface{}, 0)
|
|
|
+ summaryPars = append(summaryPars, queryRegisterIds)
|
|
|
+ summaryList, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
|
|
|
+ if e != nil {
|
|
|
+ resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ summaryIds := make([]int, 0)
|
|
|
+ paymentIds := make([]int, 0)
|
|
|
+ for i := range summaryList {
|
|
|
+ summaryIds = append(summaryIds, summaryList[i].SummaryId)
|
|
|
+ if summaryList[i].PaymentId > 0 {
|
|
|
+ paymentIds = append(paymentIds, summaryList[i].PaymentId)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var listErr, totalErr, totalGroupErr error
|
|
|
+ wg := sync.WaitGroup{}
|
|
|
+
|
|
|
+ // 响应列表
|
|
|
+ respList := make([]*fms.InvoicePaymentCensusItem, 0)
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+
|
|
|
+ // 合同套餐
|
|
|
+ contractServiceCond := `contract_register_id IN ?`
|
|
|
+ contractServicePars := make([]interface{}, 0)
|
|
|
+ contractServicePars = append(contractServicePars, queryRegisterIds)
|
|
|
+ contractServiceOB := new(fms.ContractService)
|
|
|
+ contractServiceList, e := contractServiceOB.List(contractServiceCond, contractServicePars)
|
|
|
+ if e != nil {
|
|
|
+ listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ contractServiceMap := make(map[int][]*fms.ContractService, 0)
|
|
|
+ servicesNameMap := make(map[int][]string, 0)
|
|
|
+ for i := range contractServiceList {
|
|
|
+ if contractServiceMap[contractServiceList[i].ContractRegisterId] == nil {
|
|
|
+ contractServiceMap[contractServiceList[i].ContractRegisterId] = make([]*fms.ContractService, 0)
|
|
|
+ }
|
|
|
+ contractServiceMap[contractServiceList[i].ContractRegisterId] = append(contractServiceMap[contractServiceList[i].ContractRegisterId], contractServiceList[i])
|
|
|
+ servicesNameMap[contractServiceList[i].ContractRegisterId] = append(servicesNameMap[contractServiceList[i].ContractRegisterId], contractServiceList[i].Title)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 到款套餐分配
|
|
|
+ serviceAmountMap := make(map[int][]*fms.ContractPaymentServiceAmount, 0)
|
|
|
+ if len(paymentIds) > 0 {
|
|
|
+ serviceAmountCond := `contract_payment_id IN ?`
|
|
|
+ serviceAmountPars := make([]interface{}, 0)
|
|
|
+ serviceAmountPars = append(serviceAmountPars, paymentIds)
|
|
|
+ serviceAmountOB := new(fms.ContractPaymentServiceAmount)
|
|
|
+ serviceAmountList, e := serviceAmountOB.List(serviceAmountCond, serviceAmountPars)
|
|
|
+ if e != nil {
|
|
|
+ listErr = fmt.Errorf("获取到款套餐分配列表失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for i := range serviceAmountList {
|
|
|
+ if serviceAmountMap[serviceAmountList[i].ContractPaymentId] == nil {
|
|
|
+ serviceAmountMap[serviceAmountList[i].ContractPaymentId] = make([]*fms.ContractPaymentServiceAmount, 0)
|
|
|
+ }
|
|
|
+ serviceAmountMap[serviceAmountList[i].ContractPaymentId] = append(serviceAmountMap[serviceAmountList[i].ContractPaymentId], serviceAmountList[i])
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重组汇总数据
|
|
|
+ summaryMap := make(map[int][]*fms.InvoicePaymentCensusInfo)
|
|
|
+ amountMap := make(map[string]*fms.ContractPaymentServiceAmount)
|
|
|
+ for i := range summaryList {
|
|
|
+ if summaryMap[summaryList[i].RegisterId] == nil {
|
|
|
+ summaryMap[summaryList[i].RegisterId] = make([]*fms.InvoicePaymentCensusInfo, 0)
|
|
|
+ }
|
|
|
+ v := new(fms.InvoicePaymentCensusInfo)
|
|
|
+ v.InvoiceId = summaryList[i].InvoiceId
|
|
|
+ v.InvoiceDate = utils.TimeTransferString(utils.FormatDate, summaryList[i].InvoiceDate)
|
|
|
+ v.InvoiceAmount = summaryList[i].InvoiceAmount
|
|
|
+ v.SellerId = summaryList[i].SellerId
|
|
|
+ v.SellerName = summaryList[i].SellerName
|
|
|
+ v.SellerGroupId = summaryList[i].SellerGroupId
|
|
|
+ v.SellerGroupName = summaryList[i].SellerGroupName
|
|
|
+ v.PaymentId = summaryList[i].PaymentId
|
|
|
+ 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]
|
|
|
+ if amountList != nil {
|
|
|
+ for i := range amountList {
|
|
|
+ k := fmt.Sprintf("%d-%d", amountList[i].ContractPaymentId, amountList[i].ServiceTemplateId)
|
|
|
+ amountMap[k] = amountList[i]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 合同对应的所有套餐
|
|
|
+ svList := contractServiceMap[summaryList[i].RegisterId]
|
|
|
+ if svList != nil {
|
|
|
+ for ii := range svList {
|
|
|
+ vv := new(fms.ContractPaymentServiceAmountItem)
|
|
|
+ vv.ServiceTemplateId = svList[ii].ServiceTemplateId
|
|
|
+ vv.ServiceTemplateName = svList[ii].Title
|
|
|
+ k2 := fmt.Sprintf("%d-%d", summaryList[i].PaymentId, svList[ii].ServiceTemplateId)
|
|
|
+ a := amountMap[k2]
|
|
|
+ if a != nil {
|
|
|
+ vv.ContractPaymentServiceAmountId = a.ContractPaymentServiceAmountId
|
|
|
+ vv.ContractPaymentId = a.ContractPaymentId
|
|
|
+ vv.Amount = a.Amount
|
|
|
+ }
|
|
|
+ svaList = append(svaList, vv)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ v.ServiceAmountList = svaList
|
|
|
+
|
|
|
+ summaryMap[summaryList[i].RegisterId] = append(summaryMap[summaryList[i].RegisterId], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 响应列表
|
|
|
+ for i := range registerList {
|
|
|
+ v := new(fms.InvoicePaymentCensusItem)
|
|
|
+ v.ContractRegisterId = registerList[i].ContractRegisterId
|
|
|
+ v.CompanyName = registerList[i].CompanyName
|
|
|
+ v.NewCompany = registerList[i].NewCompany
|
|
|
+ v.StartDate = utils.TimeTransferString(utils.FormatDate, registerList[i].StartDate)
|
|
|
+ v.EndDate = utils.TimeTransferString(utils.FormatDate, registerList[i].EndDate)
|
|
|
+ svList := servicesNameMap[registerList[i].ContractRegisterId]
|
|
|
+ v.ServicesName = strings.Join(svList, ",")
|
|
|
+ v.InvoicePaymentList = summaryMap[registerList[i].ContractRegisterId]
|
|
|
+ respList = append(respList, v)
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 开票到款金额合计(换算后)
|
|
|
+ var invoiceTotal, paymentTotal float64
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+
|
|
|
+ if len(summaryIds) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ amountTotalCond := `a.id IN ?`
|
|
|
+ amountTotalPars := make([]interface{}, 0)
|
|
|
+ amountTotalPars = append(amountTotalPars, summaryIds)
|
|
|
+ invoiceSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 1)
|
|
|
+ if e != nil {
|
|
|
+ totalErr = fmt.Errorf("获取汇总开票金额合计失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ invoiceTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSum), 64)
|
|
|
+ paymentSum, e := fms.GetContractSummaryInvoicePaymentAmountTotal(amountTotalCond, amountTotalPars, 2)
|
|
|
+ if e != nil {
|
|
|
+ totalErr = fmt.Errorf("获取汇总到款金额合计失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ paymentTotal, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSum), 64)
|
|
|
+ }()
|
|
|
+
|
|
|
+ // 分币种金额统计
|
|
|
+ invoiceCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
|
|
|
+ paymentCurrencyTotals := make([]*fms.InvoiceListCurrencyTotal, 0)
|
|
|
+ wg.Add(1)
|
|
|
+ go func() {
|
|
|
+ defer wg.Done()
|
|
|
+
|
|
|
+ currencyOB := new(fms.CurrencyUnit)
|
|
|
+ currencyCond := `enable = 1`
|
|
|
+ currencyPars := make([]interface{}, 0)
|
|
|
+ currencyList, e := currencyOB.List(currencyCond, currencyPars)
|
|
|
+ if e != nil {
|
|
|
+ totalGroupErr = fmt.Errorf("获取货币列表失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ unitMap := make(map[string]string)
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(summaryIds) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ totalGroupCond := `a.id IN ?`
|
|
|
+ totalGroupPars := make([]interface{}, 0)
|
|
|
+ totalGroupPars = append(totalGroupPars, summaryIds)
|
|
|
+ invoiceSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 1)
|
|
|
+ if e != nil {
|
|
|
+ totalGroupErr = fmt.Errorf("获取汇总货币合计开票金额失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ paymentSumGroup, e := fms.GetSummaryListCurrencySum(totalGroupCond, totalGroupPars, 2)
|
|
|
+ if e != nil {
|
|
|
+ totalGroupErr = fmt.Errorf("获取汇总货币合计到款金额失败, Err: %s", e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ invoiceSumMap := make(map[string]float64)
|
|
|
+ paymentSumMap := make(map[string]float64)
|
|
|
+ for i := range invoiceSumGroup {
|
|
|
+ invoiceSumMap[invoiceSumGroup[i].CurrencyUnit] = invoiceSumGroup[i].OriginAmountTotal
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for i := range paymentSumGroup {
|
|
|
+ paymentSumMap[paymentSumGroup[i].CurrencyUnit] = paymentSumGroup[i].OriginAmountTotal
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ for i := range invoiceCurrencyTotals {
|
|
|
+ a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", invoiceSumMap[invoiceCurrencyTotals[i].Code]), 64)
|
|
|
+ invoiceCurrencyTotals[i].Amount = a
|
|
|
+ }
|
|
|
+ for i := range paymentCurrencyTotals {
|
|
|
+ a, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", paymentSumMap[paymentCurrencyTotals[i].Code]), 64)
|
|
|
+ paymentCurrencyTotals[i].Amount = a
|
|
|
+ }
|
|
|
+ }()
|
|
|
+
|
|
|
+ wg.Wait()
|
|
|
+
|
|
|
+ if listErr != nil {
|
|
|
+ resp.FailMsg("获取失败", listErr.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if totalErr != nil {
|
|
|
+ resp.FailMsg("获取失败", totalErr.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if totalGroupErr != nil {
|
|
|
+ resp.FailMsg("获取失败", totalGroupErr.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ results.DataList = respList
|
|
|
+ results.InvoiceTotal = invoiceTotal
|
|
|
+ results.PaymentTotal = paymentTotal
|
|
|
+ results.InvoiceCurrencyTotal = invoiceCurrencyTotals
|
|
|
+ results.PaymentCurrencyTotal = paymentCurrencyTotals
|
|
|
+ }
|
|
|
+
|
|
|
+ // 是否导出
|
|
|
+ if req.IsExport == 1 {
|
|
|
+ ExportInvoicePaymentCensusList(c, results)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ page.SetTotal(total)
|
|
|
+ baseData := new(base.BaseData)
|
|
|
+ baseData.SetPage(page)
|
|
|
+ baseData.SetList(results)
|
|
|
+ resp.OkData("获取成功", baseData, c)
|
|
|
+}
|