proof.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package service
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. _ "github.com/go-sql-driver/mysql"
  8. "github.com/k0kubun/pp/v3"
  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) LaidProof(p interface{}) {
  16. pr := new(http.ProofPartReq)
  17. r := new(http.ProofReq)
  18. rp := new(http.ProofResp)
  19. parts := []model.PartLaid{}
  20. bp := new(model.BigPartLaid)
  21. bbd := new(model.BigBuyDetail)
  22. pa := new(model.Pasture)
  23. if e := util.Unmarshal(p, pr); e != nil {
  24. log.Error(e, p)
  25. return
  26. }
  27. if e := s.d.DB.First(bp, model.BigPartLaid{LaidCode: pr.LaidCode}).Error; e != nil {
  28. log.Error(e)
  29. return
  30. }
  31. if e := s.d.DB.Find(&parts, model.PartLaid{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  32. log.Error(e)
  33. return
  34. }
  35. if e := s.d.DB.First(bbd, model.BigBuyDetail{BuyeCode: bp.PurchaseID}).Error; e != nil {
  36. log.Error(e)
  37. }
  38. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  39. log.Error(e)
  40. return
  41. }
  42. proofDest(r)
  43. r.Data.EqNumber = pr.LaidCode
  44. r.Data.CreateTime = time.Now().Format("20060102")
  45. if t, e := util.ParseDate(pr.StorageTime); e != nil {
  46. log.Errorf("parse time error:err=%v,param=%v", e, pr.StorageTime)
  47. } else {
  48. r.Data.PostDate = t.Format("20060102")
  49. }
  50. for i, v := range parts {
  51. sdetail := http.ProofDetail{MoveType: "101"}
  52. c := new(model.Contract)
  53. bc := new(model.BigContract)
  54. bd := new(model.BuyDetail)
  55. w := new(model.Warehouse)
  56. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  57. log.Errorf("contract query error:%v", e)
  58. return
  59. }
  60. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  61. log.Errorf("bigcontract query error:%v", e)
  62. return
  63. }
  64. if e := s.d.DB.First(bd, model.BuyDetail{BigID: bbd.ID, ContractID: v.ContractID}).Error; e != nil {
  65. log.Error(e)
  66. return
  67. }
  68. if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  69. log.Error(e)
  70. return
  71. }
  72. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  73. sdetail.Quantity = fmt.Sprintf("%f", v.StorageAmount)
  74. sdetail.Unit = c.Unit
  75. sdetail.Code = c.PartCode
  76. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  77. sdetail.Factory = pa.FactoryCode
  78. sdetail.Location = w.WarehoseCode
  79. sdetail.OrderNumber = bbd.SapNumber
  80. sdetail.OrderRowNum = fmt.Sprintf("%d", bd.RowNumber)
  81. r.Data.Detail = append(r.Data.Detail, sdetail)
  82. }
  83. if e := s.SyncSap(r, rp); e != nil {
  84. log.Error(e)
  85. } else {
  86. if rp.Dest.Status == "S" {
  87. log.Printf("laid proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber)
  88. if e := s.d.DB.Model(&model.BigPartLaid{}).Where("laidcode = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  89. log.Errorln(e)
  90. }
  91. } else {
  92. log.Warnf("%s laid proof failed:causetext=%s", rp.Dest.MessText)
  93. pp.Print(r)
  94. // if b, e := json.MarshalIndent(r, "", " "); e != nil {
  95. // log.Error("json.MarshalIndent error")
  96. // } else {
  97. // fmt.Println(string(b))
  98. // }
  99. }
  100. }
  101. }
  102. // 出库单推送sap,包括了报废出库
  103. func (s *Service) UseProof(p interface{}) {
  104. pr := new(http.ProofPartReq)
  105. r := new(http.ProofReq)
  106. rp := new(http.ProofResp)
  107. parts := []model.PartUse{}
  108. bp := new(model.BigPartUse)
  109. pa := new(model.Pasture)
  110. dp := new(model.Department)
  111. if e := util.Unmarshal(p, pr); e != nil {
  112. log.Error(e)
  113. return
  114. }
  115. if e := s.d.DB.First(bp, model.BigPartUse{UseForm: pr.UseForm}).Error; e != nil {
  116. log.Error(e)
  117. return
  118. }
  119. if e := s.d.DB.Find(&parts, model.PartUse{BigID: bp.ID}).Error; e != nil {
  120. log.Error(e)
  121. return
  122. }
  123. // pp.Print(bp)
  124. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  125. log.Error(e)
  126. return
  127. }
  128. if e := s.d.DB.First(dp, model.Department{ID: bp.DepartmentID}).Error; e != nil {
  129. log.Error(e)
  130. return
  131. }
  132. // pp.Println(bp, d)
  133. proofDest(r)
  134. r.Data.EqNumber = pr.UseForm
  135. r.Data.CreateTime = time.Now().Format("20060102")
  136. if t, e := util.ParseDate(pr.ReceiveTime); e != nil {
  137. log.Error(e, pr.ReceiveTime)
  138. } else {
  139. r.Data.PostDate = t.Format("20060102")
  140. }
  141. for i, v := range parts {
  142. bc := new(model.BigContract)
  143. c := new(model.Contract)
  144. w := new(model.Warehouse)
  145. pv := new(model.Provider)
  146. eq := new(model.Equipment)
  147. storage := new(model.PartRepertory)
  148. if v.ContractID < 0 {
  149. if e := s.d.DB.First(storage, model.PartRepertory{ContractID: v.ContractID}).Error; e != nil {
  150. log.Errorln(e, v.ContractID)
  151. return
  152. }
  153. if e := s.d.DB.First(pv, model.Provider{ID: storage.ProviderID}).Error; e != nil {
  154. log.Errorln(e, storage.ProviderID)
  155. return
  156. }
  157. } else {
  158. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  159. log.Errorf("contract query error:%v", e)
  160. return
  161. }
  162. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  163. log.Errorf("bigcontract query error:%v", e)
  164. return
  165. }
  166. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  167. log.Errorln(e, bc.ProviderID)
  168. return
  169. }
  170. }
  171. if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  172. log.Errorln(e, v.LocationID)
  173. return
  174. }
  175. if e := s.d.DB.First(eq, model.Equipment{Id: v.EqID}).Error; e != nil {
  176. log.Errorln(e, v.EqID)
  177. return
  178. }
  179. sdetail := http.ProofDetail{}
  180. // 报废出库
  181. if pr.UseType == 5 {
  182. sdetail.MoveType = "Z19"
  183. if v.ContractID > 0 && bc.IsZeroStock != 0 {
  184. log.Info("寄售不能报废=======")
  185. return
  186. }
  187. }
  188. // 领用出库、手动出库
  189. if pr.UseType == 0 || pr.UseType == 1 {
  190. sdetail.MoveType = "201"
  191. sdetail.CostCenter = dp.CostCenterCode
  192. }
  193. sdetail.Factory = pa.FactoryCode
  194. if pa.FactoryCode == "M005" && strings.Contains(dp.Name, "二期") {
  195. sdetail.Factory = "M017"
  196. }
  197. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  198. sdetail.Quantity = fmt.Sprintf("%f", v.CheckoutNumber)
  199. // sdetail.Unit = c.Unit
  200. sdetail.Unit = v.Unit
  201. // sdetail.Code = c.PartCode
  202. sdetail.Code = v.PartCode
  203. sdetail.AssetCode = eq.FinanceCode
  204. sdetail.Location = w.WarehoseCode
  205. if v.ContractID > 0 && bc.IsZeroStock == 1 {
  206. sdetail.SaveType = "K"
  207. }
  208. if sdetail.SaveType == "K" {
  209. sdetail.Provider = pv.ProviderNumber
  210. }
  211. r.Data.Detail = append(r.Data.Detail, sdetail)
  212. }
  213. pp.Print(r)
  214. log.Infof("bp.pastureId:%d,pa.name:%s,pa.companycode:%s", bp.PastureID, pa.Name, pa.CompanyCode)
  215. if e := s.SyncSap(r, rp); e != nil {
  216. log.Errorf("laid proof error:%v", e)
  217. } else {
  218. if rp.Dest.Status == "S" {
  219. log.Printf("use proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  220. if e := s.d.DB.Model(&model.BigPartUse{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  221. log.Errorln(e)
  222. }
  223. } else {
  224. log.Warnf(" use proof :%s", rp.Dest.MessText)
  225. }
  226. }
  227. }
  228. // 退库单推送sap
  229. func (s *Service) QuitProof(p interface{}) {
  230. pr := new(http.ProofPartReq)
  231. r := new(http.ProofReq)
  232. rp := new(http.ProofResp)
  233. parts := []model.Partquit{}
  234. bp := new(model.BigPartquit)
  235. pa := new(model.Pasture)
  236. d := new(model.Department)
  237. if e := util.Unmarshal(p, pr); e != nil {
  238. log.Error(e)
  239. return
  240. }
  241. if e := s.d.DB.First(bp, model.BigPartquit{UseForm: pr.UseForm}).Error; e != nil {
  242. log.Error(e)
  243. return
  244. }
  245. if e := s.d.DB.Find(&parts, model.Partquit{BigID: bp.ID}).Error; e != nil {
  246. log.Error(e)
  247. return
  248. }
  249. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  250. log.Error(e)
  251. return
  252. }
  253. if e := s.d.DB.First(d, model.Department{ID: bp.DeptID}).Error; e != nil {
  254. log.Error(e)
  255. return
  256. }
  257. proofDest(r)
  258. r.Data.EqNumber = pr.UseForm
  259. r.Data.CreateTime = time.Now().Format("20060102")
  260. if t, e := util.ParseDate(pr.QuitDate); e != nil {
  261. log.Error(e, pr.QuitDate)
  262. } else {
  263. r.Data.PostDate = t.Format("20060102")
  264. }
  265. for i, v := range parts {
  266. c := new(model.Contract)
  267. bc := new(model.BigContract)
  268. pv := new(model.Provider)
  269. // w := new(model.Warehouse)
  270. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  271. log.Errorf("contract query error:%v", e)
  272. return
  273. }
  274. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  275. log.Errorf("bigcontract query error:%v", e)
  276. return
  277. }
  278. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  279. log.Errorln(e, bc.ProviderID)
  280. return
  281. }
  282. sdetail := http.ProofDetail{MoveType: "202"}
  283. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  284. sdetail.Quantity = fmt.Sprintf("%f", v.QuitNumber)
  285. sdetail.Unit = c.Unit
  286. sdetail.Code = c.PartCode
  287. sdetail.Factory = pa.FactoryCode
  288. sdetail.CostCenter = d.CostCenterCode
  289. sdetail.Location = v.Location
  290. sdetail.Provider = pv.ProviderNumber
  291. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  292. r.Data.Detail = append(r.Data.Detail, sdetail)
  293. }
  294. if e := s.SyncSap(r, rp); e != nil {
  295. log.Errorf("laid proof error:%v", e)
  296. } else {
  297. if rp.Dest.Status == "S" {
  298. log.Printf("quit proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  299. if e := s.d.DB.Model(&model.BigPartquit{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  300. log.Errorln(e)
  301. }
  302. } else {
  303. log.Warnf("%s quit proof failed:causetext=%s", rp.Dest.MessText)
  304. pp.Print(r)
  305. }
  306. }
  307. }
  308. // 退货单推送sap
  309. func (s *Service) RefundProof(p interface{}) {
  310. pr := new(model.RefundParam)
  311. r := new(http.ProofReq)
  312. rp := new(http.ProofResp)
  313. parts := []model.PartRefund{}
  314. bp := new(model.BigPartRefund)
  315. pa := new(model.Pasture)
  316. if e := util.Unmarshal(p, pr); e != nil {
  317. log.Error(e)
  318. log.Info("%v", pr)
  319. return
  320. }
  321. pid, err := strconv.ParseInt(pr.ID, 10, 32)
  322. if err != nil {
  323. log.Errorf("parseInt purchase id :%v", err)
  324. }
  325. if e := s.d.DB.First(bp, model.BigPartRefund{ID: int(pid)}).Error; e != nil {
  326. log.Error(e)
  327. return
  328. }
  329. if e := s.d.DB.Find(&parts, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  330. log.Error(e)
  331. return
  332. }
  333. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  334. log.Error(e)
  335. return
  336. }
  337. proofDest(r)
  338. r.Data.EqNumber = bp.UseForm
  339. r.Data.PostDate = bp.CreateTime.Format("20060102")
  340. r.Data.CreateTime = time.Now().Format("20060102")
  341. for i, v := range parts {
  342. sdetail := http.ProofDetail{}
  343. c := new(model.Contract)
  344. bc := new(model.BigContract)
  345. bd := new(model.BuyDetail)
  346. bbd := new(model.BigBuyDetail)
  347. pv := new(model.Provider)
  348. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  349. log.Errorf("contract query error:%v", e)
  350. return
  351. }
  352. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  353. log.Errorf("bigcontract query error:%v", e)
  354. return
  355. }
  356. if e := s.d.DB.Order("id desc").First(bd, model.BuyDetail{ContractID: v.ContractID}).Error; e != nil {
  357. log.Error(e)
  358. return
  359. }
  360. if e := s.d.DB.First(bbd, model.BigBuyDetail{ID: bd.BigID}).Error; e != nil {
  361. log.Errorf("refund proof bigbuydetail:id=%d,%v", bd.BigID, e)
  362. return
  363. }
  364. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  365. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  366. return
  367. }
  368. if bc.IsZeroStock == 0 {
  369. sdetail.MoveType = "161"
  370. } else {
  371. sdetail.MoveType = "502"
  372. }
  373. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  374. sdetail.Quantity = fmt.Sprintf("%f", v.RefundNumber)
  375. sdetail.Unit = c.Unit
  376. sdetail.Code = c.PartCode
  377. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  378. sdetail.Factory = pa.FactoryCode
  379. sdetail.Location = v.Location
  380. sdetail.OrderNumber = bbd.SapNumber
  381. sdetail.OrderRowNum = fmt.Sprintf("%d", bd.RowNumber)
  382. sdetail.Provider = pv.ProviderNumber
  383. r.Data.Detail = append(r.Data.Detail, sdetail)
  384. }
  385. if e := s.SyncSap(r, rp); e != nil {
  386. log.Error(e)
  387. } else {
  388. if rp.Dest.Status == "S" {
  389. log.Printf("refund proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber)
  390. if e := s.d.DB.Model(&model.BigPartRefund{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  391. log.Errorln(e)
  392. }
  393. } else {
  394. log.Warnf("return good fail: %s", rp.Dest.MessText)
  395. // if b, e := json.MarshalIndent(r, "", " "); e != nil {
  396. // log.Error("json.MarshalIndent error")
  397. // } else {
  398. // fmt.Println(string(b))
  399. // }
  400. }
  401. }
  402. }
  403. func proofDest(p *http.ProofReq) {
  404. p.Dest.DestID = "EQMAN"
  405. p.Dest.BussTp = "MM001"
  406. p.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM001/ModifyMblnr"
  407. }