ziwen 1 年之前
父节点
当前提交
0338fa8411
共有 3 个文件被更改,包括 906 次插入2 次删除
  1. 848 0
      controller/census/invoice_payment.go
  2. 56 2
      models/fms/invoice_payment_summary.go
  3. 2 0
      routers/census.go

+ 848 - 0
controller/census/invoice_payment.go

@@ -944,3 +944,851 @@ func (ct *InvoicePaymentController) List(c *gin.Context) {
 }
 
 
+// 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	string		false	"是否已开票: 0-否; 1-是; 空-全部"
+// @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   SortType        query   string  true       "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
+// @Success 200 {object} fms.ContractRegisterItem
+// @router /census/invoice_payment/not_invoice/list [get]
+func (ct *InvoicePaymentController) NotInvoiceList(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 ?`
+		pars = append(pars, kw)
+	}
+	if req.SellerIds != "" {
+		sellerIds := strings.Split(req.SellerIds, ",")
+		cond += ` AND (c.seller_id in ?)`
+		pars = append(pars, sellerIds)
+	}
+	// 套餐筛选
+	if req.ServiceTypes != "" {
+		serviceTypes := strings.Split(req.ServiceTypes, ",")
+		tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
+		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.ListParam == 1{
+		cond += ` AND a.service_product_id = 1 `
+	}else if req.ListParam == 2 {
+		cond += ` AND a.service_product_id = 2 `
+	}
+
+	// 开票到款日期
+	//if req.TimeType != 0 {
+	//	if req.TimeType == 1 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed)
+	//	}else if req.TimeType == 2 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed)
+	//	}else if req.TimeType == 3 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed, st, ed)
+	//	}
+	//} else if req.StartDate != "" && req.EndDate != "" {
+	//	st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//	ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//	cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
+	//	pars = append(pars, st, ed, st, ed)
+	//}
+
+
+	//if req.HasInvoice == "1" {
+	//	cond += ` AND a.invoice_id > 0 `
+	//}else if req.HasInvoice == "0" {
+	//	cond += ` AND a.invoice_id = 0 `
+	//}
+	//
+	//if req.HasPayment == "1" {
+	//	cond += ` AND a.payment_id > 0 `
+	//}else if req.HasPayment == "0" {
+	//	cond += ` AND a.payment_id = 0 `
+	//}
+
+
+
+	page := new(base.Page)
+	page.SetPageSize(req.PageSize)
+	page.SetCurrent(req.Current)
+	//排序
+	if req.SortType == "" {
+		req.SortType = "desc"
+	}
+	if req.SortType != "desc" && req.SortType != "asc" {
+		resp.Fail("排序类型不正确", c)
+		return
+	}
+	if req.SortParam == "" {
+		page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
+	}else if req.SortParam == "start_date" {
+		if req.SortType == "asc" {
+			page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: true})
+		}else{
+			page.AddOrderItem(base.OrderItem{Column: "b.start_date", Asc: false})
+		}
+	}else if req.SortParam == "end_date" {
+		if req.SortType == "asc" {
+			page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: true})
+		}else{
+			page.AddOrderItem(base.OrderItem{Column: "b.end_date", Asc: false})
+		}
+	}else{
+		resp.Fail("排序字段不正确", c)
+		return
+	}
+
+
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
+
+	registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
+		return
+	}
+	queryRegisterIds := make([]int, 0)
+	for i := range registerList {
+		queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
+	}
+
+	//fmt.Println("queryRegisterIds:",queryRegisterIds)
+	results := new(fms.InvoicePaymentCensusResp)
+	if len(queryRegisterIds) > 0 {
+		// 获取汇总数据IDs, 用于查询合计数据
+		summaryIdsCond := cond
+		summaryIdsPars := pars
+		summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
+		if e != nil {
+			resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
+			return
+		}
+
+		//fmt.Println("summaryIds:",summaryIds)
+		var listErr, totalErr, totalGroupErr error
+		wg := sync.WaitGroup{}
+
+		// 响应列表
+		respList := make([]*fms.InvoicePaymentCensusItem, 0)
+		summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+
+			// 获取汇总数据
+			summaryCond := cond
+			summaryCond += ` AND a.register_id IN ?`
+			summaryPars := pars
+			summaryPars = append(summaryPars, queryRegisterIds)
+			summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
+			if e != nil {
+				resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
+				return
+			}
+			summaryList = summaryData
+			//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)
+				}
+			}
+
+			// 合同套餐
+			/*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)
+			}
+			*/
+			servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
+			if e != nil {
+				listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
+				return
+			}
+			// 到款套餐分配
+			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 {
+				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.SellerType = summaryList[i].ServiceProductId
+				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 := serviceFormatMap[summaryList[i].RegisterId]
+				if svList != nil {
+					for ii := range svList {
+						vv := new(fms.ContractPaymentServiceAmountItem)
+						vv.ServiceTemplateId = svList[ii].ServiceTemplateId
+						vv.ServiceTemplateName = svList[ii].FormatTitle
+						vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
+						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].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
+			}
+
+			// 响应列表
+			for i := range registerList {
+				v := new(fms.InvoicePaymentCensusItem)
+				v.SummaryId = registerList[i].SummaryId
+				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)
+				v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
+				v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
+				v.ContractType = registerList[i].ContractType
+				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)
+}
+
+// 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	string		false	"是否已开票: 0-否; 1-是; 空-全部"
+// @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   SortType        query   string  true       "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`"
+// @Success 200 {object} fms.ContractRegisterItem
+// @router /census/invoice_payment/not_payment/list [get]
+func (ct *InvoicePaymentController) NotPaymentList(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 ?`
+		pars = append(pars, kw)
+	}
+	if req.SellerIds != "" {
+		sellerIds := strings.Split(req.SellerIds, ",")
+		cond += ` AND (c.seller_id in ?)`
+		pars = append(pars, sellerIds)
+	}
+	// 套餐筛选
+	if req.ServiceTypes != "" {
+		serviceTypes := strings.Split(req.ServiceTypes, ",")
+		tempRegisterIds, e := fms.GetContractRegisterIdsByTempId(serviceTypes)
+		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.ListParam == 1{
+		cond += ` AND a.service_product_id = 1 `
+	}else if req.ListParam == 2 {
+		cond += ` AND a.service_product_id = 2 `
+	}
+
+	// 开票到款日期
+	//if req.TimeType != 0 {
+	//	if req.TimeType == 1 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed)
+	//	}else if req.TimeType == 2 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (d.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed)
+	//	}else if req.TimeType == 3 &&  req.StartDate != "" && req.EndDate != "" {
+	//		st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//		ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//		cond += ` AND (c.invoice_time BETWEEN ? AND ?) AND (d.invoice_time BETWEEN ? AND ?) `
+	//		pars = append(pars, st, ed, st, ed)
+	//	}
+	//} else if req.StartDate != "" && req.EndDate != "" {
+	//	st := fmt.Sprint(req.StartDate, " 00:00:00")
+	//	ed := fmt.Sprint(req.EndDate, " 23:59:59")
+	//	cond += ` AND ((c.invoice_time BETWEEN ? AND ?) or (d.invoice_time BETWEEN ? AND ?))`
+	//	pars = append(pars, st, ed, st, ed)
+	//}
+
+
+	//if req.HasInvoice == "1" {
+	//	cond += ` AND a.invoice_id > 0 `
+	//}else if req.HasInvoice == "0" {
+	//	cond += ` AND a.invoice_id = 0 `
+	//}
+	//
+	//if req.HasPayment == "1" {
+	//	cond += ` AND a.payment_id > 0 `
+	//}else if req.HasPayment == "0" {
+	//	cond += ` AND a.payment_id = 0 `
+	//}
+
+
+
+	page := new(base.Page)
+	page.SetPageSize(req.PageSize)
+	page.SetCurrent(req.Current)
+	//排序
+	if req.SortType == "" {
+		req.SortType = "desc"
+	}
+	if req.SortType != "desc" && req.SortType != "asc" {
+		resp.Fail("排序类型不正确", c)
+		return
+	}
+	if req.SortType == "asc" {
+		page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: true})
+	}else{
+		page.AddOrderItem(base.OrderItem{Column: "c.invoice_time", Asc: false})
+	}
+
+
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
+
+	registerList, total, e := fms.GetNoPaymentCensusPageList(page, cond, pars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
+		return
+	}
+	queryRegisterIds := make([]int, 0)
+	for i := range registerList {
+		queryRegisterIds = append(queryRegisterIds, registerList[i].ContractRegisterId)
+	}
+
+	//fmt.Println("queryRegisterIds:",queryRegisterIds)
+	results := new(fms.InvoicePaymentCensusResp)
+	if len(queryRegisterIds) > 0 {
+		// 获取汇总数据IDs, 用于查询合计数据
+		summaryIdsCond := cond
+		summaryIdsPars := pars
+		summaryIds, e := fms.GetInvoicePaymentCensusSummaryDataIds(summaryIdsCond, summaryIdsPars)
+		if e != nil {
+			resp.FailMsg("获取失败", "获取商品到款汇总IDs失败, Err: "+e.Error(), c)
+			return
+		}
+
+		//fmt.Println("summaryIds:",summaryIds)
+		var listErr, totalErr, totalGroupErr error
+		wg := sync.WaitGroup{}
+
+		// 响应列表
+		respList := make([]*fms.InvoicePaymentCensusItem, 0)
+		summaryList := make([]*fms.InvoicePaymentSummaryItem, 0)
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+
+			// 获取汇总数据
+			summaryCond := cond
+			summaryCond += ` AND a.register_id IN ?`
+			summaryPars := pars
+			summaryPars = append(summaryPars, queryRegisterIds)
+			summaryData, e := fms.GetInvoicePaymentCensusSummaryData(summaryCond, summaryPars)
+			if e != nil {
+				resp.FailMsg("获取失败", "获取商品到款汇总列表失败, Err: "+e.Error(), c)
+				return
+			}
+			summaryList = summaryData
+			//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)
+				}
+			}
+
+			// 合同套餐
+			/*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)
+			}
+			*/
+			servicesNameMap, serviceFormatMap, e := fmsService.GetContractServiceNameFormat(queryRegisterIds)
+			if e != nil {
+				listErr = fmt.Errorf("获取合同套餐列表失败, Err: %s", e.Error())
+				return
+			}
+			// 到款套餐分配
+			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 {
+				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.SellerType = summaryList[i].ServiceProductId
+				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 := serviceFormatMap[summaryList[i].RegisterId]
+				if svList != nil {
+					for ii := range svList {
+						vv := new(fms.ContractPaymentServiceAmountItem)
+						vv.ServiceTemplateId = svList[ii].ServiceTemplateId
+						vv.ServiceTemplateName = svList[ii].FormatTitle
+						vv.ServiceTemplatePid = svList[ii].ServiceTemplatePid
+						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].SummaryId] = append(summaryMap[summaryList[i].SummaryId], v)
+			}
+
+			// 响应列表
+			for i := range registerList {
+				v := new(fms.InvoicePaymentCensusItem)
+				v.SummaryId = registerList[i].SummaryId
+				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)
+				v.ServicesName = servicesNameMap[registerList[i].ContractRegisterId]
+				v.InvoicePaymentList = summaryMap[registerList[i].SummaryId]
+				v.ContractType = registerList[i].ContractType
+				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)
+}

+ 56 - 2
models/fms/invoice_payment_summary.go

@@ -26,6 +26,7 @@ func (c *InvoicePaymentSummary) Create() (err error) {
 	err = global.DEFAULT_MYSQL.Create(c).Error
 	return
 }
+
 // DeleteAndCreate 删除并新增汇总
 func (c *InvoicePaymentSummary) DeleteAndCreate(registerId int, summaryList []*InvoicePaymentSummary) (err error) {
 	tx := global.DEFAULT_MYSQL.Begin()
@@ -49,9 +50,10 @@ func (c *InvoicePaymentSummary) DeleteAndCreate(registerId int, summaryList []*I
 }
 
 type ContractRegisterSummary struct {
-	SummaryId       int       `json:"summary_id" description:"汇总ID"`
+	SummaryId int `json:"summary_id" description:"汇总ID"`
 	ContractRegister
 }
+
 // GetInvoicePaymentCensusPageList 获取商品到款统计列表-总数
 func GetInvoicePaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*ContractRegisterSummary, count int64, err error) {
 	query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
@@ -161,4 +163,56 @@ func DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(invoiceId, arriveId, reg
 	sql := `DELETE FROM invoice_payment_summary WHERE invoice_id = ? AND payment_id = ? AND register_id = ?`
 	err = global.DEFAULT_MYSQL.Exec(sql, invoiceId, arriveId, registerId).Error
 	return
-}
+}
+
+type InvoiceSummary struct {
+	SummaryId           int     `json:"summary_id" description:"汇总ID"`
+	ContractAmountCount float64 `gorm:"column:contract_amount_count" json:"contract_amount_count" description:"合同金额"`
+	InvoicedAmountCount float64 `gorm:"column:invoiced_amount_count" json:"invoiced_amount_count" description:"开票金额"`
+	ContractRegister
+}
+
+// GetInvoiceCensusPageList 获取未开票统计列表-总数
+func GetInvoiceCensusPageList(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 ").
+		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 ")
+	err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
+	if err != nil {
+		return
+	}
+	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 ").
+		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 ")
+	err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
+	if err != nil {
+		return
+	}
+	return
+}

+ 2 - 0
routers/census.go

@@ -11,6 +11,8 @@ func InitCensus(rg *gin.RouterGroup) {
 	inv := new(census.InvoicePaymentController)
 	invGroup := rg.Group("invoice_payment/").Use(middleware.Token())
 	invGroup.GET("list", inv.List)
+	invGroup.GET("not_invoice/list", inv.NotInvoiceList)
+
 
 	// 销售
 	sel := new(census.SellerController)