invoice_payment_summary.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package fms
  2. import (
  3. "fmt"
  4. "hongze/fms_api/global"
  5. "hongze/fms_api/models/base"
  6. "strings"
  7. "time"
  8. )
  9. // InvoicePaymentSummary 开票到款汇总表
  10. type InvoicePaymentSummary struct {
  11. Id int `gorm:"primaryKey;column:id" json:"id" description:"汇总ID"`
  12. RegisterId int `gorm:"column:register_id" json:"register_id" description:"登记ID"`
  13. InvoiceId int `gorm:"column:invoice_id" json:"invoice_id" description:"开票ID"`
  14. PaymentId int `gorm:"column:payment_id" json:"payment_id" description:"到款ID"`
  15. base.TimeBase
  16. }
  17. func (c *InvoicePaymentSummary) TableName() string {
  18. return "invoice_payment_summary"
  19. }
  20. // DeleteAndCreate 删除并新增汇总
  21. func (c *InvoicePaymentSummary) DeleteAndCreate(registerId int, summaryList []*InvoicePaymentSummary) (err error) {
  22. tx := global.DEFAULT_MYSQL.Begin()
  23. defer func() {
  24. if err != nil {
  25. tx.Rollback()
  26. } else {
  27. tx.Commit()
  28. }
  29. }()
  30. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  31. tx.Exec(sql, registerId)
  32. if len(summaryList) > 0 {
  33. err = tx.CreateInBatches(summaryList, len(summaryList)).Error
  34. if err != nil {
  35. return
  36. }
  37. }
  38. return
  39. }
  40. // GetInvoicePaymentCensusPageList 获取商品到款统计列表-总数
  41. func GetInvoicePaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*ContractRegister, registerIds []int, err error) {
  42. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  43. Select("b.*").
  44. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  45. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  46. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  47. Where(condition, pars...).
  48. Group("a.register_id")
  49. if len(page.GetOrderItemsString()) > 0 {
  50. query = query.Order(page.GetOrderItemsString())
  51. }
  52. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  53. if err != nil {
  54. return
  55. }
  56. queryCount := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  57. Select("a.register_id").
  58. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  59. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  60. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  61. Where(condition, pars...).
  62. Group("a.register_id")
  63. queryCount.Find(&registerIds)
  64. //summaryQuery := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  65. // Select("a.id").
  66. // Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  67. // Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  68. // Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  69. // Where(condition, pars...)
  70. //summaryQuery.Find(&summaryIds)
  71. //err = summaryQuery.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&summaryPageIds).Error
  72. //Group("a.register_id")
  73. return
  74. }
  75. type InvoicePaymentSummaryItem struct {
  76. SummaryId int `json:"summary_id" description:"汇总ID"`
  77. RegisterId int `json:"register_id" description:"登记ID"`
  78. CompanyName string `json:"company_name" description:"客户名称"`
  79. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  80. StartDate time.Time `json:"start_date" description:"合同开始日期"`
  81. EndDate time.Time `json:"end_date" description:"合同结束日期"`
  82. InvoiceId int `json:"invoice_id" description:"开票ID"`
  83. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  84. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  85. SellerId int `json:"seller_id" description:"销售ID"`
  86. SellerName string `json:"seller_name" description:"销售名称"`
  87. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  88. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  89. PaymentId int `json:"payment_id" description:"到款ID"`
  90. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  91. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  92. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  93. }
  94. // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  95. func GetInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*InvoicePaymentSummaryItem, err error) {
  96. fields := []string{"a.id AS summary_id", "a.register_id", "a.invoice_id", "a.payment_id", "b.company_name", "b.start_date", "b.end_date",
  97. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "c.seller_id", "c.seller_name", "c.seller_group_id",
  98. "c.seller_group_name", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date", "d.pay_type",
  99. }
  100. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  101. Select(strings.Join(fields, ",")).
  102. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  103. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  104. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  105. Where(condition, pars...)
  106. query.Find(&results)
  107. return
  108. }
  109. // GetInvoicePaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  110. func GetInvoicePaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  111. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  112. Select("a.id AS summary_id").
  113. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  114. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  115. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  116. Where(condition, pars...)
  117. query.Find(&summaryIds)
  118. return
  119. }
  120. // GetContractSummaryInvoicePaymentAmountTotal 获取汇总金额合计信息
  121. func GetContractSummaryInvoicePaymentAmountTotal(condition string, pars []interface{}, amountType int) (amountTotal float64, err error) {
  122. joinCond := `a.invoice_id = b.contract_invoice_id`
  123. if amountType == 2 {
  124. joinCond = `a.payment_id = b.contract_invoice_id`
  125. }
  126. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  127. Select("SUM(b.amount)").
  128. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  129. Where(condition, pars...)
  130. err = query.Find(&amountTotal).Error
  131. return
  132. }
  133. // GetSummaryListCurrencySum 获取汇总分货币合计
  134. func GetSummaryListCurrencySum(condition string, pars []interface{}, amountType int) (results []*InvoiceListCurrencySum, err error) {
  135. joinCond := `a.invoice_id = b.contract_invoice_id`
  136. if amountType == 2 {
  137. joinCond = `a.payment_id = b.contract_invoice_id`
  138. }
  139. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  140. Select("b.currency_unit, b.invoice_type, SUM(b.amount) AS amount_total, SUM(b.origin_amount) AS origin_amount_total").
  141. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  142. Where(condition, pars...).
  143. Group("b.currency_unit")
  144. err = query.Find(&results).Error
  145. return
  146. }