order.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "github.com/pkg/errors"
  8. _ "github.com/go-sql-driver/mysql"
  9. log "github.com/sirupsen/logrus"
  10. "kpt.xdmy/apiserver/model"
  11. "kpt.xdmy/apiserver/model/http"
  12. "kpt.xdmy/pkg/util"
  13. )
  14. // 推送采购订单到sap(业务触发),已关闭
  15. func (s *Service) PurchaseOrder(p interface{}) {
  16. r := new(model.BigBuyDetailReq)
  17. if e := util.Unmarshal(p, r); e != nil {
  18. log.Errorf("purchaseorder param :%v ", e)
  19. log.Info(p)
  20. return
  21. }
  22. if r.ID == "" && r.Code == "" {
  23. log.Error("buy order request param is empty")
  24. return
  25. }
  26. bdetails := []model.BigBuyDetail{}
  27. bpp := new(model.Bigpartpurchase)
  28. if r.ID != "" {
  29. pid, err := strconv.ParseInt(r.ID, 10, 32)
  30. if err != nil {
  31. log.Errorf("parseInt purchase id :%v", err)
  32. }
  33. id := int(pid)
  34. if e := s.d.DB.First(bpp, model.Bigpartpurchase{ID: id}).Error; e != nil {
  35. log.Error("Bigpartpurchase query error", e)
  36. return
  37. }
  38. if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: bpp.OrderNumber}).Error; e != nil || len(bdetails) == 0 {
  39. log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
  40. return
  41. }
  42. } else if r.Code != "" {
  43. if e := s.d.DB.Where(" ZeroCou >0 ").Find(&bdetails, model.BigBuyDetail{MatchCode: r.Code}).Error; e != nil || len(bdetails) == 0 {
  44. log.Errorf("Bigbuydetail query :matchcode =%s,e=%v", r.Code, e)
  45. return
  46. }
  47. }
  48. for _, v := range bdetails {
  49. SapOrder(&v)
  50. SrmOrder(&v)
  51. }
  52. }
  53. // 定时(每分钟 一次)读取未同步的采购订单,同步到sap
  54. // 同步失败的采购订单,变更状态,改为手动同步
  55. // 状态(sapStatus):0,未同步;1,成功;2,失败
  56. func AutoSapOrderJob() {
  57. orders := []model.BigBuyDetail{}
  58. if e := s.d.DB.Where("sapStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{}).Error; e != nil {
  59. log.Errorf("AutoSapOrderJob error: %v", e)
  60. return
  61. }
  62. for _, v := range orders {
  63. e, sapNumber := SapOrder(&v)
  64. up := model.BigBuyDetail{SapNumber: sapNumber}
  65. if e != nil {
  66. up.SapStatus = 2
  67. up.SapError = e.Error()
  68. } else {
  69. up.SapStatus = 1
  70. }
  71. if e := s.d.DB.Model(&v).Where("buyeCode =?", v.BuyeCode).Updates(up); e != nil {
  72. log.Errorf("BigBuyDetail update error: %v", e)
  73. }
  74. }
  75. }
  76. // 手动同步采购订单到sap
  77. func ManaulSapOrderJob(buyeCode string) (err error) {
  78. order := new(model.BigBuyDetail)
  79. if e := s.d.DB.First(order, model.BigBuyDetail{SapStatus: 2, BuyeCode: buyeCode}).Error; e != nil {
  80. err = errors.Wrapf(e, "ManaulSapOrderJob BigBuyDetail query ")
  81. return
  82. }
  83. e, sapNumber := SapOrder(order)
  84. up := model.BigBuyDetail{SapNumber: sapNumber}
  85. if e != nil {
  86. up.SapStatus = 2
  87. up.SapError = e.Error()
  88. err = errors.Wrapf(e, "ManaulSapOrder sap ")
  89. } else {
  90. up.SapStatus = 1
  91. }
  92. if e := s.d.DB.Model(order).Where("buyeCode=?", buyeCode).Updates(up); e != nil {
  93. log.Errorf("BigBuyDetail update error: %v", e)
  94. }
  95. return
  96. }
  97. // 发送采购订单到sap
  98. func SapOrder(p *model.BigBuyDetail) (error, string) {
  99. r := new(http.SapOrderReq)
  100. rp := new(http.OrderResp)
  101. parts := []model.BuyDetail{}
  102. pa := new(model.Pasture)
  103. pv := new(model.Provider)
  104. // dp := new(model.Department)
  105. r.Dest.DestID = "EQMAN"
  106. r.Dest.BussTp = "MM007"
  107. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  108. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  109. return errors.Wrapf(e, "provider query:id=%d", p.ProviderID), ""
  110. }
  111. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  112. return errors.Wrapf(e, "pasture query:id=%d", p.PastureID), ""
  113. }
  114. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  115. return errors.Wrapf(e, "buydetail query:bigid=%d", p.ID), ""
  116. }
  117. r.Data.IsCancle = ""
  118. r.Data.PayCondition = ""
  119. r.Data.CompanyCode = pa.CompanyCode
  120. r.Data.SupplierCode = pv.ProviderNumber
  121. r.Data.OrderDate = p.BuyerDate.Format("20060102")
  122. r.Data.CurrencyCode = "CNY"
  123. r.Data.ProofType = "ZNB4"
  124. r.Data.EqSysCode = p.BuyeCode[4:]
  125. // r.Data.Organization = pa.ParchaseOrganization
  126. r.Data.Organization = pa.CompanyCode
  127. r.Data.Group = "A02"
  128. for i, v := range parts {
  129. detail := http.SapOrderDetail{}
  130. c := new(model.Contract)
  131. bc := new(model.BigContract)
  132. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  133. return errors.Wrapf(e, "contract query :id=%d", v.ContractID), ""
  134. }
  135. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  136. return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID), ""
  137. }
  138. detail.RowNumber = fmt.Sprintf("%d", i+1)
  139. detail.Factory = pa.FactoryCode
  140. detail.Quantity = fmt.Sprintf("%d", v.Amount)
  141. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  142. detail.TaxCode = "J0"
  143. detail.Per = "1"
  144. detail.NetPrice = c.Price
  145. detail.Unit = c.Unit
  146. detail.MaterialCode = c.PartCode
  147. detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  148. r.Data.Detail = append(r.Data.Detail, detail)
  149. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  150. log.Errorln(e, v.ID)
  151. }
  152. }
  153. if e := s.SyncSap(r, rp); e != nil && rp.Dest.Status == "S" {
  154. log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  155. return nil, rp.Data.SapNumber
  156. } else {
  157. saplog := new(model.SapLog)
  158. if param, e := json.Marshal(r); e != nil {
  159. log.Errorf("sap order param marshal:%v", e)
  160. } else {
  161. saplog.Param = string(param)
  162. saplog.Name = "sap-order"
  163. s.d.DB.Create(saplog)
  164. }
  165. if e == nil {
  166. return errors.Errorf("sap order fail:%s", rp.Data.MsgText), ""
  167. } else {
  168. return errors.Wrapf(e, "sap order error:%v", e), ""
  169. }
  170. }
  171. }
  172. func AutoSrmOrderJob() {
  173. orders := []model.BigBuyDetail{}
  174. if e := s.d.DB.Where("srmStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  175. log.Errorf("AutoSrmOrderJob error: %v", e)
  176. return
  177. }
  178. for _, v := range orders {
  179. SrmOrder(&v)
  180. }
  181. }
  182. func ManaulSrmOrderJob() {
  183. orders := []model.BigBuyDetail{}
  184. if e := s.d.DB.Where("srmStatus=2").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  185. log.Errorf("ManaulSrmOrderJob error: %v", e)
  186. return
  187. }
  188. for _, v := range orders {
  189. SrmOrder(&v)
  190. }
  191. }
  192. func SrmOrder(p *model.BigBuyDetail) {
  193. r := new(http.SrmOrderReq)
  194. // rp := new(http.OrderResp)
  195. parts := []model.BuyDetail{}
  196. pa := new(model.Pasture)
  197. pv := new(model.Provider)
  198. // dp := new(model.Department)
  199. r.Dest.DestID = "EQMAN"
  200. r.Dest.BussTp = "MM007"
  201. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  202. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  203. log.Error(e)
  204. return
  205. }
  206. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  207. log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
  208. return
  209. }
  210. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  211. log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
  212. return
  213. }
  214. // r.Data.IsCancle = ""
  215. // r.Data.PayCondition = ""
  216. r.Data.CompanyCode = pa.CompanyCode
  217. r.Data.VendorErpCode = pv.ProviderNumber
  218. r.Data.PurchaseOrderDate = p.BuyerDate.Format("20060102")
  219. r.Data.CurrencyCode = "CNY"
  220. r.Data.PurchaseOrderType = "ZNB4"
  221. // r.Data.EqSysCode = p.BuyeCode[4:]
  222. // r.Data.Organization = pa.ParchaseOrganization
  223. r.Data.PurchasingOrgCode = pa.CompanyCode
  224. r.Data.PurchasingGroupCode = "A02"
  225. for i, v := range parts {
  226. detail := http.SrmOrderDetail{}
  227. c := new(model.Contract)
  228. bc := new(model.BigContract)
  229. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  230. log.Errorf("order detail error:%v", e)
  231. return
  232. }
  233. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  234. log.Errorf("order detail error:%v", e)
  235. return
  236. }
  237. detail.RowNo = fmt.Sprintf("%d", i+1)
  238. detail.Manufacturer = pa.FactoryCode
  239. detail.OrderQty = fmt.Sprintf("%d", v.Amount)
  240. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  241. detail.TaxRateCode = "J0"
  242. // detail.OrderUnitCode = "1"
  243. detail.NonTaxPrice = c.Price
  244. detail.OrderUnitCode = c.Unit
  245. detail.MaterialCode = c.PartCode
  246. // detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  247. r.Data.Detail = append(r.Data.Detail, detail)
  248. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  249. log.Errorln(e, v.ID)
  250. }
  251. }
  252. // if e := s.SyncSap(r, rp); e != nil {
  253. // log.Errorf("sap buy order error: %v", e)
  254. // p.SapStatus = 3
  255. // } else {
  256. // if rp.Dest.Status == "S" {
  257. // log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  258. // p.SapStatus = 1
  259. // p.SapNumber = rp.Data.SapNumber
  260. // } else {
  261. // log.Warnf("buy order fail :msg=%s,r=%v", rp.Data.MsgText, r)
  262. // p.SapStatus = 2
  263. // pp.Print(r)
  264. // }
  265. // }
  266. // if p.SapStatus == 1 {
  267. // if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SapStatus: p.SapStatus, SapNumber: p.SapNumber}).Error; e != nil {
  268. // log.Errorf("update big buy detail error: %v", e)
  269. // }
  270. // }
  271. }