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) } } // 定时(每分钟 一次)读取未同步的采购订单,同步到sap // 同步失败的采购订单,变更状态,改为手动同步 // 状态(sapStatus):0,未同步;1,成功;2,失败 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) } } } // 手动同步采购订单到sap 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).Error; e != nil { log.Errorf("BigBuyDetail update error: %v", e) } return } // 发送采购订单到sap 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" //sap 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) } } e := s.SyncSap(r, &rp) fmt.Println(e != nil, rp.Dest.Status == "S") if 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 :%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' and sapStatus=1").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").Where("sapStatus=1").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.SrmResp) 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 = "http://192.168.61.100/gateway/serviceApi/invoke/QZbycode/saveByEquipment" 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.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.PurchasingOrgCode = pa.CompanyCode r.Data.PurchasingGroupCode = "A02" r.Data.VendorName = pv.ProviderName r.Data.DeliveryTypeCode = "1" r.Data.IsReturn = "0" r.Data.Remark = "" r.Data.ErpPurchaseOrderNo = p.SapNumber r.Data.PurchaseOrderNo = p.BuyeCode[4:] for i, v := range parts { detail := http.SrmOrderDetail{} c := new(model.Contract) bc := new(model.BigContract) pr := new(model.PartRepertory) 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 } s.d.DB.First(pr, model.PartRepertory{ContractID: v.ContractID}) // e != nil { // log.Errorf("pasture query :id=%d,%v", p.PastureID, e) // return // } r.Data.TotalNonTaxAmount += c.Price 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.MaterialName = c.PartName detail.MaterialCode = "14.01.01.02.001072" detail.MaterialName = "荧光灯电子镇流器" detail.MaterialDesc = c.Specification detail.MaterialGroupCode = c.Unit detail.PricingUnitCode = c.Unit detail.TaxPrice = c.Price detail.NonTaxAmount = c.Price detail.Tax = "0" detail.TaxAmount = c.Price detail.ConfirmDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102") detail.PlantCode = pa.PastureNumber detail.StockLocationCode = pr.Location detail.QualityCheck = "0" detail.IsReturn = "0" detail.OverDeliveryLimit = "0" detail.ShortDeliveryLimit = "0" detail.SourceNo = p.BuyeCode[4:] detail.ContractCode = bc.ContractCode detail.Remark = c.Remark r.Data.Detail = append(r.Data.Detail, detail) detail.PurchaseCategory = "0" detail.IsFree = "0" if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil { log.Errorln(e, v.ID) } } rbyte, _ := json.Marshal([]interface{}{r.Data}) fmt.Println(string(rbyte)) if e := s.SyncSrm(r, rp, rbyte); e != nil { log.Errorf("sap buy order error: %v", e) p.SapStatus = 3 } else { if rp.Success { // log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber) p.SapStatus = 1 p.SapNumber = rp.Code } else { log.Warnf("buy order fail :msg=%s,r=%v", rp.Code, r) p.SapStatus = 2 // pp.Print(r) } } if p.SapStatus == 1 { if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SrmStatus: p.SapStatus, SrmNumber: p.SapNumber}).Error; e != nil { log.Errorf("update big buy detail error: %v", e) } } }