order.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package http
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. "kpt.xdmy/apiserver/dao"
  6. "kpt.xdmy/apiserver/model"
  7. "kpt.xdmy/pkg/log"
  8. "kpt.xdmy/pkg/util"
  9. )
  10. type OrderResp struct {
  11. Dest `json:"DEST"`
  12. Data OrderRespData `json:"DATA"`
  13. }
  14. type OrderRespData struct {
  15. Status string `json:"MSGTY"`
  16. MsgText string `json:"MSGTX"`
  17. EqNumber string `json:"EXT01"`
  18. SapNumber string `json:"EXT02"`
  19. }
  20. type OrderReq struct {
  21. Dest `json:"DEST"`
  22. Data Order `json:"DATA"`
  23. }
  24. type Order struct {
  25. CompanyCode string `json:"BUKRS"` // 公司代码
  26. SupplierCode string `json:"LIFNR"` // 供应商编码
  27. IsCancle string `json:"ZKHFLG"` // 是否退货 "是:X
  28. ProofType string `json:"BSART"` // 采购凭证类型 字典
  29. OrderDate string `json:"BEDAT"` // 订单日期
  30. Organization string `json:"EKORG"` // 采购组织 字典
  31. Group string `json:"EKGRP"` // 采购组 字典
  32. PayCondition string `json:"ZTERM"` // 付款条件 字典
  33. CurrencyCode string `json:"WAERS"` // 货币码 默认:CNY
  34. EqSysCode string `json:"ZEBELN"` // 订单编号
  35. Detail []OrderDetail `json:"TEKPO"`
  36. }
  37. type OrderDetail struct {
  38. IsOwn string `json:"PSTYP"` // 自有订单,"自有:空// 寄售:'K' "
  39. MaterialCode string `json:"MATNR"` //物料编码
  40. Quantity string `json:"MENGE"` // 数量
  41. DeliveryDate string `json:"EINDT"` // 交货日期
  42. Unit string `json:"MEINS"` // 单位
  43. NetPrice string `json:"NETPR"` // 净价
  44. Per string `json:"PEINH"` // 每 默认:1
  45. Factory string `json:"WERKS"` // 工厂
  46. Location string `json:"LGORT"` //库存地点 字典
  47. IsFree string `json:"UMSON"` //是否免费 "是:X
  48. TaxCode string `json:"MWSKZ"` // 税码 字典
  49. RowNumber string `json:"EBELP"` //行号
  50. Dflag string `json:"LOEKZ"` // 删除标识 "是:L// 否:空"
  51. }
  52. func (o *OrderReq) NewReq(p *model.BigBuyDetail) (e error) {
  53. path := "http OrderReq NewReq"
  54. o.Dest = Dest{
  55. DestID: "EQMAN",
  56. BussTp: "MM007",
  57. Url: "https://app.modernfarming.cn:7443/sap/Common/MM007/PurchaseOrder",
  58. }
  59. pv := &model.Provider{ID: p.ProviderID}
  60. ps := &model.Pasture{ID: p.PastureID}
  61. bds := make([]model.BuyDetail, 0)
  62. c := util.NewMap("bigid", p.ID)
  63. sl := make([]OrderDetail, 0)
  64. if e = model.First(pv); e != nil {
  65. log.Printf("%s:采购订单供应商查不到", path)
  66. return
  67. }
  68. if e = model.First(ps); e != nil {
  69. log.Printf("%s:采购订单牧场查不到", path)
  70. return
  71. }
  72. model.Find(c, &bds)
  73. if len(bds) == 0 {
  74. e = errors.Errorf("%s:采购订单明细查询为空", path)
  75. log.Print(e.Error())
  76. return
  77. }
  78. o.Data = Order{
  79. IsCancle: "",
  80. SupplierCode: "100224",
  81. OrderDate: p.BuyerDate.Format("20060102"),
  82. PayCondition: "",
  83. CompanyCode: "1004",
  84. CurrencyCode: "CNY",
  85. ProofType: "ZNB4",
  86. EqSysCode: p.BuyeCode[4:],
  87. // EqSysCode: "A0001",
  88. Organization: "9099",
  89. Group: "A02",
  90. // CompanyCode: ps.PastureNumber,
  91. // SupplierCode: pv.ProviderNumber,
  92. // OrderDate: p.BuyerDate.Format("20060102"),
  93. }
  94. for _, v := range bds {
  95. dao.D.DB.Raw(`SELECT p.partcode from parts p join contract c on c.partid=p.id where
  96. c.id=?`, v.ContractID).Scan(&v.PartCode)
  97. pd := OrderDetail{
  98. // Quantity: v.Amount,
  99. // DeliveryDate: v.ReceiveDate.Format("20060102"),
  100. // IsOwn: util.ZeroStr(c.IsZeroStock == 1, "K"),
  101. // NetPrice: v.Price,
  102. // Unit: ps.Unit,
  103. // Factory: p.Data.CompanyCode,
  104. // Location: "",
  105. // IsFree: "",
  106. MaterialCode: v.PartCode,
  107. // MaterialCode: "14.01.07.01.000243",
  108. Quantity: "11",
  109. DeliveryDate: p.BuyerDate.Add(time.Hour * 24 * 7).Format("20060102"),
  110. Unit: "EA",
  111. NetPrice: "11",
  112. Per: "1",
  113. Factory: "M001",
  114. TaxCode: "J0",
  115. RowNumber: "10",
  116. }
  117. sl = append(sl, pd)
  118. }
  119. o.Data.Detail = sl
  120. return
  121. }