order.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. func AutoSapOrderJob() {
  54. orders := []model.BigBuyDetail{}
  55. if e := s.d.DB.Where("sapStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{}).Error; e != nil {
  56. log.Errorf("AutoSapOrderJob error: %v", e)
  57. return
  58. }
  59. for _, v := range orders {
  60. e, sapNumber := SapOrder(&v)
  61. up := model.BigBuyDetail{SapNumber: sapNumber}
  62. if e != nil {
  63. up.SapStatus = 2
  64. up.SapError = e.Error()
  65. } else {
  66. up.SapStatus = 1
  67. }
  68. if e := s.d.DB.Model(&v).Where("buyeCode =?", v.BuyeCode).Updates(up); e != nil {
  69. log.Errorf("BigBuyDetail update error: %v", e)
  70. }
  71. }
  72. }
  73. func ManaulSapOrderJob(buyeCode string) (err error) {
  74. order := new(model.BigBuyDetail)
  75. if e := s.d.DB.First(order, model.BigBuyDetail{SapStatus: 2, BuyeCode: buyeCode}).Error; e != nil {
  76. err = errors.Wrapf(e, "ManaulSapOrderJob BigBuyDetail query ")
  77. return
  78. }
  79. e, sapNumber := SapOrder(order)
  80. up := model.BigBuyDetail{SapNumber: sapNumber}
  81. if e != nil {
  82. up.SapStatus = 2
  83. up.SapError = e.Error()
  84. err = errors.Wrapf(e, "ManaulSapOrder sap ")
  85. } else {
  86. up.SapStatus = 1
  87. }
  88. if e := s.d.DB.Model(order).Where("buyeCode=?", buyeCode).Updates(up); e != nil {
  89. log.Errorf("BigBuyDetail update error: %v", e)
  90. }
  91. return
  92. }
  93. func SapOrder(p *model.BigBuyDetail) (error, string) {
  94. r := new(http.SapOrderReq)
  95. rp := new(http.OrderResp)
  96. parts := []model.BuyDetail{}
  97. pa := new(model.Pasture)
  98. pv := new(model.Provider)
  99. // dp := new(model.Department)
  100. r.Dest.DestID = "EQMAN"
  101. r.Dest.BussTp = "MM007"
  102. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  103. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  104. return errors.Wrapf(e, "provider query:id=%d", p.ProviderID), ""
  105. }
  106. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  107. return errors.Wrapf(e, "pasture query:id=%d", p.PastureID), ""
  108. }
  109. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  110. return errors.Wrapf(e, "buydetail query:bigid=%d", p.ID), ""
  111. }
  112. r.Data.IsCancle = ""
  113. r.Data.PayCondition = ""
  114. r.Data.CompanyCode = pa.CompanyCode
  115. r.Data.SupplierCode = pv.ProviderNumber
  116. r.Data.OrderDate = p.BuyerDate.Format("20060102")
  117. r.Data.CurrencyCode = "CNY"
  118. r.Data.ProofType = "ZNB4"
  119. r.Data.EqSysCode = p.BuyeCode[4:]
  120. // r.Data.Organization = pa.ParchaseOrganization
  121. r.Data.Organization = pa.CompanyCode
  122. r.Data.Group = "A02"
  123. for i, v := range parts {
  124. detail := http.SapOrderDetail{}
  125. c := new(model.Contract)
  126. bc := new(model.BigContract)
  127. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  128. return errors.Wrapf(e, "contract query :id=%d", v.ContractID), ""
  129. }
  130. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  131. return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID), ""
  132. }
  133. detail.RowNumber = fmt.Sprintf("%d", i+1)
  134. detail.Factory = pa.FactoryCode
  135. detail.Quantity = fmt.Sprintf("%d", v.Amount)
  136. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  137. detail.TaxCode = "J0"
  138. detail.Per = "1"
  139. detail.NetPrice = c.Price
  140. detail.Unit = c.Unit
  141. detail.MaterialCode = c.PartCode
  142. detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  143. r.Data.Detail = append(r.Data.Detail, detail)
  144. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  145. log.Errorln(e, v.ID)
  146. }
  147. }
  148. if e := s.SyncSap(r, rp); e != nil && rp.Dest.Status == "S" {
  149. log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  150. return nil, rp.Data.SapNumber
  151. } else {
  152. saplog := new(model.SapLog)
  153. if param, e := json.Marshal(r); e != nil {
  154. log.Errorf("sap order param marshal:%v", e)
  155. } else {
  156. saplog.Param = string(param)
  157. saplog.Name = "sap-order"
  158. s.d.DB.Create(saplog)
  159. }
  160. if e == nil {
  161. return errors.Errorf("sap order fail:%s", rp.Data.MsgText), ""
  162. } else {
  163. return errors.Wrapf(e, "sap order error:%v", e), ""
  164. }
  165. }
  166. }
  167. func AutoSrmOrderJob() {
  168. orders := []model.BigBuyDetail{}
  169. if e := s.d.DB.Where("srmStatus=0 and buyerDate >'2022-10-01'").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  170. log.Errorf("AutoSrmOrderJob error: %v", e)
  171. return
  172. }
  173. for _, v := range orders {
  174. SrmOrder(&v)
  175. }
  176. }
  177. func ManaulSrmOrderJob() {
  178. orders := []model.BigBuyDetail{}
  179. if e := s.d.DB.Where("srmStatus=2").Find(&orders, model.BigBuyDetail{SrmStatus: 0}).Error; e != nil {
  180. log.Errorf("ManaulSrmOrderJob error: %v", e)
  181. return
  182. }
  183. for _, v := range orders {
  184. SrmOrder(&v)
  185. }
  186. }
  187. func SrmOrder(p *model.BigBuyDetail) {
  188. r := new(http.SrmOrderReq)
  189. // rp := new(http.OrderResp)
  190. parts := []model.BuyDetail{}
  191. pa := new(model.Pasture)
  192. pv := new(model.Provider)
  193. // dp := new(model.Department)
  194. r.Dest.DestID = "EQMAN"
  195. r.Dest.BussTp = "MM007"
  196. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder"
  197. if e := s.d.DB.First(pv, model.Provider{ID: p.ProviderID}).Error; e != nil {
  198. log.Error(e)
  199. return
  200. }
  201. if e := s.d.DB.First(pa, model.Pasture{ID: p.PastureID}).Error; e != nil {
  202. log.Errorf("pasture query :id=%d,%v", p.PastureID, e)
  203. return
  204. }
  205. if e := s.d.DB.Where("amount>0").Find(&parts, model.BuyDetail{BigID: p.ID}).Error; e != nil || len(parts) == 0 {
  206. log.Errorf("buydetail query :bigid=%d,%v", p.ID, e)
  207. return
  208. }
  209. // r.Data.IsCancle = ""
  210. // r.Data.PayCondition = ""
  211. r.Data.CompanyCode = pa.CompanyCode
  212. r.Data.VendorErpCode = pv.ProviderNumber
  213. r.Data.PurchaseOrderDate = p.BuyerDate.Format("20060102")
  214. r.Data.CurrencyCode = "CNY"
  215. r.Data.PurchaseOrderType = "ZNB4"
  216. // r.Data.EqSysCode = p.BuyeCode[4:]
  217. // r.Data.Organization = pa.ParchaseOrganization
  218. r.Data.PurchasingOrgCode = pa.CompanyCode
  219. r.Data.PurchasingGroupCode = "A02"
  220. for i, v := range parts {
  221. detail := http.SrmOrderDetail{}
  222. c := new(model.Contract)
  223. bc := new(model.BigContract)
  224. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  225. log.Errorf("order detail error:%v", e)
  226. return
  227. }
  228. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  229. log.Errorf("order detail error:%v", e)
  230. return
  231. }
  232. detail.RowNo = fmt.Sprintf("%d", i+1)
  233. detail.Manufacturer = pa.FactoryCode
  234. detail.OrderQty = fmt.Sprintf("%d", v.Amount)
  235. detail.DeliveryDate = p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102")
  236. detail.TaxRateCode = "J0"
  237. // detail.OrderUnitCode = "1"
  238. detail.NonTaxPrice = c.Price
  239. detail.OrderUnitCode = c.Unit
  240. detail.MaterialCode = c.PartCode
  241. // detail.IsOwn = util.ZeroStr(bc.IsZeroStock == 1, "K")
  242. r.Data.Detail = append(r.Data.Detail, detail)
  243. if e := s.d.DB.Model(&model.BuyDetail{}).Where("id=?", v.ID).Update("rowNumber", i+1).Error; e != nil {
  244. log.Errorln(e, v.ID)
  245. }
  246. }
  247. // if e := s.SyncSap(r, rp); e != nil {
  248. // log.Errorf("sap buy order error: %v", e)
  249. // p.SapStatus = 3
  250. // } else {
  251. // if rp.Dest.Status == "S" {
  252. // log.Infof("buy order success :eq=%s,sap=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  253. // p.SapStatus = 1
  254. // p.SapNumber = rp.Data.SapNumber
  255. // } else {
  256. // log.Warnf("buy order fail :msg=%s,r=%v", rp.Data.MsgText, r)
  257. // p.SapStatus = 2
  258. // pp.Print(r)
  259. // }
  260. // }
  261. // if p.SapStatus == 1 {
  262. // if e := s.d.DB.Where("id=?", p.ID).Updates(&model.BigBuyDetail{SapStatus: p.SapStatus, SapNumber: p.SapNumber}).Error; e != nil {
  263. // log.Errorf("update big buy detail error: %v", e)
  264. // }
  265. // }
  266. }