charge_off.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/k0kubun/pp/v3"
  7. log "github.com/sirupsen/logrus"
  8. "gorm.io/gorm"
  9. "kpt.xdmy/apiserver/config"
  10. "kpt.xdmy/apiserver/model"
  11. "kpt.xdmy/apiserver/model/http"
  12. "kpt.xdmy/pkg/util"
  13. "strconv"
  14. "strings"
  15. )
  16. // 凭证冲销
  17. func (s *Service) ProofOff(p *model.ProofReq) (hrp *http.Response) {
  18. r := new(http.ChargeOffReq)
  19. rp := new(http.SapResp)
  20. hrp = new(http.Response)
  21. hrp.Code = 200
  22. hrp.Msg = "fail"
  23. r.Dest.DestID = "EQMAN"
  24. r.Dest.BussTp = "MM015"
  25. //r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM015/CancelMblnr"
  26. //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM015/CancelMblnr"
  27. r.Dest.Url = fmt.Sprintf("%s/Common/MM015/CancelMblnr", config.Conf.Http.Routing)
  28. r.Data.ProofCode = p.ProofCode
  29. r.Data.ProofYear = p.ProofYear
  30. r.Data.ChargeDate = p.ChargeDate
  31. r.Data.OrderNumber = p.OrderNumber
  32. r.Data.HasTicket = util.ZeroStr(p.HasTicket == 1, "X")
  33. log.Infof("proofcode: %s", r.Data.ProofCode)
  34. sl := r.Data.RowsNumber
  35. for i := range p.RowsNumber {
  36. sl = append(sl, http.ChargeOffDetail{RowNumber: p.RowsNumber[i]})
  37. }
  38. tx := s.d.DB.Begin()
  39. err := UpdateProof(p, tx)
  40. if err != nil {
  41. hrp.Message += fmt.Sprintf("%s . bigid = %d ,rows=%v", err.Error(), p.OrderNumberId, p.RowsNumber)
  42. tx.Rollback()
  43. }
  44. r.Data.RowsNumber = sl
  45. rbyte, _ := json.Marshal(r)
  46. //rbyte, _ := json.Marshal(r)
  47. fmt.Println(string(rbyte))
  48. e := s.SyncSap(r, rp, rbyte)
  49. pp.Print(r)
  50. if e == nil {
  51. if rp.Data.Status == "S" {
  52. hrp.Message = fmt.Sprintf("sap物料冲销成功:%d %s %s", p.NumberType, rp.Data.ProofNumber, rp.Data.EqNumber)
  53. log.Info(hrp.Message)
  54. } else {
  55. hrp.Message = fmt.Sprintf("sap凭证冲销失败:%s", rp.Dest.MessText)
  56. log.Infoln(hrp.Message, r, p)
  57. tx.Rollback()
  58. return
  59. }
  60. } else {
  61. hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e)
  62. log.Infoln(hrp.Message, r, p)
  63. tx.Rollback()
  64. return
  65. }
  66. tx.Commit()
  67. hrp.Msg = "ok"
  68. return
  69. }
  70. //凭证冲销后状态更新
  71. func UpdateProof(p *model.ProofReq, tx *gorm.DB) error {
  72. var tname string
  73. switch p.NumberType {
  74. case 1: // 入库
  75. tname = "partlaid"
  76. case 2: // 退货
  77. tname = "partrefund"
  78. case 3: // 出库
  79. tname = "partuse"
  80. case 4: // 退库
  81. tname = "partquit"
  82. }
  83. //strings.Join(p.RowsNumber, ",")
  84. rowsNumbers := []string{}
  85. for _, rowsNumber := range p.RowsNumber {
  86. rowsNumbers = append(rowsNumbers, strconv.Itoa(rowsNumber))
  87. }
  88. if p.NumberType == 1 {
  89. //for _, item := range p.ProofPart {
  90. if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ",
  91. strings.Join(rowsNumbers, ",")).Update("dflag", 1).Error; e != nil {
  92. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  93. tx.Rollback()
  94. return e
  95. }
  96. //}
  97. //select id,name from user where FIND_IN_SET(id,#{strlist})
  98. if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
  99. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  100. }
  101. } else if p.NumberType == 3 {
  102. if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")).
  103. Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil {
  104. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  105. }
  106. if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
  107. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  108. }
  109. } else if p.NumberType == 2 {
  110. if e := tx.Table(tname).Where("bigid=? ", p.OrderNumberId).Where(" FIND_IN_SET(rowNumber,? ) ", strings.Join(rowsNumbers, ",")).
  111. Update("dflag", 1).Update("hasTicket", p.HasTicket).Error; e != nil {
  112. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  113. }
  114. if e := tx.Exec(fmt.Sprintf(` update big%s set sterilisation = 1 where id = ? `, tname), p.OrderNumberId).Error; e != nil {
  115. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  116. }
  117. }
  118. if p.NumberType == 1 {
  119. partlaidList := make([]*model.PartLaid, 0)
  120. err := tx.Raw(`select * from partlaid where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partlaidList).Error
  121. if err != nil {
  122. log.Error(err)
  123. return err
  124. }
  125. for _, part := range partlaidList {
  126. err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery - ?
  127. where pr.partCode = ? and pr.contractId = ? and
  128. (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartlaid where id = ? ) `,
  129. part.StorageAmount, part.PartCode, part.ContractID, p.OrderNumberId).Error
  130. if err != nil {
  131. log.Error(err)
  132. return err
  133. }
  134. }
  135. } else if p.NumberType == 3 {
  136. partUseList := make([]*model.PartUse, 0)
  137. err := tx.Raw(`select * from partuse where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).Find(&partUseList).Error
  138. if err != nil {
  139. log.Error(err)
  140. return err
  141. }
  142. for _, part := range partUseList {
  143. err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ?
  144. where pr.partCode = ? and pr.contractId = ?
  145. and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureid from bigpartuse where id = ? ) `,
  146. part.CheckoutNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error
  147. if err != nil {
  148. log.Error(err)
  149. return err
  150. }
  151. }
  152. } else if p.NumberType == 2 {
  153. partRefundList := make([]*model.PartRefund, 0)
  154. err := tx.Raw(`select * from partrefund where bigid = ? and FIND_IN_SET(rowNumber,? ) `, p.OrderNumberId, strings.Join(rowsNumbers, ",")).
  155. Find(&partRefundList).Error
  156. if err != nil {
  157. log.Error(err)
  158. return err
  159. }
  160. for _, part := range partRefundList {
  161. err := tx.Exec(` update part_repertory pr set pr.reportery = pr.reportery + ?
  162. where pr.partCode = ? and pr.contractId = ?
  163. and (select pastureid from warehouse where id = pr.locationId and status = 1 limit 1 ) = (select pastureId from bigpartrefund where id = ? ) `,
  164. part.RefundNumber, part.PartCode, part.ContractID, p.OrderNumberId).Error
  165. if err != nil {
  166. log.Error(err)
  167. return err
  168. }
  169. }
  170. }
  171. return nil
  172. }
  173. // 查询 单据 对应备件明细
  174. func ProofList(p *model.ProofReq) (hrp *http.Response) {
  175. switch p.NumberType {
  176. case 1: // 入库
  177. return s.laidProofList(p)
  178. case 2: // 退货
  179. return s.returnProofList(p)
  180. case 3: // 出库
  181. return s.useProofList(p)
  182. case 4: // 退库
  183. return s.quitProofList(p)
  184. }
  185. return
  186. }
  187. // 入库冲销
  188. func (s *Service) laidProofList(p *model.ProofReq) (hrp *http.Response) {
  189. bp := new(model.BigPartLaid)
  190. proofs := make([]model.ProofPart, 0)
  191. var e error
  192. hrp = new(http.Response)
  193. hrp.Code = 200
  194. hrp.Msg = "fail"
  195. if e = s.d.DB.First(bp, model.BigPartLaid{LaidCode: p.OrderNumber}).Error; e != nil {
  196. hrp.Message = fmt.Sprintf("%s ,error=%v,", p.OrderNumber, e)
  197. return
  198. }
  199. p.OrderNumberId = bp.ID
  200. p.ProofCode = bp.ProofCode
  201. p.ChargeDate = bp.CreatDate.Format("2006-01-02")
  202. p.ProofYear = bp.CreatDate.Format("2006")
  203. parts := make([]model.PartLaid, 0)
  204. if e = s.d.DB.Raw(` select * from partlaid where bigid = ? and dflag != 1 order by rowNumber,id desc `, bp.ID).Order("rowNumber,id desc ").
  205. Find(&parts).Error; e != nil || len(parts) == 0 {
  206. hrp.Message = fmt.Sprintf("入库单明细查询报错或结果为空:bigid=%v error=%v,", bp.ID, e)
  207. hrp.Msg = "fail"
  208. return
  209. }
  210. for i, v := range parts {
  211. mp := new(model.ProofPart)
  212. br := new(model.Brand)
  213. w := new(model.Warehouse)
  214. pv := new(model.Provider)
  215. if e := s.d.DB.First(&pv, model.Provider{ID: v.ProviderID}).Error; e != nil {
  216. log.Errorln(v.ProviderID, e)
  217. //continue
  218. }
  219. if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
  220. log.Errorln(e, v.BrandID)
  221. //continue
  222. }
  223. if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  224. log.Errorln(v.LocationID, e)
  225. //continue
  226. }
  227. if v.RowNumber > 0 {
  228. mp.RowNumber = v.RowNumber
  229. } else {
  230. mp.RowNumber = len(parts) - i
  231. }
  232. mp.PartCode = v.PartCode
  233. mp.PartName = v.PartName
  234. mp.Specification = v.Specification
  235. mp.ChangeCount = v.StorageAmount
  236. mp.UnitPrice = v.Price
  237. mp.TotalPrice = v.SumPrice
  238. mp.PartBrand = br.BrandName
  239. mp.Supplier = pv.SapCode
  240. mp.Warehouse = w.SapCode
  241. if v.Dflag == 0 {
  242. proofs = append(proofs, *mp)
  243. }
  244. }
  245. p.ProofPart = proofs
  246. hrp.Msg = "ok"
  247. hrp.Data = p
  248. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  249. return
  250. }
  251. // 退货冲销
  252. func (s *Service) returnProofList(p *model.ProofReq) (hrp *http.Response) {
  253. hrp = new(http.Response)
  254. bp := new(model.BigPartRefund)
  255. proofs := make([]model.ProofPart, 0)
  256. hrp.Code = 200
  257. var e error
  258. if e = s.d.DB.First(bp, model.BigPartRefund{UseForm: p.OrderNumber}).Error; e != nil {
  259. hrp.Message = fmt.Sprintf("bigpartrefund : %d,%s", p.NumberType, p.OrderNumber)
  260. hrp.Msg = "fail"
  261. return
  262. }
  263. p.OrderNumberId = bp.ID
  264. p.ProofCode = bp.ProofCode
  265. p.ChargeDate = bp.CreateTime.Format("2006-01-02")
  266. p.ProofYear = bp.CreateTime.Format("2006")
  267. funds := []model.PartRefund{}
  268. if e = s.d.DB.Where(" bigId = ? ", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&funds).Error; e != nil || len(funds) == 0 {
  269. hrp.Message = fmt.Sprintf("PartRefund query error . bigid=%v error=%v,", bp.ID, e)
  270. hrp.Msg = "fail"
  271. return
  272. }
  273. for i, v := range funds {
  274. mp := new(model.ProofPart)
  275. pv := new(model.Provider)
  276. if e := s.d.DB.First(&pv, model.Provider{ID: bp.ProviderID}).Error; e != nil {
  277. log.Errorln(bp.ProviderID, e)
  278. continue
  279. }
  280. if v.RowNumber > 0 {
  281. mp.RowNumber = v.RowNumber
  282. } else {
  283. mp.RowNumber = len(funds) - i
  284. }
  285. mp.Supplier = pv.SapCode
  286. mp.PartBrand = v.Brand
  287. mp.Warehouse = "1004"
  288. mp.PartCode = v.PartCode
  289. mp.PartName = v.PartName
  290. mp.Specification = v.Specification
  291. mp.ChangeCount = v.RefundNumber
  292. mp.UnitPrice = v.Price
  293. mp.TotalPrice = v.SumPrice
  294. proofs = append(proofs, *mp)
  295. }
  296. p.ProofPart = proofs
  297. hrp.Msg = "ok"
  298. hrp.Data = p
  299. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  300. return
  301. }
  302. // 出库冲销
  303. func (s *Service) useProofList(p *model.ProofReq) (hrp *http.Response) {
  304. hrp = new(http.Response)
  305. bp := new(model.BigPartUse)
  306. proofs := make([]model.ProofPart, 0)
  307. hrp.Code = 200
  308. hrp.Msg = "fail"
  309. var e error
  310. if e = s.d.DB.First(bp, model.BigPartUse{UseForm: p.OrderNumber}).Error; e != nil {
  311. log.Error(e)
  312. hrp.Message = fmt.Sprintf("bigpartuse useform=%s", p.OrderNumber)
  313. return
  314. }
  315. p.OrderNumberId = bp.ID
  316. p.ProofCode = bp.ProofCode
  317. p.ChargeDate = bp.ReceiveTime.Format("2006-01-02")
  318. p.ProofYear = bp.ReceiveTime.Format("2006")
  319. uses := make([]model.PartUse, 0)
  320. if e = s.d.DB.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&uses).Error; e != nil || len(uses) == 0 {
  321. log.Error(e)
  322. hrp.Message = fmt.Sprintf("PartUse bigid = %v ", bp.ID)
  323. return
  324. }
  325. for i, v := range uses {
  326. mp := new(model.ProofPart)
  327. br := new(model.Brand)
  328. w := new(model.Warehouse)
  329. c := new(model.Contract)
  330. bc := new(model.BigContract)
  331. pv := new(model.Provider)
  332. pa := new(model.PartRepertory)
  333. if v.ContractID > 0 {
  334. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  335. log.Error(e)
  336. hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
  337. //return
  338. }
  339. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  340. log.Error(e)
  341. hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
  342. //return
  343. }
  344. if e := s.d.DB.First(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  345. log.Errorln(bc.ProviderID, e)
  346. //continue
  347. }
  348. } else {
  349. if e := s.d.DB.First(pa, model.PartRepertory{ContractID: v.ContractID, PartCode: v.PartCode, LocationID: v.LocationID}).Error; e != nil {
  350. log.Error(e)
  351. hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
  352. //return
  353. }
  354. if e := s.d.DB.First(&pv, model.Provider{ID: pa.ProviderID}).Error; e != nil {
  355. log.Errorln(pa.ProviderID, e)
  356. //continue
  357. }
  358. }
  359. if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
  360. log.Error(e)
  361. hrp.Message = fmt.Sprintf("brand id =%d", v.BrandID)
  362. return
  363. }
  364. if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  365. log.Errorln(v.LocationID, e)
  366. return
  367. }
  368. mp.PartBrand = br.BrandName
  369. mp.Warehouse = w.SapCode
  370. mp.Supplier = pv.SapCode
  371. if v.RowNumber > 0 {
  372. mp.RowNumber = v.RowNumber
  373. } else {
  374. mp.RowNumber = len(uses) - i
  375. }
  376. mp.PartCode = v.PartCode
  377. mp.PartName = v.PartName
  378. mp.Specification = v.Specification
  379. mp.ChangeCount = v.CheckoutNumber
  380. mp.UnitPrice = v.Price
  381. mp.TotalPrice = v.SumPrice
  382. proofs = append(proofs, *mp)
  383. }
  384. p.ProofPart = proofs
  385. hrp.Msg = "ok"
  386. hrp.Data = p
  387. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  388. return
  389. }
  390. // 退库冲销
  391. func (s *Service) quitProofList(p *model.ProofReq) (hrp *http.Response) {
  392. hrp = new(http.Response)
  393. bp := new(model.BigPartquit)
  394. proofs := make([]model.ProofPart, 0)
  395. hrp.Code = 200
  396. var e error
  397. if e = s.d.DB.First(bp, model.BigPartquit{UseForm: p.OrderNumber}).Error; e != nil {
  398. log.Error(e)
  399. hrp.Message = fmt.Sprintf("bigpartquit %d,%s", p.NumberType, p.OrderNumber)
  400. hrp.Msg = "fail"
  401. return
  402. }
  403. p.OrderNumberId = bp.ID
  404. p.ProofCode = bp.ProofCode
  405. p.ChargeDate = bp.QuitDate.Format("2006-01-02")
  406. p.ProofYear = bp.QuitDate.Format("2006")
  407. parts := make([]model.Partquit, 0)
  408. if e = s.d.DB.Where("bigId = ?", bp.ID).Where(" dflag != 1 ").Order("rowNumber,id desc ").Find(&parts).Error; e != nil || len(parts) == 0 {
  409. hrp.Message = fmt.Sprintf("partquit bigid = %d ", bp.ID)
  410. hrp.Msg = "fail"
  411. return
  412. }
  413. for i, v := range parts {
  414. mp := new(model.ProofPart)
  415. c := new(model.Contract)
  416. bc := new(model.BigContract)
  417. pv := new(model.Provider)
  418. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  419. log.Error(e)
  420. hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
  421. return
  422. }
  423. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  424. log.Error(e)
  425. hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
  426. return
  427. }
  428. if e := s.d.DB.First(&pv, model.Provider{ID: bc.ProviderID}).Error; e != nil {
  429. log.Errorln(bc.ProviderID, e)
  430. continue
  431. }
  432. mp.Supplier = pv.SapCode
  433. mp.PartBrand = v.Brand
  434. mp.Warehouse = "1004"
  435. if v.RowNumber > 0 {
  436. mp.RowNumber = v.RowNumber
  437. } else {
  438. mp.RowNumber = len(parts) - i
  439. }
  440. mp.PartCode = v.PartCode
  441. mp.PartName = v.PartName
  442. mp.Specification = v.Specification
  443. mp.ChangeCount = v.QuitNumber
  444. mp.UnitPrice = v.Price
  445. mp.TotalPrice = v.SumPrice
  446. proofs = append(proofs, *mp)
  447. }
  448. p.ProofPart = proofs
  449. hrp.Msg = "ok"
  450. hrp.Data = p
  451. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  452. return
  453. }