123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- package service
- import (
- "encoding/json"
- "fmt"
- _ "github.com/go-sql-driver/mysql"
- "github.com/k0kubun/pp/v3"
- log "github.com/sirupsen/logrus"
- "gorm.io/gorm"
- "kpt.xdmy/apiserver/config"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- "kpt.xdmy/pkg/util"
- "strconv"
- "strings"
- )
- // 凭证冲销
- 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.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM015/CancelMblnr"
- r.Dest.Url = fmt.Sprintf("%s/Common/MM015/CancelMblnr", config.Conf.Http.Routing)
- 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]})
- }
- tx := s.d.DB.Begin()
- err := UpdateProof(p, tx)
- if err != nil {
- hrp.Message += fmt.Sprintf("%s . bigid = %d ,rows=%v", err.Error(), p.OrderNumberId, p.RowsNumber)
- tx.Rollback()
- }
- r.Data.RowsNumber = sl
- rbyte, _ := json.Marshal(r)
- //rbyte, _ := json.Marshal(r)
- fmt.Println(string(rbyte))
- e := s.SyncSap(r, rp, rbyte)
- 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)
- tx.Rollback()
- return
- }
- } else {
- hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e)
- log.Infoln(hrp.Message, r, p)
- tx.Rollback()
- return
- }
- tx.Commit()
- hrp.Msg = "ok"
- return
- }
- //凭证冲销后状态更新
- func UpdateProof(p *model.ProofReq, tx *gorm.DB) error {
- var tname string
- switch p.NumberType {
- case 1: // 入库
- tname = "partlaid"
- case 2: // 退货
- tname = "partrefund"
- case 3: // 出库
- tname = "partuse"
- case 4: // 退库
- tname = "partquit"
- }
- //strings.Join(p.RowsNumber, ",")
- rowsNumbers := []string{}
- for _, rowsNumber := range p.RowsNumber {
- rowsNumbers = append(rowsNumbers, strconv.Itoa(rowsNumber))
- }
- if p.NumberType == 1 {
- //for _, item := range p.ProofPart {
- if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ",
- strings.Join(rowsNumbers, ",")).Update("dflag", 1).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- tx.Rollback()
- return e
- }
- //}
- //select id,name from user where FIND_IN_SET(id,#{strlist})
- if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- } else if p.NumberType == 3 {
- if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")).
- Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- } else if p.NumberType == 2 {
- if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")).
- Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
- log.Errorln(e, p.OrderNumberId, p.RowsNumber)
- }
- }
- if p.NumberType == 1 {
- partlaidList := make([]*model.PartLaid, 0)
- err := tx.Raw(`select * from partlaid where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partlaidList).Error
- if err != nil {
- log.Error(err)
- return err
- }
- for _, part := range partlaidList {
- err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery - ?
- where pr.partCode = ? and pr.contractId = ? and
- (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartlaid where id = ? ) `,
- part.StorageAmount, part.PartCode, part.ContractID, p.OrderNumberId).Error
- if err != nil {
- log.Error(err)
- return err
- }
- }
- } else if p.NumberType == 3 {
- partUseList := make([]*model.PartUse, 0)
- err := tx.Raw(`select * from partuse where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partUseList).Error
- if err != nil {
- log.Error(err)
- return err
- }
- for _, part := range partUseList {
- err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ?
- where pr.partCode = ? and pr.contractId = ?
- and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartuse where id = ? ) `,
- part.CheckoutNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error
- if err != nil {
- log.Error(err)
- return err
- }
- }
- } else if p.NumberType == 2 {
- partRefundList := make([]*model.PartRefund, 0)
- err := tx.Raw(`select * from partrefund where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).
- Find(&partRefundList).Error
- if err != nil {
- log.Error(err)
- return err
- }
- for _, part := range partRefundList {
- err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ?
- where pr.partCode = ? and pr.contractId = ?
- and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureId from bigpartrefund where id = ? ) `,
- part.RefundNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error
- if err != nil {
- log.Error(err)
- return err
- }
- }
- }
- return nil
- }
- // 查询 单据 对应备件明细
- 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.Raw(` select * from partlaid where bigid = ? and dflag != 1 order by rowNumber,id desc `, bp.ID).Order("rowNumber,id desc ").
- Find(&parts).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
- }
- if v.RowNumber > 0 {
- mp.RowNumber = v.RowNumber
- } else {
- mp.RowNumber = len(parts) - i
- }
- 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.SapCode
- mp.Warehouse = w.SapCode
- 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.Where(" bigId = ? ", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&funds).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
- }
- if v.RowNumber > 0 {
- mp.RowNumber = v.RowNumber
- } else {
- mp.RowNumber = len(funds) - i
- }
- mp.Supplier = pv.SapCode
- mp.PartBrand = v.Brand
- mp.Warehouse = "1004"
- 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.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&uses).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)
- pv := new(model.Provider)
- pa := new(model.PartRepertory)
- if v.ContractID > 0 {
- 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(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
- log.Errorln(bc.ProviderID, e)
- //continue
- }
- } else {
- if e := s.d.DB.First(pa, model.PartRepertory{ContractID: v.ContractID, PartCode: v.PartCode, LocationID: v.LocationID}).Error; e != nil {
- log.Error(e)
- hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
- //return
- }
- if e := s.d.DB.First(&pv, model.Provider{ID: pa.ProviderID}).Error; e != nil {
- log.Errorln(pa.ProviderID, e)
- //continue
- }
- }
- 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.SapCode
- mp.Supplier = pv.SapCode
- if v.RowNumber > 0 {
- mp.RowNumber = v.RowNumber
- } else {
- mp.RowNumber = len(uses) - i
- }
- 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.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&parts).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)
- pv := new(model.Provider)
- 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(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
- log.Errorln(bc.ProviderID, e)
- continue
- }
- mp.Supplier = pv.SapCode
- mp.PartBrand = v.Brand
- mp.Warehouse = "1004"
- if v.RowNumber > 0 {
- mp.RowNumber = v.RowNumber
- } else {
- mp.RowNumber = len(parts) - 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
- }
|