proof.go 12 KB

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