invoice_record_dao.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package dao
  2. import (
  3. "errors"
  4. "youngee_b_api/app/entity"
  5. )
  6. type InvoiceRecordDao struct{}
  7. func (d InvoiceRecordDao) Insert(invoiceRecord *entity.InvoiceRecord) error {
  8. err := Db.Debug().Model(&entity.InvoiceRecord{}).Omit("billing_at").Create(invoiceRecord).Error
  9. if err != nil {
  10. return err
  11. }
  12. return nil
  13. }
  14. // 开票记录
  15. func (d InvoiceRecordDao) GetBillList(enterpriseId string, subAccountId int64, status int64, page int, pageSize int) ([]*entity.InvoiceRecord, int64, error) {
  16. invoiceRecords := []*entity.InvoiceRecord{}
  17. var total int64
  18. query := Db.Debug().Model(&entity.InvoiceRecord{}).Where("status = ?", status)
  19. if subAccountId == 0 {
  20. if enterpriseId == "" {
  21. return invoiceRecords, 0, errors.New("enterpriseId is empty")
  22. }
  23. query = query.Where("enterprise_id = ?", enterpriseId)
  24. } else {
  25. query = query.Where("sub_account_id = ?", subAccountId)
  26. }
  27. query.Count(&total)
  28. query = query.Select("billing_id, enterprise_id, sub_account_id, invoice_amount, invoice_body, invoice_type, task_ids, status, submit_at, billing_at, invoice_url")
  29. offset := (page - 1) * pageSize
  30. var err error
  31. if status == 1 {
  32. err = query.Order("submit_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
  33. } else if status == 2 {
  34. err = query.Order("billing_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
  35. }
  36. if err != nil {
  37. return nil, 0, err
  38. }
  39. return invoiceRecords, total, nil
  40. }
  41. // 可开票账单——电商带货
  42. func (d InvoiceRecordDao) GetBillableSelectionList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.SelectionInfo, int64, error) {
  43. billableSelections := []*entity.SelectionInfo{}
  44. var total int64
  45. query := Db.Debug().Model(&entity.SelectionInfo{}).Where("selection_status = ? AND invoice_status = ?", 8, 0)
  46. if subAccountId == 0 {
  47. if enterpriseId == "" {
  48. return billableSelections, 0, errors.New("enterpriseId is empty")
  49. }
  50. query = query.Where("enterprise_id = ?", enterpriseId)
  51. } else {
  52. query = query.Where("sub_account_id = ?", subAccountId)
  53. }
  54. query.Count(&total)
  55. query = query.Select("selection_id, enterprise_id, sub_account_id, product_id, platform, settlement_amount")
  56. //offset := (page - 1) * pageSize
  57. //err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
  58. err := query.Order("finish_at desc").Find(&billableSelections).Error
  59. if err != nil {
  60. return nil, 0, err
  61. }
  62. return billableSelections, total, nil
  63. }
  64. // 可开票账单——品牌种草
  65. func (d InvoiceRecordDao) GetBillableProjectList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.Project, int64, error) {
  66. billableProjects := []*entity.Project{}
  67. var total int64
  68. query := Db.Debug().Model(&entity.Project{}).Where("project_status = ? AND invoice_status = ?", 10, 0)
  69. if subAccountId == 0 {
  70. if enterpriseId == "" {
  71. return billableProjects, 0, errors.New("enterpriseId is empty")
  72. }
  73. query = query.Where("enterprise_id = ?", enterpriseId)
  74. } else {
  75. query = query.Where("sub_account_id = ?", subAccountId)
  76. }
  77. query.Count(&total)
  78. query = query.Select("project_id, enterprise_id, sub_account_id, product_id, project_platform, settlement_amount")
  79. //offset := (page - 1) * pageSize
  80. //err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
  81. err := query.Order("finish_at desc").Find(&billableProjects).Error
  82. if err != nil {
  83. return nil, 0, err
  84. }
  85. return billableProjects, total, nil
  86. }
  87. // 可开票账单——本地生活
  88. //func (d InvoiceRecordDao) GetBillableProjectList(enterpriseId string, subAccountId int64, page int, pageSize int) ([]*entity.Project, int64, error) {
  89. // billableProjects := []*entity.Project{}
  90. // var total int64
  91. // query := Db.Debug().Model(&entity.Project{}).Where("project_status = ? AND invoice_status = ?", 10, 0)
  92. // if subAccountId == 0 {
  93. // if enterpriseId == "" {
  94. // return billableProjects, 0, errors.New("enterpriseId is empty")
  95. // }
  96. // query = query.Where("enterprise_id = ?", enterpriseId)
  97. // } else {
  98. // query = query.Where("sub_account_id = ?", subAccountId)
  99. // }
  100. // query.Count(&total)
  101. // query = query.Select("project_id, enterprise_id, sub_account_id, product_id, project_platform, settlement_amount")
  102. // //offset := (page - 1) * pageSize
  103. // //err := query.Order("finish_at desc").Offset(offset).Limit(pageSize).Find(&invoiceRecords).Error
  104. // err := query.Order("finish_at desc").Find(&billableProjects).Error
  105. // if err != nil {
  106. // return nil, 0, err
  107. // }
  108. //
  109. // return billableProjects, total, nil
  110. //}
  111. // 获取指定企业id的开票中/已开票金额
  112. func (d InvoiceRecordDao) GetInvoiceAmount(enterpriseId string, status int64) (float64, error) {
  113. var totalAmount float64
  114. query := Db.Debug().Model(&entity.InvoiceRecord{})
  115. err := query.Where("enterprise_id = ? AND status = ?", enterpriseId, status).Select("COALESCE(SUM(invoice_amount), 0)").Scan(&totalAmount).Error
  116. if err != nil {
  117. return 0, err
  118. }
  119. return totalAmount, nil
  120. }