transfer.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package service
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. log "github.com/sirupsen/logrus"
  6. "github.com/pkg/errors"
  7. "kpt.xdmy/apiserver/model"
  8. "kpt.xdmy/apiserver/model/http"
  9. )
  10. // 定时每分钟读取已完成 未同步的的调拨单,同步到sap
  11. // 每个调拨单定时同步只进行一次
  12. func autoTransfer() {
  13. bp := []model.BigPartTransfer{}
  14. if e := s.d.DB.Where(" CreatDate > '2022-09-28' and sapStatus =0 ").Find(&bp, model.BigPartTransfer{Statue: 1}).Error; e != nil {
  15. log.Errorf("autoTransfer find :%v", e)
  16. return
  17. }
  18. log.Infof("sap transfer len=%d", len(bp))
  19. for _, v := range bp {
  20. err := SapTransfer(&v)
  21. TransferLog("auto", v.ApplyCode, err)
  22. }
  23. }
  24. // 手动同步调拨单到sap
  25. func ManualTransfer(code string) (int, map[string]interface{}) {
  26. bp := []model.BigPartTransfer{}
  27. re := make(map[string]interface{})
  28. r := s.d.DB.Where("CreatDate >'2022-09-28' ")
  29. if code != "" {
  30. r = r.Where("applycode", code)
  31. }
  32. r = r.Find(&bp, model.BigPartTransfer{Statue: 1, SapStatus: 2})
  33. if r.Error != nil {
  34. log.Errorf("ManualTransfers find :%v", r.Error)
  35. re["find"] = r.Error
  36. return 0, re
  37. }
  38. var count int
  39. for _, v := range bp {
  40. err := SapTransfer(&v)
  41. TransferLog("manual", v.ApplyCode, err)
  42. if err == nil {
  43. count++
  44. } else {
  45. re[v.ApplyCode] = err.Error()
  46. }
  47. }
  48. return count, re
  49. }
  50. // 调拨接口日志
  51. func TransferLog(c, code string, err error) {
  52. m := make(map[string]interface{})
  53. if err != nil {
  54. err = errors.Wrap(err, code)
  55. m["sapText"] = err.Error()
  56. if c == "auto" {
  57. m["sapStatus"] = 2
  58. }
  59. } else {
  60. m["sapStatus"] = 1
  61. }
  62. if e := s.d.DB.Model(&model.BigPartTransfer{}).Where("applycode =?", code).Updates(m).Error; e != nil {
  63. log.Errorf("BigPartTransfer update :%v", e)
  64. }
  65. }
  66. func SapTransfer(bp *model.BigPartTransfer) error {
  67. // 寄售(零库存)无调拨
  68. r := new(http.TransferReq)
  69. parts := []model.PartTransfer{}
  70. blaid := new(model.BigPartLaid)
  71. inpa := new(model.Pasture)
  72. outpa := new(model.Pasture)
  73. dp := new(model.Department)
  74. if e := s.d.DB.Find(&parts, model.PartTransfer{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  75. return errors.Wrapf(e, "parttarnsfer query bigid: %d", bp.ID)
  76. }
  77. if e := s.d.DB.First(blaid, model.BigPartLaid{LaidCode: bp.LaidCode}).Error; e != nil {
  78. return errors.Wrapf(e, "bigpartlaid query laidcode: %s", bp.LaidCode)
  79. }
  80. if e := s.d.DB.First(inpa, model.Pasture{ID: bp.InPastureID}).Error; e != nil {
  81. return errors.Wrapf(e, "pasture :id=%d", bp.InPastureID)
  82. }
  83. if e := s.d.DB.First(outpa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  84. return errors.Wrapf(e, "pasture :id=%d", bp.PastureID)
  85. }
  86. if e := s.d.DB.First(dp, model.Department{ID: bp.DeptId}).Error; e != nil {
  87. return errors.Wrapf(e, "pasture :id=%d", bp.DeptId)
  88. }
  89. r.Dest.DestID = "EQMAN"
  90. r.Dest.BussTp = "MM012"
  91. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM012/TransferStock"
  92. r.Data.OrderDate = bp.CreatDate.Format("20060102")
  93. r.Data.PostDate = bp.ApplyDate.Format("20060102")
  94. r.Data.TransferCode = bp.ApplyCode
  95. r.Data.Group = "B11"
  96. for i, v := range parts {
  97. detail := new(http.TransferDetail)
  98. outStock := new(model.PartRepertory)
  99. pslaid := new(model.PartLaid)
  100. inw := new(model.Warehouse)
  101. c := new(model.Contract)
  102. bc := new(model.BigContract)
  103. if e := s.d.DB.First(outStock, model.PartRepertory{ID: v.PartRepID}).Error; e != nil {
  104. return errors.Wrapf(e, "partrepertory query :id=%d", v.PartRepID)
  105. }
  106. if e := s.d.DB.First(pslaid, model.PartLaid{BigID: blaid.ID, PartID: v.PartID}).Error; e != nil {
  107. return errors.Wrapf(e, "partlaid query :bigid=%d", blaid.ID)
  108. }
  109. if e := s.d.DB.First(inw, model.Warehouse{ID: pslaid.LocationID}).Error; e != nil {
  110. return errors.Wrapf(e, "warehouse query :id=%d", pslaid.LocationID)
  111. }
  112. if outStock.ContractID > 0 {
  113. // if e := s.d.DB.Table("BigContract").Select("BigContract.IsZeroStock ").Joins("join Contract c on c.bigid =BigContract.id").
  114. // Where("c.id =?", outStock.ContractID).First(bc).Error; e != nil {
  115. // return errors.Wrapf(e, "BigContract IsZeroStock :id=%d", outStock.ContractID)
  116. // }
  117. if e := s.d.DB.First(c, model.Contract{ID: outStock.ContractID}).Error; e != nil {
  118. }
  119. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  120. return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID)
  121. }
  122. if bc.IsZeroStock == 1 {
  123. return errors.New("寄售无调拨")
  124. }
  125. }
  126. detail.RowNumber = fmt.Sprintf("%d", i+1)
  127. detail.MaterialCode = v.PartCode
  128. detail.Quantity = v.Amount
  129. detail.Unit = v.Unit
  130. detail.Per = "1"
  131. detail.DeliverDate = blaid.StorageTime.Format("20060102")
  132. detail.OutFactory = outpa.FactoryCode
  133. detail.InFactory = inpa.FactoryCode
  134. detail.OutLocation = inw.WarehoseCode
  135. detail.InLocation = outStock.Location
  136. r.Data.Detail = append(r.Data.Detail, *detail)
  137. }
  138. rp := new(http.TransferResp)
  139. if e := s.SyncSap(r, rp); e != nil {
  140. log.Info(r)
  141. return errors.Wrapf(e, "transfer error:%v")
  142. } else {
  143. if rp.Dest.Status == "S" {
  144. log.Printf("transfer success:%s,%s", rp.Data.Proof.EqTransferCode, rp.Data.Proof.ProofCode)
  145. return nil
  146. } else {
  147. return errors.Wrapf(e, "tranfer fail: %s", rp.Dest.MessText)
  148. }
  149. }
  150. }