ziwen 1 年之前
父节点
当前提交
cca10ee122
共有 1 个文件被更改,包括 424 次插入0 次删除
  1. 424 0
      controller/census/invoice_payment.go

+ 424 - 0
controller/census/invoice_payment.go

@@ -10,8 +10,10 @@ import (
 	"hongze/fms_api/global"
 	"hongze/fms_api/models"
 	"hongze/fms_api/models/base"
+	"hongze/fms_api/models/crm"
 	"hongze/fms_api/models/fms"
 	"hongze/fms_api/services/alarm_msg"
+	crmService "hongze/fms_api/services/crm"
 	fmsService "hongze/fms_api/services/fms"
 	"hongze/fms_api/utils"
 	"net/http"
@@ -2846,4 +2848,426 @@ func ExportNotPaymentCensusList(c *gin.Context, results *fms.NotInvoicePaymentCe
 	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)
+}
+
+func (ct *InvoicePaymentController) NotInvoiceList2(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 b.contract_code LIKE ?)`
+		pars = append(pars, kw, 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 = "asc"
+	}
+	if req.SortType != "desc" && req.SortType != "asc" {
+		resp.Fail("排序类型不正确", c)
+		return
+	}
+	if req.SortParam == "" {
+		page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
+	} else if req.SortParam == "start_date" {
+		if req.SortType == "asc" {
+			page.AddOrderItem(base.OrderItem{Column: "e.start_date", Asc: true})
+		} else {
+			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: "e.end_date", Asc: true})
+		} else {
+			page.AddOrderItem(base.OrderItem{Column: "e.end_date", Asc: false})
+		}
+	} else {
+		resp.Fail("排序字段不正确", c)
+		return
+	}
+
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
+
+	cond += ` AND b.payment_amount > b.invoiced_amount AND b.contract_amount <> b.invoiced_amount `
+	registerList, total, e := fms.GetInvoiceCensusPageList(page, cond, pars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取商品到款统计列表总数失败, Err: "+e.Error(), c)
+		return
+	}
+	queryRegisterIds := make([]int, 0)
+	for _, v := range registerList {
+		queryRegisterIds = append(queryRegisterIds, v.ContractRegisterId)
+	}
+	//fmt.Println("queryRegisterIds:",queryRegisterIds)
+	results := new(fms.NotInvoicePaymentCensusResp)
+	if len(queryRegisterIds) > 0 {
+		// 获取合同的套餐金额信息
+		serviceAmountList, e := fms.GetContractServiceAmountByContractRegisterIds(queryRegisterIds)
+		if e != nil {
+			resp.FailData("获取合同的套餐金额信息失败", "Err:"+e.Error(), c)
+			return
+		}
+		serviceAmountMap := make(map[int]map[int]*fms.ContractServiceAmount, 0)
+		for _, v := range serviceAmountList {
+			if _, ok := serviceAmountMap[v.ContractRegisterId]; !ok {
+				serviceAmountMap[v.ContractRegisterId] = make(map[int]*fms.ContractServiceAmount)
+			}
+			serviceAmountMap[v.ContractRegisterId][v.ProductId] = v
+		}
+
+		// 获取汇总数据IDs, 用于查询合计数据
+		summaryIdsCond := cond
+		summaryIdsPars := pars
+		if req.SellerIds != "" {
+			sellerIds := strings.Split(req.SellerIds, ",")
+			summaryIdsCond += ` AND c.seller_id in ? AND a.payment_id <> 0 AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+			summaryIdsPars = append(summaryIdsPars, sellerIds)
+		} else {
+			summaryIdsCond += ` AND a.payment_id <> 0  AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+			summaryIdsPars = append(summaryIdsPars)
+		}
+		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.NotPaymentCensusItem, 0)
+		summaryList := make([]*fms.NotInvoicePaymentSummaryItem, 0)
+		wg.Add(1)
+		go func() {
+			defer wg.Done()
+
+			// 获取汇总数据
+			summaryCond := cond
+			summaryCond += ` AND a.register_id IN ?`
+			summaryPars := pars
+			summaryPars = append(summaryPars, queryRegisterIds)
+			//if req.SellerIds != "" {
+			//	sellerIds := strings.Split(req.SellerIds, ",")
+			//	summaryCond += ` AND c.seller_id in ? AND a.payment_id <> 0 AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+			//	summaryPars = append(summaryPars, sellerIds, sellerIds)
+			//} else {
+			//	summaryCond += ` AND a.payment_id <> 0 AND (d.origin_amount > c.origin_amount OR c.origin_amount IS NULL) `
+			//	summaryPars = append(summaryPars)
+			//}
+
+			summaryData, e := fms.GetNotInvoicePaymentCensusSummaryData(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)
+			//}
+			NotInvoiceSummaryMap := make(map[int][]*fms.NotInvoicePaymentSummaryItem, 0)
+			for _, v := range summaryList {
+				NotInvoiceSummaryMap[v.RegisterId] = append(NotInvoiceSummaryMap[v.RegisterId], v)
+			}
+
+			// 重组汇总数据
+			summaryMap := make(map[int][]*fms.NotPaymentCensusInfo)
+			sellerList, e := crmService.GetSellerDepartmentListWithGroupAndTeam()
+			if e != nil {
+				resp.FailData("获取销售失败", "Err:"+e.Error(), c)
+				return
+			}
+			sellerMap := make(map[int]*crm.SellerAdminWithGroupTeam)
+			for i := range sellerList {
+				sellerMap[sellerList[i].SellerId] = sellerList[i]
+			}
+
+
+
+			// 货币列表
+			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 registerId, list := range NotInvoiceSummaryMap {
+				for _, v := range list {
+					v.NotInvoicedAmountTotal = serviceAmountMap[registerId][v.ServiceProductId].ServiceAmount - v.InvoiceAmountTotal
+					if v.ServiceProductId == 1 {
+						v.SellerGroupId = sellerMap[v.SellerId].GroupId
+						v.SellerGroupName = sellerMap[v.SellerId].GroupName
+					} else {
+						v.RaiSellerGroupId = sellerMap[v.RaiSellerId].GroupId
+						v.RaiSellerGroupName = sellerMap[v.RaiSellerId].GroupName
+					}
+				}
+			}
+
+			// 响应列表
+			for i := range registerList {
+				v := new(fms.NotPaymentCensusItem)
+				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.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
+				v.NotInvoiceList = NotInvoiceSummaryMap[summaryList[i].RegisterId]
+				respList = append(respList, v)
+			}
+		}()
+
+		// 开票到款金额合计(换算后)
+		var invoiceTotal, paymentTotal, contractAmount 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)
+		contractAmountCurrencyTotals := 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,
+				})
+				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 {
+				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)
+			contractSumMap := 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
+			}
+
+			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()
+
+		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.NotInvoiceTotal = contractAmount - invoiceTotal
+		results.InvoiceCurrencyTotal = invoiceCurrencyTotals
+		results.PaymentCurrencyTotal = paymentCurrencyTotals
+		results.NotInvoiceCurrencyTotal = contractAmountCurrencyTotals
+	}
+
+	// 是否导出
+	if req.IsExport == 1 {
+		ExportNotInvoiceCensusList(c, results)
+		return
+	}
+	page.SetTotal(total)
+	baseData := new(base.BaseData)
+	baseData.SetPage(page)
+	baseData.SetList(results)
+	resp.OkData("获取成功", baseData, c)
 }