123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package service
- import (
- "ElectronicInvoice/internal/consts"
- "context"
- "fmt"
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/errors/gerror"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/util/gconv"
- "strconv"
- "strings"
- "time"
- )
- // simpleMakeInvoice 简单开票(包含一个订单开多张票)
- func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
- var (
- res gdb.Result
- )
- //查询需要开票的数据
- //(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
- //线上申请发票【invoice_order_code is null 】, 管理后台一个订单拆分多个发票【invoice_order_code not like '%,%'】
- res, err = g.DB().Query(ctx, "SELECT a.*,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())
- if err != nil {
- err = gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
- return
- }
- total, okNum = res.Len(), 0
- for _, m := range res.List() {
- select {
- case im.RunPool <- true:
- case <-time.After(time.Minute * 5):
- err = gerror.Wrap(consts.WaitTimeOut, "simpleMakeInvoice-开票等待超时")
- return
- }
- var (
- iType = gconv.String(m["invoice_type"])
- remark = gconv.String(m["remark"])
- invoiceVariety = gconv.String(m["invoice_variety"])
- invoiceContent = gconv.String(m["invoice_content"])
- prices = gconv.Float64(m["invoice_money"]) / float64(100)
- id = gconv.Int64(m["id"])
- )
- if prices == 0 { //开票金额为空,取支付金额
- prices = gconv.Float64(m["pay_money"]) / float64(100)
- }
- c := MakeInvoiceData{
- Type: "2",
- Id: fmt.Sprintf("id:%d", id), //因为orderCode不唯一,此处禁止使用orderCode
- Notes: remark,
- Lxdh: gconv.String(m["phone"]),
- Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
- Xsfkhh: g.Cfg().MustGet(ctx, "company.bank", "").String(),
- Xsfyhzh: g.Cfg().MustGet(ctx, "company.bankNum", "").String(),
- InvoiceArr: []MakeInvoiceItems{{
- Xmmc: consts.GetTaxCodeByName(invoiceContent), //开票项
- WhStatus: 1, //开票项是否维护
- Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
- Sl: "1", //数量
- }},
- }
- // 1 增值税专用发票;2 普通发票
- if strings.Contains(invoiceVariety, "专用") {
- c.Type = "1"
- }
- if iType == "单位" {
- c.Gmfmc = gconv.String(m["company_name"])
- c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
- c.Gmfdz = gconv.String(m["company_address"]) //"购买方地址"
- c.Yhyywdmc = gconv.String(m["bank_name"]) //"购买方开户行"
- c.Yhzh = gconv.String(m["bank_account"]) //"购买方银行账号"
- } else {
- c.Gmfmc = iType
- }
- err = im.Auth.MakeSingleInvoice(c)
- if err != nil {
- <-im.RunPool
- if gerror.Is(err, consts.LoginOutErr) {
- end = true
- g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
- return
- } else if strings.Contains(err.Error(), "税号有误") { //发票状态改为-1
- invoiceErr(ctx, g.Map{"id": id})
- g.Log().Infof(ctx, "simpleMakeInvoice-开票异常-税号异常")
- } else {
- g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
- }
- } else {
- okNum++
- }
- }
- return
- }
- // multipleOrdersMakeInvoice 联合开票(多个订单开一张发票)
- func (im *InvoiceManager) multipleOrdersMakeInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
- var (
- res gdb.Result
- )
- //(新开发票【a.invoice_status=0 AND a.invoice_changed=0】,红冲后新开【a.invoice_status=2 AND a.invoice_changed=1】)
- 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,a.company_address,a.phone,a.bank_account,a.bank_name 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 only_Identifying", consts.InvoiceStartTime.Unix())
- if err != nil {
- err = gerror.Wrap(err, "multipleOrdersMakeInvoice-查询待开票异常")
- return
- }
- total, okNum = res.Len(), 0
- for _, m := range res.List() {
- select {
- case im.RunPool <- true:
- case <-time.After(time.Minute * 5):
- err = gerror.Wrap(consts.WaitTimeOut, "multipleOrdersMakeInvoice-开票等待超时")
- return
- }
- var (
- onlyIdentifying = gconv.String(m["only_Identifying"])
- iType = gconv.String(m["invoice_type"])
- prices = gconv.Float64(m["invoice_money"]) / float64(100)
- remark = gconv.String(m["remark"])
- invoiceVariety = gconv.String(m["invoice_variety"])
- invoiceContent = gconv.String(m["invoice_content"])
- )
- if onlyIdentifying == "" {
- continue
- }
- c := MakeInvoiceData{
- Type: "2",
- Id: fmt.Sprintf("only_Identifying:%s", onlyIdentifying),
- Notes: remark,
- Lxdh: gconv.String(m["phone"]),
- Fhr: g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
- Xsfkhh: g.Cfg().MustGet(ctx, "company.bank", "").String(),
- Xsfyhzh: g.Cfg().MustGet(ctx, "company.bankNum", "").String(),
- InvoiceArr: []MakeInvoiceItems{{
- Xmmc: consts.GetTaxCodeByName(invoiceContent), //开票项
- WhStatus: 1, //开票项是否维护
- Je: strconv.FormatFloat(prices, 'f', -1, 64), //金额
- Sl: "1", //数量
- }},
- }
- // 1 增值税专用发票;2 普通发票
- if strings.Contains(invoiceVariety, "专用") {
- c.Type = "1"
- }
- if iType == "单位" {
- c.Gmfmc = gconv.String(m["company_name"])
- c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
- c.Gmfdz = gconv.String(m["company_address"]) //"购买方地址"
- c.Yhyywdmc = gconv.String(m["bank_name"]) //"购买方开户行"
- c.Yhzh = gconv.String(m["bank_account"]) //"购买方银行账号"
- } else {
- c.Gmfmc = iType
- }
- err = im.Auth.MakeSingleInvoice(c)
- if err != nil {
- <-im.RunPool
- if gerror.Is(err, consts.LoginOutErr) {
- g.Log().Infof(ctx, "multipleOrdersMakeInvoice-身份过期,需要重新登录")
- end = true
- return
- } else if strings.Contains(err.Error(), "税号有误") { //发票状态改为-1
- invoiceErr(ctx, g.Map{"only_Identifying": onlyIdentifying})
- g.Log().Infof(ctx, "multipleOrdersMakeInvoice-开票异常-税号异常")
- } else {
- g.Log().Errorf(ctx, "multipleOrdersMakeInvoice-开票接口调用异常 %v", err)
- }
- } else {
- okNum++
- }
- }
- return
- }
- // makeRedInvoice 单票红冲(包含一个订单开多张票)
- func (im *InvoiceManager) makeRedInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
- var (
- res gdb.Result
- )
- //冲红任务
- 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())
- if err != nil {
- err = gerror.Wrap(err, "makeRedInvoice-查询待冲红订单异常")
- return
- }
- total, okNum = res.Len(), 0
- for _, m := range res.List() {
- select {
- case im.RunPool <- true:
- case <-time.After(time.Minute * 5):
- err = gerror.Wrap(consts.WaitTimeOut, "makeRedInvoice-开票等待超时")
- return
- }
- var (
- invoiceNumber = gconv.String(m["invoice_number"])
- invoiceDate = time.Unix(gconv.Int64(m["billing_time"]), 0)
- )
- err = im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
- Num: invoiceNumber,
- Date: invoiceDate.Format(consts.DateFormat_Short),
- })
- if err != nil {
- <-im.RunPool
- if gerror.Is(err, consts.LoginOutErr) {
- g.Log().Infof(ctx, "makeRedInvoice-身份过期,需要重新登录")
- end = true
- return
- }
- } else {
- okNum++
- }
- }
- return
- }
- // multipleOrdersMakeRedInvoice 联合开票红冲(多个订单开一张发票)
- func (im *InvoiceManager) multipleOrdersMakeRedInvoice(ctx context.Context) (total, okNum int, end bool, err error) {
- var (
- res gdb.Result
- )
- //冲红任务
- 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 only_Identifying", consts.InvoiceStartTime.Unix())
- if err != nil {
- err = gerror.Wrap(err, "multipleOrdersMakeRedInvoice-查询待冲红订单异常")
- return
- }
- total, okNum = res.Len(), 0
- for _, m := range res.List() {
- select {
- case im.RunPool <- true:
- case <-time.After(time.Minute * 5):
- err = gerror.Wrap(consts.WaitTimeOut, "multipleOrdersMakeRedInvoice-开票等待超时")
- return
- }
- var (
- invoiceNumber = gconv.String(m["invoice_number"])
- invoiceDate = time.Unix(gconv.Int64(m["billing_time"]), 0)
- )
- err = im.Auth.MakeSingleRedInvoice(MakeRedInvoiceData{
- Num: invoiceNumber,
- Date: invoiceDate.Format(consts.DateFormat_Short),
- })
- if err != nil {
- <-im.RunPool
- if gerror.Is(err, consts.LoginOutErr) {
- g.Log().Infof(ctx, "multipleOrdersMakeRedInvoice-身份过期,需要重新登录")
- end = true
- return
- }
- } else {
- okNum++
- }
- }
- return
- }
- // invoiceErr 开票异常
- func invoiceErr(ctx context.Context, query map[string]interface{}) {
- _, err := g.DB().Update(ctx, "invoice", g.Map{
- "invoice_status": -1,
- }, query)
- if err != nil {
- g.Log().Errorf(ctx, "更改开票失败-状态异常 %+v", query)
- }
- }
|