123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- package service
- import (
- "fmt"
- _ "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) ProofOff(p *model.ProofReq) (hrp *http.Response) {
- r := new(http.ChargeOffReq)
- rp := new(http.SapResp)
- hrp = new(http.Response)
- hrp.Code = 200
- hrp.Msg = "fail"
- r.Dest.DestID = "EQMAN"
- r.Dest.BussTp = "MM015"
- r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM015/CancelMblnr"
- r.Data.ProofCode = p.ProofCode
- r.Data.ProofYear = p.ProofYear
- r.Data.ChargeDate = p.ChargeDate
- r.Data.OrderNumber = p.OrderNumber
- r.Data.HasTicket = util.ZeroStr(p.HasTicket == 1, "X")
- log.Infof("proofcode: %s", r.Data.ProofCode)
- sl := r.Data.RowsNumber
- for i := range p.RowsNumber {
- sl = append(sl, http.ChargeOffDetail{RowNumber: p.RowsNumber[i]})
- }
- r.Data.RowsNumber = sl
- e := s.SyncSap(r, rp)
- pp.Print(r)
- if e == nil {
- if rp.Data.Status == "S" {
- hrp.Message = fmt.Sprintf("sap物料冲销成功:%d %s %s", p.NumberType, rp.Data.ProofNumber, rp.Data.EqNumber)
- log.Info(hrp.Message)
- } else {
- hrp.Message = fmt.Sprintf("sap凭证冲销失败:%s", rp.Dest.MessText)
- log.Infoln(hrp.Message, r, p)
- return
- }
- } else {
- hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e)
- log.Infoln(hrp.Message, r, p)
- return
- }
- hrp.Msg = "ok"
- e = UpdateProof(p)
- if e != nil {
- hrp.Message += fmt.Sprintf("%s . bigid = %d ,rows=%v", e.Error(), p.OrderNumberId, p.RowsNumber)
- }
- return
- }
- //凭证冲销后状态更新
- func UpdateProof(p *model.ProofReq) (e error) {
- var tname string
- switch p.NumberType {
- case 1: // 入库
- tname = "partlaid"
- case 2: // 退货
- tname = "PartRefund"
- case 3: // 出库
- tname = "PartUse"
- case 4: // 退库
- tname = "Partquit"
- }
- if e = s.d.DB.Table(tname).Where("bigid=? and rownumber in (?)", p.OrderNumberId, p.RowsNumber).Update("dflag", 1).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- return e
- }
- // 查询 单据 对应备件明细
- func ProofList(p *model.ProofReq) (hrp *http.Response) {
- switch p.NumberType {
- case 1: // 入库
- return s.laidProofList(p)
- case 2: // 退货
- return s.returnProofList(p)
- case 3: // 出库
- return s.useProofList(p)
- case 4: // 退库
- return s.quitProofList(p)
- }
- return
- }
- // 入库冲销
- func (s *Service) laidProofList(p *model.ProofReq) (hrp *http.Response) {
- bp := new(model.BigPartLaid)
- proofs := make([]model.ProofPart, 0)
- var e error
- hrp = new(http.Response)
- hrp.Code = 200
- hrp.Msg = "fail"
- if e = s.d.DB.First(bp, model.BigPartLaid{LaidCode: p.OrderNumber}).Error; e != nil {
- hrp.Message = fmt.Sprintf("%s ,error=%v,", p.OrderNumber, e)
- return
- }
- p.OrderNumberId = bp.ID
- p.ProofCode = bp.ProofCode
- p.ChargeDate = bp.CreatDate.Format("2006-01-02")
- p.ProofYear = bp.CreatDate.Format("2006")
- parts := make([]model.PartLaid, 0)
- if e = s.d.DB.Order("id asc ").Find(&parts, model.PartLaid{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
- hrp.Message = fmt.Sprintf("入库单明细查询报错或结果为空:bigid=%v error=%v,", bp.ID, e)
- hrp.Msg = "fail"
- return
- }
- for i, v := range parts {
- mp := new(model.ProofPart)
- br := new(model.Brand)
- w := new(model.Warehouse)
- pv := new(model.Provider)
- if e := s.d.DB.First(&pv, model.Provider{ID: v.ProviderID}).Error; e != nil {
- log.Errorln(v.ProviderID, e)
- continue
- }
- if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
- log.Errorln(e, v.BrandID)
- continue
- }
- if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
- log.Errorln(v.LocationID, e)
- continue
- }
- mp.RowNumber = i + 1
- mp.PartCode = v.PartCode
- mp.PartName = v.PartName
- mp.Specification = v.Specification
- mp.ChangeCount = v.StorageAmount
- mp.UnitPrice = v.Price
- mp.TotalPrice = v.SumPrice
- mp.PartBrand = br.BrandName
- mp.Supplier = pv.ProviderName
- mp.Warehouse = w.WarehoseCode
- if v.Dflag == 0 {
- proofs = append(proofs, *mp)
- }
- }
- p.ProofPart = proofs
- hrp.Msg = "ok"
- hrp.Data = p
- hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
- return
- }
- // 退货冲销
- func (s *Service) returnProofList(p *model.ProofReq) (hrp *http.Response) {
- hrp = new(http.Response)
- bp := new(model.BigPartRefund)
- proofs := make([]model.ProofPart, 0)
- hrp.Code = 200
- var e error
- if e = s.d.DB.First(bp, model.BigPartRefund{UseForm: p.OrderNumber}).Error; e != nil {
- hrp.Message = fmt.Sprintf("bigpartrefund : %d,%s", p.NumberType, p.OrderNumber)
- hrp.Msg = "fail"
- return
- }
- p.OrderNumberId = bp.ID
- p.ProofCode = bp.ProofCode
- p.ChargeDate = bp.CreateTime.Format("2006-01-02")
- p.ProofYear = bp.CreateTime.Format("2006")
- funds := []model.PartRefund{}
- if e = s.d.DB.Find(&funds, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(funds) == 0 {
- hrp.Message = fmt.Sprintf("PartRefund query error . bigid=%v error=%v,", bp.ID, e)
- hrp.Msg = "fail"
- return
- }
- for i, v := range funds {
- mp := new(model.ProofPart)
- pv := new(model.Provider)
- if e := s.d.DB.First(&pv, model.Provider{ID: bp.ProviderID}).Error; e != nil {
- log.Errorln(bp.ProviderID, e)
- continue
- }
- mp.Supplier = pv.ProviderName
- mp.RowNumber = i
- mp.PartBrand = v.Brand
- mp.Warehouse = v.Location
- mp.PartCode = v.PartCode
- mp.PartName = v.PartName
- mp.Specification = v.Specification
- mp.ChangeCount = v.RefundNumber
- mp.UnitPrice = v.Price
- mp.TotalPrice = v.SumPrice
- proofs = append(proofs, *mp)
- }
- p.ProofPart = proofs
- hrp.Msg = "ok"
- hrp.Data = p
- hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
- return
- }
- // 出库冲销
- func (s *Service) useProofList(p *model.ProofReq) (hrp *http.Response) {
- hrp = new(http.Response)
- bp := new(model.BigPartUse)
- proofs := make([]model.ProofPart, 0)
- hrp.Code = 200
- hrp.Msg = "fail"
- var e error
- if e = s.d.DB.First(bp, model.BigPartUse{UseForm: p.OrderNumber}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("bigpartuse useform=%s", p.OrderNumber)
- return
- }
- p.OrderNumberId = bp.ID
- p.ProofCode = bp.ProofCode
- p.ChargeDate = bp.ReceiveTime.Format("2006-01-02")
- p.ProofYear = bp.ReceiveTime.Format("2006")
- uses := make([]model.PartUse, 0)
- if e = s.d.DB.Find(&uses, model.PartUse{BigID: bp.ID}).Error; e != nil || len(uses) == 0 {
- log.Error(e)
- hrp.Message = fmt.Sprintf("PartUse bigid = %v ", bp.ID)
- return
- }
- for i, v := range uses {
- mp := new(model.ProofPart)
- br := new(model.Brand)
- w := new(model.Warehouse)
- c := new(model.Contract)
- bc := new(model.BigContract)
- if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
- return
- }
- if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
- return
- }
- if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("brand id =%d", v.BrandID)
- return
- }
- if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
- log.Errorln(v.LocationID, e)
- return
- }
- mp.PartBrand = br.BrandName
- mp.Warehouse = w.WarehoseCode
- mp.Supplier = bc.ProviderName
- mp.RowNumber = i + 1
- mp.PartCode = v.PartCode
- mp.PartName = v.PartName
- mp.Specification = v.Specification
- mp.ChangeCount = v.CheckoutNumber
- mp.UnitPrice = v.Price
- mp.TotalPrice = v.SumPrice
- proofs = append(proofs, *mp)
- }
- p.ProofPart = proofs
- hrp.Msg = "ok"
- hrp.Data = p
- hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
- return
- }
- // 退库冲销
- func (s *Service) quitProofList(p *model.ProofReq) (hrp *http.Response) {
- hrp = new(http.Response)
- bp := new(model.BigPartquit)
- proofs := make([]model.ProofPart, 0)
- hrp.Code = 200
- var e error
- if e = s.d.DB.First(bp, model.BigPartquit{UseForm: p.OrderNumber}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("bigpartquit %d,%s", p.NumberType, p.OrderNumber)
- hrp.Msg = "fail"
- return
- }
- p.OrderNumberId = bp.ID
- p.ProofCode = bp.ProofCode
- p.ChargeDate = bp.QuitDate.Format("2006-01-02")
- p.ProofYear = bp.QuitDate.Format("2006")
- parts := make([]model.Partquit, 0)
- if e = s.d.DB.Find(&parts, model.Partquit{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
- hrp.Message = fmt.Sprintf("partquit bigid = %d ", bp.ID)
- hrp.Msg = "fail"
- return
- }
- for i, v := range parts {
- mp := new(model.ProofPart)
- c := new(model.Contract)
- bc := new(model.BigContract)
- if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
- return
- }
- if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
- return
- }
- mp.Supplier = bc.ProviderName
- mp.PartBrand = v.Brand
- mp.Warehouse = v.Location
- mp.RowNumber = i
- mp.PartCode = v.PartCode
- mp.PartName = v.PartName
- mp.Specification = v.Specification
- mp.ChangeCount = v.QuitNumber
- mp.UnitPrice = v.Price
- mp.TotalPrice = v.SumPrice
- proofs = append(proofs, *mp)
- }
- p.ProofPart = proofs
- hrp.Msg = "ok"
- hrp.Data = p
- hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
- return
- }
|