selfInvoice.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package order
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "github.com/gogf/gf/v2/frame/g"
  8. "jyOrderManager/internal/model"
  9. )
  10. // SelfInvoice 自助开票
  11. func SelfInvoice(ctx context.Context, param model.OrderDetailParams) (map[string]interface{}, error) {
  12. var (
  13. errArr []string
  14. isFullBilling, isApplyBilling bool
  15. )
  16. //订单信息查询
  17. 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))
  18. if orderData.IsEmpty() {
  19. return nil, errors.New("订单编号不存在")
  20. }
  21. if common.Int64All(orderData.Map()["applybill_status"]) == 2 {
  22. isFullBilling = true
  23. errArr = append(errArr, "该订单已全额开票")
  24. }
  25. if common.ObjToString(orderData.Map()["signing_subject"]) == "h02" {
  26. errArr = append(errArr, `签约主体不是"北京剑鱼信息技术有限公司",请联系财务线下开票`)
  27. }
  28. //发票信息查询
  29. var invoiceMoney int64
  30. 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))
  31. if invoiceData.IsEmpty() {
  32. //从来没有开过票
  33. invoiceMoney = common.Int64All(orderData.Map()["pay_money"])
  34. } else {
  35. //开过几次票
  36. payMoney := common.Int64All(orderData.Map()["pay_money"])
  37. allMoney := int64(0)
  38. sourceStr := common.InterfaceToStr(invoiceData.List()[0]["source"])
  39. source := common.Int64All(sourceStr)
  40. if source == 0 && sourceStr != "" && !isFullBilling {
  41. errArr = append(errArr, "该订单已全额开票")
  42. }
  43. for _, invoice := range invoiceData.List() {
  44. invoice_status := common.Int64All(invoice["invoice_status"])
  45. money := common.Int64All(invoice["money"])
  46. if invoice_status == 0 && !isApplyBilling {
  47. isApplyBilling = true
  48. errArr = append(errArr, `该订单存在"已申请"的开票信息`)
  49. } else {
  50. allMoney += money
  51. }
  52. }
  53. if allMoney == payMoney && !isFullBilling {
  54. errArr = append(errArr, "该订单已全额开票")
  55. }
  56. invoiceMoney = payMoney - allMoney
  57. }
  58. return map[string]interface{}{
  59. "money": invoiceMoney,
  60. "errMsg": errArr,
  61. }, nil
  62. }