invoiceAdd.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package controller
  2. import (
  3. "ElectronicInvoice/internal/service/tripartite"
  4. "github.com/gogf/gf/v2/errors/gerror"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/net/ghttp"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. )
  9. type (
  10. addInvoiceAddParam struct {
  11. OrderCode string `json:"Swno"` //orderCode订单号
  12. CustType string `json:"custType"` //购货方企业类型 01:企业 02:机关执业单位 03:个人 04:其他
  13. CustTaxNo string `json:"custTaxNo"` //纳税人税号
  14. Phone string `json:"phone"` //手机号
  15. BillDate string `json:"billDate"` //单据日期 格式:yyyy-MM-dd HH:mm:ss
  16. CustName string `json:"custName"` //购方名称
  17. Orders []*orders `json:"orders"` //发票内容
  18. }
  19. orders struct {
  20. BillNo string `json:"billNo"` //订单号
  21. Items []*items `json:"items"`
  22. }
  23. items struct {
  24. Name string `json:"name"` //商品名称
  25. Code string `json:"code"` //商品编号(税收分类编码)
  26. Yhzcbs string `json:"yhzcbs"` //享受税收优惠政策内容
  27. LineType string `json:"lineType"` //发票行性质 0:正常行1:折扣行2:被折扣行
  28. TaxRate string `json:"taxRate"` //税率
  29. TaxPrice string `json:"taxPrice"` //单价
  30. TotalAmount string `json:"totalAmount"` //含税金额
  31. Quantity string `json:"quantity"` //数量
  32. }
  33. addInvoiceAddResp struct {
  34. Code int `json:"code"` //0 开票成功;2 开票中 ;其他开票失败
  35. Msg interface{} `json:"msg,omitempty"`
  36. Data addInvoiceAddData `json:"data,omitempty"` //异步开票,通过回调通知
  37. }
  38. addInvoiceAddData struct {
  39. Swno string `json:"swno"` //流水号 invoice_serialnum
  40. Fpdm string `json:"fpdm"` //发票代码 invoice_code
  41. Fphm string `json:"fphm"` //发票号码 invoice_number
  42. Path string `json:"path"` //pdf地址 url
  43. }
  44. )
  45. // InvoiceAdd 开票
  46. func InvoiceAdd(r *ghttp.Request) {
  47. var param *addInvoiceAddParam
  48. err := func() error {
  49. err := gconv.Struct(r.GetBody(), param)
  50. if err != nil {
  51. return gerror.Wrap(err, "获取参数异常")
  52. }
  53. // 存入开票记录表
  54. // 调用第三方开票接口
  55. if err := tripartite.MakeSingleInvoice(tripartite.MakeInvoiceData{
  56. Type: "2",
  57. Gmfmc: param.CustName,
  58. Gmfnsrsbh: param.CustTaxNo,
  59. Id: param.OrderCode,
  60. //Gmfdz: "北京市朝阳区安定路5号院13号楼B座12层1201室",
  61. Lxdh: param.Phone,
  62. //Yhyywdmc: "郑州交通银行总行",
  63. //Yhzh: "6320123123000121",
  64. InvoiceArr: []tripartite.MakeInvoiceItems{{
  65. Xmmc: "0fccdac71c36a8552ba662e7a2f42726",
  66. WhStatus: 1,
  67. Je: param.Orders[0].Items[0].TaxPrice,
  68. Sl: "1",
  69. }},
  70. }); err != nil {
  71. return gerror.Wrapf(err, "调用开票接口异常%v", err)
  72. }
  73. return nil
  74. }()
  75. if err != nil {
  76. g.Log().Errorf(r.Context(), "处理开票业务异常")
  77. r.Response.Write(addInvoiceAddResp{
  78. Code: -1,
  79. Msg: err.Error(),
  80. })
  81. return
  82. }
  83. r.Response.Write(addInvoiceAddResp{
  84. Code: 0,
  85. Msg: "开票中",
  86. })
  87. }