package service import ( "fmt" "strconv" "strings" "time" _ "github.com/go-sql-driver/mysql" "github.com/k0kubun/pp/v3" log "github.com/sirupsen/logrus" "kpt.xdmy/apiserver/model" "kpt.xdmy/apiserver/model/http" "kpt.xdmy/pkg/util" ) func (s *Service) LaidProof(p interface{}) { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := []model.PartLaid{} bp := new(model.BigPartLaid) bbd := new(model.BigBuyDetail) pa := new(model.Pasture) if e := util.Unmarshal(p, pr); e != nil { log.Error(e, p) return } if e := s.d.DB.First(bp, model.BigPartLaid{LaidCode: pr.LaidCode}).Error; e != nil { log.Error(e) return } if e := s.d.DB.Find(&parts, model.PartLaid{BigID: bp.ID}).Error; e != nil || len(parts) == 0 { log.Error(e) return } if e := s.d.DB.First(bbd, model.BigBuyDetail{BuyeCode: bp.PurchaseID}).Error; e != nil { log.Error(e) } if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return } proofDest(r) r.Data.EqNumber = pr.LaidCode r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.StorageTime); e != nil { log.Errorf("parse time error:err=%v,param=%v", e, pr.StorageTime) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { sdetail := http.ProofDetail{MoveType: "101"} c := new(model.Contract) bc := new(model.BigContract) bd := new(model.BuyDetail) w := new(model.Warehouse) if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return } if e := s.d.DB.First(bd, model.BuyDetail{BigID: bbd.ID, ContractID: v.ContractID}).Error; e != nil { log.Error(e) return } if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil { log.Error(e) return } sdetail.RowNumber = fmt.Sprintf("%d", i+1) sdetail.Quantity = fmt.Sprintf("%f", v.StorageAmount) sdetail.Unit = c.Unit sdetail.Code = c.PartCode sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") sdetail.Factory = pa.FactoryCode sdetail.Location = w.WarehoseCode sdetail.OrderNumber = bbd.SapNumber sdetail.OrderRowNum = fmt.Sprintf("%d", bd.RowNumber) r.Data.Detail = append(r.Data.Detail, sdetail) } if e := s.SyncSap(r, rp); e != nil { log.Error(e) } else { if rp.Dest.Status == "S" { log.Printf("laid proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber) if e := s.d.DB.Model(&model.BigPartLaid{}).Where("laidcode = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { log.Errorln(e) } } else { log.Warnf("%s laid proof failed:causetext=%s", rp.Dest.MessText) pp.Print(r) // if b, e := json.MarshalIndent(r, "", " "); e != nil { // log.Error("json.MarshalIndent error") // } else { // fmt.Println(string(b)) // } } } } func (s *Service) UseProof(p interface{}) { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := []model.PartUse{} bp := new(model.BigPartUse) pa := new(model.Pasture) dp := new(model.Department) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) return } if e := s.d.DB.First(bp, model.BigPartUse{UseForm: pr.UseForm}).Error; e != nil { log.Error(e) return } if e := s.d.DB.Find(&parts, model.PartUse{BigID: bp.ID}).Error; e != nil { log.Error(e) return } // pp.Print(bp) if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return } if e := s.d.DB.First(dp, model.Department{ID: bp.DepartmentID}).Error; e != nil { log.Error(e) return } // pp.Println(bp, d) proofDest(r) r.Data.EqNumber = pr.UseForm r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.ReceiveTime); e != nil { log.Error(e, pr.ReceiveTime) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { bc := new(model.BigContract) c := new(model.Contract) w := new(model.Warehouse) pv := new(model.Provider) eq := new(model.Equipment) storage := new(model.PartRepertory) if v.ContractID < 0 { if e := s.d.DB.First(storage, model.PartRepertory{ContractID: v.ContractID}).Error; e != nil { log.Errorln(e, v.ContractID) return } if e := s.d.DB.First(pv, model.Provider{ID: storage.ProviderID}).Error; e != nil { log.Errorln(e, storage.ProviderID) return } } else { if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return } } if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil { log.Errorln(e, v.LocationID) return } if e := s.d.DB.First(eq, model.Equipment{Id: v.EqID}).Error; e != nil { log.Errorln(e, v.EqID) return } sdetail := http.ProofDetail{} // 报废出库 if pr.UseType == 5 { sdetail.MoveType = "Z19" if v.ContractID > 0 && bc.IsZeroStock != 0 { log.Info("寄售不能报废=======") return } } // 领用出库、手动出库 if pr.UseType == 0 || pr.UseType == 1 { sdetail.MoveType = "201" sdetail.CostCenter = dp.CostCenterCode } sdetail.Factory = pa.FactoryCode if pa.FactoryCode == "M005" && strings.Contains(dp.Name, "二期") { sdetail.Factory = "M017" } sdetail.RowNumber = fmt.Sprintf("%d", i+1) sdetail.Quantity = fmt.Sprintf("%f", v.CheckoutNumber) // sdetail.Unit = c.Unit sdetail.Unit = v.Unit // sdetail.Code = c.PartCode sdetail.Code = v.PartCode sdetail.AssetCode = eq.FinanceCode sdetail.Location = w.WarehoseCode if v.ContractID > 0 && bc.IsZeroStock == 1 { sdetail.SaveType = "K" } if sdetail.SaveType == "K" { sdetail.Provider = pv.ProviderNumber } r.Data.Detail = append(r.Data.Detail, sdetail) } pp.Print(r) log.Infof("bp.pastureId:%d,pa.name:%s,pa.companycode:%s", bp.PastureID, pa.Name, pa.CompanyCode) if e := s.SyncSap(r, rp); e != nil { log.Errorf("laid proof error:%v", e) } else { if rp.Dest.Status == "S" { log.Printf("use proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber) if e := s.d.DB.Model(&model.BigPartUse{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { log.Errorln(e) } } else { log.Warnf(" use proof :%s", rp.Dest.MessText) } } } // 退库 func (s *Service) QuitProof(p interface{}) { pr := new(http.ProofPartReq) r := new(http.ProofReq) rp := new(http.ProofResp) parts := []model.Partquit{} bp := new(model.BigPartquit) pa := new(model.Pasture) d := new(model.Department) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) return } if e := s.d.DB.First(bp, model.BigPartquit{UseForm: pr.UseForm}).Error; e != nil { log.Error(e) return } if e := s.d.DB.Find(&parts, model.Partquit{BigID: bp.ID}).Error; e != nil { log.Error(e) return } if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return } if e := s.d.DB.First(d, model.Department{ID: bp.DeptID}).Error; e != nil { log.Error(e) return } proofDest(r) r.Data.EqNumber = pr.UseForm r.Data.CreateTime = time.Now().Format("20060102") if t, e := util.ParseDate(pr.QuitDate); e != nil { log.Error(e, pr.QuitDate) } else { r.Data.PostDate = t.Format("20060102") } for i, v := range parts { c := new(model.Contract) bc := new(model.BigContract) pv := new(model.Provider) // w := new(model.Warehouse) if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorln(e, bc.ProviderID) return } sdetail := http.ProofDetail{MoveType: "202"} sdetail.RowNumber = fmt.Sprintf("%d", i+1) sdetail.Quantity = fmt.Sprintf("%f", v.QuitNumber) sdetail.Unit = c.Unit sdetail.Code = c.PartCode sdetail.Factory = pa.FactoryCode sdetail.CostCenter = d.CostCenterCode sdetail.Location = v.Location sdetail.Provider = pv.ProviderNumber sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") r.Data.Detail = append(r.Data.Detail, sdetail) } if e := s.SyncSap(r, rp); e != nil { log.Errorf("laid proof error:%v", e) } else { if rp.Dest.Status == "S" { log.Printf("quit proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber) if e := s.d.DB.Model(&model.BigPartquit{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { log.Errorln(e) } } else { log.Warnf("%s quit proof failed:causetext=%s", rp.Dest.MessText) pp.Print(r) } } } func (s *Service) RefundProof(p interface{}) { pr := new(model.RefundParam) r := new(http.ProofReq) rp := new(http.ProofResp) parts := []model.PartRefund{} bp := new(model.BigPartRefund) pa := new(model.Pasture) if e := util.Unmarshal(p, pr); e != nil { log.Error(e) log.Info("%v", pr) return } pid, err := strconv.ParseInt(pr.ID, 10, 32) if err != nil { log.Errorf("parseInt purchase id :%v", err) } if e := s.d.DB.First(bp, model.BigPartRefund{ID: int(pid)}).Error; e != nil { log.Error(e) return } if e := s.d.DB.Find(&parts, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(parts) == 0 { log.Error(e) return } if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil { log.Error(e) return } proofDest(r) r.Data.EqNumber = bp.UseForm r.Data.PostDate = bp.CreateTime.Format("20060102") r.Data.CreateTime = time.Now().Format("20060102") for i, v := range parts { sdetail := http.ProofDetail{} c := new(model.Contract) bc := new(model.BigContract) bd := new(model.BuyDetail) bbd := new(model.BigBuyDetail) pv := new(model.Provider) if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil { log.Errorf("contract query error:%v", e) return } if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil { log.Errorf("bigcontract query error:%v", e) return } if e := s.d.DB.Order("id desc").First(bd, model.BuyDetail{ContractID: v.ContractID}).Error; e != nil { log.Error(e) return } if e := s.d.DB.First(bbd, model.BigBuyDetail{ID: bd.BigID}).Error; e != nil { log.Errorf("refund proof bigbuydetail:id=%d,%v", bd.BigID, e) return } if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil { log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e) return } if bc.IsZeroStock == 0 { sdetail.MoveType = "161" } else { sdetail.MoveType = "502" } sdetail.RowNumber = fmt.Sprintf("%d", i+1) sdetail.Quantity = fmt.Sprintf("%f", v.RefundNumber) sdetail.Unit = c.Unit sdetail.Code = c.PartCode sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K") sdetail.Factory = pa.FactoryCode sdetail.Location = v.Location sdetail.OrderNumber = bbd.SapNumber sdetail.OrderRowNum = fmt.Sprintf("%d", bd.RowNumber) sdetail.Provider = pv.ProviderNumber r.Data.Detail = append(r.Data.Detail, sdetail) } if e := s.SyncSap(r, rp); e != nil { log.Error(e) } else { if rp.Dest.Status == "S" { log.Printf("refund proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber) if e := s.d.DB.Model(&model.BigPartRefund{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil { log.Errorln(e) } } else { log.Warnf("return good fail: %s", rp.Dest.MessText) // if b, e := json.MarshalIndent(r, "", " "); e != nil { // log.Error("json.MarshalIndent error") // } else { // fmt.Println(string(b)) // } } } } func proofDest(p *http.ProofReq) { p.Dest.DestID = "EQMAN" p.Dest.BussTp = "MM001" p.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM001/ModifyMblnr" }