ソースを参照

wip:自助开票代码提交

wangkaiyue 1 年間 前
コミット
4bbfec2697
1 ファイル変更64 行追加18 行削除
  1. 64 18
      internal/service/invoiceManager.go

+ 64 - 18
internal/service/invoiceManager.go

@@ -111,16 +111,16 @@ func (im *InvoiceManager) RunJob(ctx context.Context) {
 		g.Log().Infof(ctx, "RunJob-开蓝票任务完成 共%d个 完成%d个", total, okNum)
 	}
 	// TODO 蓝票自助开票(管理后台扫码开票)
-	//total, okNum, err = im.selfMakeInvoice(ctx)
-	//if err != nil {
-	//	if gerror.Is(err, consts.LoginOutErr) {
-	//		g.Log().Infof(ctx, "RunJob-任务中止-身份过期,需要重新登录")
-	//		return
-	//	}
-	//	g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
-	//} else {
-	//	g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
-	//}
+	total, okNum, err = im.selfMakeInvoice(ctx)
+	if err != nil {
+		if gerror.Is(err, consts.LoginOutErr) {
+			g.Log().Infof(ctx, "RunJob-任务中止-身份过期,需要重新登录")
+			return
+		}
+		g.Log().Errorf(ctx, "RunJob-开自助蓝票任务异常 %v", err)
+	} else {
+		g.Log().Infof(ctx, "RunJob-开自助蓝票任务完成 共%d个 完成%d个", total, okNum)
+	}
 
 	//TODO 红票任务
 
@@ -131,25 +131,22 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 	var (
 		res gdb.Result
 	)
-
 	//查询需要开票的数据
 	res, err = g.DB().Query(ctx, "SELECT a.*,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_status=0 AND a.invoice_changed=0 AND  a.invoice_variety='普通发票(电子发票)' AND a.invoice_order_code is NULL")
 	if err != nil {
-		g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-查询待开票异常 %s", err)
 		return -1, -1, gerror.Wrap(err, "simpleMakeInvoice-查询待开票异常")
 	}
-	g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
+	g.Log().Infof(ctx, "simpleMakeInvoice-本次共加载%d条开票记录", res.Len())
 	total, okNum = res.Len(), 0
 	for _, m := range res.List() {
 		select {
 		case im.runPool <- true:
 		case <-time.After(time.Minute * 5):
-			g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票等待超时,结束此次任务")
+			err = gerror.New("simpleMakeInvoice-开票等待超时")
 			return
 		}
 		var (
 			orderCode = gconv.String(m["order_code"])
-			phone     = gconv.String(m["phone_num"])
 			iType     = gconv.String(m["invoice_type"])
 			prices    float64
 		)
@@ -165,7 +162,6 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 		c := MakeInvoiceData{
 			Type: "2",
 			Id:   orderCode,
-			Lxdh: phone,
 			Fhr:  g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
 			InvoiceArr: []MakeInvoiceItems{{
 				Xmmc:     g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
@@ -185,10 +181,10 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 		if err != nil {
 			im.ReleasePool()
 			if gerror.Is(err, consts.LoginOutErr) {
-				g.Log().Infof(ctx, "RunJob-simpleMakeInvoice-身份过期,需要重新登录")
+				g.Log().Infof(ctx, "simpleMakeInvoice-身份过期,需要重新登录")
 				return
 			}
-			g.Log().Errorf(ctx, "RunJob-simpleMakeInvoice-开票接口调用异常 %v", err)
+			g.Log().Errorf(ctx, "simpleMakeInvoice-开票接口调用异常 %v", err)
 			continue
 		}
 		okNum++
@@ -198,5 +194,55 @@ func (im *InvoiceManager) simpleMakeInvoice(ctx context.Context) (total, okNum i
 
 // selfMakeInvoice 自助开票
 func (im *InvoiceManager) selfMakeInvoice(ctx context.Context) (total, okNum int, err error) {
+	var (
+		res gdb.Result
+	)
+	res, err = g.DB().Query(ctx, "SELECT a.invoice_money,a.only_Identifying,a.invoice_type,a.company_name,a.taxpayer_identnum FROM invoice a WHERE a.invoice_status=0 AND a.invoice_changed=0 AND  a.invoice_variety='电子普通发票' AND a.invoice_order_code is not NULL GROUP BY invoice_order_code")
+	if err != nil {
+		return -1, -1, gerror.Wrap(err, "selfMakeInvoice-查询待开票异常")
+	}
+	total, okNum = res.Len(), 0
+	for _, m := range res.List() {
+		select {
+		case im.runPool <- true:
+		case <-time.After(time.Minute * 5):
+			err = gerror.New("selfMakeInvoice-开票等待超时")
+			return
+		}
+		var (
+			orderCode = gconv.String(m["only_Identifying"])
+			iType     = gconv.String(m["invoice_type"])
+			prices    = gconv.String(m["invoice_money"])
+		)
+		c := MakeInvoiceData{
+			Type: "2",
+			Id:   orderCode,
+			Fhr:  g.Cfg().MustGet(ctx, "company.hfr", "贺鹏飞").String(),
+			InvoiceArr: []MakeInvoiceItems{{
+				Xmmc:     g.Cfg().MustGet(ctx, "company.taxCode").String(), //开票项
+				WhStatus: 1,                                                //开票项是否维护
+				Je:       prices,                                           //金额
+				Sl:       "1",                                              //数量
+			}},
+		}
+		if iType == "单位" {
+			c.Gmfmc = gconv.String(m["company_name"])
+			c.Gmfnsrsbh = gconv.String(m["taxpayer_identnum"])
+		} else {
+			c.Gmfmc = iType
+		}
+
+		err = im.Auth.MakeSingleInvoice(c)
+		if err != nil {
+			im.ReleasePool()
+			if gerror.Is(err, consts.LoginOutErr) {
+				g.Log().Infof(ctx, "selfMakeInvoice-身份过期,需要重新登录")
+				return
+			}
+			g.Log().Errorf(ctx, "selfMakeInvoice-开票接口调用异常 %v", err)
+			continue
+		}
+		okNum++
+	}
 	return
 }