123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package order
- import (
- "app.yhyue.com/moapp/jybase/common"
- "context"
- "errors"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "jyOrderManager/internal/model"
- )
- // SelfInvoice 自助开票
- func SelfInvoice(ctx context.Context, param model.OrderDetailParams) (map[string]interface{}, error) {
- var (
- errArr []string
- isFullBilling, isApplyBilling bool
- )
- //订单信息查询
- orderData, _ := g.DB().GetOne(ctx, fmt.Sprintf("select (IFNULL(CASE WHEN a.is_backstage_order = 1 THEN a.pay_money ELSE a.order_money END, 0)) as pay_money+(select IFNULL(sum(payMoney),0) as return_money from moneyCorrection where orderCode='%s' ) as pay_money,applybill_status,signing_subject from dataexport_order where order_code ='%s'", param.OrderCode, param.OrderCode))
- if orderData.IsEmpty() {
- return nil, errors.New("订单编号不存在")
- }
- if common.Int64All(orderData.Map()["applybill_status"]) == 2 {
- isFullBilling = true
- errArr = append(errArr, "该订单已全额开票")
- }
- if common.ObjToString(orderData.Map()["signing_subject"]) == "h02" {
- errArr = append(errArr, `签约主体不是"北京剑鱼信息技术有限公司",请联系财务线下开票`)
- }
- //发票信息查询
- var invoiceMoney int64
- invoiceData, _ := g.DB().Query(ctx, fmt.Sprintf("select invoice_status,sum(invoice_order_money) as money,min(source) as source from invoice where order_code='%s' and (invoice_status>=0 or invoice_status = -1) GROUP BY invoice_status", param.OrderCode))
- if invoiceData.IsEmpty() {
- //从来没有开过票
- invoiceMoney = common.Int64All(orderData.Map()["pay_money"])
- } else {
- //开过几次票
- payMoney := common.Int64All(orderData.Map()["pay_money"])
- allMoney := int64(0)
- sourceStr := common.InterfaceToStr(invoiceData.List()[0]["source"])
- source := common.Int64All(sourceStr)
- if source == 0 && sourceStr != "" && !isFullBilling {
- errArr = append(errArr, "该订单已全额开票")
- }
- for _, invoice := range invoiceData.List() {
- invoice_status := common.Int64All(invoice["invoice_status"])
- money := common.Int64All(invoice["money"])
- if invoice_status == 0 && !isApplyBilling {
- isApplyBilling = true
- errArr = append(errArr, `该订单存在"已申请"的开票信息`)
- } else {
- allMoney += money
- }
- }
- if allMoney == payMoney && !isFullBilling {
- errArr = append(errArr, "该订单已全额开票")
- }
- invoiceMoney = payMoney - allMoney
- }
- return map[string]interface{}{
- "money": invoiceMoney,
- "errMsg": errArr,
- }, nil
- }
|