charge_off.go 9.2 KB

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