Browse Source

Merge branch 'dev/v4.9.21_dx' of qmx/jy into feature/v4.9.21

duxin 1 year ago
parent
commit
3a3859bf7b
1 changed files with 219 additions and 10 deletions
  1. 219 10
      src/jfw/modules/subscribepay/src/service/invoice.go

+ 219 - 10
src/jfw/modules/subscribepay/src/service/invoice.go

@@ -13,6 +13,7 @@ import (
 	"encoding/json"
 	"encoding/json"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
+	"github.com/google/uuid"
 	"io/ioutil"
 	"io/ioutil"
 	"jy/src/jfw/modules/subscribepay/src/config"
 	"jy/src/jfw/modules/subscribepay/src/config"
 	"jy/src/jfw/modules/subscribepay/src/util"
 	"jy/src/jfw/modules/subscribepay/src/util"
@@ -26,16 +27,16 @@ import (
 
 
 type Invoice struct {
 type Invoice struct {
 	*xweb.Action
 	*xweb.Action
-	addinvoice      xweb.Mapper `xweb:"/invoice/add"`       //开发票
-	showinvoice     xweb.Mapper `xweb:"/invoice/show"`      //查看发票
-	callbackinvoice xweb.Mapper `xweb:"/invoice/callback"`  //开发票回调
-	replaceinvoice  xweb.Mapper `xweb:"/invoice/replace"`   //换发票
-	refundinvoice   xweb.Mapper `xweb:"/invoice/refund"`    //退票
-	invoiceinfo     xweb.Mapper `xweb:"/invoice/info"`      //订单详情数据
-	available       xweb.Mapper `xweb:"/invoice/available"` //是否可用开发票
-	invoiceswitch   xweb.Mapper `xweb:"/invoice/switch"`    //发票开关
-	invoiceQuery    xweb.Mapper `xweb:"/invoice/query"`     //发票信息查询
-	invoiceSubmit   xweb.Mapper `xweb:"/invoice/submit"`    //发票信息提交
+	addinvoice        xweb.Mapper `xweb:"/invoice/add"`       //开发票
+	showinvoice       xweb.Mapper `xweb:"/invoice/show"`      //查看发票
+	callbackinvoice   xweb.Mapper `xweb:"/invoice/callback"`  //开发票回调
+	newReplaceinvoice xweb.Mapper `xweb:"/invoice/replace"`   //换发票
+	refundinvoice     xweb.Mapper `xweb:"/invoice/refund"`    //退票
+	invoiceinfo       xweb.Mapper `xweb:"/invoice/info"`      //订单详情数据
+	available         xweb.Mapper `xweb:"/invoice/available"` //是否可用开发票
+	invoiceswitch     xweb.Mapper `xweb:"/invoice/switch"`    //发票开关
+	invoiceQuery      xweb.Mapper `xweb:"/invoice/query"`     //发票信息查询
+	invoiceSubmit     xweb.Mapper `xweb:"/invoice/submit"`    //发票信息提交
 }
 }
 
 
 var dbname = "invoice"
 var dbname = "invoice"
@@ -440,6 +441,61 @@ func (this *Invoice) Showinvoice() {
 
 
 }
 }
 
 
+// 查看发票
+func (this *Invoice) ShowinvoiceList() {
+	order_code := this.GetString("order_code")
+	//红冲发票开关
+	redSwitchInfo := map[string]interface{}{
+		"ris": config.InvoiceConfig.Red_invoice_switch,
+		"rim": config.InvoiceConfig.Red_invoice_msg,
+	}
+	invoices := util.Mysql.SelectBySql(fmt.Sprintf(`SELECT * FROM invoice WHERE order_code = '%s'`, order_code))
+	if invoices != nil {
+		isIos := util.IsMobileIOS(this.Request.UserAgent())
+		var invoiceData []map[string]interface{}
+		for _, data := range *invoices {
+			delete(data, "user_id")
+			var changed bool
+			if data["only_Identifying"] != nil {
+				//查询是否换票
+				changed = util.Mysql.CountBySql(`SELECT count(*) FROM invoice   WHERE order_code = '?' and invoice_status in (0,1) and only_Identifying = ?`, order_code, data["only_Identifying"]) > 1
+			}
+			//if changed && data["invoice_status"] == 0 { //此纪录为换票中的发票
+			//	continue
+			//}
+			data["changed"] = changed
+			data["status"] = "processing"
+			if url := qutil.InterfaceToStr(data["url"]); url != "" {
+				data["status"] = "success"
+				//ios 移动端
+				if isIos {
+					//国家税务局-增值税电子发票公共服务平台
+					if strings.Contains(url, "tysl.beijing.chinatax") {
+						data["url"] = strings.ReplaceAll(url, "/preview.html?code=", "/web-reader/reader?file=")
+					}
+				}
+			}
+			invoiceData = append(invoiceData, data)
+		}
+		this.ServeJson(map[string]interface{}{
+			"invoiceData":   invoiceData,
+			"redSwitchInfo": redSwitchInfo,
+		})
+	} else {
+		data := util.Mysql.FindOne("dataexport_order", map[string]interface{}{
+			"order_code": order_code,
+		}, "order_status,applybill_status,order_code,user_mail,applybill_type,applybill_company,user_phone,applybill_taxnum", "")
+		if data != nil {
+			(*data)["status"] = "fail"
+		}
+
+		this.ServeJson(map[string]interface{}{
+			"orderData":     data,
+			"redSwitchInfo": redSwitchInfo,
+		})
+	}
+}
+
 // 开发票回调
 // 开发票回调
 func (this *Invoice) Callbackinvoice() {
 func (this *Invoice) Callbackinvoice() {
 	isRed, _ := this.GetBool("isRed")
 	isRed, _ := this.GetBool("isRed")
@@ -1003,6 +1059,159 @@ func (this *Invoice) Replaceinvoice() {
 
 
 }
 }
 
 
+// 新换票
+func (this *Invoice) NewReplaceinvoice() {
+	//红冲开关
+	if !config.InvoiceConfig.Red_invoice_switch {
+		this.ServeJson(map[string]interface{}{
+			"invoice_status": -1,
+			"invoice_msg":    config.InvoiceConfig.Red_invoice_msg,
+		})
+		return
+	}
+	invoice_type := this.GetString("invoice_type")
+	phone := this.GetString("phone_num")
+	e_mail := this.GetString("email")
+	company_name := this.GetString("company")
+	id_num := strings.ToUpper(this.GetString("id_num")) //纳税人识别号
+	invoice_status := 0
+	userId := qutil.ObjToString(this.GetSession("userId"))
+	//order_code := this.GetString("order_code")
+	sid := this.GetString("sid")
+	oRes := util.Mysql.FindOne(dbname, map[string]interface{}{"id": sid, "user_id": userId, "invoie_changed": 0}, "", "") //仅线上开发票可换票
+	if oRes != nil && len(*oRes) > 0 {
+		var (
+			qSql    string
+			errCode []string
+			//saveData []map[string]interface{}
+			resArr []map[string]interface{}
+		)
+		if qutil.IntAll((*oRes)["invoice_status"]) == -2 {
+			this.ServeJson(map[string]interface{}{
+				"error_code": -1,
+				"error_msg":  "发票状态错误",
+			})
+			return
+		}
+		if invoice_order_code := qutil.InterfaceToStr((*oRes)["invoice_order_code"]); invoice_order_code == "" ||
+			len(strings.Split(invoice_order_code, ",")) <= 1 {
+			resArr = append(resArr, *oRes)
+		} else { //多订单联合开票
+			qSql = fmt.Sprintf(`SELECT * FROM invoice WHERE invoice_order_code = '%s'`, qutil.InterfaceToStr((*oRes)["invoice_order_code"]))
+			res1 := util.Mysql.SelectBySql(qSql)
+			if res1 != nil && len(*res1) > 0 {
+				resArr = *res1
+			}
+		}
+		var isMail bool
+		for _, res := range resArr {
+			newUuid := uuid.New()
+			if res["only_Identifying"] == nil { //无唯一id 允许换票一次 生成唯一id
+				res["only_Identifying"] = newUuid
+				util.Mysql.Update(dbname, map[string]interface{}{"id": res["id"]}, map[string]interface{}{"only_Identifying": newUuid})
+			}
+			data := res
+			delete(data, "id")
+			switch IsEdit(res, phone, e_mail, id_num, company_name, invoice_type) {
+			case true:
+				if util.Mysql.Count(dbname, map[string]interface{}{
+					"order_code":       res["order_code"],
+					"user_id":          userId,
+					"only_Identifying": res["only_Identifying"],
+				}) > 1 {
+					continue
+				}
+				var html string
+				if invoice_type == "个人" {
+					emailHtml := emailHtml_gr
+					emailHtmls := fmt.Sprintf(emailHtml, phone, e_mail)
+					html = fmt.Sprintf(email_format, res["order_code"], res["url"], emailHtmls)
+				} else {
+					emailHtml := emailHtml_gs
+					emailHtmls := fmt.Sprintf(emailHtml, company_name, id_num, phone, e_mail)
+					html = fmt.Sprintf(email_format, res["order_code"], res["url"], emailHtmls)
+				}
+				data["invoice_changed"] = 1
+				data["invoice_status"] = 1
+				data["billing_time"] = time.Now().Unix()
+				data["create_time"] = time.Now().Unix()
+				data["user_id"] = userId
+				order_id := util.Mysql.Insert(dbname, data)
+				if order_id == -1 {
+					errCode = append(errCode, fmt.Sprintf("%s_%s", res["order_code"], res["invoice_order_code"]))
+				}
+				if !isMail { //成功发邮件
+					isMail = true
+					InvoiceSendMail(res["mail"].(string), html, "电子发票")
+				}
+			case false:
+				if util.Mysql.Count(dbname, map[string]interface{}{
+					"order_code":       res["order_code"],
+					"user_id":          userId,
+					"only_Identifying": res["only_Identifying"],
+				}) > 1 {
+					continue
+				}
+				dataexport_data := make(map[string]interface{})
+				if invoice_type == "个人" {
+					dataexport_data["applyBill_type"] = 0
+					dataexport_data["applyBill_taxnum"] = ""
+					dataexport_data["applybill_company"] = ""
+				} else {
+					dataexport_data["applyBill_type"] = 1
+					dataexport_data["applyBill_taxnum"] = id_num
+					dataexport_data["applybill_company"] = company_name
+				}
+				dataexport_data["user_mail"] = e_mail
+				util.Mysql.Update("dataexport_order", map[string]interface{}{"order_code": res["order_code"], "user_id": userId}, dataexport_data)
+
+				data["mail"] = e_mail
+				data["phone"] = phone
+				data["invoice_type"] = invoice_type
+				data["company_name"] = company_name
+				data["taxpayer_identnum"] = id_num
+				data["invoice_changed"] = 1
+				data["create_time"] = time.Now().Unix()
+				data["user_id"] = userId
+				data["invoice_status"] = invoice_status
+				order_id := util.Mysql.Insert(dbname, data)
+				if order_id == -1 {
+					errCode = append(errCode, fmt.Sprintf("%s_%s", res["order_code"], res["invoice_order_code"]))
+				}
+			}
+		}
+		log.Println("换票错误信息:", errCode)
+		if !config.InvoiceConfig.Third_party_switch {
+			this.ServeJson(map[string]interface{}{
+				"invoice_status": invoice_status,
+			})
+			return
+		}
+	} else {
+		if qutil.IntAll((*oRes)["invoice_status"]) < 0 {
+			this.ServeJson(map[string]interface{}{
+				"error_code": -1,
+				"error_msg":  "发票状态错误",
+			})
+			return
+		}
+	}
+
+}
+
+func IsEdit(res map[string]interface{}, phone, e_mail, id_num, company_name, invoice_type string) bool {
+	if invoice_type == "个人" {
+		if phone == res["phone"] && e_mail == res["mail"] && res["invoice_type"] == "个人" {
+			return true
+		}
+	} else {
+		if phone == res["phone"] && e_mail == res["mail"] && id_num == res["taxpayer_identnum"] && company_name == res["company_name"] && res["invoice_type"] == "单位" {
+			return true
+		}
+	}
+	return false
+}
+
 // 退票
 // 退票
 func (this *Invoice) Refundinvoice() {
 func (this *Invoice) Refundinvoice() {
 	if !config.InvoiceConfig.Third_party_switch {
 	if !config.InvoiceConfig.Third_party_switch {