order.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "time"
  6. "github.com/k0kubun/pp/v3"
  7. _ "github.com/go-sql-driver/mysql"
  8. log "github.com/sirupsen/logrus"
  9. "kpt.xdmy/apiserver/model"
  10. "kpt.xdmy/apiserver/model/http"
  11. "kpt.xdmy/pkg/util"
  12. )
  13. // sap采购订单接口
  14. func (s *Service) PurchaseOrder(p interface{}) {
  15. r := new(model.BigBuyDetailReq)
  16. if e := util.Unmarshal(p, r); e != nil {
  17. log.Errorf("purchaseorder param :%v ", e)
  18. log.Info(p)
  19. return
  20. }
  21. if r.ID == "" && r.Code == "" {
  22. log.Error("buy order request param is empty")
  23. return
  24. }
  25. bdetails := []model.BigBuyDetail{}
  26. bpp := new(model.Bigpartpurchase)
  27. if r.ID != "" {
  28. pid, err := strconv.ParseInt(r.ID, 10, 32)
  29. if err != nil {
  30. log.Errorf("parseInt purchase id :%v", err)
  31. }
  32. id := int(pid)
  33. if e := s.d.DB.First(bpp, model.Bigpartpurchase{ID: id}).Error; e != nil {
  34. log.Error("Bigpartpurchase query error", e)
  35. return
  36. }
  37. if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: bpp.OrderNumber}).Error; e != nil || len(bdetails) == 0 {
  38. log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
  39. return
  40. }
  41. } else if r.Code != "" {
  42. if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: r.Code}).Error; e != nil || len(bdetails) == 0 {
  43. log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
  44. return
  45. }
  46. }
  47. for _, v := range bdetails {
  48. s.SapOrder(&v)
  49. }
  50. }
  51. func (s *Service) SapOrder(p *model.BigBuyDetail) {
  52. r := new(http.OrderReq)
  53. rp := new(http.OrderResp)
  54. parts := []model.BuyDetail{}
  55. pa := new(model.Pasture)
  56. pv := new(model.Provider)
  57. // dp := new(model.Department)
  58. r.Dest.DestID = "EQMAN"
  59. r.Dest.BussTp = "MM007"
  60. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  61. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  62. log.Error(e)
  63. return
  64. }
  65. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  66. log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
  67. return
  68. }
  69. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  70. log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
  71. return
  72. }
  73. r.Data.IsCancle = ""
  74. r.Data.CompanyCode = pa.CompanyCode
  75. r.Data.SupplierCode = pv.ProviderNumber
  76. r.Data.OrderDate = p.BuyerDate.Format("20060102")
  77. r.Data.PayCondition = ""
  78. r.Data.CurrencyCode = "CNY"
  79. r.Data.ProofType = "ZNB4"
  80. r.Data.EqSysCode = p.BuyeCode[4:]
  81. // r.Data.Organization = pa.ParchaseOrganization
  82. r.Data.Organization = pa.CompanyCode
  83. r.Data.Group = "A02"
  84. for i, v := range parts {
  85. detail := http.OrderDetail{}
  86. c := new(model.Contract)
  87. bc := new(model.BigContract)
  88. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  89. log.Errorf("order detail error:%v", e)
  90. return
  91. }
  92. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  93. log.Errorf("order detail error:%v", e)
  94. return
  95. }
  96. detail.RowNumber = fmt.Sprintf("%d", i+1)
  97. detail.Factory = pa.FactoryCode
  98. detail.Quantity = fmt.Sprintf("%d", v.Amount)
  99. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  100. detail.TaxCode = "J0"
  101. detail.Per = "1"
  102. detail.NetPrice = c.Price
  103. detail.Unit = c.Unit
  104. detail.MaterialCode = c.PartCode
  105. detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  106. r.Data.Detail = append(r.Data.Detail, detail)
  107. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  108. log.Errorln(e, v.ID)
  109. }
  110. }
  111. if e := s.SyncSap(r, rp); e != nil {
  112. log.Errorf("sap buy order error: %v", e)
  113. p.SapStatus = 3
  114. } else {
  115. if rp.Dest.Status == "S" {
  116. log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  117. p.SapStatus = 1
  118. p.SapNumber = rp.Data.SapNumber
  119. } else {
  120. log.Warnf("buy order fail :msg=%s,r=%v", rp.Data.MsgText, r)
  121. p.SapStatus = 2
  122. pp.Print(r)
  123. }
  124. }
  125. if p.SapStatus == 1 {
  126. if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SapStatus: p.SapStatus, SapNumber: p.SapNumber}).Error; e != nil {
  127. log.Errorf("update big buy detail error: %v", e)
  128. }
  129. }
  130. }