123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- 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"
- )
- // sap入库凭证接口,业务触发
- 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))
- // }
- }
- }
- }
- // 出库单推送sap,包括了报废出库
- 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)
- }
- }
- }
- // 退库单推送sap
- 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)
- }
- }
- }
- // 退货单推送sap
- 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"
- }
|