invoice_payment_summary.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package fms
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "strings"
  5. "time"
  6. )
  7. // InvoicePaymentSummary 开票到款汇总表
  8. type InvoicePaymentSummary struct {
  9. Id int `gorm:"primaryKey;column:id" json:"id" description:"汇总ID"`
  10. RegisterId int `gorm:"column:register_id" json:"register_id" description:"登记ID"`
  11. InvoiceId int `gorm:"column:invoice_id" json:"invoice_id" description:"开票ID"`
  12. PaymentId int `gorm:"column:payment_id" json:"payment_id" description:"到款ID"`
  13. ServiceProductId int `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  14. CreateTime time.Time `gorm:"autoCreateTime;column:create_time" json:"create_time" description:"创建时间"`
  15. ModifyTime time.Time `gorm:"autoUpdateTime:milli;column:modify_time" json:"modify_time" description:"最后更新时间"`
  16. }
  17. type ContractRegisterSummary struct {
  18. SummaryId int `json:"summary_id" description:"汇总ID"`
  19. ContractRegister
  20. }
  21. // GetInvoicePaymentCensusPageList 获取商品到款统计列表-总数
  22. func GetInvoicePaymentCensusPageList(condition string, pars []interface{}, order string, startSize, pageSize int) (results []*ContractRegisterSummary, total int64, err error) {
  23. o := orm.NewOrmUsingDB("fms")
  24. sql := `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 FROM invoice_payment_summary AS a
  25. JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0
  26. LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0
  27. LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0
  28. WHERE `
  29. if condition != "" {
  30. sql += condition
  31. }
  32. totalSQl := `SELECT COUNT(1) total FROM (`+sql+`) as a`
  33. if err = o.Raw(totalSQl, pars).QueryRow(&total); err != nil {
  34. return
  35. }
  36. if order != "" {
  37. sql += order
  38. } else {
  39. sql += ` ORDER BY sort_invoice_id DESC`
  40. }
  41. sql += ` LIMIT ?,?`
  42. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&results)
  43. return
  44. }
  45. type InvoicePaymentSummaryItem struct {
  46. SummaryId int `json:"summary_id" description:"汇总ID"`
  47. RegisterId int `json:"register_id" description:"登记ID"`
  48. CompanyName string `json:"company_name" description:"客户名称"`
  49. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  50. StartDate time.Time `json:"start_date" description:"合同开始日期"`
  51. EndDate time.Time `json:"end_date" description:"合同结束日期"`
  52. InvoiceId int `json:"invoice_id" description:"开票ID"`
  53. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  54. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  55. SellerId int `json:"seller_id" description:"销售ID"`
  56. SellerName string `json:"seller_name" description:"销售名称"`
  57. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  58. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  59. PaymentId int `json:"payment_id" description:"到款ID"`
  60. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  61. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  62. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  63. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  64. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  65. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  66. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  67. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  68. }
  69. // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  70. func GetInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*InvoicePaymentSummaryItem, err error) {
  71. fields := []string{"a.id AS summary_id", "a.register_id", "a.invoice_id", "a.payment_id", "a.service_product_id", "b.company_name", "b.start_date", "b.end_date",
  72. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "IF(c.seller_id > 0, c.seller_id, d.seller_id) AS seller_id", "IF(c.seller_name = '' OR c.seller_name IS NULL, d.seller_name, c.seller_name) AS seller_name", "IF(c.seller_group_id > 0,c.seller_group_id,d.seller_group_id) AS seller_group_id",
  73. "IF(c.seller_group_name = '' OR c.seller_group_name IS NULL, d.seller_group_name, c.seller_group_name) AS seller_group_name", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date", "d.pay_type",
  74. }
  75. o := orm.NewOrmUsingDB("fms")
  76. sql := `SELECT `+strings.Join(fields, ",")+ ` FROM invoice_payment_summary AS a
  77. JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0
  78. LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0
  79. LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0
  80. WHERE `
  81. sql += condition
  82. _,err = o.Raw(sql, pars).QueryRows(&results)
  83. return
  84. }
  85. // GetInvoicePaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  86. func GetInvoicePaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  87. o := orm.NewOrmUsingDB("fms")
  88. sql := `SELECT a.id AS summary_id FROM invoice_payment_summary AS a
  89. JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0
  90. LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0
  91. LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0
  92. WHERE `
  93. sql += condition
  94. _,err = o.Raw(sql, pars).QueryRows(&summaryIds)
  95. return
  96. }
  97. // GetContractSummaryInvoicePaymentAmountTotal 获取汇总金额合计信息
  98. func GetContractSummaryInvoicePaymentAmountTotal(condition string, pars []interface{}, amountType int) (amountTotal float64, err error) {
  99. joinCond := `a.invoice_id = b.contract_invoice_id`
  100. if amountType == 2 {
  101. joinCond = `a.payment_id = b.contract_invoice_id`
  102. }
  103. o := orm.NewOrmUsingDB("fms")
  104. sql := `SELECT IFNULL(SUM(b.amount),0) FROM invoice_payment_summary AS a JOIN contract_invoice AS b ON `+ joinCond+` AND b.is_deleted = 0 WHERE `
  105. sql += condition
  106. err = o.Raw(sql, pars).QueryRow(&amountTotal)
  107. return
  108. }
  109. // GetSummaryListCurrencySum 获取汇总分货币合计
  110. func GetSummaryListCurrencySum(condition string, pars []interface{}, amountType int) (results []*InvoiceListCurrencySum, err error) {
  111. joinCond := `a.invoice_id = b.contract_invoice_id`
  112. if amountType == 2 {
  113. joinCond = `a.payment_id = b.contract_invoice_id`
  114. }
  115. o := orm.NewOrmUsingDB("fms")
  116. sql := `SELECT b.currency_unit, b.invoice_type, SUM(b.amount) AS amount_total, SUM(b.origin_amount) AS origin_amount_total
  117. FROM invoice_payment_summary AS a JOIN contract_invoice AS b ON `+ joinCond+` AND b.is_deleted = 0 WHERE `
  118. sql += condition
  119. sql += ` GROUP BY b.currency_unit `
  120. _,err = o.Raw(sql, pars).QueryRows(&results)
  121. return
  122. }
  123. // GetContractSummaryInvoicePaymentAmount 获取汇总金额合计信息
  124. func GetContractSummaryInvoicePaymentAmount(condition string, pars []interface{}) (amountTotal float64, err error) {
  125. joinCond := ` (a.invoice_id = b.contract_invoice_id OR a.payment_id = b.contract_invoice_id) `
  126. o := orm.NewOrmUsingDB("fms")
  127. sql := `SELECT b.amount FROM invoice_payment_summary AS a JOIN contract_invoice AS b ON `+ joinCond+` AND b.is_deleted = 0 WHERE `
  128. sql += condition
  129. sql += ` GROUP BY id `
  130. nsql := `SELECT IFNULL( SUM( e.amount ), 0 ) FROM (`+sql+`) as e`
  131. err = o.Raw(nsql, pars).QueryRow(&amountTotal)
  132. return
  133. }
  134. type InvoiceSummary struct {
  135. SummaryId int `json:"summary_id" description:"汇总ID"`
  136. ContractAmountCount float64 `gorm:"column:contract_amount_count" json:"contract_amount_count" description:"合同金额"`
  137. InvoicedAmountCount float64 `gorm:"column:invoiced_amount_count" json:"invoiced_amount_count" description:"开票金额"`
  138. ContractRegister
  139. }