proof.go 29 KB

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