invoice_payment_summary.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. InvoiceOriginAmount float64 `json:"invoice_origin_amount" description:"开票原始金额"`
  77. InvoiceAmount float64 `json:"invoice_amount" description:"开票换算金额"`
  78. SellerId int `json:"seller_id" description:"销售ID"`
  79. SellerName string `json:"seller_name" description:"销售名称"`
  80. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  81. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  82. PaymentId int `json:"payment_id" description:"到款ID"`
  83. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  84. PaymentOriginAmount float64 `json:"payment_origin_amount" description:"到款原始金额"`
  85. PaymentAmount float64 `json:"payment_amount" description:"到款换算金额"`
  86. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  87. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  88. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  89. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  90. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  91. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  92. UnitName string `json:"unit_name" description:"单位名称"`
  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", "a.service_product_id", "b.company_name", "b.start_date", "b.end_date",
  97. "c.origin_amount AS invoice_origin_amount", "c.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",
  98. "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.amount AS payment_amount", "d.origin_amount AS payment_origin_amount", "d.invoice_time AS payment_date", "d.pay_type", "b.currency_unit",
  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("IFNULL(SUM(b.amount),0)").
  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, IFNULL(SUM(b.amount),0) AS amount_total, IFNULL(SUM(b.origin_amount),0) 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. }
  147. // DeleteInvoicePaymentSummaryByRegisterId 根据合同登记ID删除汇总数据
  148. func DeleteInvoicePaymentSummaryByRegisterId(registerId int) (err error) {
  149. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  150. err = global.DEFAULT_MYSQL.Exec(sql, registerId).Error
  151. return
  152. }
  153. // DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId 根据开票到款ID删除汇总数据
  154. func DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(invoiceId, arriveId, registerId int) (err error) {
  155. sql := `DELETE FROM invoice_payment_summary WHERE invoice_id = ? AND payment_id = ? AND register_id = ?`
  156. err = global.DEFAULT_MYSQL.Exec(sql, invoiceId, arriveId, registerId).Error
  157. return
  158. }
  159. // GetContractSummaryInvoicePaymentAmount 获取汇总金额合计信息
  160. func GetContractSummaryInvoicePaymentAmount(condition string, pars []interface{}) (amountTotal float64, err error) {
  161. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  162. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount").
  163. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  164. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  165. Where(condition, pars...).Group("id")
  166. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  167. Select(" IFNULL( SUM( e.amount ), 0 ) ")
  168. err = nq.Find(&amountTotal).Error
  169. return
  170. }
  171. // GetInvoicePaymentSummaryByRegisterIdAndInvoiceId 根据合同登记ID和开票ID获取汇总数据
  172. func GetInvoicePaymentSummaryByRegisterIdAndInvoiceId(registerId, invocieId int) (result *InvoicePaymentSummary, err error) {
  173. sql := `SELECT * FROM invoice_payment_summary WHERE register_id = ? AND invoice_id= ? `
  174. err = global.DEFAULT_MYSQL.Raw(sql, registerId, invocieId).First(&result).Error
  175. return
  176. }
  177. type IncomeSummaryItem struct {
  178. CompanyName string `json:"company_name" description:"客户名称"`
  179. ContractType int `json:"contract_type" description:"是否为新客户: 2-否; 1-是"`
  180. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  181. OriginAmount float64 `json:"origin_amount" description:"原始金额"`
  182. Amount float64 `json:"amount" description:"金额"`
  183. SellerName string `json:"seller_name" description:"销售名称"`
  184. FinalSellerId int `json:"final_seller_id" description:"最终销售ID"`
  185. SellerGroupId int `json:"seller_group_id" description:"销售分组ID"`
  186. SellerGroupName int `json:"seller_group_name" description:"销售分组名称"`
  187. CurrencyUnit string `json:"currency_unit" description:"货币单位"`
  188. ServicesName string `json:"services" description:"套餐名称"`
  189. }
  190. // GetContractSummaryIncomeAmount 获取汇总金额合计信息
  191. func GetContractSummaryIncomeAmount(condition string, pars []interface{}) (results []*IncomeSummaryItem, err error) {
  192. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  193. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,"+
  194. "IF(a.invoice_id=0,d.origin_amount, b.origin_amount) AS origin_amount,"+
  195. "IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date,"+
  196. "IF(a.invoice_id=0,d.currency_unit, b.currency_unit) AS currency_unit,"+
  197. " c.contract_type,c.company_name,"+
  198. "IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name,"+
  199. "IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id,"+
  200. "IF(a.invoice_id=0,d.seller_group_id, b.seller_group_id) AS seller_group_id,"+
  201. "GROUP_CONCAT(s.title) AS services_name ").
  202. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  203. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  204. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  205. Joins("LEFT JOIN contract_service AS s ON s.contract_register_id = c.contract_register_id").
  206. Where(condition, pars...).Group("id").Order("invoice_date ")
  207. //nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  208. // Select(" IFNULL( SUM( e.amount ), 0 ) ")
  209. err = query.Find(&results).Error
  210. return
  211. }
  212. type InvoiceSummary struct {
  213. SummaryId int `json:"summary_id" description:"汇总ID"`
  214. PaymentAmountCount float64 `gorm:"column:payment_amount_count" json:"payment_amount_count" description:"到款金额"`
  215. InvoicedAmountCount float64 `gorm:"column:invoiced_amount_count" json:"invoiced_amount_count" description:"开票金额"`
  216. InvoiceOrigin float64 `gorm:"column:invoice_origin" json:"invoice_origin" description:"开票原始金额"`
  217. PaymentOrigin float64 `gorm:"column:payment_origin" json:"payment_origin" description:"到款原始金额"`
  218. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  219. ContractRegister
  220. }
  221. // GetInvoiceCensusPageList 获取未开票统计列表-总数
  222. func GetInvoiceCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  223. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  224. 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, COALESCE(SUM( c.origin_amount ),0) AS invoiced_amount_count,COALESCE(SUM( d.origin_amount ),0) AS payment_amount_count ").
  225. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  226. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  227. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  228. Where(condition, pars...).Group("contract_register_id")
  229. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  230. Select("*").Where(" (e.invoiced_amount_count <> e.contract_amount AND e.contract_amount <> 0) OR (e.contract_amount = 0 AND e.invoiced_amount_count <> e.payment_amount_count)")
  231. nq.Count(&count)
  232. if len(page.GetOrderItemsString()) > 0 {
  233. nq = nq.Order(page.GetOrderItemsString())
  234. }
  235. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  236. if err != nil {
  237. return
  238. }
  239. return
  240. }
  241. // GetNoPaymentCensusPageList 获取开票未到款统计计列表-总数
  242. func GetNoPaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  243. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  244. 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, COALESCE(SUM( c.amount ),0) AS invoiced_amount_count,COALESCE(SUM( d.amount ),0) AS payment_amount_count "+
  245. " ,c.invoice_time ", "c.origin_amount AS summary_invoice_amount", "d.origin_amount AS summary_payment_amount", "a.invoice_id", "a.payment_id").
  246. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  247. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  248. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  249. Where(condition, pars...).Group("id")
  250. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  251. Select("*").Where(" e.invoiced_amount_count <> 0 AND e.invoice_id <> 0 AND ( e.summary_invoice_amount > e.summary_payment_amount OR e.summary_payment_amount IS NULL ) ")
  252. nq.Count(&count)
  253. if len(page.GetOrderItemsString()) > 0 {
  254. nq = nq.Order(page.GetOrderItemsString())
  255. }
  256. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  257. if err != nil {
  258. return
  259. }
  260. return
  261. }
  262. // NotInvoicePaymentCensusResp 未开票到款到款统计响应体
  263. type NotInvoicePaymentCensusResp struct {
  264. DataList []*NotPaymentCensusItem `json:"data_list"`
  265. InvoiceTotal float64 `json:"invoice_total" description:"开票总金额(换算后)"`
  266. PaymentTotal float64 `json:"payment_total" description:"到款总金额(换算后)"`
  267. InvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
  268. PaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
  269. NotInvoiceTotal float64 `json:"not_invoice_total" description:"未开票总金额(换算后)"`
  270. NotInvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_invoice_currency_total" description:"未开票-分币种总金额"`
  271. NotPaymentTotal float64 `json:"not_payment_total" description:"未开票总金额(换算后)"`
  272. NotPaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_payment_currency_total" description:"未开票-分币种总金额"`
  273. }
  274. type IncomeSummaryItemList []*IncomeSummaryItem
  275. func (c IncomeSummaryItemList) Len() int {
  276. return len(c)
  277. }
  278. func (c IncomeSummaryItemList) Less(i, j int) bool {
  279. return c[i].InvoiceDate.Before(c[j].InvoiceDate)
  280. }
  281. func (c IncomeSummaryItemList) Swap(i, j int) {
  282. c[i], c[j] = c[j], c[i]
  283. }
  284. type NotInvoicePaymentSummaryItem struct {
  285. SummaryId int `json:"summary_id" description:"汇总ID"`
  286. RegisterId int `json:"register_id" description:"登记ID"`
  287. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  288. InvoiceId int `json:"invoice_id" description:"开票ID"`
  289. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  290. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  291. InvoiceOriginAmount float64 `json:"invoice_origin_amount" description:"开票原始金额"`
  292. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  293. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  294. RaiSellerGroupId int `json:"rai_seller_group_id" description:"销售组别ID"`
  295. RaiSellerGroupName string `json:"rai_seller_group_name" description:"销售组别名称"`
  296. PaymentId int `json:"payment_id" description:"到款ID"`
  297. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  298. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  299. PaymentOriginAmount float64 `json:"payment_origin_amount" description:"到款原始金额"`
  300. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  301. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  302. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  303. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  304. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  305. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  306. PaymentAmountTotal float64 `gorm:"column:payment_amount_total" json:"payment_amount_total" description:"合同金额"`
  307. InvoiceAmountTotal float64 `gorm:"column:invoice_amount_total" json:"invoice_amount_total" description:"开票金额"`
  308. NotInvoicedAmountOriginTotal float64 ` json:"not_invoiced_amount_origin_total" description:"未开票金额"`
  309. NotInvoicedAmountTotal float64 ` json:"not_invoiced_amount_total" description:"未开票转换金额"`
  310. SellerType string `json:"seller_type" description:"销售类型"`
  311. ContractRegister
  312. }
  313. // GetNotInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  314. func GetNotInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*NotInvoicePaymentSummaryItem, err error) {
  315. fields := []string{"a.id AS summary_id", "a.register_id", "a.invoice_id", "a.payment_id", "a.service_product_id", "b.*",
  316. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date",
  317. "d.pay_type", "COALESCE(SUM( c.origin_amount ),0) AS invoice_amount_total", "COALESCE(SUM( d.origin_amount ),0) AS payment_amount_total",
  318. }
  319. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  320. Select(strings.Join(fields, ",")).
  321. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  322. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  323. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  324. Where(condition, pars...).Group("a.register_id,service_product_id")
  325. query.Find(&results)
  326. return
  327. }
  328. // GetNoInvoicePaymentCensusData
  329. func GetNoInvoicePaymentCensusData(condition string, pars []interface{}) (results []*NotInvoicePaymentSummaryItem, err error) {
  330. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  331. Select("*").
  332. Where(condition, pars...).Group("contract_register_id")
  333. query.Find(&results)
  334. return
  335. }
  336. // GetInvoiceCensusList 获取未开票统计列表
  337. func GetInvoiceCensusList(condition string, pars []interface{}) (results []*InvoiceSummary, err error) {
  338. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  339. 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, COALESCE(SUM( c.origin_amount ),0) AS invoiced_amount_count,COALESCE(SUM( d.origin_amount ),0) AS payment_amount_count,a.service_product_id,c.origin_amount AS invoice_origin,d.origin_amount AS payment_origin ").
  340. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  341. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  342. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  343. Where(condition, pars...).Group("contract_register_id")
  344. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  345. Select("*").Where(" (e.invoiced_amount_count <> e.contract_amount AND e.contract_amount <> 0) OR (e.contract_amount = 0 AND e.invoiced_amount_count <> e.payment_amount_count) ")
  346. err = nq.Find(&results).Error
  347. if err != nil {
  348. return
  349. }
  350. return
  351. }
  352. // UpdateInvoicePaymentSummaryByRegisterId
  353. func UpdateInvoicePaymentSummaryByRegisterId(newId int, registerIds []int) (err error) {
  354. sql := `UPDATE invoice_payment_summary SET register_id = ? WHERE register_id IN (?) `
  355. err = global.DEFAULT_MYSQL.Exec(sql, newId, registerIds).Error
  356. return
  357. }
  358. // GetContractSummaryPaymentIncomeAmount 获取汇总金额合计信息-到款收入统计
  359. func GetContractSummaryPaymentIncomeAmount(condition string, pars []interface{}) (results []*IncomeSummaryItem, err error) {
  360. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  361. Select("d.amount AS amount,d.invoice_time AS invoice_date,"+
  362. " c.contract_type,c.company_name,d.seller_name,"+
  363. " d.seller_id AS final_seller_id, d.seller_group_id AS seller_group_id").
  364. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  365. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  366. Where(condition, pars...).Group("id").Order("invoice_date ")
  367. //nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  368. // Select(" IFNULL( SUM( e.amount ), 0 ) ")
  369. err = query.Find(&results).Error
  370. return
  371. }
  372. type IncomeSummaryRespItem struct {
  373. CompanyName string `json:"company_name" description:"客户名称"`
  374. ContractType int `json:"contract_type" description:"是否为新客户: 2-否; 1-是"`
  375. InvoiceDate string `json:"invoice_time" description:"开票日期"`
  376. OriginAmount float64 `json:"origin_amount" description:"原始金额"`
  377. Amount float64 `json:"amount" description:"金额"`
  378. SellerName string `json:"seller_name" description:"销售名称"`
  379. FinalSellerId int `json:"final_seller_id" description:"最终销售ID"`
  380. UnitName string `json:"unit_name" description:"货币单位名称"`
  381. ServicesName string `json:"services_name" description:"套餐名称"`
  382. }
  383. // GetContractSummaryPaymentIncomeAmountPage 获取汇总金额合计信息-到款收入统计
  384. func GetContractSummaryPaymentIncomeAmountPage(condition string, pars []interface{}, page *base.Page) (results []*IncomeSummaryItem, count int64, err error) {
  385. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  386. Select("d.origin_amount, d.amount AS amount,d.currency_unit, d.invoice_time AS invoice_date,"+
  387. " c.contract_type,c.company_name,d.seller_name,"+
  388. " d.seller_id AS final_seller_id").
  389. Joins("JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  390. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  391. Where(condition, pars...).Group("id")
  392. //nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  393. // Select(" IFNULL( SUM( e.amount ), 0 ) ")
  394. query.Count(&count)
  395. query = global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  396. Select("d.origin_amount, d.amount AS amount,d.currency_unit, d.invoice_time AS invoice_date,"+
  397. " c.contract_type,c.company_name,d.seller_name,"+
  398. " d.seller_id AS final_seller_id, GROUP_CONCAT(s.title) AS services_name ").
  399. Joins("JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  400. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  401. Joins("LEFT JOIN contract_service AS s ON s.contract_register_id = c.contract_register_id").
  402. Where(condition, pars...).Group("id")
  403. if len(page.GetOrderItemsString()) > 0 {
  404. query = query.Order(page.GetOrderItemsString())
  405. }
  406. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  407. return
  408. }
  409. // GetContractSummaryIncomeAmountPage 获取汇总金额合计信息-开票到款收入统计
  410. func GetContractSummaryIncomeAmountPage(condition string, pars []interface{}, page *base.Page) (results []*IncomeSummaryItem, count int64, err error) {
  411. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  412. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,"+
  413. "IF(a.invoice_id=0,d.origin_amount, b.origin_amount) AS origin_amount,"+
  414. "IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date,"+
  415. "IF(a.invoice_id=0,d.currency_unit, b.currency_unit) AS currency_unit,"+
  416. " c.contract_type,c.company_name,"+
  417. "IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name,"+
  418. "IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id,"+
  419. "IF(a.invoice_id=0,d.seller_group_id, b.seller_group_id) AS seller_group_id").
  420. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  421. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  422. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  423. Where(condition, pars...).Group("id")
  424. //nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  425. // Select(" IFNULL( SUM( e.amount ), 0 ) ")
  426. query.Count(&count)
  427. query = global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  428. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,"+
  429. "IF(a.invoice_id=0,d.origin_amount, b.origin_amount) AS origin_amount,"+
  430. "IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date,"+
  431. "IF(a.invoice_id=0,d.currency_unit, b.currency_unit) AS currency_unit,"+
  432. " c.contract_type,c.company_name,"+
  433. "IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name,"+
  434. "IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id,"+
  435. "IF(a.invoice_id=0,d.seller_group_id, b.seller_group_id) AS seller_group_id,"+
  436. "GROUP_CONCAT(s.title) AS services_name ").
  437. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  438. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  439. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  440. Joins("LEFT JOIN contract_service AS s ON s.contract_register_id = c.contract_register_id").
  441. Where(condition, pars...).Group("id")
  442. if len(page.GetOrderItemsString()) > 0 {
  443. query = query.Order(page.GetOrderItemsString())
  444. }
  445. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  446. return
  447. }
  448. // GetPaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  449. func GetPaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  450. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  451. Select("a.id AS summary_id").
  452. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  453. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  454. Where(condition, pars...)
  455. query.Find(&summaryIds)
  456. return
  457. }