invoice_payment_summary.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. return
  65. }
  66. type InvoicePaymentSummaryItem struct {
  67. SummaryId int `json:"summary_id" description:"汇总ID"`
  68. RegisterId int `json:"register_id" description:"登记ID"`
  69. CompanyName string `json:"company_name" description:"客户名称"`
  70. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  71. StartDate time.Time `json:"start_date" description:"合同开始日期"`
  72. EndDate time.Time `json:"end_date" description:"合同结束日期"`
  73. InvoiceId int `json:"invoice_id" description:"开票ID"`
  74. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  75. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  76. SellerId int `json:"seller_id" description:"销售ID"`
  77. SellerName string `json:"seller_name" description:"销售名称"`
  78. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  79. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  80. PaymentId int `json:"payment_id" description:"到款ID"`
  81. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  82. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  83. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  84. }
  85. // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  86. func GetInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*InvoicePaymentSummaryItem, err error) {
  87. 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",
  88. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "c.seller_id", "c.seller_name", "c.seller_group_id",
  89. "c.seller_group_name", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date", "d.pay_type",
  90. }
  91. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  92. Select(strings.Join(fields, ",")).
  93. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  94. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  95. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  96. Where(condition, pars...)
  97. query.Find(&results)
  98. return
  99. }
  100. // GetInvoicePaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  101. func GetInvoicePaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  102. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  103. Select("a.id AS summary_id").
  104. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  105. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  106. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  107. Where(condition, pars...)
  108. query.Find(&summaryIds)
  109. return
  110. }
  111. // GetContractSummaryInvoicePaymentAmountTotal 获取汇总金额合计信息
  112. func GetContractSummaryInvoicePaymentAmountTotal(condition string, pars []interface{}, amountType int) (amountTotal float64, err error) {
  113. joinCond := `a.invoice_id = b.contract_invoice_id`
  114. if amountType == 2 {
  115. joinCond = `a.payment_id = b.contract_invoice_id`
  116. }
  117. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  118. Select("IFNULL(SUM(b.amount),0)").
  119. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  120. Where(condition, pars...)
  121. err = query.Find(&amountTotal).Error
  122. return
  123. }
  124. // GetSummaryListCurrencySum 获取汇总分货币合计
  125. func GetSummaryListCurrencySum(condition string, pars []interface{}, amountType int) (results []*InvoiceListCurrencySum, err error) {
  126. joinCond := `a.invoice_id = b.contract_invoice_id`
  127. if amountType == 2 {
  128. joinCond = `a.payment_id = b.contract_invoice_id`
  129. }
  130. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  131. Select("b.currency_unit, b.invoice_type, SUM(b.amount) AS amount_total, SUM(b.origin_amount) AS origin_amount_total").
  132. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  133. Where(condition, pars...).
  134. Group("b.currency_unit")
  135. err = query.Find(&results).Error
  136. return
  137. }
  138. // DeleteInvoicePaymentSummaryByRegisterId 根据合同登记ID删除汇总数据
  139. func DeleteInvoicePaymentSummaryByRegisterId(registerId int) (err error) {
  140. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  141. err = global.DEFAULT_MYSQL.Exec(sql, registerId).Error
  142. return
  143. }