invoice_payment_summary.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. ServiceProductId int `gorm:"column:service_product_id" json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  16. base.TimeBase
  17. }
  18. func (c *InvoicePaymentSummary) TableName() string {
  19. return "invoice_payment_summary"
  20. }
  21. func (c *InvoicePaymentSummary) Create() (err error) {
  22. err = global.DEFAULT_MYSQL.Create(c).Error
  23. return
  24. }
  25. // DeleteAndCreate 删除并新增汇总
  26. func (c *InvoicePaymentSummary) DeleteAndCreate(registerId int, summaryList []*InvoicePaymentSummary) (err error) {
  27. tx := global.DEFAULT_MYSQL.Begin()
  28. defer func() {
  29. if err != nil {
  30. tx.Rollback()
  31. } else {
  32. tx.Commit()
  33. }
  34. }()
  35. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  36. tx.Exec(sql, registerId)
  37. if len(summaryList) > 0 {
  38. err = tx.CreateInBatches(summaryList, len(summaryList)).Error
  39. if err != nil {
  40. return
  41. }
  42. }
  43. return
  44. }
  45. type ContractRegisterSummary struct {
  46. SummaryId int `json:"summary_id" description:"汇总ID"`
  47. ContractRegister
  48. }
  49. // GetInvoicePaymentCensusPageList 获取商品到款统计列表-总数
  50. func GetInvoicePaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*ContractRegisterSummary, count int64, err error) {
  51. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  52. 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").
  53. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  54. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  55. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  56. Where(condition, pars...)
  57. query.Count(&count)
  58. if len(page.GetOrderItemsString()) > 0 {
  59. query = query.Order(page.GetOrderItemsString())
  60. }
  61. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  62. if err != nil {
  63. return
  64. }
  65. return
  66. }
  67. type InvoicePaymentSummaryItem struct {
  68. SummaryId int `json:"summary_id" description:"汇总ID"`
  69. RegisterId int `json:"register_id" description:"登记ID"`
  70. CompanyName string `json:"company_name" description:"客户名称"`
  71. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  72. StartDate time.Time `json:"start_date" description:"合同开始日期"`
  73. EndDate time.Time `json:"end_date" description:"合同结束日期"`
  74. InvoiceId int `json:"invoice_id" description:"开票ID"`
  75. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  76. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  77. SellerId int `json:"seller_id" description:"销售ID"`
  78. SellerName string `json:"seller_name" description:"销售名称"`
  79. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  80. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  81. PaymentId int `json:"payment_id" description:"到款ID"`
  82. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  83. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  84. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  85. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  86. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  87. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  88. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  89. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  90. }
  91. // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  92. func GetInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*InvoicePaymentSummaryItem, err error) {
  93. 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",
  94. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "c.seller_id", "c.seller_name", "c.seller_group_id",
  95. "c.seller_group_name", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date", "d.pay_type",
  96. }
  97. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  98. Select(strings.Join(fields, ",")).
  99. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  100. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  101. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  102. Where(condition, pars...)
  103. query.Find(&results)
  104. return
  105. }
  106. // GetInvoicePaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  107. func GetInvoicePaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  108. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  109. Select("a.id AS summary_id").
  110. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  111. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  112. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  113. Where(condition, pars...)
  114. query.Find(&summaryIds)
  115. return
  116. }
  117. // GetContractSummaryInvoicePaymentAmountTotal 获取汇总金额合计信息
  118. func GetContractSummaryInvoicePaymentAmountTotal(condition string, pars []interface{}, amountType int) (amountTotal float64, err error) {
  119. joinCond := `a.invoice_id = b.contract_invoice_id`
  120. if amountType == 2 {
  121. joinCond = `a.payment_id = b.contract_invoice_id`
  122. }
  123. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  124. Select("IFNULL(SUM(b.amount),0)").
  125. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  126. Where(condition, pars...)
  127. err = query.Find(&amountTotal).Error
  128. return
  129. }
  130. // GetSummaryListCurrencySum 获取汇总分货币合计
  131. func GetSummaryListCurrencySum(condition string, pars []interface{}, amountType int) (results []*InvoiceListCurrencySum, err error) {
  132. joinCond := `a.invoice_id = b.contract_invoice_id`
  133. if amountType == 2 {
  134. joinCond = `a.payment_id = b.contract_invoice_id`
  135. }
  136. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  137. Select("b.currency_unit, b.invoice_type, SUM(b.amount) AS amount_total, SUM(b.origin_amount) AS origin_amount_total").
  138. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  139. Where(condition, pars...).
  140. Group("b.currency_unit")
  141. err = query.Find(&results).Error
  142. return
  143. }
  144. // DeleteInvoicePaymentSummaryByRegisterId 根据合同登记ID删除汇总数据
  145. func DeleteInvoicePaymentSummaryByRegisterId(registerId int) (err error) {
  146. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  147. err = global.DEFAULT_MYSQL.Exec(sql, registerId).Error
  148. return
  149. }
  150. // DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId 根据开票到款ID删除汇总数据
  151. func DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(invoiceId, arriveId, registerId int) (err error) {
  152. sql := `DELETE FROM invoice_payment_summary WHERE invoice_id = ? AND payment_id = ? AND register_id = ?`
  153. err = global.DEFAULT_MYSQL.Exec(sql, invoiceId, arriveId, registerId).Error
  154. return
  155. }
  156. type InvoiceSummary struct {
  157. SummaryId int `json:"summary_id" description:"汇总ID"`
  158. ContractAmountCount float64 `gorm:"column:contract_amount_count" json:"contract_amount_count" description:"合同金额"`
  159. InvoicedAmountCount float64 `gorm:"column:invoiced_amount_count" json:"invoiced_amount_count" description:"开票金额"`
  160. ContractRegister
  161. }
  162. // GetInvoiceCensusPageList 获取未开票统计列表-总数
  163. func GetInvoiceCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  164. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  165. 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 ").
  166. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  167. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  168. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  169. Where(condition, pars...).Group("b.contract_register_id")
  170. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  171. Select("*").Where(" e.invoiced_amount_count <> e.contract_amount ")
  172. nq.Count(&count)
  173. if len(page.GetOrderItemsString()) > 0 {
  174. nq = nq.Order(page.GetOrderItemsString())
  175. }
  176. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  177. if err != nil {
  178. return
  179. }
  180. return
  181. }
  182. // GetNoPaymentCensusPageList 获取开票未到款统计计列表-总数
  183. func GetNoPaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  184. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  185. 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 " +
  186. " ,c.invoice_time ").
  187. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  188. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  189. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  190. Where(condition, pars...).Group("b.contract_register_id")
  191. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  192. Select("*").Where(" e.invoiced_amount_count <> e.contract_amount AND e.payment_amount = 0 ")
  193. nq.Count(&count)
  194. if len(page.GetOrderItemsString()) > 0 {
  195. nq = nq.Order(page.GetOrderItemsString())
  196. }
  197. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  198. if err != nil {
  199. return
  200. }
  201. return
  202. }
  203. // NotInvoicePaymentCensusResp 未开票到款到款统计响应体
  204. type NotInvoicePaymentCensusResp struct {
  205. DataList []*InvoicePaymentCensusItem `json:"data_list"`
  206. InvoiceTotal float64 `json:"invoice_total" description:"开票总金额(换算后)"`
  207. PaymentTotal float64 `json:"payment_total" description:"到款总金额(换算后)"`
  208. InvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
  209. PaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
  210. NotInvoiceTotal float64 `json:"not_invoice_total" description:"未开票总金额(换算后)"`
  211. NotInvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_invoice_currency_total" description:"未开票-分币种总金额"`
  212. }