charge_off.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package service
  2. import (
  3. "fmt"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/k0kubun/pp/v3"
  6. log "github.com/sirupsen/logrus"
  7. "kpt.xdmy/apiserver/model"
  8. "kpt.xdmy/apiserver/model/http"
  9. "kpt.xdmy/pkg/util"
  10. )
  11. // 凭证冲销
  12. func (s *Service) ProofOff(p *model.ProofReq) (hrp *http.Response) {
  13. r := new(http.ChargeOffReq)
  14. rp := new(http.SapResp)
  15. hrp = new(http.Response)
  16. hrp.Code = 200
  17. hrp.Msg = "fail"
  18. r.Dest.DestID = "EQMAN"
  19. r.Dest.BussTp = "MM015"
  20. r.Dest.Url = "https://app.modernfarming.cn:7443/sap/Common/MM015/CancelMblnr"
  21. r.Data.ProofCode = p.ProofCode
  22. r.Data.ProofYear = p.ProofYear
  23. r.Data.ChargeDate = p.ChargeDate
  24. r.Data.OrderNumber = p.OrderNumber
  25. r.Data.HasTicket = util.ZeroStr(p.HasTicket == 1, "X")
  26. log.Infof("proofcode: %s", r.Data.ProofCode)
  27. sl := r.Data.RowsNumber
  28. for i := range p.RowsNumber {
  29. sl = append(sl, http.ChargeOffDetail{RowNumber: p.RowsNumber[i]})
  30. }
  31. r.Data.RowsNumber = sl
  32. e := s.SyncSap(r, rp)
  33. pp.Print(r)
  34. if e == nil {
  35. if rp.Data.Status == "S" {
  36. hrp.Message = fmt.Sprintf("sap物料冲销成功:%d %s %s", p.NumberType, rp.Data.ProofNumber, rp.Data.EqNumber)
  37. log.Info(hrp.Message)
  38. } else {
  39. hrp.Message = fmt.Sprintf("sap凭证冲销失败:%s", rp.Dest.MessText)
  40. log.Infoln(hrp.Message, r, p)
  41. return
  42. }
  43. } else {
  44. hrp.Message = fmt.Sprintf("sap冲销接口报错:error:%s", e)
  45. log.Infoln(hrp.Message, r, p)
  46. return
  47. }
  48. hrp.Msg = "ok"
  49. e = UpdateProof(p)
  50. if e != nil {
  51. hrp.Message += fmt.Sprintf("%s . bigid = %d ,rows=%v", e.Error(), p.OrderNumberId, p.RowsNumber)
  52. }
  53. return
  54. }
  55. //凭证冲销后状态更新
  56. func UpdateProof(p *model.ProofReq) (e error) {
  57. var tname string
  58. switch p.NumberType {
  59. case 1: // 入库
  60. tname = "partlaid"
  61. case 2: // 退货
  62. tname = "PartRefund"
  63. case 3: // 出库
  64. tname = "PartUse"
  65. case 4: // 退库
  66. tname = "Partquit"
  67. }
  68. if e = s.d.DB.Table(tname).Where("bigid=? and rownumber in (?)", p.OrderNumberId, p.RowsNumber).Update("dflag", 1).Error; e != nil {
  69. log.Errorln(e, p.OrderNumberId, p.RowsNumber)
  70. }
  71. return e
  72. }
  73. // 查询 单据 对应备件明细
  74. func ProofList(p *model.ProofReq) (hrp *http.Response) {
  75. switch p.NumberType {
  76. case 1: // 入库
  77. return s.laidProofList(p)
  78. case 2: // 退货
  79. return s.returnProofList(p)
  80. case 3: // 出库
  81. return s.useProofList(p)
  82. case 4: // 退库
  83. return s.quitProofList(p)
  84. }
  85. return
  86. }
  87. // 入库冲销
  88. func (s *Service) laidProofList(p *model.ProofReq) (hrp *http.Response) {
  89. bp := new(model.BigPartLaid)
  90. proofs := make([]model.ProofPart, 0)
  91. var e error
  92. hrp = new(http.Response)
  93. hrp.Code = 200
  94. hrp.Msg = "fail"
  95. if e = s.d.DB.First(bp, model.BigPartLaid{LaidCode: p.OrderNumber}).Error; e != nil {
  96. hrp.Message = fmt.Sprintf("%s ,error=%v,", p.OrderNumber, e)
  97. return
  98. }
  99. p.OrderNumberId = bp.ID
  100. p.ProofCode = bp.ProofCode
  101. p.ChargeDate = bp.CreatDate.Format("2006-01-02")
  102. p.ProofYear = bp.CreatDate.Format("2006")
  103. parts := make([]model.PartLaid, 0)
  104. if e = s.d.DB.Order("id asc ").Find(&parts, model.PartLaid{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  105. hrp.Message = fmt.Sprintf("入库单明细查询报错或结果为空:bigid=%v error=%v,", bp.ID, e)
  106. hrp.Msg = "fail"
  107. return
  108. }
  109. for i, v := range parts {
  110. mp := new(model.ProofPart)
  111. br := new(model.Brand)
  112. w := new(model.Warehouse)
  113. pv := new(model.Provider)
  114. if e := s.d.DB.First(&pv, model.Provider{ID: v.ProviderID}).Error; e != nil {
  115. log.Errorln(v.ProviderID, e)
  116. continue
  117. }
  118. if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
  119. log.Errorln(e, v.BrandID)
  120. continue
  121. }
  122. if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  123. log.Errorln(v.LocationID, e)
  124. continue
  125. }
  126. mp.RowNumber = i + 1
  127. mp.PartCode = v.PartCode
  128. mp.PartName = v.PartName
  129. mp.Specification = v.Specification
  130. mp.ChangeCount = v.StorageAmount
  131. mp.UnitPrice = v.Price
  132. mp.TotalPrice = v.SumPrice
  133. mp.PartBrand = br.BrandName
  134. mp.Supplier = pv.ProviderName
  135. mp.Warehouse = w.WarehoseCode
  136. if v.Dflag == 0 {
  137. proofs = append(proofs, *mp)
  138. }
  139. }
  140. p.ProofPart = proofs
  141. hrp.Msg = "ok"
  142. hrp.Data = p
  143. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  144. return
  145. }
  146. // 退货冲销
  147. func (s *Service) returnProofList(p *model.ProofReq) (hrp *http.Response) {
  148. hrp = new(http.Response)
  149. bp := new(model.BigPartRefund)
  150. proofs := make([]model.ProofPart, 0)
  151. hrp.Code = 200
  152. var e error
  153. if e = s.d.DB.First(bp, model.BigPartRefund{UseForm: p.OrderNumber}).Error; e != nil {
  154. hrp.Message = fmt.Sprintf("bigpartrefund : %d,%s", p.NumberType, p.OrderNumber)
  155. hrp.Msg = "fail"
  156. return
  157. }
  158. p.OrderNumberId = bp.ID
  159. p.ProofCode = bp.ProofCode
  160. p.ChargeDate = bp.CreateTime.Format("2006-01-02")
  161. p.ProofYear = bp.CreateTime.Format("2006")
  162. funds := []model.PartRefund{}
  163. if e = s.d.DB.Find(&funds, model.PartRefund{BigID: bp.ID}).Error; e != nil || len(funds) == 0 {
  164. hrp.Message = fmt.Sprintf("PartRefund query error . bigid=%v error=%v,", bp.ID, e)
  165. hrp.Msg = "fail"
  166. return
  167. }
  168. for i, v := range funds {
  169. mp := new(model.ProofPart)
  170. pv := new(model.Provider)
  171. if e := s.d.DB.First(&pv, model.Provider{ID: bp.ProviderID}).Error; e != nil {
  172. log.Errorln(bp.ProviderID, e)
  173. continue
  174. }
  175. mp.Supplier = pv.ProviderName
  176. mp.RowNumber = i
  177. mp.PartBrand = v.Brand
  178. mp.Warehouse = v.Location
  179. mp.PartCode = v.PartCode
  180. mp.PartName = v.PartName
  181. mp.Specification = v.Specification
  182. mp.ChangeCount = v.RefundNumber
  183. mp.UnitPrice = v.Price
  184. mp.TotalPrice = v.SumPrice
  185. proofs = append(proofs, *mp)
  186. }
  187. p.ProofPart = proofs
  188. hrp.Msg = "ok"
  189. hrp.Data = p
  190. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  191. return
  192. }
  193. // 出库冲销
  194. func (s *Service) useProofList(p *model.ProofReq) (hrp *http.Response) {
  195. hrp = new(http.Response)
  196. bp := new(model.BigPartUse)
  197. proofs := make([]model.ProofPart, 0)
  198. hrp.Code = 200
  199. hrp.Msg = "fail"
  200. var e error
  201. if e = s.d.DB.First(bp, model.BigPartUse{UseForm: p.OrderNumber}).Error; e != nil {
  202. log.Error(e)
  203. hrp.Message = fmt.Sprintf("bigpartuse useform=%s", p.OrderNumber)
  204. return
  205. }
  206. p.OrderNumberId = bp.ID
  207. p.ProofCode = bp.ProofCode
  208. p.ChargeDate = bp.ReceiveTime.Format("2006-01-02")
  209. p.ProofYear = bp.ReceiveTime.Format("2006")
  210. uses := make([]model.PartUse, 0)
  211. if e = s.d.DB.Find(&uses, model.PartUse{BigID: bp.ID}).Error; e != nil || len(uses) == 0 {
  212. log.Error(e)
  213. hrp.Message = fmt.Sprintf("PartUse bigid = %v ", bp.ID)
  214. return
  215. }
  216. for i, v := range uses {
  217. mp := new(model.ProofPart)
  218. br := new(model.Brand)
  219. w := new(model.Warehouse)
  220. c := new(model.Contract)
  221. bc := new(model.BigContract)
  222. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  223. log.Error(e)
  224. hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
  225. return
  226. }
  227. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  228. log.Error(e)
  229. hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
  230. return
  231. }
  232. if e := s.d.DB.First(&br, model.Brand{ID: v.BrandID}).Error; e != nil {
  233. log.Error(e)
  234. hrp.Message = fmt.Sprintf("brand id =%d", v.BrandID)
  235. return
  236. }
  237. if e := s.d.DB.First(&w, model.Warehouse{ID: v.LocationID}).Error; e != nil {
  238. log.Errorln(v.LocationID, e)
  239. return
  240. }
  241. mp.PartBrand = br.BrandName
  242. mp.Warehouse = w.WarehoseCode
  243. mp.Supplier = bc.ProviderName
  244. mp.RowNumber = i + 1
  245. mp.PartCode = v.PartCode
  246. mp.PartName = v.PartName
  247. mp.Specification = v.Specification
  248. mp.ChangeCount = v.CheckoutNumber
  249. mp.UnitPrice = v.Price
  250. mp.TotalPrice = v.SumPrice
  251. proofs = append(proofs, *mp)
  252. }
  253. p.ProofPart = proofs
  254. hrp.Msg = "ok"
  255. hrp.Data = p
  256. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  257. return
  258. }
  259. // 退库冲销
  260. func (s *Service) quitProofList(p *model.ProofReq) (hrp *http.Response) {
  261. hrp = new(http.Response)
  262. bp := new(model.BigPartquit)
  263. proofs := make([]model.ProofPart, 0)
  264. hrp.Code = 200
  265. var e error
  266. if e = s.d.DB.First(bp, model.BigPartquit{UseForm: p.OrderNumber}).Error; e != nil {
  267. log.Error(e)
  268. hrp.Message = fmt.Sprintf("bigpartquit %d,%s", p.NumberType, p.OrderNumber)
  269. hrp.Msg = "fail"
  270. return
  271. }
  272. p.OrderNumberId = bp.ID
  273. p.ProofCode = bp.ProofCode
  274. p.ChargeDate = bp.QuitDate.Format("2006-01-02")
  275. p.ProofYear = bp.QuitDate.Format("2006")
  276. parts := make([]model.Partquit, 0)
  277. if e = s.d.DB.Find(&parts, model.Partquit{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  278. hrp.Message = fmt.Sprintf("partquit bigid = %d ", bp.ID)
  279. hrp.Msg = "fail"
  280. return
  281. }
  282. for i, v := range parts {
  283. mp := new(model.ProofPart)
  284. c := new(model.Contract)
  285. bc := new(model.BigContract)
  286. if e := s.d.DB.First(c, model.Contract{ID: v.ContractID}).Error; e != nil {
  287. log.Error(e)
  288. hrp.Message = fmt.Sprintf("contract id =%d", v.ContractID)
  289. return
  290. }
  291. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  292. log.Error(e)
  293. hrp.Message = fmt.Sprintf("bigcontract id =%d", v.BigID)
  294. return
  295. }
  296. mp.Supplier = bc.ProviderName
  297. mp.PartBrand = v.Brand
  298. mp.Warehouse = v.Location
  299. mp.RowNumber = i
  300. mp.PartCode = v.PartCode
  301. mp.PartName = v.PartName
  302. mp.Specification = v.Specification
  303. mp.ChangeCount = v.QuitNumber
  304. mp.UnitPrice = v.Price
  305. mp.TotalPrice = v.SumPrice
  306. proofs = append(proofs, *mp)
  307. }
  308. p.ProofPart = proofs
  309. hrp.Msg = "ok"
  310. hrp.Data = p
  311. hrp.Message = fmt.Sprintf("success :type = %v OrderNumber = %s", p.NumberType, p.OrderNumber)
  312. return
  313. }