proof.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/jmoiron/sqlx"
  6. "github.com/pkg/errors"
  7. "github.com/xormplus/xorm/schemas"
  8. "kpt.xdmy/apiserver/config"
  9. "reflect"
  10. "regexp"
  11. "strconv"
  12. "strings"
  13. "time"
  14. _ "github.com/go-sql-driver/mysql"
  15. "github.com/k0kubun/pp/v3"
  16. log "github.com/sirupsen/logrus"
  17. "kpt.xdmy/apiserver/model"
  18. "kpt.xdmy/apiserver/model/http"
  19. "kpt.xdmy/pkg/util"
  20. )
  21. // sap入库凭证接口,业务触发
  22. func (s *Service) LaidProof(p interface{}, tx *sqlx.Tx) error {
  23. pr := new(http.ProofPartReq)
  24. r := new(http.ProofReq)
  25. rp := new(http.ProofResp)
  26. parts := make([]*model.PartLaidStr, 0)
  27. //buyDetail := []model.BuyDetail{}
  28. //pc := new(model.Bigpartpurchase)
  29. //dp := new(model.Department)
  30. //prs := []model.BigPartLaid{}
  31. if e := util.Unmarshal(p, pr); e != nil {
  32. log.Error(e, p)
  33. return errors.New("转码失败!!!")
  34. }
  35. bp := new(model.BigPartLaid)
  36. bbd := new(model.BigBuyDetailStr)
  37. pa := new(model.Pasture)
  38. bigpartlaidByte, err := MapStr(tx, `select * from bigpartlaid where laidCode = ? `, pr.LaidCode)
  39. if err != nil {
  40. log.Error(err)
  41. return errors.New("无入库信息!!!")
  42. }
  43. if bigpartlaidByte != nil {
  44. data := make(map[string]interface{})
  45. err := json.Unmarshal(bigpartlaidByte, &data)
  46. if err != nil {
  47. log.Error(err)
  48. return errors.New("入库信息转码出错!!!")
  49. }
  50. bp.ID, _ = strconv.Atoi(data["id"].(string))
  51. bp.PurchaseID, _ = data["purchaseId"].(string)
  52. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  53. bp.ProofCode, _ = data["ProofCode"].(string)
  54. }
  55. if len(bp.ProofCode) > 1 {
  56. return errors.New(fmt.Sprintf("errors"))
  57. }
  58. partlaidByte, err := GetDataList(tx, `select * from partlaid where bigid = ? `, []interface{}{bp.ID})
  59. if err != nil {
  60. log.Error(err)
  61. return errors.New("无入库详情信息!!!")
  62. }
  63. if partlaidByte != nil {
  64. err := json.Unmarshal(partlaidByte, &parts)
  65. if err != nil {
  66. log.Error(err)
  67. return errors.New("入库信息转码出错!!!")
  68. }
  69. }
  70. bigbuydetailByte, err := MapStr(tx, `select * from bigbuydetail where buyeCode = ? `, bp.PurchaseID)
  71. if err != nil {
  72. log.Error(err)
  73. return errors.New("无入库信息!!!")
  74. }
  75. if bigbuydetailByte != nil {
  76. err := json.Unmarshal(bigbuydetailByte, &bbd)
  77. if err != nil {
  78. log.Error(err)
  79. return errors.New("入库信息转码出错!!!")
  80. }
  81. }
  82. if e := s.d.DB.Raw(` select * from pasture where id = ? `, bp.PastureID).First(pa).Error; e != nil {
  83. log.Error(e)
  84. return errors.New("查询牧场详情出错!!!")
  85. }
  86. proofDest(r)
  87. r.Dest.MESSIDOS = pr.LaidCode
  88. r.Dest.BUSSDOCOS = pr.LaidCode
  89. r.Data.EqNumber = pr.LaidCode
  90. r.Data.CreateTime = time.Now().Format("20060102")
  91. if t, e := util.ParseDate(pr.StorageTime[:10]); e != nil {
  92. log.Errorf("parse time error:err=%v,param=%v", e, pr.StorageTime)
  93. } else {
  94. r.Data.PostDate = t.Format("20060102")
  95. }
  96. for i, v := range parts {
  97. if v.StorageAmount == "0" {
  98. continue
  99. }
  100. sdetail := http.ProofDetail{MoveType: "101"}
  101. c := new(model.Contract)
  102. bc := new(model.BigContract)
  103. bd := new(model.BuyDetailStr)
  104. pro := new(model.Provider)
  105. //w := new(model.Warehouse)
  106. contractID, _ := v.ContractID.(float64)
  107. if e := s.d.DB.Raw(` select * from contract where id = ?`, contractID).First(c).Error; e != nil {
  108. log.Errorf("contract query error:%v", e)
  109. //return
  110. }
  111. if e := s.d.DB.Raw(`select * from bigcontract where id = ? `, c.BigID).First(bc).Error; e != nil {
  112. log.Errorf("bigcontract query error:%v", e)
  113. //return
  114. }
  115. //if e := s.d.DB.Where("id = ?", v.LocationID).First(w).Error; e != nil {
  116. // log.Error(e)
  117. // return errors.New("查询库位信息出错!!!")
  118. //}
  119. if e := s.d.DB.Raw(`select * from provider where id = ? `, v.ProviderID).First(pro).Error; e != nil {
  120. log.Error(e)
  121. return errors.New("查询供应商信息出错!!!")
  122. }
  123. if contractID > 0 {
  124. buydetailByte, err := MapStr(tx, `select * from buydetail where bigid = ? and contractId = ? `, bbd.ID, v.ContractID)
  125. if err != nil {
  126. log.Error(err)
  127. //return errors.New("查询供应商信息出错!!!")
  128. }
  129. if buydetailByte != nil {
  130. err := json.Unmarshal(buydetailByte, &bd)
  131. if err != nil {
  132. return errors.New("查询BuyDetail转码出错!!!")
  133. }
  134. }
  135. sdetail.OrderRowNum = bd.RowNumber
  136. } else {
  137. buydetailByte, err := MapStr(tx, `select * from buydetail where bigid = ? and partCode = ? `, bbd.ID, v.PartCode)
  138. if err != nil {
  139. log.Error(err)
  140. //return errors.New("查询供应商信息出错!!!")
  141. }
  142. if buydetailByte != nil {
  143. err := json.Unmarshal(buydetailByte, &bd)
  144. if err != nil {
  145. return errors.New("查询BuyDetail转码出错!!!")
  146. }
  147. }
  148. sdetail.OrderRowNum = bd.RowNumber
  149. }
  150. //rowNumber, _ :=
  151. if v.RowNumber.(float64) > 0 {
  152. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  153. } else {
  154. sdetail.RowNumber = fmt.Sprintf("%d", (i + 1))
  155. }
  156. sdetail.Quantity = fmt.Sprintf("%v", v.StorageAmount)
  157. sdetail.Unit = v.Unit.(string)
  158. sdetail.Code = v.PartCode.(string)
  159. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  160. //if dp.Factory == "" {
  161. sdetail.Factory = pa.FactoryCode
  162. //} else {
  163. // sdetail.Factory = dp.Factory
  164. //}
  165. sdetail.Location = "1004"
  166. sdetail.OrderNumber = bbd.SapNumber
  167. sdetail.Provider = pro.SapCode
  168. r.Data.Detail = append(r.Data.Detail, sdetail)
  169. //if e := s.d.DB.Model(&model.PartLaid{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil {
  170. // log.Errorln(e, v.ID)
  171. //}
  172. _, err = tx.Exec(` update partlaid set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  173. if err != nil {
  174. return err
  175. }
  176. }
  177. ///*
  178. rbyte, _ := json.Marshal(r)
  179. fmt.Println(string(rbyte))
  180. if e := s.SyncSap(r, rp, rbyte); e != nil {
  181. log.Error(e)
  182. fmt.Println(e)
  183. return errors.New(fmt.Sprintf("传输sap errors:%s", r.MessText))
  184. } else {
  185. if rp.Dest.Status == "S" {
  186. log.Printf("laid proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber)
  187. tx.Exec(` update bigpartlaid set ProofCode = ? where laidcode = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  188. } else {
  189. log.Warnf("%s laid proof failed:causetext=%s", rp.Dest.MessText, rp.Dest.MessText)
  190. pp.Print(r)
  191. if strings.Index(rp.Dest.MessText, "采购信息记录") > -1 {
  192. return errors.New("SAP中没有相应的合同\n解决方案:合同管理页面备件明细最左侧勾选没有合同的备件,点击SAP上传按钮。十分钟后再次进行入库操作。")
  193. }
  194. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  195. }
  196. }
  197. //*/
  198. return nil
  199. }
  200. // UseProof 出库单推送sap,包括了报废出库
  201. func (s *Service) UseProof(p interface{}, tx *sqlx.Tx) error {
  202. pr := new(http.ProofPartReq)
  203. r := new(http.ProofReq)
  204. rp := new(http.ProofResp)
  205. parts := make([]*model.PartUseStr, 0)
  206. bp := new(model.BigPartUse)
  207. pa := new(model.Pasture)
  208. dp := new(model.Department)
  209. if e := util.Unmarshal(p, pr); e != nil {
  210. log.Error(e)
  211. return errors.New(fmt.Sprintf("数据转码 errors:%v", e))
  212. }
  213. bigpartUseByte, err := MapStr(tx, `select * from bigpartUse where useForm = ? `, pr.UseForm)
  214. if err != nil {
  215. log.Error(err)
  216. return errors.New(fmt.Sprintf("查询不到出库单 errors:%v", err))
  217. }
  218. if bigpartUseByte != nil {
  219. data := make(map[string]interface{})
  220. err := json.Unmarshal(bigpartUseByte, &data)
  221. if err != nil {
  222. log.Error(err)
  223. return errors.New(fmt.Sprintf("查询出库单 errors:%v", err))
  224. }
  225. bp.ID, _ = strconv.Atoi(data["id"].(string))
  226. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  227. bp.ProofCode, _ = data["ProofCode"].(string)
  228. bp.DepartmentID, _ = strconv.Atoi(data["departmentId"].(string))
  229. bp.UseType, _ = strconv.Atoi(data["useType"].(string))
  230. if data["useType"] != "6" {
  231. if _, ok := data["oddCode"]; !ok {
  232. return errors.New(fmt.Sprintf("查询出库单 OddCode 出错!!!"))
  233. }
  234. bp.OddCode = data["oddCode"].(string)
  235. if bp.OddCode == "" {
  236. return errors.New(fmt.Sprintf("查询出库单 OddCode 出错!!!"))
  237. }
  238. }
  239. }
  240. if len(bp.ProofCode) > 1 {
  241. return errors.New("出库单已传输Sap!!!")
  242. }
  243. partusebyte, err := GetDataList(tx, ` select * from partuse where bigid = ?`, []interface{}{bp.ID})
  244. if err != nil {
  245. log.Error(err)
  246. return errors.New("查询入库详情出错!!!")
  247. }
  248. if partusebyte != nil {
  249. err := json.Unmarshal(partusebyte, &parts)
  250. if err != nil {
  251. log.Error(err)
  252. return errors.New("查询入库详情出错!!!")
  253. }
  254. }
  255. pp.Print(bp)
  256. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  257. log.Error(e)
  258. return errors.New(fmt.Sprintf("查询牧场信息 errors:%v", e))
  259. }
  260. //if e := s.d.DB.First(dp, model.big{ID: bp.DepartmentID, PastureID: bp.PastureID}).Error; e != nil {
  261. // log.Error(e)
  262. // return errors.New("查询部门信息出错!!!")
  263. //}
  264. departmentSqlStr := fmt.Sprintf("select * from Department where id = (select departmentId from bigpartapply where applyCode = '%s' )", bp.OddCode)
  265. if bp.UseType == 6 {
  266. departmentSqlStr = fmt.Sprintf("select * from Department where id = %d", bp.DepartmentID)
  267. }
  268. if e := s.d.DB.Raw(departmentSqlStr).First(dp).Error; e != nil {
  269. log.Error(e)
  270. return errors.New("查询部门信息出错!!!")
  271. }
  272. // pp.Println(bp, d)
  273. proofDest(r)
  274. //b, _ := json.Marshal(pr)
  275. //restful.Dbs.DB.Exec(` insert into equipmentlingshi(text)values(?)`, string(b))
  276. r.Dest.MESSIDOS = pr.UseForm
  277. r.Dest.BUSSDOCOS = pr.UseForm
  278. r.Data.EqNumber = pr.UseForm
  279. r.Data.CreateTime = time.Now().Format("20060102")
  280. if t, e := util.ParseDate(pr.ReceiveTime[:10]); e != nil {
  281. log.Error(e, pr.ReceiveTime)
  282. } else {
  283. r.Data.PostDate = t.Format("20060102")
  284. }
  285. for i, v := range parts {
  286. bc := new(model.BigContract)
  287. c := new(model.Contract)
  288. //w := new(model.Warehouse)
  289. pv := new(model.Provider)
  290. eq := new(model.Equipment)
  291. storage := new(model.PartRepertory)
  292. dict := new(model.Dictlist)
  293. contractID, _ := strconv.Atoi(v.ContractID.(string))
  294. if contractID < 0 {
  295. if e := s.d.DB.First(storage, model.PartRepertory{ContractID: contractID}).Error; e != nil {
  296. log.Errorln(e, v.ContractID)
  297. return errors.New(fmt.Sprintf("查询库存信息出错 errors:%v", e))
  298. }
  299. if e := s.d.DB.First(pv, model.Provider{ID: storage.ProviderID}).Error; e != nil {
  300. log.Errorln(e, storage.ProviderID)
  301. return errors.New(fmt.Sprintf("查询供应商信息出错 errors:%v", e))
  302. }
  303. } else {
  304. if e := s.d.DB.Where("id = ? ", v.ContractID).First(c).Error; e != nil {
  305. log.Errorf("contract query error:%v", e)
  306. return errors.New(fmt.Sprintf("查询合同信息 errors:%v", e))
  307. }
  308. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  309. log.Errorf("bigcontract query error:%v", e)
  310. return errors.New(fmt.Sprintf("查询合同信息 errors:%v", e))
  311. }
  312. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  313. log.Errorln(e, bc.ProviderID)
  314. return errors.New(fmt.Sprintf("无供应商信息请检查合同%s对应的供应商", bc.ContractCode))
  315. }
  316. }
  317. s.d.DB.First(dict, model.Dictlist{Pid: 21, Value: v.UseTypeV.(string)})
  318. //if e := s.d.DB.First(w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  319. // log.Errorln(e, v.LocationID)
  320. // return errors.New(fmt.Sprintf("errors:%v", e))
  321. //}
  322. if e := s.d.DB.Where("eqCode = ? and pastureid = ? ", v.EqCode, pr.PastureID).First(eq).Error; e != nil {
  323. log.Errorln(e, v.EqID)
  324. return errors.New(fmt.Sprintf("errors:%v", e))
  325. }
  326. sdetail := http.ProofDetail{}
  327. sdetail.EXT01 = dict.Label
  328. // 报废出库
  329. if pr.UseType == 5 {
  330. sdetail.MoveType = "Z19"
  331. if contractID > 0 && bc.IsZeroStock != 0 {
  332. log.Info("寄售不能报废=======")
  333. return errors.New(fmt.Sprintf("寄售不能报废======= errors"))
  334. }
  335. }
  336. // 领用出库、手动出库
  337. //if pr.UseType == 0 || pr.UseType == 1 {
  338. sdetail.MoveType = "201"
  339. //if dp.Factory == "" {
  340. sdetail.Factory = pa.FactoryCode
  341. //} else {
  342. // sdetail.Factory = dp.Factory
  343. //}
  344. // 青贮技术
  345. if pr.UseType == 6 {
  346. sdetail.MoveType = "Z69"
  347. sdetail.AUFNR = pr.GreenFodderNumber
  348. } else {
  349. sdetail.CostCenter = dp.CostCenterCode
  350. }
  351. if sdetail.Factory == "M005" && strings.Contains(dp.Name, "二期") {
  352. sdetail.Factory = "M017"
  353. }
  354. if v.RowNumber.(float64) > 0 {
  355. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  356. } else {
  357. sdetail.RowNumber = fmt.Sprintf("%d", (i + 1))
  358. }
  359. sdetail.Quantity = fmt.Sprintf("%v", v.CheckoutNumber)
  360. sdetail.Unit = v.Unit.(string)
  361. sdetail.Code = v.PartCode.(string)
  362. sdetail.AssetCode = eq.SapCode
  363. sdetail.AssetSon = eq.SonCode
  364. sdetail.Location = "1004"
  365. if contractID > 0 && bc.IsZeroStock == 1 {
  366. sdetail.SaveType = "K"
  367. }
  368. if sdetail.SaveType == "K" {
  369. sdetail.Provider = pv.SapCode
  370. }
  371. r.Data.Detail = append(r.Data.Detail, sdetail)
  372. //if e := s.d.DB.Model(&model.PartUse{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil {
  373. // log.Errorln(e, v.ID)
  374. //}
  375. _, err = tx.Exec(` update partuse set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  376. if err != nil {
  377. return err
  378. }
  379. }
  380. //}
  381. ///*
  382. pp.Print(r)
  383. log.Infof("bp.pastureId:%d,pa.name:%s,pa.companycode:%s", bp.PastureID, pa.Name, pa.CompanyCode)
  384. rbyte, _ := json.Marshal(r)
  385. fmt.Println(string(rbyte))
  386. if e := s.SyncSap(r, rp, rbyte); e != nil {
  387. log.Errorf("laid proof error:%v", e)
  388. return errors.New(fmt.Sprintf("errors:%s", r.MessText))
  389. } else {
  390. if rp.Dest.Status == "S" {
  391. log.Printf("use proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  392. //if e := s.d.DB.Model(&model.BigPartUse{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  393. // log.Errorln(e)
  394. //}
  395. tx.Exec(` update bigpartuse set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  396. } else {
  397. log.Warnf(" use proof :%s", rp.Dest.MessText)
  398. fmt.Println(strings.Index(rp.Dest.MessText, "短缺"))
  399. if strings.Index(rp.Dest.MessText, "短缺") > -1 {
  400. messText, err := extractCode(rp.Dest.MessText)
  401. if err != nil {
  402. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  403. }
  404. return errors.New(fmt.Sprintf("SAP库存不足 %s "+
  405. "\n解决方案:在SAP系统中查询MB51,核对该备件库存。", strings.Join(messText, ",")))
  406. }
  407. if strings.Index(rp.Dest.MessText, "管道") > -1 {
  408. return errors.New("SAP中没有相应的合同\n解决方案:合同管理页面备件明细最左侧勾选没有合同的备件,点击SAP上传按钮。十分钟后再次进行入库操作。")
  409. }
  410. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  411. }
  412. }
  413. return nil
  414. }
  415. // 退库单推送sap
  416. func (s *Service) QuitProof(p interface{}, tx *sqlx.Tx) error {
  417. pr := new(http.ProofPartReq)
  418. r := new(http.ProofReq)
  419. rp := new(http.ProofResp)
  420. parts := make([]*model.PartquitStr, 0)
  421. bp := new(model.BigPartquit)
  422. pa := new(model.Pasture)
  423. d := new(model.Department)
  424. if e := util.Unmarshal(p, pr); e != nil {
  425. log.Error(e)
  426. return errors.New(fmt.Sprintf("errors:%v", e))
  427. }
  428. //defer func() {
  429. // if msg := recover(); msg != nil {
  430. // fmt.Println("panic信息:", msg, "---recover恢复---")
  431. // }
  432. //}()
  433. //if e := s.d.DB.First(bp, model.BigPartquit{UseForm: pr.UseForm}).Error; e != nil {
  434. // log.Error(e)
  435. // return errors.New(fmt.Sprintf("errors:%v", e))
  436. //}
  437. bigpartquitByte, err := MapStr(tx, `select * from bigpartquit where useForm = ? `, pr.UseForm)
  438. if err != nil {
  439. log.Error(err)
  440. return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err))
  441. }
  442. if bigpartquitByte != nil {
  443. data := make(map[string]interface{})
  444. err := json.Unmarshal(bigpartquitByte, &data)
  445. if err != nil {
  446. log.Error(err)
  447. return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err))
  448. }
  449. bp.ID, _ = strconv.Atoi(data["id"].(string))
  450. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  451. bp.ProofCode, _ = data["ProofCode"].(string)
  452. bp.DeptID, _ = strconv.Atoi(data["deptId"].(string))
  453. }
  454. if len(bp.ProofCode) > 1 {
  455. return errors.New(fmt.Sprintf("errors"))
  456. }
  457. partquitByte, err := GetDataList(tx, `select * from partquit where bigid = ? `, []interface{}{bp.ID})
  458. if err != nil {
  459. log.Error(err)
  460. return errors.New("无退货详情信息!!!")
  461. }
  462. if partquitByte != nil {
  463. err := json.Unmarshal(partquitByte, &parts)
  464. if err != nil {
  465. log.Error(err)
  466. return errors.New("退货详情信息转码出错!!!")
  467. }
  468. }
  469. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  470. log.Error(e)
  471. return errors.New(fmt.Sprintf("无牧场信息 errors:%v", err))
  472. }
  473. if e := s.d.DB.First(d, model.Department{ID: bp.DeptID}).Error; e != nil {
  474. log.Error(e)
  475. return errors.New(fmt.Sprintf("无部门信息 errors:%v", err))
  476. }
  477. var useFormCount int64
  478. if e := s.d.DB.Table("bigpartquit").Where(" useForm = ? ", pr.UseForm).Count(&useFormCount).Error; e != nil {
  479. log.Error(e)
  480. }
  481. useForm := fmt.Sprintf("%stk%d", pr.UseForm, useFormCount+1)
  482. proofDest(r)
  483. r.Dest.MESSIDOS = useForm
  484. r.Dest.BUSSDOCOS = useForm
  485. r.Data.EqNumber = useForm
  486. r.Data.CreateTime = time.Now().Format("20060102")
  487. if t, e := util.ParseDate(pr.QuitDate); e != nil {
  488. log.Error(e, pr.QuitDate)
  489. } else {
  490. r.Data.PostDate = t.Format("20060102")
  491. }
  492. for i, v := range parts {
  493. c := new(model.Contract)
  494. bc := new(model.BigContract)
  495. pv := new(model.Provider)
  496. // w := new(model.Warehouse)
  497. //
  498. /*
  499. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  500. log.Errorf("contract query error:%v", e)
  501. return errors.New(fmt.Sprintf("errors:%v", e))
  502. }
  503. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  504. log.Errorf("bigcontract query error:%v", e)
  505. return errors.New(fmt.Sprintf("errors:%v", e))
  506. }
  507. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  508. log.Errorln(e, bc.ProviderID)
  509. return errors.New(fmt.Sprintf("errors:%v", e))
  510. }
  511. */
  512. contractID, _ := strconv.Atoi(v.ContractID.(string))
  513. if contractID > 0 {
  514. if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil {
  515. log.Errorf("contract query error:%v", e)
  516. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  517. }
  518. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  519. log.Errorf("bigcontract query error:%v", e)
  520. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  521. }
  522. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  523. log.Errorln(e, bc.ProviderID)
  524. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  525. }
  526. } else {
  527. if e := s.d.DB.Where("id = ?", v.ProviderID).First(pv).Error; e != nil {
  528. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  529. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  530. }
  531. }
  532. sdetail := http.ProofDetail{MoveType: "202"}
  533. if v.RowNumber.(float64) > 0 {
  534. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  535. } else {
  536. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  537. }
  538. sdetail.Quantity = fmt.Sprintf("%v", v.QuitNumber)
  539. sdetail.Unit = c.Unit
  540. sdetail.Factory = pa.FactoryCode
  541. sdetail.CostCenter = d.CostCenterCode
  542. sdetail.Location = "1004"
  543. sdetail.Provider = pv.SapCode
  544. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  545. r.Data.Detail = append(r.Data.Detail, sdetail)
  546. _, err = tx.Exec(` update partquit set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  547. if err != nil {
  548. log.Error(err)
  549. return err
  550. }
  551. }
  552. rbyte, _ := json.Marshal(r)
  553. fmt.Println(string(rbyte))
  554. if e := s.SyncSap(r, rp, rbyte); e != nil {
  555. log.Errorf("laid proof error:%v", e)
  556. return errors.New(fmt.Sprintf("errors:%v", e))
  557. } else {
  558. if rp.Dest.Status == "S" {
  559. log.Printf("quit proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  560. //if e := s.d.DB.Model(&model.BigPartquit{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  561. // log.Errorln(e)
  562. // return err
  563. //}
  564. tx.Exec(` update bigpartquit set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  565. } else {
  566. log.Warnf("%s quit proof failed:causetext=%s", rp.Dest.MessText, rp.Dest.MessText)
  567. pp.Print(r)
  568. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  569. }
  570. }
  571. return nil
  572. }
  573. // 退货单推送sap
  574. func (s *Service) RefundProof(p interface{}, tx *sqlx.Tx) error {
  575. pr := new(model.RefundParam)
  576. r := new(http.ProofReq)
  577. rp := new(http.ProofResp)
  578. parts := make([]*model.PartRefundStr, 0)
  579. bp := new(model.BigPartRefund)
  580. pa := new(model.Pasture)
  581. if e := util.Unmarshal(p, pr); e != nil {
  582. log.Error(e)
  583. log.Info("%v", pr)
  584. return errors.New(fmt.Sprintf("errors:%v", e))
  585. }
  586. pid := pr.ID
  587. bigpartrefundByte, err := MapStr(tx, `select * from bigpartrefund where id = ? `, pid)
  588. if err != nil {
  589. log.Error(err)
  590. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  591. }
  592. if bigpartrefundByte != nil {
  593. data := make(map[string]interface{})
  594. err := json.Unmarshal(bigpartrefundByte, &data)
  595. if err != nil {
  596. log.Error(err)
  597. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  598. }
  599. bp.ID, _ = strconv.Atoi(data["id"].(string))
  600. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  601. bp.ProofCode, _ = data["ProofCode"].(string)
  602. bp.UseForm = data["useForm"].(string)
  603. bp.CreateTime, _ = data["createTime"].(time.Time)
  604. t, err := time.Parse("2006-01-02T15:04:05", data["createTime"].(string)[:len("2006-01-02 15:04:05")])
  605. if err != nil {
  606. log.Error(err)
  607. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  608. }
  609. fmt.Println(t)
  610. bp.CreateTime = t
  611. bp.ProviderID, _ = strconv.Atoi(data["providerId"].(string))
  612. if _, ok := data["purchaseId"]; ok {
  613. bp.PurchaseId = data["purchaseId"].(string)
  614. }
  615. }
  616. if len(bp.ProofCode) > 1 {
  617. return errors.New(fmt.Sprintf("errors"))
  618. }
  619. //if e := s.d.DB.Find(&parts, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  620. // log.Error(e)
  621. // return errors.New("无退货详情信息!!!")
  622. //}
  623. partquitByte, err := GetDataList(tx, `select * from partrefund where bigid = ? `, []interface{}{bp.ID})
  624. if err != nil {
  625. log.Error(err)
  626. return errors.New("无退货详情信息!!!")
  627. }
  628. if partquitByte != nil {
  629. err := json.Unmarshal(partquitByte, &parts)
  630. if err != nil {
  631. log.Error(err)
  632. return errors.New("退货详情信息转码出错!!!")
  633. }
  634. }
  635. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  636. log.Error(e)
  637. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  638. }
  639. proofDest(r)
  640. r.Dest.MESSIDOS = bp.UseForm
  641. r.Dest.BUSSDOCOS = bp.UseForm
  642. r.Data.EqNumber = bp.UseForm
  643. r.Data.PostDate = bp.CreateTime.Format("20060102")
  644. r.Data.CreateTime = time.Now().Format("20060102")
  645. for i, v := range parts {
  646. sdetail := http.ProofDetail{}
  647. c := new(model.Contract)
  648. bc := new(model.BigContract)
  649. //bd := new(model.BuyDetail)
  650. //bbd := new(model.BigBuyDetail)
  651. pv := new(model.Provider)
  652. /*
  653. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  654. log.Errorf("contract query error:%v", e)
  655. return errors.New(fmt.Sprintf("errors:%v", e))
  656. }
  657. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  658. log.Errorf("bigcontract query error:%v", e)
  659. return errors.New(fmt.Sprintf("errors:%v", e))
  660. }
  661. if e := s.d.DB.Order("id desc").First(bd, model.BuyDetail{ContractID: v.ContractID}).Error; e != nil {
  662. log.Error(e)
  663. return errors.New(fmt.Sprintf("errors:%v", e))
  664. }
  665. if e := s.d.DB.First(bbd, model.BigBuyDetail{ID: bd.BigID}).Error; e != nil {
  666. log.Errorf("refund proof bigbuydetail:id=%d,%v", bd.BigID, e)
  667. return errors.New(fmt.Sprintf("errors:%v", e))
  668. }
  669. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  670. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  671. return errors.New(fmt.Sprintf("errors:%v", e))
  672. }
  673. */
  674. contractID, _ := strconv.Atoi(v.ContractID.(string))
  675. if contractID > 0 {
  676. if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil {
  677. log.Errorf("contract query error:%v", e)
  678. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  679. }
  680. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  681. log.Errorf("bigcontract query error:%v", e)
  682. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  683. }
  684. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  685. log.Errorln(e, bc.ProviderID)
  686. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  687. }
  688. //return nil
  689. } else {
  690. if e := s.d.DB.First(pv, model.Provider{ID: bp.ProviderID}).Error; e != nil {
  691. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  692. return errors.New(fmt.Sprintf("errors:%v", e))
  693. }
  694. }
  695. refunddetail := new(model.BigRefunddetail)
  696. s.d.DB.Raw(` select * from bigrefunddetail where buyeCode = ? `, bp.PurchaseId).First(&refunddetail)
  697. if bc.IsZeroStock == 0 {
  698. sdetail.MoveType = "161"
  699. } else {
  700. sdetail.MoveType = "502"
  701. }
  702. if v.RowNumber.(float64) > 0 {
  703. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  704. } else {
  705. sdetail.RowNumber = fmt.Sprintf("%d", (i+1)*10)
  706. }
  707. sdetail.Quantity = fmt.Sprintf("%v", v.RefundNumber)
  708. if v.PartCode == "" {
  709. sdetail.Unit = c.Unit
  710. sdetail.Code = c.PartCode
  711. } else {
  712. sdetail.Unit = v.Unit.(string)
  713. sdetail.Code = v.PartCode.(string)
  714. }
  715. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  716. sdetail.Factory = pa.FactoryCode
  717. sdetail.Location = "1004"
  718. sdetail.OrderNumber = refunddetail.SapNumber
  719. sdetail.OrderRowNum = fmt.Sprintf("%s", sdetail.RowNumber)
  720. sdetail.Provider = pv.SapCode
  721. r.Data.Detail = append(r.Data.Detail, sdetail)
  722. //if e := s.d.DB.Model(&model.PartRefund{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil {
  723. // log.Errorln(e, v.ID)
  724. //}
  725. _, err = tx.Exec(` update partrefund set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  726. if err != nil {
  727. log.Error(err)
  728. return err
  729. }
  730. }
  731. rbyte, _ := json.Marshal(r)
  732. fmt.Println(string(rbyte))
  733. if e := s.SyncSap(r, rp, rbyte); e != nil {
  734. log.Error(e)
  735. return errors.New(fmt.Sprintf("errors:%v", e))
  736. } else {
  737. if rp.Dest.Status == "S" {
  738. log.Printf("refund proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber)
  739. //if e := s.d.DB.Model(&model.BigPartRefund{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  740. // log.Errorln(e)
  741. //}
  742. tx.Exec(` update bigpartrefund set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  743. } else {
  744. log.Warnf("return good fail: %s", rp.Dest.MessText)
  745. if strings.Index(rp.Dest.MessText, "不正确的移动类型") > -1 {
  746. return errors.New("寄售件退货订单中含有自购件\n解决方案:检查退货订单中的备件属性。自购件先做退货采购订单,再做备件退货,类型选择自购件。寄售件直接做备件退货,类型选择寄售件。")
  747. }
  748. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  749. }
  750. }
  751. return nil
  752. }
  753. func proofDest(p *http.ProofReq) {
  754. p.Dest.DestID = "EQMAN"
  755. p.Dest.BussTp = "MM001"
  756. //p.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM001/ModifyMblnr"
  757. p.Dest.Url = fmt.Sprintf("%s/Common/MM001/ModifyMblnr", config.Conf.Http.Routing)
  758. //p.Dest.Url = "http://192.168.61.117/SAPQ0/Common/MM001/ModifyMblnr"
  759. }
  760. func MapStr(tx *sqlx.Tx, sql string, param ...interface{}) ([]byte, error) {
  761. m := make(map[string]interface{})
  762. rows, err := tx.Queryx(sql, param...)
  763. if err != nil {
  764. return nil, err
  765. }
  766. col, err := rows.Columns()
  767. if err != nil {
  768. return nil, err
  769. }
  770. if rows.Next() {
  771. rs, err := rows.SliceScan()
  772. if err != nil {
  773. return nil, err
  774. }
  775. for i, r := range rs {
  776. m[col[i]] = r
  777. switch r.(type) {
  778. case []uint8:
  779. m[col[i]] = string(r.([]uint8))
  780. case int64:
  781. m[col[i]] = strconv.FormatInt(r.(int64), 10)
  782. }
  783. }
  784. }
  785. defer func() {
  786. rows.Close()
  787. }()
  788. b, err := json.Marshal(m)
  789. if err != nil {
  790. return nil, err
  791. }
  792. return b, nil
  793. }
  794. func GetDataList(tx *sqlx.Tx, sqlstr string, args []interface{}) ([]byte, error) {
  795. row, err := tx.Queryx(sqlstr, args...)
  796. if err != nil {
  797. return nil, err
  798. }
  799. defer row.Close()
  800. results, err := rows2mapObjects(row)
  801. if err != nil {
  802. return nil, err
  803. }
  804. b, err := json.Marshal(results)
  805. return b, err
  806. }
  807. func rows2mapObjects(rows *sqlx.Rows) (resultsSlice []map[string]interface{}, err error) {
  808. fields, err := rows.Columns()
  809. if err != nil {
  810. return nil, err
  811. }
  812. defer rows.Close()
  813. for rows.Next() {
  814. result, err := rows2mapObject(rows, fields)
  815. if err != nil {
  816. return nil, err
  817. }
  818. resultsSlice = append(resultsSlice, result)
  819. }
  820. return resultsSlice, nil
  821. }
  822. func rows2mapObject(rows *sqlx.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
  823. result := make(map[string]interface{})
  824. scanResultContainers := make([]interface{}, len(fields))
  825. for i := 0; i < len(fields); i++ {
  826. var scanResultContainer interface{}
  827. scanResultContainers[i] = &scanResultContainer
  828. }
  829. if err := rows.Scan(scanResultContainers...); err != nil {
  830. return nil, err
  831. }
  832. for ii, key := range fields {
  833. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  834. //if row is null then ignore
  835. if rawValue.Interface() == nil {
  836. continue
  837. }
  838. if data, err := value2Object(&rawValue); err == nil {
  839. result[key] = data
  840. } else {
  841. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  842. }
  843. }
  844. return result, nil
  845. }
  846. func value2Object(rawValue *reflect.Value) (data interface{}, err error) {
  847. data, err = reflect2object(rawValue)
  848. if err != nil {
  849. return
  850. }
  851. return
  852. }
  853. func reflect2object(rawValue *reflect.Value) (value interface{}, err error) {
  854. aa := reflect.TypeOf((*rawValue).Interface())
  855. vv := reflect.ValueOf((*rawValue).Interface())
  856. switch aa.Kind() {
  857. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  858. value = vv.Int()
  859. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  860. value = vv.Uint()
  861. case reflect.Float32, reflect.Float64:
  862. value = vv.Float()
  863. case reflect.String:
  864. value = vv.String()
  865. case reflect.Array, reflect.Slice:
  866. switch aa.Elem().Kind() {
  867. case reflect.Uint8:
  868. data := rawValue.Interface().([]byte)
  869. value = string(data)
  870. default:
  871. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  872. }
  873. // time type
  874. case reflect.Struct:
  875. if aa.ConvertibleTo(schemas.TimeType) {
  876. value = vv.Convert(schemas.TimeType).Interface().(time.Time)
  877. } else {
  878. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  879. }
  880. case reflect.Bool:
  881. value = vv.Bool()
  882. case reflect.Complex128, reflect.Complex64:
  883. value = vv.Complex()
  884. /* TODO: unsupported types below
  885. case reflect.Map:
  886. case reflect.Ptr:
  887. case reflect.Uintptr:
  888. case reflect.UnsafePointer:
  889. case reflect.Chan, reflect.Func, reflect.Interface:
  890. */
  891. default:
  892. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  893. }
  894. return
  895. }
  896. func extractCode(input string) ([]string, error) {
  897. re := regexp.MustCompile(`\d{2}\.\d{2}\.\d{2}\.\d{2}\.\d{6}`)
  898. matches := re.FindAllString(input, -1)
  899. if matches == nil {
  900. return nil, fmt.Errorf("No matches found")
  901. }
  902. return matches, nil
  903. }
  904. func (s *Service) GetEnsiling(pastureId string) ([]*model.Ensiling, error) {
  905. ensilingList := make([]*model.Ensiling, 0)
  906. err := s.d.DB.Where("pastureId = ? ", pastureId).Find(&ensilingList).Error
  907. if err != nil {
  908. return ensilingList, err
  909. }
  910. return ensilingList, nil
  911. }