123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package service
- import (
- "fmt"
- "strconv"
- "time"
- "github.com/k0kubun/pp/v3"
- _ "github.com/go-sql-driver/mysql"
- log "github.com/sirupsen/logrus"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- "kpt.xdmy/pkg/util"
- )
- // sap采购订单接口
- func (s *Service) PurchaseOrder(p interface{}) {
- r := new(model.BigBuyDetailReq)
- if e := util.Unmarshal(p, r); e != nil {
- log.Errorf("purchaseorder param :%v ", e)
- log.Info(p)
- return
- }
- if r.ID == "" && r.Code == "" {
- log.Error("buy order request param is empty")
- return
- }
- bdetails := []model.BigBuyDetail{}
- bpp := new(model.Bigpartpurchase)
- if r.ID != "" {
- pid, err := strconv.ParseInt(r.ID, 10, 32)
- if err != nil {
- log.Errorf("parseInt purchase id :%v", err)
- }
- id := int(pid)
- if e := s.d.DB.First(bpp, model.Bigpartpurchase{ID: id}).Error; e != nil {
- log.Error("Bigpartpurchase query error", e)
- return
- }
- if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: bpp.OrderNumber}).Error; e != nil || len(bdetails) == 0 {
- log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
- return
- }
- } else if r.Code != "" {
- if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: r.Code}).Error; e != nil || len(bdetails) == 0 {
- log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
- return
- }
- }
- for _, v := range bdetails {
- s.SapOrder(&v)
- }
- }
- func (s *Service) SapOrder(p *model.BigBuyDetail) {
- r := new(http.OrderReq)
- rp := new(http.OrderResp)
- parts := []model.BuyDetail{}
- pa := new(model.Pasture)
- pv := new(model.Provider)
- // dp := new(model.Department)
- r.Dest.DestID = "EQMAN"
- r.Dest.BussTp = "MM007"
- r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
- if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
- log.Error(e)
- return
- }
- if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
- log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
- return
- }
- if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
- log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
- return
- }
- r.Data.IsCancle = ""
- r.Data.CompanyCode = pa.CompanyCode
- r.Data.SupplierCode = pv.ProviderNumber
- r.Data.OrderDate = p.BuyerDate.Format("20060102")
- r.Data.PayCondition = ""
- r.Data.CurrencyCode = "CNY"
- r.Data.ProofType = "ZNB4"
- r.Data.EqSysCode = p.BuyeCode[4:]
- // r.Data.Organization = pa.ParchaseOrganization
- r.Data.Organization = pa.CompanyCode
- r.Data.Group = "A02"
- for i, v := range parts {
- detail := http.OrderDetail{}
- c := new(model.Contract)
- bc := new(model.BigContract)
- if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
- log.Errorf("order detail error:%v", e)
- return
- }
- if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
- log.Errorf("order detail error:%v", e)
- return
- }
- detail.RowNumber = fmt.Sprintf("%d", i+1)
- detail.Factory = pa.FactoryCode
- detail.Quantity = fmt.Sprintf("%d", v.Amount)
- detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
- detail.TaxCode = "J0"
- detail.Per = "1"
- detail.NetPrice = c.Price
- detail.Unit = c.Unit
- detail.MaterialCode = c.PartCode
- detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
- r.Data.Detail = append(r.Data.Detail, detail)
- if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
- log.Errorln(e, v.ID)
- }
- }
- if e := s.SyncSap(r, rp); e != nil {
- log.Errorf("sap buy order error: %v", e)
- p.SapStatus = 3
- } else {
- if rp.Dest.Status == "S" {
- log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
- p.SapStatus = 1
- p.SapNumber = rp.Data.SapNumber
- } else {
- log.Warnf("buy order fail :msg=%s,r=%v", rp.Data.MsgText, r)
- p.SapStatus = 2
- pp.Print(r)
- }
- }
- if p.SapStatus == 1 {
- if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SapStatus: p.SapStatus, SapNumber: p.SapNumber}).Error; e != nil {
- log.Errorf("update big buy detail error: %v", e)
- }
- }
- }
|