invoiceMake.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. "strings"
  12. "time"
  13. )
  14. // simpleMakeInvoice 简单开票(包含一个订单开多张票)
  15. func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  16. var (
  17. res gdb.Result
  18. )
  19. //查询需要开票的数据
  20. //(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
  21. //线上申请发票【invoice_order_code is null 】, 管理后台一个订单拆分多个发票【invoice_order_code not like '%,%'】
  22. res, err = g.DB().Query(ctx, "SELECT a.id,a.invoice_type,a.remark,a.invoice_variety,a.taxpayer_identnum,a.company_name,a.invoice_content,a.invoice_money,b.pay_money FROM invoice a INNER JOIN dataexport_order b ON a.order_code=b.order_code WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1 )) AND (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
  23. if err != nil {
  24. err = gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
  25. return
  26. }
  27. total, okNum = res.Len(), 0
  28. for _, m := range res.List() {
  29. select {
  30. case im.RunPool <- true:
  31. case <-time.After(time.Minute * 5):
  32. err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
  33. return
  34. }
  35. var (
  36. iType = gconv.String(m["invoice_type"])
  37. remark = gconv.String(m["remark"])
  38. invoiceVariety = gconv.String(m["invoice_variety"])
  39. invoiceContent = gconv.String(m["invoice_content"])
  40. prices = gconv.Float64(m["invoice_money"]) / float64(100)
  41. )
  42. if prices == 0 { //开票金额为空,取支付金额
  43. prices = gconv.Float64(m["pay_money"]) / float64(100)
  44. }
  45. c := MakeInvoiceData{
  46. Type: "2",
  47. Id: fmt.Sprintf("id:%d", gconv.Int64(m["id"])), //因为orderCode不唯一,此处禁止使用orderCode
  48. Notes: remark,
  49. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  50. InvoiceArr: []MakeInvoiceItems{{
  51. Xmmc: consts.GetTaxCodeByName(invoiceContent), //开票项
  52. WhStatus: 1, //开票项是否维护
  53. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  54. Sl: "1", //数量
  55. }},
  56. }
  57. // 1 增值税专用发票;2 普通发票
  58. if strings.Contains(invoiceVariety, "专用") {
  59. c.Type = "1"
  60. }
  61. if iType == "单位" {
  62. c.Gmfmc = gconv.String(m["company_name"])
  63. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  64. } else {
  65. c.Gmfmc = iType
  66. }
  67. err = im.Auth.MakeSingleInvoice(c)
  68. if err != nil {
  69. <-im.RunPool
  70. if gerror.Is(err, consts.LoginOutErr) {
  71. end = true
  72. g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
  73. return
  74. }
  75. g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
  76. continue
  77. }
  78. okNum++
  79. }
  80. return
  81. }
  82. // multipleOrdersMakeInvoice 联合开票(多个订单开一张发票)
  83. func (im *InvoiceManager) multipleOrdersMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  84. var (
  85. res gdb.Result
  86. )
  87. //(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
  88. res, err = g.DB().Query(ctx, "SELECT a.only_Identifying,a.invoice_type,a.remark,a.invoice_variety,a.taxpayer_identnum,a.company_name,a.invoice_content,a.invoice_money FROM invoice a WHERE a.invoice_variety like '%电子%' AND ((a.invoice_status=0 AND a.invoice_changed=0) or (a.invoice_status=2 AND a.invoice_changed=1)) AND a.invoice_order_code like '%,%' AND a.create_time > ? GROUP BY invoice_order_code", consts.InvoiceStartTime.Unix())
  89. if err != nil {
  90. err = gerror.Wrap(err, "multipleOrdersMakeInvoice-查询待开票异常")
  91. return
  92. }
  93. total, okNum = res.Len(), 0
  94. for _, m := range res.List() {
  95. select {
  96. case im.RunPool <- true:
  97. case <-time.After(time.Minute * 5):
  98. err = gerror.Wrap(consts.WaitTimeOut, "multipleOrdersMakeInvoice-开票等待超时")
  99. return
  100. }
  101. var (
  102. onlyIdentifying = gconv.String(m["only_Identifying"])
  103. iType = gconv.String(m["invoice_type"])
  104. prices = gconv.Float64(m["invoice_money"]) / float64(100)
  105. remark = gconv.String(m["remark"])
  106. invoiceVariety = gconv.String(m["invoice_variety"])
  107. invoiceContent = gconv.String(m["invoice_content"])
  108. )
  109. c := MakeInvoiceData{
  110. Type: "2",
  111. Id: fmt.Sprintf("only_Identifying:%s", onlyIdentifying),
  112. Notes: remark,
  113. Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
  114. InvoiceArr: []MakeInvoiceItems{{
  115. Xmmc: consts.GetTaxCodeByName(invoiceContent), //开票项
  116. WhStatus: 1, //开票项是否维护
  117. Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
  118. Sl: "1", //数量
  119. }},
  120. }
  121. // 1 增值税专用发票;2 普通发票
  122. if strings.Contains(invoiceVariety, "专用") {
  123. c.Type = "1"
  124. }
  125. if iType == "单位" {
  126. c.Gmfmc = gconv.String(m["company_name"])
  127. c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
  128. } else {
  129. c.Gmfmc = iType
  130. }
  131. err = im.Auth.MakeSingleInvoice(c)
  132. if err != nil {
  133. <-im.RunPool
  134. if gerror.Is(err, consts.LoginOutErr) {
  135. g.Log().Infof(ctx, "multipleOrdersMakeInvoice-身份过期,需要重新登录")
  136. end = true
  137. return
  138. }
  139. g.Log().Errorf(ctx, "multipleOrdersMakeInvoice-开票接口调用异常 %v", err)
  140. continue
  141. }
  142. okNum++
  143. }
  144. return
  145. }
  146. // makeRedInvoice 单票红冲(包含一个订单开多张票)
  147. func (im *InvoiceManager) makeRedInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  148. var (
  149. res gdb.Result
  150. )
  151. //冲红任务
  152. res, err = g.DB().Query(ctx, "SELECT invoice_number,billing_time FROM invoice a WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND (a.invoice_order_code is NULL OR a.invoice_order_code not like '%,%') AND a.create_time > ? ", consts.InvoiceStartTime.Unix())
  153. if err != nil {
  154. err = gerror.Wrap(err, "makeRedInvoice-查询待冲红订单异常")
  155. return
  156. }
  157. total, okNum = res.Len(), 0
  158. for _, m := range res.List() {
  159. select {
  160. case im.RunPool <- true:
  161. case <-time.After(time.Minute * 5):
  162. err = gerror.Wrap(consts.WaitTimeOut, "makeRedInvoice-开票等待超时")
  163. return
  164. }
  165. var (
  166. invoiceNumber = gconv.String(m["invoice_number"])
  167. invoiceDate = time.Unix(gconv.Int64(m["billing_time"]), 0)
  168. )
  169. err = im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
  170. Num: invoiceNumber,
  171. Date: invoiceDate.Format(consts.DateFormat_Short),
  172. })
  173. if err != nil {
  174. <-im.RunPool
  175. if gerror.Is(err, consts.LoginOutErr) {
  176. g.Log().Infof(ctx, "makeRedInvoice-身份过期,需要重新登录")
  177. end = true
  178. return
  179. }
  180. }
  181. okNum++
  182. }
  183. return
  184. }
  185. // multipleOrdersMakeRedInvoice 联合开票红冲(多个订单开一张发票)
  186. func (im *InvoiceManager) multipleOrdersMakeRedInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
  187. var (
  188. res gdb.Result
  189. )
  190. //冲红任务
  191. res, err = g.DB().Query(ctx, "SELECT invoice_number,billing_time FROM invoice a WHERE a.invoice_variety like '%电子%' AND a.invoice_status=0 AND a.invoice_changed=1 AND a.invoice_order_code like '%,%' AND a.create_time > ? GROUP BY invoice_order_code", consts.InvoiceStartTime.Unix())
  192. if err != nil {
  193. err = gerror.Wrap(err, "multipleOrdersMakeRedInvoice-查询待冲红订单异常")
  194. return
  195. }
  196. total, okNum = res.Len(), 0
  197. for _, m := range res.List() {
  198. select {
  199. case im.RunPool <- true:
  200. case <-time.After(time.Minute * 5):
  201. err = gerror.Wrap(consts.WaitTimeOut, "multipleOrdersMakeRedInvoice-开票等待超时")
  202. return
  203. }
  204. var (
  205. invoiceNumber = gconv.String(m["invoice_number"])
  206. invoiceDate = time.Unix(gconv.Int64(m["billing_time"]), 0)
  207. )
  208. err = im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
  209. Num: invoiceNumber,
  210. Date: invoiceDate.Format(consts.DateFormat_Short),
  211. })
  212. if err != nil {
  213. <-im.RunPool
  214. if gerror.Is(err, consts.LoginOutErr) {
  215. g.Log().Infof(ctx, "multipleOrdersMakeRedInvoice-身份过期,需要重新登录")
  216. end = true
  217. return
  218. }
  219. }
  220. okNum++
  221. }
  222. return
  223. }