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