|
@@ -1435,8 +1435,16 @@ func InvoiceAmount(orderCode string) (invoicedMoney int64) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-// InvoiceSubmit 发票信息提交管理后台扫码页面-线下开票,发票种类没有限制
|
|
|
+// InvoiceSubmit 发票信息提交
|
|
|
func (this *Invoice) InvoiceSubmit() {
|
|
|
+ if !R.CheckReqParam(this.ResponseWriter, this.Request, "invoice_variety", "invoiceContent", "invoice_type", "phone", "mail") {
|
|
|
+ this.ServeJson(NewResult("", errors.New("缺少参数")))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if this.GetString("invoice_type") == "单位" && !R.CheckReqParam(this.ResponseWriter, this.Request, "company_name", "taxpayer_identnum") {
|
|
|
+ this.ServeJson(NewResult("", errors.New("缺少参数")))
|
|
|
+ return
|
|
|
+ }
|
|
|
now := time.Now()
|
|
|
again, _ := this.GetInteger("again") //是否是失败重新开票 0 首次提交 1 再此提交
|
|
|
oldOnlyIdentifying := this.GetString("oldOnlyIdentifying") //最后一次失败的发票信息唯一标识
|
|
@@ -1470,17 +1478,9 @@ func (this *Invoice) InvoiceSubmit() {
|
|
|
//"product_type": qutil.ObjToString((*orderInfo)["product_type"]),
|
|
|
}
|
|
|
if again == 1 {
|
|
|
- ok := InvoiceFailRecord(oldOnlyIdentifying)
|
|
|
- if !ok {
|
|
|
- this.ServeJson(NewResult("", errors.New("插入失败记录出错")))
|
|
|
- return
|
|
|
- }
|
|
|
- if util.Mysql.Count("invoice_fail_record", map[string]interface{}{"only_Identifying": oldOnlyIdentifying}) >= int64(config.Config.SubmitNum) {
|
|
|
- this.ServeJson(NewResult("", errors.New("提交次数已达上限")))
|
|
|
- return
|
|
|
- }
|
|
|
- if !util.Mysql.Update("invoice", map[string]interface{}{"only_Identifying": oldOnlyIdentifying}, data) {
|
|
|
- this.ServeJson(NewResult("", errors.New("再次开票更新发票信息出错")))
|
|
|
+ err := OpenInvoiceAgain(oldOnlyIdentifying, data)
|
|
|
+ if err != nil {
|
|
|
+ this.ServeJson(NewResult("", err))
|
|
|
return
|
|
|
}
|
|
|
this.ServeJson(NewResult("保存信息成功", nil))
|
|
@@ -1490,38 +1490,14 @@ func (this *Invoice) InvoiceSubmit() {
|
|
|
switch source {
|
|
|
case 0: //用户端开票
|
|
|
userId := qutil.ObjToString(this.GetSession("userId"))
|
|
|
- orderCode := this.GetString("order_code")
|
|
|
- onlyIdentifying := fmt.Sprintf("%s%s%s", "aa", time.Now().Format("150405"), qutil.GetRandom(4))
|
|
|
- orderInfo := util.Mysql.FindOne("dataexport_order", map[string]interface{}{"order_code": orderCode}, "product_type,user_mail,pay_money", "")
|
|
|
- if orderInfo == nil {
|
|
|
- this.ServeJson(NewResult("", errors.New("订单不存在")))
|
|
|
+ if userId == "" {
|
|
|
+ this.ServeJson(NewResult("", errors.New("userId为空")))
|
|
|
return
|
|
|
}
|
|
|
- invoicedMoney := InvoiceAmount(orderCode) //已开票金额
|
|
|
- invoiceMoney := qutil.Int64All((*orderInfo)["pay_money"]) - invoicedMoney
|
|
|
- data["order_code"] = orderCode
|
|
|
- data["invoice_order_code"] = orderCode
|
|
|
- data["only_Identifying"] = onlyIdentifying
|
|
|
- data["invoice_money"] = invoiceMoney
|
|
|
- data["invoice_order_money"] = invoiceMoney
|
|
|
- data["product_type"] = qutil.ObjToString((*orderInfo)["product_type"])
|
|
|
- data["user_id"] = userId
|
|
|
- if !util.Mysql.ExecTx("用户端开发票", func(tx *sql.Tx) bool {
|
|
|
- if util.Mysql.InsertByTx(tx, "invoice", data) <= 0 {
|
|
|
- log.Println("插入invoice出错", orderCode)
|
|
|
- return false
|
|
|
- }
|
|
|
- if !util.Mysql.UpdateByTx(tx, "dataexport_order", map[string]interface{}{
|
|
|
- "order_code": orderCode,
|
|
|
- }, map[string]interface{}{
|
|
|
- "applybill_status": 1,
|
|
|
- }) {
|
|
|
- log.Println("更新dataexport_order出错", orderCode)
|
|
|
- return false
|
|
|
- }
|
|
|
- return true
|
|
|
- }) {
|
|
|
- this.ServeJson(NewResult("", errors.New("用户端开票插入数据出错")))
|
|
|
+ orderCode := this.GetString("order_code")
|
|
|
+ err := JyOpenInvoice(orderCode, userId, data)
|
|
|
+ if err != nil {
|
|
|
+ this.ServeJson(NewResult("", err))
|
|
|
return
|
|
|
}
|
|
|
case 1: //管理后台扫码开票
|
|
@@ -1532,68 +1508,140 @@ func (this *Invoice) InvoiceSubmit() {
|
|
|
this.ServeJson(NewResult("", errors.New("传入参数不正确")))
|
|
|
return
|
|
|
}
|
|
|
- oldData := util.Mysql.FindOne("invoice", map[string]interface{}{
|
|
|
- "only_Identifying": onlyIdentifying,
|
|
|
- }, "id", "")
|
|
|
- if oldData != nil {
|
|
|
- this.ServeJson(NewResult("", errors.New("该订单发票信息已经提交过申请")))
|
|
|
+ err := AdminOpenInvoice(onlyIdentifying, invoiceMoney, operator, data)
|
|
|
+ if err != nil {
|
|
|
+ this.ServeJson(NewResult("", err))
|
|
|
return
|
|
|
}
|
|
|
- orderArr := []string{}
|
|
|
- mailOrderArr := []string{}
|
|
|
- orderMap := map[string]interface{}{}
|
|
|
- Allmoney := int64(0)
|
|
|
- for _, v := range strings.Split(invoiceMoney, ",") {
|
|
|
- invoiceMoneyArr := strings.Split(v, ":")
|
|
|
- orderArr = append(orderArr, invoiceMoneyArr[0])
|
|
|
- Allmoney += qutil.Int64All(invoiceMoneyArr[1])
|
|
|
- orderMap[invoiceMoneyArr[0]] = invoiceMoneyArr[1]
|
|
|
- prices := qutil.Float64All(invoiceMoneyArr[1]) / float64(100)
|
|
|
- price := strconv.FormatFloat(prices, 'f', -1, 64)
|
|
|
- mailOrderArr = append(mailOrderArr, fmt.Sprintf("%s(%s元)", invoiceMoneyArr[0], price))
|
|
|
+ }
|
|
|
+ this.ServeJson(NewResult("保存信息成功", nil))
|
|
|
+}
|
|
|
+
|
|
|
+func OpenInvoiceAgain(oldOnlyIdentifying string, data map[string]interface{}) error {
|
|
|
+ if oldOnlyIdentifying == "" {
|
|
|
+ return errors.New("缺少关键参数")
|
|
|
+ }
|
|
|
+ ok := InvoiceFailRecord(oldOnlyIdentifying)
|
|
|
+ if !ok {
|
|
|
+ return errors.New("插入失败记录出错")
|
|
|
+ }
|
|
|
+ if util.Mysql.Count("invoice_fail_record", map[string]interface{}{"only_Identifying": oldOnlyIdentifying}) >= int64(config.Config.SubmitNum) {
|
|
|
+ return errors.New("提交次数已达上限")
|
|
|
+ }
|
|
|
+ if !util.Mysql.Update("invoice", map[string]interface{}{"only_Identifying": oldOnlyIdentifying}, data) {
|
|
|
+ return errors.New("再次开票更新发票信息出错")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func JyOpenInvoice(orderCode, userId string, data map[string]interface{}) error {
|
|
|
+ onlyIdentifying := fmt.Sprintf("%s%s%s", "aa", time.Now().Format("150405"), qutil.GetRandom(4))
|
|
|
+ orderInfo := util.Mysql.FindOne("dataexport_order", map[string]interface{}{"order_code": orderCode}, "product_type,user_mail,pay_money,create_time,signing_subject", "")
|
|
|
+ if orderInfo == nil {
|
|
|
+ return errors.New("订单不存在")
|
|
|
+ }
|
|
|
+ createTime, _ := time.ParseInLocation("2006-01-02 15:04:05", qutil.ObjToString((*orderInfo)["create_time"]), time.Local)
|
|
|
+ if createTime.Unix() < config.InvoiceConfig.Order_createtime {
|
|
|
+ return errors.New("2024年之前的订单不在开票")
|
|
|
+ }
|
|
|
+ if qutil.ObjToString((*orderInfo)["signing_subject"]) == "h02" {
|
|
|
+ return errors.New("签约主体为拓普")
|
|
|
+ }
|
|
|
+ invoicedMoney := InvoiceAmount(orderCode) //已开票金额
|
|
|
+ invoiceMoney := qutil.Int64All((*orderInfo)["pay_money"]) - invoicedMoney
|
|
|
+ data["order_code"] = orderCode
|
|
|
+ data["invoice_order_code"] = orderCode
|
|
|
+ data["only_Identifying"] = onlyIdentifying
|
|
|
+ data["invoice_money"] = invoiceMoney
|
|
|
+ data["invoice_order_money"] = invoiceMoney
|
|
|
+ data["product_type"] = qutil.ObjToString((*orderInfo)["product_type"])
|
|
|
+ data["user_id"] = userId
|
|
|
+ if !util.Mysql.ExecTx("用户端开发票", func(tx *sql.Tx) bool {
|
|
|
+ if util.Mysql.InsertByTx(tx, "invoice", data) <= 0 {
|
|
|
+ log.Println("插入invoice出错", orderCode)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if !util.Mysql.UpdateByTx(tx, "dataexport_order", map[string]interface{}{
|
|
|
+ "order_code": orderCode,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "applybill_status": 1,
|
|
|
+ }) {
|
|
|
+ log.Println("更新dataexport_order出错", orderCode)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ }) {
|
|
|
+ return errors.New("用户端开票插入数据出错")
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
|
|
|
+func AdminOpenInvoice(onlyIdentifying, invoiceMoney, operator string, data map[string]interface{}) error {
|
|
|
+ oldData := util.Mysql.FindOne("invoice", map[string]interface{}{
|
|
|
+ "only_Identifying": onlyIdentifying,
|
|
|
+ }, "id", "")
|
|
|
+ if oldData != nil {
|
|
|
+ return errors.New("该订单发票信息已经提交过申请")
|
|
|
+ }
|
|
|
+ orderArr := []string{}
|
|
|
+ mailOrderArr := []string{}
|
|
|
+ orderMap := map[string]interface{}{}
|
|
|
+ Allmoney := int64(0)
|
|
|
+ for _, v := range strings.Split(invoiceMoney, ",") {
|
|
|
+ invoiceMoneyArr := strings.Split(v, ":")
|
|
|
+ orderArr = append(orderArr, invoiceMoneyArr[0])
|
|
|
+ Allmoney += qutil.Int64All(invoiceMoneyArr[1])
|
|
|
+ orderMap[invoiceMoneyArr[0]] = invoiceMoneyArr[1]
|
|
|
+ prices := qutil.Float64All(invoiceMoneyArr[1]) / float64(100)
|
|
|
+ price := strconv.FormatFloat(prices, 'f', -1, 64)
|
|
|
+ mailOrderArr = append(mailOrderArr, fmt.Sprintf("%s(%s元)", invoiceMoneyArr[0], price))
|
|
|
+
|
|
|
+ }
|
|
|
+ //提交时订单金额再做一次校验
|
|
|
+ for orderCode, money := range orderMap {
|
|
|
+ if ResidueMoney(orderCode) < qutil.Int64All(money) {
|
|
|
+ return errors.New("该订单发票信息已经提交过申请")
|
|
|
}
|
|
|
- //提交时订单金额再做一次校验
|
|
|
+ }
|
|
|
+ if !util.Mysql.ExecTx("发票信息保存", func(tx *sql.Tx) bool {
|
|
|
for orderCode, money := range orderMap {
|
|
|
- if ResidueMoney(orderCode) < qutil.Int64All(money) {
|
|
|
- this.ServeJson(NewResult("", errors.New("该订单发票信息已经提交过申请")))
|
|
|
- return
|
|
|
+ orderInfo := util.Mysql.FindOne("dataexport_order", map[string]interface{}{"order_code": orderCode}, "product_type,user_mail,user_id,create_time", "")
|
|
|
+ productType := ""
|
|
|
+ if orderInfo != nil {
|
|
|
+ productType = qutil.InterfaceToStr((*orderInfo)["product_type"])
|
|
|
}
|
|
|
- }
|
|
|
- if !util.Mysql.ExecTx("发票信息保存", func(tx *sql.Tx) bool {
|
|
|
- for orderCode, money := range orderMap {
|
|
|
- orderInfo := util.Mysql.FindOne("dataexport_order", map[string]interface{}{"order_code": orderCode}, "product_type,user_mail,user_id", "")
|
|
|
- productType := ""
|
|
|
- if orderInfo != nil {
|
|
|
- productType = qutil.InterfaceToStr((*orderInfo)["product_type"])
|
|
|
- }
|
|
|
- data["order_code"] = orderCode
|
|
|
- data["invoice_order_code"] = strings.Join(orderArr, ",")
|
|
|
- data["only_Identifying"] = onlyIdentifying
|
|
|
- data["invoice_money"] = Allmoney
|
|
|
- data["invoice_order_money"] = money
|
|
|
- data["product_type"] = productType
|
|
|
- data["user_id"] = qutil.ObjToString((*orderInfo)["user_id"])
|
|
|
- data["operator"] = operator
|
|
|
- if util.Mysql.Insert("invoice", data) <= 0 {
|
|
|
- return false
|
|
|
- }
|
|
|
- //订单发票状态改成已申请
|
|
|
- if !util.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
- "order_code": orderCode,
|
|
|
- }, map[string]interface{}{
|
|
|
- "applybill_status": 1,
|
|
|
- }) {
|
|
|
- return false
|
|
|
- }
|
|
|
+ createTime, _ := time.ParseInLocation("2006-01-02 15:04:05", qutil.ObjToString((*orderInfo)["create_time"]), time.Local)
|
|
|
+ if createTime.Unix() < config.InvoiceConfig.Order_createtime {
|
|
|
+ log.Println("2024年之前的订单不在开票", orderCode)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ data["order_code"] = orderCode
|
|
|
+ data["invoice_order_code"] = strings.Join(orderArr, ",")
|
|
|
+ data["only_Identifying"] = onlyIdentifying
|
|
|
+ data["invoice_money"] = Allmoney
|
|
|
+ data["invoice_order_money"] = money
|
|
|
+ data["product_type"] = productType
|
|
|
+ data["user_id"] = qutil.ObjToString((*orderInfo)["user_id"])
|
|
|
+ data["operator"] = operator
|
|
|
+ if util.Mysql.Insert("invoice", data) <= 0 {
|
|
|
+ log.Println("扫码开票插入invoice表出错", orderCode)
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ //订单发票状态改成已申请
|
|
|
+ if !util.Mysql.Update("dataexport_order", map[string]interface{}{
|
|
|
+ "order_code": orderCode,
|
|
|
+ }, map[string]interface{}{
|
|
|
+ "applybill_status": 1,
|
|
|
+ }) {
|
|
|
+ log.Println("扫码开票更新dataexport_order表出错", orderCode)
|
|
|
+ return false
|
|
|
}
|
|
|
- return true
|
|
|
- }) {
|
|
|
- this.ServeJson(NewResult("", errors.New("发票信息保存失败")))
|
|
|
- return
|
|
|
}
|
|
|
+ return true
|
|
|
+ }) {
|
|
|
+ return errors.New("发票信息保存失败")
|
|
|
}
|
|
|
- this.ServeJson(NewResult("保存信息成功", nil))
|
|
|
+ return nil
|
|
|
}
|
|
|
|
|
|
func InvoiceFailRecord(oldOnlyIdentifying string) bool {
|