proof.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. // 出库单推送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. if sdetail.Factory == "M005" && strings.Contains(dp.Name, "二期") {
  337. sdetail.Factory = "M017"
  338. }
  339. if v.RowNumber.(float64) > 0 {
  340. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  341. } else {
  342. sdetail.RowNumber = fmt.Sprintf("%d", (i + 1))
  343. }
  344. sdetail.Quantity = fmt.Sprintf("%v", v.CheckoutNumber)
  345. sdetail.Unit = v.Unit.(string)
  346. sdetail.Code = v.PartCode.(string)
  347. sdetail.AssetCode = eq.SapCode
  348. sdetail.AssetSon = eq.SonCode
  349. sdetail.Location = "1004"
  350. if contractID > 0 && bc.IsZeroStock == 1 {
  351. sdetail.SaveType = "K"
  352. }
  353. if sdetail.SaveType == "K" {
  354. sdetail.Provider = pv.SapCode
  355. }
  356. r.Data.Detail = append(r.Data.Detail, sdetail)
  357. //if e := s.d.DB.Model(&model.PartUse{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil {
  358. // log.Errorln(e, v.ID)
  359. //}
  360. _, err = tx.Exec(` update partuse set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  361. if err != nil {
  362. return err
  363. }
  364. }
  365. //}
  366. ///*
  367. pp.Print(r)
  368. log.Infof("bp.pastureId:%d,pa.name:%s,pa.companycode:%s", bp.PastureID, pa.Name, pa.CompanyCode)
  369. rbyte, _ := json.Marshal(r)
  370. fmt.Println(string(rbyte))
  371. if e := s.SyncSap(r, rp, rbyte); e != nil {
  372. log.Errorf("laid proof error:%v", e)
  373. return errors.New(fmt.Sprintf("errors:%s", r.MessText))
  374. } else {
  375. if rp.Dest.Status == "S" {
  376. log.Printf("use proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  377. //if e := s.d.DB.Model(&model.BigPartUse{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  378. // log.Errorln(e)
  379. //}
  380. tx.Exec(` update bigpartuse set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  381. } else {
  382. log.Warnf(" use proof :%s", rp.Dest.MessText)
  383. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  384. }
  385. }
  386. return nil
  387. }
  388. // 退库单推送sap
  389. func (s *Service) QuitProof(p interface{}, tx *sqlx.Tx) error {
  390. pr := new(http.ProofPartReq)
  391. r := new(http.ProofReq)
  392. rp := new(http.ProofResp)
  393. parts := make([]*model.PartquitStr, 0)
  394. bp := new(model.BigPartquit)
  395. pa := new(model.Pasture)
  396. d := new(model.Department)
  397. if e := util.Unmarshal(p, pr); e != nil {
  398. log.Error(e)
  399. return errors.New(fmt.Sprintf("errors:%v", e))
  400. }
  401. //defer func() {
  402. // if msg := recover(); msg != nil {
  403. // fmt.Println("panic信息:", msg, "---recover恢复---")
  404. // }
  405. //}()
  406. //if e := s.d.DB.First(bp, model.BigPartquit{UseForm: pr.UseForm}).Error; e != nil {
  407. // log.Error(e)
  408. // return errors.New(fmt.Sprintf("errors:%v", e))
  409. //}
  410. bigpartquitByte, err := MapStr(tx, `select * from bigpartquit where useForm = ? `, pr.UseForm)
  411. if err != nil {
  412. log.Error(err)
  413. return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err))
  414. }
  415. if bigpartquitByte != nil {
  416. data := make(map[string]interface{})
  417. err := json.Unmarshal(bigpartquitByte, &data)
  418. if err != nil {
  419. log.Error(err)
  420. return errors.New(fmt.Sprintf("无退库单信息 errors:%v", err))
  421. }
  422. bp.ID, _ = strconv.Atoi(data["id"].(string))
  423. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  424. bp.ProofCode, _ = data["ProofCode"].(string)
  425. bp.DeptID, _ = strconv.Atoi(data["deptId"].(string))
  426. }
  427. if len(bp.ProofCode) > 1 {
  428. return errors.New(fmt.Sprintf("errors"))
  429. }
  430. partquitByte, err := GetDataList(tx, `select * from partquit where bigid = ? `, []interface{}{bp.ID})
  431. if err != nil {
  432. log.Error(err)
  433. return errors.New("无退货详情信息!!!")
  434. }
  435. if partquitByte != nil {
  436. err := json.Unmarshal(partquitByte, &parts)
  437. if err != nil {
  438. log.Error(err)
  439. return errors.New("退货详情信息转码出错!!!")
  440. }
  441. }
  442. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  443. log.Error(e)
  444. return errors.New(fmt.Sprintf("无牧场信息 errors:%v", err))
  445. }
  446. if e := s.d.DB.First(d, model.Department{ID: bp.DeptID}).Error; e != nil {
  447. log.Error(e)
  448. return errors.New(fmt.Sprintf("无部门信息 errors:%v", err))
  449. }
  450. var useFormCount int64
  451. if e := s.d.DB.Table("bigpartquit").Where(" useForm = ? ", pr.UseForm).Count(&useFormCount).Error; e != nil {
  452. log.Error(e)
  453. }
  454. useForm := fmt.Sprintf("%stk%d", pr.UseForm, useFormCount+1)
  455. proofDest(r)
  456. r.Dest.MESSIDOS = useForm
  457. r.Dest.BUSSDOCOS = useForm
  458. r.Data.EqNumber = useForm
  459. r.Data.CreateTime = time.Now().Format("20060102")
  460. if t, e := util.ParseDate(pr.QuitDate); e != nil {
  461. log.Error(e, pr.QuitDate)
  462. } else {
  463. r.Data.PostDate = t.Format("20060102")
  464. }
  465. for i, v := range parts {
  466. c := new(model.Contract)
  467. bc := new(model.BigContract)
  468. pv := new(model.Provider)
  469. // w := new(model.Warehouse)
  470. //
  471. /*
  472. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  473. log.Errorf("contract query error:%v", e)
  474. return errors.New(fmt.Sprintf("errors:%v", e))
  475. }
  476. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  477. log.Errorf("bigcontract query error:%v", e)
  478. return errors.New(fmt.Sprintf("errors:%v", e))
  479. }
  480. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  481. log.Errorln(e, bc.ProviderID)
  482. return errors.New(fmt.Sprintf("errors:%v", e))
  483. }
  484. */
  485. contractID, _ := strconv.Atoi(v.ContractID.(string))
  486. if contractID > 0 {
  487. if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil {
  488. log.Errorf("contract query error:%v", e)
  489. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  490. }
  491. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  492. log.Errorf("bigcontract query error:%v", e)
  493. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  494. }
  495. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  496. log.Errorln(e, bc.ProviderID)
  497. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  498. }
  499. } else {
  500. if e := s.d.DB.Where("id = ?", v.ProviderID).First(pv).Error; e != nil {
  501. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  502. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  503. }
  504. }
  505. sdetail := http.ProofDetail{MoveType: "202"}
  506. if v.RowNumber.(float64) > 0 {
  507. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  508. } else {
  509. sdetail.RowNumber = fmt.Sprintf("%d", i+1)
  510. }
  511. sdetail.Quantity = fmt.Sprintf("%v", v.QuitNumber)
  512. sdetail.Unit = c.Unit
  513. sdetail.Factory = pa.FactoryCode
  514. sdetail.CostCenter = d.CostCenterCode
  515. sdetail.Location = "1004"
  516. sdetail.Provider = pv.SapCode
  517. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  518. r.Data.Detail = append(r.Data.Detail, sdetail)
  519. _, err = tx.Exec(` update partquit set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  520. if err != nil {
  521. log.Error(err)
  522. return err
  523. }
  524. }
  525. rbyte, _ := json.Marshal(r)
  526. fmt.Println(string(rbyte))
  527. if e := s.SyncSap(r, rp, rbyte); e != nil {
  528. log.Errorf("laid proof error:%v", e)
  529. return errors.New(fmt.Sprintf("errors:%v", e))
  530. } else {
  531. if rp.Dest.Status == "S" {
  532. log.Printf("quit proof success:eqcode=%s,sapcode=%s", rp.Data.EqNumber, rp.Data.SapNumber)
  533. //if e := s.d.DB.Model(&model.BigPartquit{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  534. // log.Errorln(e)
  535. // return err
  536. //}
  537. tx.Exec(` update bigpartquit set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  538. } else {
  539. log.Warnf("%s quit proof failed:causetext=%s", rp.Dest.MessText)
  540. pp.Print(r)
  541. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  542. }
  543. }
  544. return nil
  545. }
  546. // 退货单推送sap
  547. func (s *Service) RefundProof(p interface{}, tx *sqlx.Tx) error {
  548. pr := new(model.RefundParam)
  549. r := new(http.ProofReq)
  550. rp := new(http.ProofResp)
  551. parts := make([]*model.PartRefundStr, 0)
  552. bp := new(model.BigPartRefund)
  553. pa := new(model.Pasture)
  554. if e := util.Unmarshal(p, pr); e != nil {
  555. log.Error(e)
  556. log.Info("%v", pr)
  557. return errors.New(fmt.Sprintf("errors:%v", e))
  558. }
  559. pid := pr.ID
  560. bigpartrefundByte, err := MapStr(tx, `select * from bigpartrefund where id = ? `, pid)
  561. if err != nil {
  562. log.Error(err)
  563. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  564. }
  565. if bigpartrefundByte != nil {
  566. data := make(map[string]interface{})
  567. err := json.Unmarshal(bigpartrefundByte, &data)
  568. if err != nil {
  569. log.Error(err)
  570. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  571. }
  572. bp.ID, _ = strconv.Atoi(data["id"].(string))
  573. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  574. bp.ProofCode, _ = data["ProofCode"].(string)
  575. bp.UseForm = data["useForm"].(string)
  576. bp.CreateTime, _ = data["createTime"].(time.Time)
  577. t, err := time.Parse("2006-01-02T15:04:05", data["createTime"].(string)[:len("2006-01-02 15:04:05")])
  578. if err != nil {
  579. log.Error(err)
  580. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  581. }
  582. fmt.Println(t)
  583. bp.CreateTime = t
  584. bp.ProviderID, _ = strconv.Atoi(data["providerId"].(string))
  585. if _, ok := data["purchaseId"]; ok {
  586. bp.PurchaseId = data["purchaseId"].(string)
  587. }
  588. }
  589. if len(bp.ProofCode) > 1 {
  590. return errors.New(fmt.Sprintf("errors"))
  591. }
  592. //if e := s.d.DB.Find(&parts, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  593. // log.Error(e)
  594. // return errors.New("无退货详情信息!!!")
  595. //}
  596. partquitByte, err := GetDataList(tx, `select * from partrefund where bigid = ? `, []interface{}{bp.ID})
  597. if err != nil {
  598. log.Error(err)
  599. return errors.New("无退货详情信息!!!")
  600. }
  601. if partquitByte != nil {
  602. err := json.Unmarshal(partquitByte, &parts)
  603. if err != nil {
  604. log.Error(err)
  605. return errors.New("退货详情信息转码出错!!!")
  606. }
  607. }
  608. if e := s.d.DB.First(pa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  609. log.Error(e)
  610. return errors.New(fmt.Sprintf("查询退货 errors:%v", err))
  611. }
  612. proofDest(r)
  613. r.Dest.MESSIDOS = bp.UseForm
  614. r.Dest.BUSSDOCOS = bp.UseForm
  615. r.Data.EqNumber = bp.UseForm
  616. r.Data.PostDate = bp.CreateTime.Format("20060102")
  617. r.Data.CreateTime = time.Now().Format("20060102")
  618. for i, v := range parts {
  619. sdetail := http.ProofDetail{}
  620. c := new(model.Contract)
  621. bc := new(model.BigContract)
  622. //bd := new(model.BuyDetail)
  623. //bbd := new(model.BigBuyDetail)
  624. pv := new(model.Provider)
  625. /*
  626. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  627. log.Errorf("contract query error:%v", e)
  628. return errors.New(fmt.Sprintf("errors:%v", e))
  629. }
  630. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  631. log.Errorf("bigcontract query error:%v", e)
  632. return errors.New(fmt.Sprintf("errors:%v", e))
  633. }
  634. if e := s.d.DB.Order("id desc").First(bd, model.BuyDetail{ContractID: v.ContractID}).Error; e != nil {
  635. log.Error(e)
  636. return errors.New(fmt.Sprintf("errors:%v", e))
  637. }
  638. if e := s.d.DB.First(bbd, model.BigBuyDetail{ID: bd.BigID}).Error; e != nil {
  639. log.Errorf("refund proof bigbuydetail:id=%d,%v", bd.BigID, e)
  640. return errors.New(fmt.Sprintf("errors:%v", e))
  641. }
  642. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  643. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  644. return errors.New(fmt.Sprintf("errors:%v", e))
  645. }
  646. */
  647. contractID, _ := strconv.Atoi(v.ContractID.(string))
  648. if contractID > 0 {
  649. if e := s.d.DB.First(c, model.Contract{ID: contractID}).Error; e != nil {
  650. log.Errorf("contract query error:%v", e)
  651. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  652. }
  653. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  654. log.Errorf("bigcontract query error:%v", e)
  655. return errors.New(fmt.Sprintf("合同不存在 errors:%v", err))
  656. }
  657. if e := s.d.DB.First(pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  658. log.Errorln(e, bc.ProviderID)
  659. return errors.New(fmt.Sprintf("供应商不存在 errors:%v", err))
  660. }
  661. //return nil
  662. } else {
  663. if e := s.d.DB.First(pv, model.Provider{ID: bp.ProviderID}).Error; e != nil {
  664. log.Errorf("refund proof provider:id=%d,%v", bc.ProviderID, e)
  665. return errors.New(fmt.Sprintf("errors:%v", e))
  666. }
  667. }
  668. refunddetail := new(model.BigRefunddetail)
  669. s.d.DB.Raw(` select * from bigrefunddetail where buyeCode = ? `, bp.PurchaseId).First(&refunddetail)
  670. if bc.IsZeroStock == 0 {
  671. sdetail.MoveType = "161"
  672. } else {
  673. sdetail.MoveType = "502"
  674. }
  675. if v.RowNumber.(float64) > 0 {
  676. sdetail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  677. } else {
  678. sdetail.RowNumber = fmt.Sprintf("%d", (i+1)*10)
  679. }
  680. sdetail.Quantity = fmt.Sprintf("%v", v.RefundNumber)
  681. if v.PartCode == "" {
  682. sdetail.Unit = c.Unit
  683. sdetail.Code = c.PartCode
  684. } else {
  685. sdetail.Unit = v.Unit.(string)
  686. sdetail.Code = v.PartCode.(string)
  687. }
  688. sdetail.SaveType = util.ZeroStr(bc.IsZeroStock == 1, "K")
  689. sdetail.Factory = pa.FactoryCode
  690. sdetail.Location = "1004"
  691. sdetail.OrderNumber = refunddetail.SapNumber
  692. sdetail.OrderRowNum = fmt.Sprintf("%s", sdetail.RowNumber)
  693. sdetail.Provider = pv.SapCode
  694. r.Data.Detail = append(r.Data.Detail, sdetail)
  695. //if e := s.d.DB.Model(&model.PartRefund{}).Where("id=?", v.ID).Update("rowNumber", sdetail.RowNumber).Error; e != nil {
  696. // log.Errorln(e, v.ID)
  697. //}
  698. _, err = tx.Exec(` update partrefund set rowNumber = ? where id = ? `, sdetail.RowNumber, v.ID)
  699. if err != nil {
  700. log.Error(err)
  701. return err
  702. }
  703. }
  704. rbyte, _ := json.Marshal(r)
  705. fmt.Println(string(rbyte))
  706. if e := s.SyncSap(r, rp, rbyte); e != nil {
  707. log.Error(e)
  708. return errors.New(fmt.Sprintf("errors:%v", e))
  709. } else {
  710. if rp.Dest.Status == "S" {
  711. log.Printf("refund proof success:%s,%s", rp.Data.EqNumber, rp.Data.SapNumber)
  712. //if e := s.d.DB.Model(&model.BigPartRefund{}).Where("useform = ?", rp.Data.EqNumber).Update("ProofCode", rp.Data.SapNumber).Error; e != nil {
  713. // log.Errorln(e)
  714. //}
  715. tx.Exec(` update bigpartrefund set ProofCode = ? where useform = ? `, rp.Data.SapNumber, rp.Data.EqNumber)
  716. } else {
  717. log.Warnf("return good fail: %s", rp.Dest.MessText)
  718. return errors.New(fmt.Sprintf("errors:%s", rp.Dest.MessText))
  719. }
  720. }
  721. return nil
  722. }
  723. func proofDest(p *http.ProofReq) {
  724. p.Dest.DestID = "EQMAN"
  725. p.Dest.BussTp = "MM001"
  726. //p.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM001/ModifyMblnr"
  727. p.Dest.Url = fmt.Sprintf("%s/Common/MM001/ModifyMblnr", config.Conf.Http.Routing)
  728. //p.Dest.Url = "http://192.168.61.117/SAPQ0/Common/MM001/ModifyMblnr"
  729. }
  730. func MapStr(tx *sqlx.Tx, sql string, param ...interface{}) ([]byte, error) {
  731. m := make(map[string]interface{})
  732. rows, err := tx.Queryx(sql, param...)
  733. if err != nil {
  734. return nil, err
  735. }
  736. col, err := rows.Columns()
  737. if err != nil {
  738. return nil, err
  739. }
  740. if rows.Next() {
  741. rs, err := rows.SliceScan()
  742. if err != nil {
  743. return nil, err
  744. }
  745. for i, r := range rs {
  746. m[col[i]] = r
  747. switch r.(type) {
  748. case []uint8:
  749. m[col[i]] = string(r.([]uint8))
  750. case int64:
  751. m[col[i]] = strconv.FormatInt(r.(int64), 10)
  752. }
  753. }
  754. }
  755. defer func() {
  756. rows.Close()
  757. }()
  758. b, err := json.Marshal(m)
  759. if err != nil {
  760. return nil, err
  761. }
  762. return b, nil
  763. }
  764. func GetDataList(tx *sqlx.Tx, sqlstr string, args []interface{}) ([]byte, error) {
  765. row, err := tx.Queryx(sqlstr, args...)
  766. if err != nil {
  767. return nil, err
  768. }
  769. defer row.Close()
  770. results, err := rows2mapObjects(row)
  771. if err != nil {
  772. return nil, err
  773. }
  774. b, err := json.Marshal(results)
  775. return b, err
  776. }
  777. func rows2mapObjects(rows *sqlx.Rows) (resultsSlice []map[string]interface{}, err error) {
  778. fields, err := rows.Columns()
  779. if err != nil {
  780. return nil, err
  781. }
  782. defer rows.Close()
  783. for rows.Next() {
  784. result, err := rows2mapObject(rows, fields)
  785. if err != nil {
  786. return nil, err
  787. }
  788. resultsSlice = append(resultsSlice, result)
  789. }
  790. return resultsSlice, nil
  791. }
  792. func rows2mapObject(rows *sqlx.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
  793. result := make(map[string]interface{})
  794. scanResultContainers := make([]interface{}, len(fields))
  795. for i := 0; i < len(fields); i++ {
  796. var scanResultContainer interface{}
  797. scanResultContainers[i] = &scanResultContainer
  798. }
  799. if err := rows.Scan(scanResultContainers...); err != nil {
  800. return nil, err
  801. }
  802. for ii, key := range fields {
  803. rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
  804. //if row is null then ignore
  805. if rawValue.Interface() == nil {
  806. continue
  807. }
  808. if data, err := value2Object(&rawValue); err == nil {
  809. result[key] = data
  810. } else {
  811. return nil, err // !nashtsai! REVIEW, should return err or just error log?
  812. }
  813. }
  814. return result, nil
  815. }
  816. func value2Object(rawValue *reflect.Value) (data interface{}, err error) {
  817. data, err = reflect2object(rawValue)
  818. if err != nil {
  819. return
  820. }
  821. return
  822. }
  823. func reflect2object(rawValue *reflect.Value) (value interface{}, err error) {
  824. aa := reflect.TypeOf((*rawValue).Interface())
  825. vv := reflect.ValueOf((*rawValue).Interface())
  826. switch aa.Kind() {
  827. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  828. value = vv.Int()
  829. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  830. value = vv.Uint()
  831. case reflect.Float32, reflect.Float64:
  832. value = vv.Float()
  833. case reflect.String:
  834. value = vv.String()
  835. case reflect.Array, reflect.Slice:
  836. switch aa.Elem().Kind() {
  837. case reflect.Uint8:
  838. data := rawValue.Interface().([]byte)
  839. value = string(data)
  840. default:
  841. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  842. }
  843. // time type
  844. case reflect.Struct:
  845. if aa.ConvertibleTo(schemas.TimeType) {
  846. value = vv.Convert(schemas.TimeType).Interface().(time.Time)
  847. } else {
  848. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  849. }
  850. case reflect.Bool:
  851. value = vv.Bool()
  852. case reflect.Complex128, reflect.Complex64:
  853. value = vv.Complex()
  854. /* TODO: unsupported types below
  855. case reflect.Map:
  856. case reflect.Ptr:
  857. case reflect.Uintptr:
  858. case reflect.UnsafePointer:
  859. case reflect.Chan, reflect.Func, reflect.Interface:
  860. */
  861. default:
  862. err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
  863. }
  864. return
  865. }