invoiceCallback.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package service
  2. import (
  3. "ElectronicInvoice/internal/consts"
  4. "ElectronicInvoice/util"
  5. "context"
  6. "fmt"
  7. "github.com/gogf/gf/v2/errors/gerror"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/net/ghttp"
  10. "github.com/gogf/gf/v2/util/gconv"
  11. "strings"
  12. "time"
  13. )
  14. // InvoicingCallBackLogic 开票回调逻辑
  15. func InvoicingCallBackLogic(r *ghttp.Request) error {
  16. tType := r.Get("type").Int() //type [0 需要活体检测;1 成功 返回base64]
  17. switch tType {
  18. case 0: //需要活体检测
  19. if err := util.SendQrImage2ChatBot(r.Get("img").String()); err != nil {
  20. return gerror.Wrap(err, "发送活体检测消息出错")
  21. }
  22. g.Log().Info(r.Context(), "需要活体认证,已发送二维码消息")
  23. case 1: //开票成功
  24. var (
  25. pdfBase64 = r.Get("pdf").String()
  26. orderCode = r.Get("id").String()
  27. num = r.Get("num").String()
  28. kptime = r.Get("kptime").String()
  29. invoiceTime time.Time
  30. err error
  31. path string
  32. )
  33. if pdfBase64 == "" || orderCode == "" || num == "" || kptime == "" {
  34. return gerror.New("缺少回调参数")
  35. }
  36. invoiceTime, err = time.ParseInLocation(consts.DateFormat_Full, kptime, time.Local)
  37. if err != nil {
  38. return gerror.Wrap(err, "时间格式化异常")
  39. }
  40. path, err = util.SavePdfFile(r.Context(), num, pdfBase64)
  41. if err != nil {
  42. return gerror.Wrap(err, "保存pdf文件失败")
  43. }
  44. g.Log().Infof(r.Context(), "pdf保存成功 orderCode:%s filePath:%s ", orderCode, path)
  45. return updateOrderInvoiceStatus(r.Context(), orderCode, num, invoiceTime, path)
  46. case 3:
  47. g.Log().Info(r.Context(), "活体验证已过期,未完成活体验证")
  48. case 4:
  49. g.Log().Info(r.Context(), "系统错误")
  50. case 6:
  51. g.Log().Info(r.Context(), "税率不存在")
  52. case 7:
  53. g.Log().Info(r.Context(), "非数电票试点纳税人,未核定数电票票种,不允许开票或其他原因")
  54. default:
  55. g.Log().Info(r.Context(), "InvoicingCallBackLogic tType:%s", tType)
  56. }
  57. return nil
  58. }
  59. // updateOrderInvoiceStatus 开票成功进行数据库操作
  60. // orderCode 订单编号
  61. // invoiceNum 发票号码
  62. // createData 开票时间
  63. // pdfPath 下载地址
  64. func updateOrderInvoiceStatus(ctx context.Context, orderCode, invoiceNum string, invoiceTime time.Time, pdfPath string) error {
  65. var (
  66. queryItem = "order_code"
  67. err error
  68. )
  69. if strings.Contains(orderCode, "xx") {
  70. //自助开票
  71. queryItem = "only_Identifying"
  72. }
  73. _, err = g.DB().Update(ctx, "invoice", g.Map{
  74. "invoice_number": invoiceNum,
  75. "url": pdfPath,
  76. "billing_time": invoiceTime.Unix(),
  77. "invoice_status": 1,
  78. }, fmt.Sprintf("invoice_changed = 0 AND %s = ? ", queryItem), orderCode)
  79. if err != nil {
  80. return gerror.Wrap(err, "更新发票状态异常")
  81. }
  82. // 王浩说开票成功 订单状态不用更新;以下代码来自王浩
  83. if strings.Contains(orderCode, "xx") {
  84. selfInvoicing(ctx, orderCode)
  85. }
  86. if err := SendInvoiceSuccessMail(ctx, orderCode); err != nil {
  87. g.Log().Errorf(ctx, "%s 发送邮件失败 err: %v ", orderCode, err)
  88. }
  89. return nil
  90. }
  91. // selfInvoicing 自助开票
  92. func selfInvoicing(ctx context.Context, orderCode string) {
  93. //自助开票
  94. res, err := g.DB().GetOne(ctx, "SELECT GROUP_CONCAT(order_code) as order_code from invoice where only_Identifying=?", orderCode)
  95. if err != nil {
  96. g.Log().Errorf(ctx, "自助开票异常")
  97. }
  98. orderArr := strings.Split(gconv.String(res["order_code"]), ",")
  99. for _, v := range orderArr {
  100. if v == "" {
  101. continue
  102. }
  103. updateData := map[string]interface{}{}
  104. if InvoiceStatusHandle(ctx, v) {
  105. updateData["applybill_status"] = 2
  106. } else {
  107. updateData["applybill_status"] = 3
  108. }
  109. _, err := g.DB().Update(ctx, "dataexport_order", map[string]interface{}{
  110. "order_code": v,
  111. }, updateData)
  112. if err != nil {
  113. g.Log().Errorf(ctx, "自助开票更新订单异常 orderCode:%v %v", v, err)
  114. }
  115. }
  116. }
  117. // InvoiceStatusHandle 判断是全额还是部分回款
  118. // auth :王浩
  119. func InvoiceStatusHandle(ctx context.Context, orderCode string) bool {
  120. orderData, err := g.DB().GetOne(ctx, "SELECT pay_money FROM dataexport_order WHERE order_code=?", orderCode)
  121. if err != nil || orderData.IsEmpty() {
  122. return false
  123. }
  124. invoiceData, err := g.DB().GetOne(ctx, "select sum(invoice_order_money) as money from invoice where order_code=? and invoice_status>=0 ", orderCode)
  125. if err != nil || invoiceData.IsEmpty() {
  126. return false
  127. }
  128. allMoney := gconv.Int64(invoiceData["money"])
  129. pay_money := gconv.Int64(orderData["pay_money"])
  130. if pay_money == allMoney {
  131. return true
  132. }
  133. return false
  134. }