invoice_payment_summary.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. }
  140. type ContractRegisterRaiData struct {
  141. CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
  142. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  143. RaiSellerName string `gorm:"column:rai_seller_name" json:"rai_seller_name" description:"CRM系统-权益销售名称"`
  144. RaiSellerId int `gorm:"column:rai_seller_id" json:"rai_seller_id" description:"CRM系统-权益销售ID"`
  145. Amount float64 `json:"amount" description:"分配金额"`
  146. InvoiceTime string `json:"invoice_time" description:"开票日期"`
  147. InvoiceType int `json:"invoice_type" description:"1-开票登记; 2-到款登记"`
  148. ContractType int `gorm:"column:contract_type" json:"contract_type" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
  149. }
  150. // 列表
  151. func GetContractRegisterList(condition string, pars []interface{}, startSize, pageSize int) (items []*ContractRegisterRaiData, err error) {
  152. o := orm.NewOrmUsingDB("fms")
  153. sql := `SELECT
  154. a.company_name,
  155. a.rai_seller_name,
  156. a.contract_code,
  157. b.amount,
  158. b.invoice_time
  159. FROM contract_register as a
  160. INNER JOIN contract_invoice as b ON a.contract_register_id = b.contract_register_id
  161. WHERE 1= 1 `
  162. if condition != "" {
  163. sql += condition
  164. }
  165. if startSize+pageSize > 0 {
  166. sql += ` LIMIT ?,? `
  167. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  168. } else {
  169. _, err = o.Raw(sql, pars).QueryRows(&items)
  170. }
  171. return
  172. }
  173. // 获取开票金额与未到账金额的统计
  174. func GetContractRegisterAmountList(condition string, pars []interface{}) (items []*ContractRegisterSummary, err error) {
  175. o := orm.NewOrmUsingDB("fms")
  176. sql := `SELECT
  177. SUM( invoiced_amount ) AS invoiced_amount,
  178. SUM( payment_amount ) AS payment_amount FROM contract_register as a WHERE 1= 1 `
  179. if condition != "" {
  180. sql += condition
  181. }
  182. _, err = o.Raw(sql, pars).QueryRows(&items)
  183. return
  184. }
  185. // 列表
  186. func GetContractInvoiceList(condition string, pars []interface{}, startSize, pageSize int) (items []*ContractInvoice, err error) {
  187. o := orm.NewOrmUsingDB("fms")
  188. sql := `SELECT * FROM contract_invoice as a WHERE 1= 1 `
  189. if condition != "" {
  190. sql += condition
  191. }
  192. if startSize+pageSize > 0 {
  193. sql += ` LIMIT ?,? `
  194. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  195. } else {
  196. _, err = o.Raw(sql, pars).QueryRows(&items)
  197. }
  198. return
  199. }
  200. // 获取数量
  201. func GetContractInvoiceCount(condition string, pars []interface{}) (count int, err error) {
  202. o := orm.NewOrmUsingDB("fms")
  203. sqlCount := ` SELECT COUNT(1) AS count FROM contract_register as a
  204. INNER JOIN contract_invoice as b ON a.contract_register_id = b.contract_register_id
  205. WHERE 1= 1 `
  206. if condition != "" {
  207. sqlCount += condition
  208. }
  209. err = o.Raw(sqlCount, pars).QueryRow(&count)
  210. return
  211. }
  212. // 获取开票金额与未到账金额的统计
  213. func GetContractInvoiceAmountCount(condition string, pars []interface{}) (amount float64, err error) {
  214. o := orm.NewOrmUsingDB("fms")
  215. sql := `SELECT
  216. SUM( amount ) AS amount FROM contract_register as a
  217. INNER JOIN contract_invoice as b ON a.contract_register_id = b.contract_register_id
  218. WHERE 1= 1 `
  219. if condition != "" {
  220. sql += condition
  221. }
  222. err = o.Raw(sql, pars).QueryRow(&amount)
  223. return
  224. }
  225. // GetContractRegisterListByStartDate 根据起始日期的时间段筛选合同登记信息。
  226. func GetContractRegisterListByStartDate(startDate, endDate string) (results []*ContractRegisterRaiData, err error) {
  227. o := orm.NewOrmUsingDB("fms")
  228. sql := `SELECT
  229. a.rai_seller_id,
  230. a.contract_type,
  231. b.amount,
  232. b.invoice_time,
  233. b.invoice_type
  234. FROM contract_register as a
  235. INNER JOIN contract_invoice as b ON a.contract_register_id = b.contract_register_id AND a.is_deleted = 0 AND b.invoice_time >= ? AND b.invoice_time <= ? `
  236. _, err = o.Raw(sql, startDate, endDate).QueryRows(&results)
  237. return
  238. }