order.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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).Error; 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. //sap
  108. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  109. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  110. return errors.Wrapf(e, "provider query:id=%d", p.ProviderID), ""
  111. }
  112. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  113. return errors.Wrapf(e, "pasture query:id=%d", p.PastureID), ""
  114. }
  115. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  116. return errors.Wrapf(e, "buydetail query:bigid=%d", p.ID), ""
  117. }
  118. r.Data.IsCancle = ""
  119. r.Data.PayCondition = ""
  120. r.Data.CompanyCode = pa.CompanyCode
  121. r.Data.SupplierCode = pv.ProviderNumber
  122. r.Data.OrderDate = p.BuyerDate.Format("20060102")
  123. r.Data.CurrencyCode = "CNY"
  124. r.Data.ProofType = "ZNB4"
  125. r.Data.EqSysCode = p.BuyeCode[4:]
  126. // r.Data.Organization = pa.ParchaseOrganization
  127. r.Data.Organization = pa.CompanyCode
  128. r.Data.Group = "A02"
  129. for i, v := range parts {
  130. detail := http.SapOrderDetail{}
  131. c := new(model.Contract)
  132. bc := new(model.BigContract)
  133. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  134. return errors.Wrapf(e, "contract query :id=%d", v.ContractID), ""
  135. }
  136. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  137. return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID), ""
  138. }
  139. detail.RowNumber = fmt.Sprintf("%d", i+1)
  140. detail.Factory = pa.FactoryCode
  141. detail.Quantity = fmt.Sprintf("%d", v.Amount)
  142. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  143. detail.TaxCode = "J0"
  144. detail.Per = "1"
  145. detail.NetPrice = c.Price
  146. detail.Unit = c.Unit
  147. detail.MaterialCode = c.PartCode
  148. detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  149. r.Data.Detail = append(r.Data.Detail, detail)
  150. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  151. log.Errorln(e, v.ID)
  152. }
  153. }
  154. e := s.SyncSap(r, &rp)
  155. fmt.Println(e != nil, rp.Dest.Status == "S")
  156. if e == nil && rp.Dest.Status == "S" {
  157. log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  158. return nil, rp.Data.SapNumber
  159. } else {
  160. saplog := new(model.SapLog)
  161. if param, e := json.Marshal(r); e != nil {
  162. log.Errorf("sap order param marshal:%v", e)
  163. } else {
  164. saplog.Param = string(param)
  165. saplog.Name = "sap-order"
  166. s.d.DB.Create(saplog)
  167. }
  168. if e == nil {
  169. return errors.Errorf("sap order :%s", rp.Data.MsgText), ""
  170. } else {
  171. return errors.Wrapf(e, "sap order error:%v", e), ""
  172. }
  173. }
  174. }
  175. func AutoSrmOrderJob() {
  176. orders := []model.BigBuyDetail{}
  177. if e := s.d.DB.Where("srmStatus=0 and buyerDate >'2022-10-01' and sapStatus=1").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  178. log.Errorf("AutoSrmOrderJob error: %v", e)
  179. return
  180. }
  181. for _, v := range orders {
  182. SrmOrder(&v)
  183. }
  184. }
  185. func ManaulSrmOrderJob() {
  186. orders := []model.BigBuyDetail{}
  187. if e := s.d.DB.Where("srmStatus=2").Where("sapStatus=1").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  188. log.Errorf("ManaulSrmOrderJob error: %v", e)
  189. return
  190. }
  191. for _, v := range orders {
  192. SrmOrder(&v)
  193. }
  194. }
  195. func SrmOrder(p *model.BigBuyDetail) {
  196. r := new(http.SrmOrderReq)
  197. rp := new(http.SrmResp)
  198. parts := []model.BuyDetail{}
  199. pa := new(model.Pasture)
  200. pv := new(model.Provider)
  201. // dp := new(model.Department)
  202. // r.Dest.DestID = "EQMAN"
  203. // r.Dest.BussTp = "MM007"
  204. r.Dest.Url = "http://192.168.61.100/gateway/serviceApi/invoke/QZbycode/saveByEquipment"
  205. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  206. log.Error(e)
  207. return
  208. }
  209. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  210. log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
  211. return
  212. }
  213. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  214. log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
  215. return
  216. }
  217. r.Data.CompanyCode = pa.CompanyCode
  218. r.Data.VendorErpCode = pv.ProviderNumber
  219. r.Data.PurchaseOrderDate = p.BuyerDate.Format("20060102")
  220. r.Data.CurrencyCode = "CNY"
  221. r.Data.PurchaseOrderType = "ZNB4"
  222. r.Data.PurchasingOrgCode = pa.CompanyCode
  223. r.Data.PurchasingGroupCode = "A02"
  224. r.Data.VendorName = pv.ProviderName
  225. r.Data.DeliveryTypeCode = "1"
  226. r.Data.IsReturn = "0"
  227. r.Data.Remark = ""
  228. r.Data.ErpPurchaseOrderNo = p.SapNumber
  229. r.Data.PurchaseOrderNo = p.BuyeCode[4:]
  230. for i, v := range parts {
  231. detail := http.SrmOrderDetail{}
  232. c := new(model.Contract)
  233. bc := new(model.BigContract)
  234. pr := new(model.PartRepertory)
  235. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  236. log.Errorf("order detail error:%v", e)
  237. return
  238. }
  239. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  240. log.Errorf("order detail error:%v", e)
  241. return
  242. }
  243. s.d.DB.First(pr, model.PartRepertory{ContractID: v.ContractID})
  244. // e != nil {
  245. // log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
  246. // return
  247. // }
  248. r.Data.TotalNonTaxAmount += c.Price
  249. detail.RowNo = fmt.Sprintf("%d", i+1)
  250. detail.Manufacturer = pa.FactoryCode
  251. detail.OrderQty = fmt.Sprintf("%d", v.Amount)
  252. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  253. detail.TaxRateCode = "J0"
  254. // detail.OrderUnitCode = "1"
  255. detail.NonTaxPrice = c.Price
  256. detail.OrderUnitCode = c.Unit
  257. // detail.MaterialCode = c.PartCode
  258. // detail.MaterialName = c.PartName
  259. detail.MaterialCode = "14.01.01.02.001072"
  260. detail.MaterialName = "荧光灯电子镇流器"
  261. detail.MaterialDesc = c.Specification
  262. detail.MaterialGroupCode = c.Unit
  263. detail.PricingUnitCode = c.Unit
  264. detail.TaxPrice = c.Price
  265. detail.NonTaxAmount = c.Price
  266. detail.Tax = "0"
  267. detail.TaxAmount = c.Price
  268. detail.ConfirmDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  269. detail.PlantCode = pa.PastureNumber
  270. detail.StockLocationCode = pr.Location
  271. detail.QualityCheck = "0"
  272. detail.IsReturn = "0"
  273. detail.OverDeliveryLimit = "0"
  274. detail.ShortDeliveryLimit = "0"
  275. detail.SourceNo = p.BuyeCode[4:]
  276. detail.ContractCode = bc.ContractCode
  277. detail.Remark = c.Remark
  278. r.Data.Detail = append(r.Data.Detail, detail)
  279. detail.PurchaseCategory = "0"
  280. detail.IsFree = "0"
  281. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  282. log.Errorln(e, v.ID)
  283. }
  284. }
  285. rbyte, _ := json.Marshal([]interface{}{r.Data})
  286. fmt.Println(string(rbyte))
  287. if e := s.SyncSrm(r, rp, rbyte); e != nil {
  288. log.Errorf("sap buy order error: %v", e)
  289. p.SapStatus = 3
  290. } else {
  291. if rp.Success {
  292. // log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  293. p.SapStatus = 1
  294. p.SapNumber = rp.Code
  295. } else {
  296. log.Warnf("buy order fail :msg=%s,r=%v", rp.Code, r)
  297. p.SapStatus = 2
  298. // pp.Print(r)
  299. }
  300. }
  301. if p.SapStatus == 1 {
  302. if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SrmStatus: p.SapStatus, SrmNumber: p.SapNumber}).Error; e != nil {
  303. log.Errorf("update big buy detail error: %v", e)
  304. }
  305. }
  306. }