invoiceMake.go 8.5 KB

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