invoice_payment_summary.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. UnitName string `json:"unit_name" description:"单位名称"`
  91. }
  92. // GetInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  93. func GetInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*InvoicePaymentSummaryItem, err error) {
  94. 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",
  95. "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",
  96. "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","b.currency_unit",
  97. }
  98. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  99. Select(strings.Join(fields, ",")).
  100. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  101. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  102. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  103. Where(condition, pars...)
  104. query.Find(&results)
  105. return
  106. }
  107. // GetInvoicePaymentCensusSummaryDataIds 获取商品到款统计列表-汇总数据IDs
  108. func GetInvoicePaymentCensusSummaryDataIds(condition string, pars []interface{}) (summaryIds []int, err error) {
  109. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  110. Select("a.id AS summary_id").
  111. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  112. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  113. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  114. Where(condition, pars...)
  115. query.Find(&summaryIds)
  116. return
  117. }
  118. // GetContractSummaryInvoicePaymentAmountTotal 获取汇总金额合计信息
  119. func GetContractSummaryInvoicePaymentAmountTotal(condition string, pars []interface{}, amountType int) (amountTotal float64, err error) {
  120. joinCond := `a.invoice_id = b.contract_invoice_id`
  121. if amountType == 2 {
  122. joinCond = `a.payment_id = b.contract_invoice_id`
  123. }
  124. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  125. Select("IFNULL(SUM(b.amount),0)").
  126. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  127. Where(condition, pars...)
  128. err = query.Find(&amountTotal).Error
  129. return
  130. }
  131. // GetSummaryListCurrencySum 获取汇总分货币合计
  132. func GetSummaryListCurrencySum(condition string, pars []interface{}, amountType int) (results []*InvoiceListCurrencySum, err error) {
  133. joinCond := `a.invoice_id = b.contract_invoice_id`
  134. if amountType == 2 {
  135. joinCond = `a.payment_id = b.contract_invoice_id`
  136. }
  137. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  138. Select("b.currency_unit, b.invoice_type, SUM(b.amount) AS amount_total, SUM(b.origin_amount) AS origin_amount_total").
  139. Joins(fmt.Sprintf("JOIN contract_invoice AS b ON %s AND b.is_deleted = 0", joinCond)).
  140. Where(condition, pars...).
  141. Group("b.currency_unit")
  142. err = query.Find(&results).Error
  143. return
  144. }
  145. // DeleteInvoicePaymentSummaryByRegisterId 根据合同登记ID删除汇总数据
  146. func DeleteInvoicePaymentSummaryByRegisterId(registerId int) (err error) {
  147. sql := `DELETE FROM invoice_payment_summary WHERE register_id = ?`
  148. err = global.DEFAULT_MYSQL.Exec(sql, registerId).Error
  149. return
  150. }
  151. // DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId 根据开票到款ID删除汇总数据
  152. func DeleteInvoicePaymentSummaryByInvoiceIdAndPaymentId(invoiceId, arriveId, registerId int) (err error) {
  153. sql := `DELETE FROM invoice_payment_summary WHERE invoice_id = ? AND payment_id = ? AND register_id = ?`
  154. err = global.DEFAULT_MYSQL.Exec(sql, invoiceId, arriveId, registerId).Error
  155. return
  156. }
  157. // GetContractSummaryInvoicePaymentAmount 获取汇总金额合计信息
  158. func GetContractSummaryInvoicePaymentAmount(condition string, pars []interface{}) (amountTotal float64, err error) {
  159. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  160. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount").
  161. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  162. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  163. Where(condition, pars...).Group("id")
  164. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  165. Select(" IFNULL( SUM( e.amount ), 0 ) ")
  166. err = nq.Find(&amountTotal).Error
  167. return
  168. }
  169. // GetInvoicePaymentSummaryByRegisterIdAndInvoiceId 根据合同登记ID和开票ID获取汇总数据
  170. func GetInvoicePaymentSummaryByRegisterIdAndInvoiceId(registerId, invocieId int) (results []*ContractRegisterSummary, err error) {
  171. sql := `SELECT * FROM invoice_payment_summary WHERE register_id = ? AND invoice_id= ? `
  172. err = global.DEFAULT_MYSQL.Exec(sql, registerId, invocieId).Find(&results).Error
  173. return
  174. }
  175. type IncomeSummaryItem struct {
  176. CompanyName string `json:"company_name" description:"客户名称"`
  177. ContractType int `json:"contract_type" description:"是否为新客户: 2-否; 1-是"`
  178. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  179. Amount float64 `json:"amount" description:"金额"`
  180. SellerName string `json:"seller_name" description:"销售名称"`
  181. }
  182. // GetContractSummaryIncomeAmount 获取汇总金额合计信息
  183. func GetContractSummaryIncomeAmount(condition string, pars []interface{}) (results []*IncomeSummaryItem, err error) {
  184. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  185. Select("IF(a.invoice_id=0,d.amount, b.amount) AS amount,IF(a.invoice_id=0,d.invoice_time, b.invoice_time) AS invoice_date, c.contract_type,c.company_name,IF(a.invoice_id=0,d.seller_name, b.seller_name) AS seller_name,IF(a.invoice_id=0,d.seller_id, b.seller_id) AS final_seller_id").
  186. Joins("LEFT JOIN contract_invoice AS b ON a.invoice_id = b.contract_invoice_id AND b.is_deleted = 0 ").
  187. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  188. Joins("JOIN contract_register AS c ON a.register_id = c.contract_register_id AND c.is_deleted = 0").
  189. Where(condition, pars...).Group("id").Order("invoice_date ")
  190. //nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  191. // Select(" IFNULL( SUM( e.amount ), 0 ) ")
  192. err = query.Find(&results).Error
  193. return
  194. }
  195. type InvoiceSummary struct {
  196. SummaryId int `json:"summary_id" description:"汇总ID"`
  197. PaymentAmountCount float64 `gorm:"column:payment_amount_count" json:"payment_amount_count" description:"到款金额"`
  198. InvoicedAmountCount float64 `gorm:"column:invoiced_amount_count" json:"invoiced_amount_count" description:"开票金额"`
  199. ContractRegister
  200. }
  201. // GetInvoiceCensusPageList 获取未开票统计列表-总数
  202. func GetInvoiceCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  203. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  204. 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 ").
  205. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  206. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  207. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  208. Where(condition, pars...).Group("contract_register_id")
  209. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  210. Select("*").Where(" e.invoiced_amount_count <> e.contract_amount ")
  211. nq.Count(&count)
  212. if len(page.GetOrderItemsString()) > 0 {
  213. nq = nq.Order(page.GetOrderItemsString())
  214. }
  215. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  216. if err != nil {
  217. return
  218. }
  219. return
  220. }
  221. // GetNoPaymentCensusPageList 获取开票未到款统计计列表-总数
  222. func GetNoPaymentCensusPageList(page base.IPage, condition string, pars []interface{}) (results []*InvoiceSummary, count int64, err error) {
  223. query := global.DEFAULT_MYSQL.Table("invoice_payment_summary AS a").
  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.amount ),0) AS invoiced_amount_count,COALESCE(SUM( d.amount ),0) AS payment_amount_count "+
  225. " ,c.invoice_time ", "c.origin_amount AS summary_invoice_amount", "d.origin_amount AS summary_payment_amount", "a.invoice_id", "a.payment_id").
  226. Joins("JOIN contract_register AS b ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  227. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  228. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  229. Where(condition, pars...).Group("id")
  230. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  231. 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 ) ")
  232. nq.Count(&count)
  233. if len(page.GetOrderItemsString()) > 0 {
  234. nq = nq.Order(page.GetOrderItemsString())
  235. }
  236. err = nq.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  237. if err != nil {
  238. return
  239. }
  240. return
  241. }
  242. // NotInvoicePaymentCensusResp 未开票到款到款统计响应体
  243. type NotInvoicePaymentCensusResp struct {
  244. DataList []*NotPaymentCensusItem `json:"data_list"`
  245. InvoiceTotal float64 `json:"invoice_total" description:"开票总金额(换算后)"`
  246. PaymentTotal float64 `json:"payment_total" description:"到款总金额(换算后)"`
  247. InvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"invoice_currency_total" description:"开票-分币种总金额"`
  248. PaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"payment_currency_total" description:"到款-分币种总金额"`
  249. NotInvoiceTotal float64 `json:"not_invoice_total" description:"未开票总金额(换算后)"`
  250. NotInvoiceCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_invoice_currency_total" description:"未开票-分币种总金额"`
  251. NotPaymentTotal float64 `json:"not_payment_total" description:"未开票总金额(换算后)"`
  252. NotPaymentCurrencyTotal []*InvoiceListCurrencyTotal `json:"not_payment_currency_total" description:"未开票-分币种总金额"`
  253. }
  254. type IncomeSummaryItemList []*IncomeSummaryItem
  255. func (c IncomeSummaryItemList) Len() int {
  256. return len(c)
  257. }
  258. func (c IncomeSummaryItemList) Less(i, j int) bool {
  259. return c[i].InvoiceDate.Before(c[j].InvoiceDate)
  260. }
  261. func (c IncomeSummaryItemList) Swap(i, j int) {
  262. c[i], c[j] = c[j], c[i]
  263. }
  264. type NotInvoicePaymentSummaryItem struct {
  265. SummaryId int `json:"summary_id" description:"汇总ID"`
  266. RegisterId int `json:"register_id" description:"登记ID"`
  267. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  268. InvoiceId int `json:"invoice_id" description:"开票ID"`
  269. InvoiceDate time.Time `json:"invoice_time" description:"开票日期"`
  270. InvoiceAmount float64 `json:"invoice_amount" description:"开票金额"`
  271. SellerGroupId int `json:"seller_group_id" description:"销售组别ID"`
  272. SellerGroupName string `json:"seller_group_name" description:"销售组别名称"`
  273. RaiSellerGroupId int `json:"rai_seller_group_id" description:"销售组别ID"`
  274. RaiSellerGroupName string `json:"rai_seller_group_name" description:"销售组别名称"`
  275. PaymentId int `json:"payment_id" description:"到款ID"`
  276. PaymentDate time.Time `json:"payment_date" description:"到款日期"`
  277. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  278. PayType int `json:"pay_type" description:"付款方式:0-无;1-年付;2-半年付;3-季付;4-次付;5-异常"`
  279. ServiceProductId int `json:"service_product_id" description:"套餐类型:1ficc套餐,2权益套餐"`
  280. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  281. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  282. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  283. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  284. PaymentAmountTotal float64 `gorm:"column:payment_amount_total" json:"payment_amount_total" description:"合同金额"`
  285. InvoiceAmountTotal float64 `gorm:"column:invoice_amount_total" json:"invoice_amount_total" description:"开票金额"`
  286. NotInvoicedAmountTotal float64 ` json:"not_invoiced_amount_total" description:"未开票金额"`
  287. SellerType string `json:"seller_type" description:"销售类型"`
  288. ContractRegister
  289. }
  290. // GetNotInvoicePaymentCensusSummaryData 获取商品到款统计列表-汇总数据
  291. func GetNotInvoicePaymentCensusSummaryData(condition string, pars []interface{}) (results []*NotInvoicePaymentSummaryItem, err error) {
  292. fields := []string{"a.id AS summary_id", "a.register_id", "a.invoice_id", "a.payment_id", "a.service_product_id", "b.*",
  293. "c.origin_amount AS invoice_amount", "c.invoice_time AS invoice_date", "d.origin_amount AS payment_amount", "d.invoice_time AS payment_date",
  294. "d.pay_type", "COALESCE(SUM( c.origin_amount ),0) AS invoice_amount_total", "COALESCE(SUM( d.origin_amount ),0) AS payment_amount_total",
  295. }
  296. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  297. Select(strings.Join(fields, ",")).
  298. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  299. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  300. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  301. Where(condition, pars...).Group("a.register_id,service_product_id")
  302. query.Find(&results)
  303. return
  304. }
  305. // GetNoInvoicePaymentCensusData
  306. func GetNoInvoicePaymentCensusData(condition string, pars []interface{}) (results []*NotInvoicePaymentSummaryItem, err error) {
  307. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  308. Select("*").
  309. Where(condition, pars...).Group("contract_register_id")
  310. query.Find(&results)
  311. return
  312. }
  313. // GetInvoiceCensusList 获取未开票统计列表
  314. func GetInvoiceCensusList(condition string, pars []interface{}) (results []*InvoiceSummary, err error) {
  315. query := global.DEFAULT_MYSQL.Table("contract_register AS b").
  316. 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 ").
  317. Joins("left JOIN invoice_payment_summary AS a ON a.register_id = b.contract_register_id AND b.is_deleted = 0").
  318. Joins("LEFT JOIN contract_invoice AS c ON a.invoice_id = c.contract_invoice_id AND c.is_deleted = 0").
  319. Joins("LEFT JOIN contract_invoice AS d ON a.payment_id = d.contract_invoice_id AND d.is_deleted = 0").
  320. Where(condition, pars...).Group("contract_register_id")
  321. nq := global.DEFAULT_MYSQL.Table("(?) AS e", query).
  322. Select("*").Where(" e.invoiced_amount_count <> e.contract_amount ")
  323. err = nq.Find(&results).Error
  324. if err != nil {
  325. return
  326. }
  327. return
  328. }