package service import ( "encoding/json" "fmt" "strconv" "time" "github.com/pkg/errors" _ "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 { SapOrder(&v) SrmOrder(&v) } } func AutoSapOrderJob() { orders := []model.BigBuyDetail{} if e := s.d.DB.Where("sapStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{}).Error; e != nil { log.Errorf("AutoSapOrderJob error: %v", e) return } for _, v := range orders { e, sapNumber := SapOrder(&v) up := model.BigBuyDetail{SapNumber: sapNumber} if e != nil { up.SapStatus = 2 up.SapError = e.Error() } else { up.SapStatus = 1 } if e := s.d.DB.Model(&v).Where("buyeCode =?", v.BuyeCode).Updates(up); e != nil { log.Errorf("BigBuyDetail update error: %v", e) } } } func ManaulSapOrderJob(buyeCode string) (err error) { order := new(model.BigBuyDetail) if e := s.d.DB.First(order, model.BigBuyDetail{SapStatus: 2, BuyeCode: buyeCode}).Error; e != nil { err = errors.Wrapf(e, "ManaulSapOrderJob BigBuyDetail query ") return } e, sapNumber := SapOrder(order) up := model.BigBuyDetail{SapNumber: sapNumber} if e != nil { up.SapStatus = 2 up.SapError = e.Error() err = errors.Wrapf(e, "ManaulSapOrder sap ") } else { up.SapStatus = 1 } if e := s.d.DB.Model(order).Where("buyeCode=?", buyeCode).Updates(up); e != nil { log.Errorf("BigBuyDetail update error: %v", e) } return } func SapOrder(p *model.BigBuyDetail) (error, string) { r := new(http.SapOrderReq) 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 { return errors.Wrapf(e, "provider query:id=%d", p.ProviderID), "" } if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil { return errors.Wrapf(e, "pasture query:id=%d", p.PastureID), "" } if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 { return errors.Wrapf(e, "buydetail query:bigid=%d", p.ID), "" } r.Data.IsCancle = "" r.Data.PayCondition = "" r.Data.CompanyCode = pa.CompanyCode r.Data.SupplierCode = pv.ProviderNumber r.Data.OrderDate = p.BuyerDate.Format("20060102") 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.SapOrderDetail{} c := new(model.Contract) bc := new(model.BigContract) if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { return errors.Wrapf(e, "contract query :id=%d", v.ContractID), "" } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID), "" } 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 && rp.Dest.Status == "S" { log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber) return nil, rp.Data.SapNumber } else { saplog := new(model.SapLog) if param, e := json.Marshal(r); e != nil { log.Errorf("sap order param marshal:%v", e) } else { saplog.Param = string(param) saplog.Name = "sap-order" s.d.DB.Create(saplog) } if e == nil { return errors.Errorf("sap order fail:%s", rp.Data.MsgText), "" } else { return errors.Wrapf(e, "sap order error:%v", e), "" } } } func AutoSrmOrderJob() { orders := []model.BigBuyDetail{} if e := s.d.DB.Where("srmStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil { log.Errorf("AutoSrmOrderJob error: %v", e) return } for _, v := range orders { SrmOrder(&v) } } func ManaulSrmOrderJob() { orders := []model.BigBuyDetail{} if e := s.d.DB.Where("srmStatus=2").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil { log.Errorf("ManaulSrmOrderJob error: %v", e) return } for _, v := range orders { SrmOrder(&v) } } func SrmOrder(p *model.BigBuyDetail) { r := new(http.SrmOrderReq) // 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.PayCondition = "" r.Data.CompanyCode = pa.CompanyCode r.Data.VendorErpCode = pv.ProviderNumber r.Data.PurchaseOrderDate = p.BuyerDate.Format("20060102") r.Data.CurrencyCode = "CNY" r.Data.PurchaseOrderType = "ZNB4" // r.Data.EqSysCode = p.BuyeCode[4:] // r.Data.Organization = pa.ParchaseOrganization r.Data.PurchasingOrgCode = pa.CompanyCode r.Data.PurchasingGroupCode = "A02" for i, v := range parts { detail := http.SrmOrderDetail{} 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.RowNo = fmt.Sprintf("%d", i+1) detail.Manufacturer = pa.FactoryCode detail.OrderQty = fmt.Sprintf("%d", v.Amount) detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102") detail.TaxRateCode = "J0" // detail.OrderUnitCode = "1" detail.NonTaxPrice = c.Price detail.OrderUnitCode = 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) // } // } }