proof.go 29 KB

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