invoiceMake.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package service
  2. import (
  3. "ElectronicInvoice/internal/consts"
  4. "context"
  5. "fmt"
  6. "github.com/gogf/gf/v2/database/gdb"
  7. "github.com/gogf/gf/v2/errors/gerror"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "strconv"
  11. "time"
  12. )
  13. // simpleMakeInvoice 简单开票
  14. func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  15. var (
  16. res gdb.Result
  17. )
  18. //查询需要开票的数据(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
  19. res, err = g.DB().Query(ctx, "SELECT a.*,b.pay_way,b.order_money,b.pay_money FROM invoice a INNER JOIN dataexport_order b ON a.order_code=b.order_code WHERE a.invoice_status=0 AND a.invoice_changed=0 AND a.invoice_variety like '%普通发票%' AND (a.invoice_order_code is NULL OR a.invoice_code not like '%,%') AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
  20. if err != nil {
  21. err = gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
  22. return
  23. }
  24. total, okNum = res.Len(), 0
  25. for _, m := range res.List() {
  26. select {
  27. case im.RunPool <- true:
  28. case <-time.After(time.Minute * 5):
  29. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  30. return
  31. }
  32. var (
  33. iType = gconv.String(m["invoice_type"])
  34. remark = gconv.String(m["remark"])
  35. prices float64
  36. )
  37. //公对公转账 账单金额可以修改 开发票应取实付金额 pay_money
  38. //微信支付宝支付 pay_money为订单金额减去微信or支付包红包
  39. if gconv.String(m["pay_way"]) == "transferAccounts" {
  40. prices = gconv.Float64(m["pay_money"]) / float64(100)
  41. } else {
  42. prices = gconv.Float64(m["order_money"]) / float64(100)
  43. }
  44. c := MakeInvoiceData{
  45. Type: "2",
  46. Id: fmt.Sprintf("id:%d", gconv.Int64(m["id"])), //因为orderCode不唯一,此处用订单发票数据库id
  47. Notes: remark,
  48. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  49. InvoiceArr: []MakeInvoiceItems{{
  50. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  51. WhStatus: 1, //开票项是否维护
  52. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  53. Sl: "1", //数量
  54. }},
  55. }
  56. if iType == "单位" {
  57. c.Gmfmc = gconv.String(m["company_name"])
  58. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  59. } else {
  60. c.Gmfmc = iType
  61. }
  62. err = im.Auth.MakeSingleInvoice(c)
  63. if err != nil {
  64. <-im.RunPool
  65. if gerror.Is(err, consts.LoginOutErr) {
  66. end = true
  67. g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
  68. return
  69. }
  70. g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
  71. continue
  72. }
  73. okNum++
  74. }
  75. return
  76. }
  77. // selfMakeInvoice 自助开票
  78. func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  79. var (
  80. res gdb.Result
  81. )
  82. res, err = g.DB().Query(ctx, "SELECT a.invoice_money,a.only_Identifying,a.invoice_type,a.company_name,a.taxpayer_identnum,a.remark FROM invoice a WHERE a.invoice_status=0 AND a.invoice_changed=0 AND a.invoice_variety like '%普通发票%' AND a.invoice_order_code like '%,%' AND a.create_time > ? GROUP BY invoice_order_code", consts.InvoiceStartTime.Unix())
  83. if err != nil {
  84. err = gerror.Wrap(err, "selfMakeInvoice-查询待开票异常")
  85. return
  86. }
  87. total, okNum = res.Len(), 0
  88. for _, m := range res.List() {
  89. select {
  90. case im.RunPool <- true:
  91. case <-time.After(time.Minute * 5):
  92. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  93. return
  94. }
  95. var (
  96. orderCode = gconv.String(m["only_Identifying"])
  97. iType = gconv.String(m["invoice_type"])
  98. prices = gconv.Float64(m["invoice_money"]) / float64(100)
  99. remark = gconv.String(m["remark"])
  100. )
  101. c := MakeInvoiceData{
  102. Type: "2",
  103. Id: fmt.Sprintf("only_Identifying:%s", orderCode),
  104. Notes: remark,
  105. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  106. InvoiceArr: []MakeInvoiceItems{{
  107. Xmmc: g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
  108. WhStatus: 1, //开票项是否维护
  109. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  110. Sl: "1", //数量
  111. }},
  112. }
  113. if iType == "单位" {
  114. c.Gmfmc = gconv.String(m["company_name"])
  115. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  116. } else {
  117. c.Gmfmc = iType
  118. }
  119. err = im.Auth.MakeSingleInvoice(c)
  120. if err != nil {
  121. <-im.RunPool
  122. if gerror.Is(err, consts.LoginOutErr) {
  123. g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
  124. end = true
  125. return
  126. }
  127. g.Log().Errorf(ctx, "selfMakeInvoice-开票接口调用异常 %v", err)
  128. continue
  129. }
  130. okNum++
  131. }
  132. return
  133. }
  134. // makeRedInvoice 红冲
  135. func (im *InvoiceManager) makeRedInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  136. var (
  137. res gdb.Result
  138. )
  139. //冲红任务
  140. res, err = g.DB().Query(ctx, "SELECT invoice_number,billing_time FROM invoice a WHERE a.invoice_status=0 AND a.invoice_changed=1 AND a.invoice_variety='普通发票(电子发票)' AND a.invoice_order_code is NULL AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
  141. if err != nil {
  142. err = gerror.Wrap(err, "selfMakeChangeInvoice-查询待冲红订单异常")
  143. return
  144. }
  145. total, okNum = res.Len(), 0
  146. for _, m := range res.List() {
  147. select {
  148. case im.RunPool <- true:
  149. case <-time.After(time.Minute * 5):
  150. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  151. return
  152. }
  153. var (
  154. invoiceNumber = gconv.String(m["invoice_number"])
  155. invoiceDate = time.Unix(gconv.Int64(m["billing_time"]), 0)
  156. )
  157. err = im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
  158. Num: invoiceNumber,
  159. Date: invoiceDate.Format(consts.DateFormat_Short),
  160. })
  161. if err != nil {
  162. <-im.RunPool
  163. if gerror.Is(err, consts.LoginOutErr) {
  164. g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
  165. end = true
  166. return
  167. }
  168. }
  169. okNum++
  170. }
  171. return
  172. }