InboundPurchaseService.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. package com.dk.mdm.service.ivt;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.dk.common.exception.BaseBusinessException;
  4. import com.dk.common.infrastructure.annotaiton.Pagination;
  5. import com.dk.common.infrastructure.constant.Constant;
  6. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  7. import com.dk.common.mapper.BaseMapper;
  8. import com.dk.common.model.pojo.PageList;
  9. import com.dk.common.response.ResponseCodeEnum;
  10. import com.dk.common.response.ResponseResultUtil;
  11. import com.dk.common.response.ResponseResultVO;
  12. import com.dk.common.service.BaseService;
  13. import com.dk.mdm.infrastructure.convert.ivt.InboundConvert;
  14. import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
  15. import com.dk.mdm.mapper.ivt.InboundItemMapper;
  16. import com.dk.mdm.mapper.ivt.InboundMapper;
  17. import com.dk.mdm.mapper.pur.PurchaseItemMapper;
  18. import com.dk.mdm.mapper.pur.PurchaseMapper;
  19. import com.dk.mdm.model.pojo.ivt.Inbound;
  20. import com.dk.mdm.model.pojo.ivt.InboundItem;
  21. import com.dk.mdm.model.pojo.pur.Purchase;
  22. import com.dk.mdm.model.pojo.pur.PurchaseItem;
  23. import com.dk.mdm.model.query.ivt.InboundItemQuery;
  24. import com.dk.mdm.model.query.ivt.InboundQuery;
  25. import com.dk.mdm.model.response.ivt.InboundItemResponse;
  26. import com.dk.mdm.model.response.ivt.InboundResponse;
  27. import com.dk.mdm.model.response.pur.PurchaseItemResponse;
  28. import com.dk.mdm.model.response.pur.PurchaseResponse;
  29. import com.dk.mdm.model.vo.ivt.InboundItemVO;
  30. import com.dk.mdm.model.vo.ivt.InboundVO;
  31. import com.dk.mdm.service.common.CommonService;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.math.BigDecimal;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.UUID;
  40. /**
  41. * @desc : 采购入库业务层
  42. * @date : 2024/3/7 14:11
  43. * @author : 寇珊珊
  44. */
  45. @Service
  46. public class InboundPurchaseService extends BaseService<Inbound> {
  47. @Override
  48. public BaseMapper<Inbound> getRepository() {
  49. return inboundMapper;
  50. }
  51. @Autowired
  52. private InboundMapper inboundMapper;
  53. @Autowired
  54. private InboundConvert inboundConvert;
  55. @Autowired
  56. private InboundItemMapper inboundItemMapper;
  57. @Autowired
  58. private InboundItemConvert inboundItemConvert;
  59. @Autowired
  60. private PurchaseMapper purchaseMapper;
  61. @Autowired
  62. private PurchaseItemMapper purchaseItemMapper;
  63. @Autowired
  64. private CommonService commonService;
  65. /**
  66. * @desc : 条件查询
  67. * @date : 2024/3/7 14:12
  68. * @author : 寇珊珊
  69. */
  70. @Pagination
  71. public ResponseResultVO<PageList<InboundResponse>> selectByCond(InboundQuery inboundQuery) {
  72. return super.mergeListWithCount(inboundQuery, inboundMapper.selectByCond(inboundQuery),
  73. inboundMapper.countByCond(inboundQuery));
  74. }
  75. /**
  76. * @desc : 查询明细
  77. * @date : 2024/3/9 15:43
  78. * @author : 寇珊珊
  79. */
  80. @Pagination
  81. public ResponseResultVO<Map<String, Object>> selectPurchaseInboundItemInfoById(String id) {
  82. Map<String, Object> result = new HashMap<>();
  83. // 商品明细
  84. List<InboundItemResponse> inboundItemResponses = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(id));
  85. result.put("itemList", inboundItemResponses);
  86. // 收款
  87. // 附件
  88. return ResponseResultUtil.success(result);
  89. }
  90. /**
  91. * @desc : 采购入库新建
  92. * @date : 2024/3/7 14:13
  93. * 入库中数量/金额 已入库数量/金额 由调用方传入
  94. * @author : 寇珊珊
  95. */
  96. @Transactional(rollbackFor = {Exception.class})
  97. public ResponseResultVO<?> purchaseInboundInsert(InboundVO inboundVO) {
  98. //region 总单
  99. //获取 id/单号
  100. Map<String , Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.PURCASEINBOUND.getName(),false);
  101. inboundVO.setIntoId(codeMap.get("outId").toString()).
  102. setIntoNo(codeMap.get("outNote").toString());
  103. //入库类型
  104. inboundVO.setIntoType(Constant.IntoType.SALE.getName());
  105. //自动入库标识
  106. if (inboundVO.getAutomaticFlg()) {
  107. //已入库
  108. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  109. } else {
  110. //入库中
  111. inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
  112. }
  113. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  114. if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
  115. inboundVO.setIntoQty(inboundVO.getIntoingQty())
  116. .setIntoAmt(inboundVO.getIntoingAmt())
  117. .setIntoingQty(BigDecimal.ZERO)
  118. .setIntoingAmt(BigDecimal.ZERO)
  119. ;
  120. }else{
  121. inboundVO.setIntoQty(BigDecimal.ZERO)
  122. .setIntoAmt(BigDecimal.ZERO)
  123. ;
  124. }
  125. //实体转换
  126. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  127. inboundMapper.insert(inbound);
  128. //endregion
  129. //region 采购总单
  130. if(inboundVO.getFromId()!=null){
  131. Purchase purchaseUpdate = new Purchase();
  132. purchaseUpdate.setPurId(inboundVO.getFromId());
  133. purchaseUpdate.setIntoingQty(inboundVO.getIntoingQty().negate());
  134. purchaseUpdate.setIntoingAmt(inboundVO.getIntoingAmt().negate());
  135. purchaseUpdate.setIntoAmt(inboundVO.getIntoAmt());
  136. purchaseUpdate.setIntoQty(inboundVO.getIntoQty());
  137. //入库状态
  138. String intoStatus = this.setIntoStatus(purchaseUpdate.getIntoingQty(), purchaseUpdate.getIntoQty());
  139. purchaseUpdate.setIntoStatus(intoStatus);
  140. //修改
  141. int countRow = purchaseMapper.updateById(purchaseUpdate);
  142. //数量超出
  143. if (countRow == 0) {
  144. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  145. }
  146. }
  147. //endregion
  148. //region 明细
  149. //校验明细
  150. if (inboundVO.getItemList().size() == 0) {
  151. return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
  152. ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
  153. }
  154. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  155. //总单id
  156. inboundItemVO.setIntoId(inboundVO.getIntoId());
  157. //入库类型
  158. inboundItemVO.setIntoType(inboundVO.getIntoType());
  159. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  160. if (Constant.IntoStatus.YIRUKU.getName().equals(inboundVO.getIntoStatus())) {
  161. inboundItemVO.setIntoQty(inboundItemVO.getIntoingQty())
  162. .setIntoAmt(inboundItemVO.getIntoingAmt())
  163. .setIntoingQty(BigDecimal.ZERO)
  164. .setIntoingAmt(BigDecimal.ZERO)
  165. ;
  166. }else{
  167. inboundItemVO
  168. .setIntoQty(BigDecimal.ZERO)
  169. .setIntoAmt(BigDecimal.ZERO);
  170. }
  171. //成本单价
  172. inboundItemVO.setCostPrice(inboundItemVO.getPriceInto());
  173. //成本金额
  174. inboundItemVO.setCostAmt((inboundItemVO.getIntoingQty().add(inboundItemVO.getIntoQty())).multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP));
  175. //库存数量 已入库-出库
  176. inboundItemVO.setInvQty(inboundItemVO.getIntoQty().subtract(inboundItemVO.getOutQty()));
  177. //入库状态
  178. inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
  179. //实体转换
  180. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  181. inboundItemMapper.insert(inboundItem);
  182. //endregion
  183. //region 采购明细
  184. if(inboundItemVO.getFromItemId()!=null){
  185. //根据id查询
  186. // PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
  187. PurchaseItem purchaseItem = new PurchaseItem();
  188. purchaseItem.setItemId(inboundItemVO.getFromItemId());
  189. // purchaseItem.setIntoingQty(purchaseItemResponse.getIntoingQty().add(inboundItemVO.getIntoingQty()));
  190. // purchaseItem.setIntoingAmt(purchaseItemResponse.getIntoingAmt().add(inboundItemVO.getIntoingAmt()));
  191. // purchaseItem.setIntoQty(purchaseItemResponse.getIntoQty().add(inboundItemVO.getIntoQty()));
  192. // purchaseItem.setIntoAmt(purchaseItemResponse.getIntoAmt().add(inboundItemVO.getIntoAmt()));
  193. purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty());
  194. purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt());
  195. purchaseItem.setIntoQty(inboundItemVO.getIntoQty());
  196. purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt());
  197. //入库状态
  198. String intoStatus = this.setIntoStatus(purchaseItem.getIntoingQty(), purchaseItem.getIntoQty());
  199. purchaseItem.setIntoStatus(intoStatus);
  200. int countRow = purchaseItemMapper.updateById(purchaseItem);
  201. //数量超出
  202. if (countRow == 0) {
  203. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  204. }
  205. }
  206. //endregion
  207. }
  208. //endregion
  209. //todo 如果是已入库 调用库存 后续写库存这里补上
  210. //region 库存
  211. //endregion
  212. return ResponseResultUtil.success(inboundVO);
  213. }
  214. /**
  215. * @desc : 采购入库办理
  216. * @date : 2024/3/7 15:47
  217. * @author : 寇珊珊
  218. */
  219. @Transactional(rollbackFor = {Exception.class})
  220. public ResponseResultVO<?> purchaseHandleInbound(InboundVO inboundVO) {
  221. //region 编辑明细
  222. //校验明细
  223. if (inboundVO.getItemList().size() == 0) {
  224. return ResponseResultUtil.error(ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getCode(),
  225. ErrorCodeEnum.INBOUND_ITEM_NOT_EXIST.getMessage());
  226. }
  227. for (InboundItemVO inboundItemVO : inboundVO.getItemList()) {
  228. if (inboundItemVO.getIntoId() != null) {
  229. InboundItemResponse inboundItemResponse = inboundItemMapper.selectById(inboundItemVO.getItemId());
  230. //编辑明细
  231. inboundItemVO
  232. .setIntoQty(inboundItemResponse.getIntoQty().add(inboundItemVO.getIntoingQty()))
  233. .setIntoAmt(inboundItemResponse.getIntoAmt().add(inboundItemVO.getIntoingAmt()))
  234. .setIntoingQty(inboundItemResponse.getIntoingQty().subtract(inboundItemVO.getIntoingQty()))
  235. .setIntoingAmt(inboundItemResponse.getIntoingAmt().subtract(inboundItemVO.getIntoingAmt()))
  236. ;
  237. //入库状态
  238. String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
  239. inboundItemVO.setIntoStatus(intoStatus);
  240. //实体转换
  241. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  242. //修改
  243. inboundItemMapper.update(inboundItem,
  244. new UpdateWrapper<InboundItem>().lambda()
  245. .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
  246. );
  247. }
  248. //endregion
  249. //region 新建明细
  250. else {
  251. inboundItemVO
  252. .setIntoQty(inboundItemVO.getIntoingQty())
  253. .setIntoAmt(inboundItemVO.getIntoingAmt())
  254. .setIntoId(inboundVO.getIntoId())
  255. .setCostPrice(inboundItemVO.getPriceInto())
  256. .setCostAmt((inboundItemVO.getIntoingQty().add(inboundItemVO.getIntoQty())).multiply(inboundItemVO.getPriceInto()).setScale(2, BigDecimal.ROUND_HALF_UP))
  257. .setIntoType(Constant.IntoType.SALE.getName());
  258. //入库状态
  259. String intoStatus = this.setIntoStatus(inboundItemVO.getIntoingQty(), inboundItemVO.getIntoQty());
  260. inboundItemVO.setIntoStatus(intoStatus);
  261. //实体转换
  262. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  263. //新建
  264. inboundItemMapper.insert(inboundItem);
  265. }
  266. //region 采购
  267. //采购明细
  268. if (inboundItemVO.getFromItemId() != null) {
  269. // PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
  270. PurchaseItem purchaseItem = new PurchaseItem();
  271. purchaseItem.setItemId(inboundItemVO.getFromItemId());
  272. // purchaseItem.setIntoingQty(purchaseItemResponse.getIntoingQty().add(inboundItemVO.getIntoingQty()));
  273. // purchaseItem.setIntoingAmt(purchaseItemResponse.getIntoingAmt().add(inboundItemVO.getIntoingAmt()));
  274. // purchaseItem.setIntoQty(purchaseItemResponse.getIntoQty().add(inboundItemVO.getIntoQty()));
  275. // purchaseItem.setIntoAmt(purchaseItemResponse.getIntoAmt().add(inboundItemVO.getIntoAmt()));
  276. purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty());
  277. purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt());
  278. purchaseItem.setIntoQty(inboundItemVO.getIntoQty());
  279. purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt());
  280. //入库状态
  281. String intoStatus = this.setIntoStatus(purchaseItem.getIntoingQty(), purchaseItem.getIntoQty());
  282. purchaseItem.setIntoStatus(intoStatus);
  283. int countRow = purchaseItemMapper.updateById(purchaseItem);
  284. //数量超出
  285. if (countRow == 0) {
  286. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  287. }
  288. }
  289. //endregion
  290. }
  291. //endregion
  292. //region 删除明细
  293. BigDecimal sumDelIntoQty = BigDecimal.ZERO;
  294. BigDecimal sumDelIntoAmt = BigDecimal.ZERO;
  295. BigDecimal sumDelIntoingQty = BigDecimal.ZERO;
  296. BigDecimal sumDelIntoingAmt = BigDecimal.ZERO;
  297. if (inboundVO.getDeleteItemList() != null) {
  298. sumDelIntoQty = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  299. sumDelIntoAmt = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  300. sumDelIntoingQty = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  301. sumDelIntoingAmt = inboundVO.getDeleteItemList().stream().map(InboundItemVO::getIntoingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  302. for (InboundItemVO inboundItemVO : inboundVO.getDeleteItemList()) {
  303. if (inboundItemVO.getItemId() != null) {
  304. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  305. inboundItem.setFlgValid(false);
  306. //修改
  307. inboundItemMapper.update(inboundItem,
  308. new UpdateWrapper<InboundItem>().lambda()
  309. .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
  310. );
  311. }
  312. //region 采购
  313. if (inboundItemVO.getFromItemId() != null) {
  314. //region 采购订单明细
  315. // PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemVO.getFromItemId());
  316. PurchaseItem purchaseItem = new PurchaseItem();
  317. purchaseItem.setItemId(inboundItemVO.getFromItemId());
  318. // purchaseItem.setIntoingQty(purchaseItemResponse.getIntoingQty().subtract(inboundItemVO.getIntoingQty()));
  319. // purchaseItem.setIntoingAmt(purchaseItemResponse.getIntoingAmt().subtract(inboundItemVO.getIntoingAmt()));
  320. // purchaseItem.setIntoQty(purchaseItemResponse.getIntoQty().subtract(inboundItemVO.getIntoQty()));
  321. // purchaseItem.setIntoAmt(purchaseItemResponse.getIntoAmt().subtract(inboundItemVO.getIntoAmt()));
  322. purchaseItem.setIntoingQty(inboundItemVO.getIntoingQty().negate());
  323. purchaseItem.setIntoingAmt(inboundItemVO.getIntoingAmt().negate());
  324. purchaseItem.setIntoQty(inboundItemVO.getIntoQty().negate());
  325. purchaseItem.setIntoAmt(inboundItemVO.getIntoAmt().negate());
  326. //入库状态
  327. String intoStatus = this.setIntoStatus(purchaseItem.getIntoingQty(), purchaseItem.getIntoQty());
  328. purchaseItem.setIntoStatus(intoStatus);
  329. int countRow = purchaseItemMapper.updateById(purchaseItem);
  330. //数量超出
  331. if (countRow == 0) {
  332. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  333. }
  334. //endregion
  335. }
  336. //endregion
  337. }
  338. }
  339. //endregion
  340. //region 编辑总单
  341. InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
  342. BigDecimal sumIntoQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  343. BigDecimal sumIntoAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  344. BigDecimal sumIntoingQty = inboundVO.getItemList().stream().map(InboundItemVO::getIntoingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  345. BigDecimal sumIntoingAmt = inboundVO.getItemList().stream().map(InboundItemVO::getIntoAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  346. inboundVO.setIntoQty(sumIntoQty);
  347. inboundVO.setIntoAmt(sumIntoAmt);
  348. inboundVO.setIntoingQty(inboundResponse.getIntoingQty().subtract(sumIntoingQty));
  349. inboundVO.setIntoingAmt(inboundResponse.getIntoingAmt().subtract(sumIntoingAmt));
  350. //已入库数量==入库中数量 入库完成
  351. if (inboundVO.getIntoQty().compareTo(inboundVO.getIntoingQty()) == 0) {
  352. //已入库
  353. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  354. } else {
  355. //入库中
  356. inboundVO.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
  357. }
  358. //实体转换
  359. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  360. //修改
  361. inboundMapper.update(inbound,
  362. new UpdateWrapper<Inbound>().lambda()
  363. .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
  364. );
  365. //endregion
  366. //region 修改采购订单
  367. if (inboundVO.getFromId() != null) {
  368. PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundVO.getFromId());
  369. Purchase purchase = new Purchase();
  370. purchase.setPurId(purchaseResponse.getPurId());
  371. // purchase.setIntoQty(purchaseResponse.getIntoQty().add(sumIntoQty).subtract(sumDelIntoQty));
  372. // purchase.setIntoAmt(purchaseResponse.getIntoAmt().add(sumIntoAmt).subtract(sumDelIntoAmt));
  373. // purchase.setIntoingQty(purchaseResponse.getIntoingQty().subtract(sumIntoingQty).subtract(sumDelIntoingQty));
  374. // purchase.setIntoingAmt(purchaseResponse.getIntoingAmt().subtract(sumIntoingAmt).subtract(sumDelIntoingAmt));
  375. purchase.setIntoQty(sumIntoQty.subtract(sumDelIntoQty));
  376. purchase.setIntoAmt(sumIntoAmt.subtract(sumDelIntoAmt));
  377. purchase.setIntoingQty((sumIntoingQty.add(sumDelIntoingQty)).negate());
  378. purchase.setIntoingAmt((sumIntoingAmt.add(sumDelIntoingAmt)).negate());
  379. //入库状态
  380. String intoStatus = this.setIntoStatus(purchase.getIntoingQty(), purchase.getIntoQty());
  381. purchase.setIntoStatus(intoStatus);
  382. //修改
  383. int countRow = purchaseMapper.updateById(purchase);
  384. //数量超出
  385. if (countRow == 0) {
  386. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  387. }
  388. }
  389. //endregion
  390. //todo 如果是已入库 调用库存 后续写库存这里补上
  391. //region 修改库存
  392. //endregion
  393. return ResponseResultUtil.success(inboundVO);
  394. }
  395. /**
  396. * @desc : 采购入库撤销
  397. * @date : 2024/3/7 17:06
  398. * @author : 寇珊珊
  399. */
  400. @Transactional(rollbackFor = {Exception.class})
  401. public ResponseResultVO<?> purchaseInboundCancel(InboundVO inboundVO) {
  402. //region 查询入库总单数据信息
  403. InboundResponse inboundResponse = inboundMapper.selectById(inboundVO.getIntoId());
  404. //endregion
  405. //region 修改订单数据信息
  406. if (inboundVO.getFromId() != null) {
  407. // PurchaseResponse purchaseResponse = purchaseMapper.selectById(inboundResponse.getFromId());
  408. Purchase purchase = new Purchase();
  409. purchase.setPurId(inboundResponse.getFromId());
  410. // purchase.setIntoQty(purchaseResponse.getIntoQty().subtract(inboundResponse.getIntoQty()));
  411. // purchase.setIntoAmt(purchaseResponse.getIntoAmt().subtract(inboundResponse.getIntoAmt()));
  412. // purchase.setIntoingQty(purchaseResponse.getIntoingQty().add(inboundResponse.getIntoQty()));
  413. // purchase.setIntoingAmt(purchaseResponse.getIntoingAmt().add(inboundResponse.getIntoAmt()));
  414. purchase.setIntoQty(inboundResponse.getIntoQty().negate());
  415. purchase.setIntoAmt(inboundResponse.getIntoAmt().negate());
  416. purchase.setIntoingQty(inboundResponse.getIntoQty());
  417. purchase.setIntoingAmt(inboundResponse.getIntoAmt());
  418. //入库状态
  419. String intoStatus = this.setIntoStatus(purchase.getIntoingQty(), purchase.getIntoQty());
  420. purchase.setIntoStatus(intoStatus);
  421. int countRow = purchaseMapper.updateById(purchase);
  422. //数量超出
  423. if (countRow == 0) {
  424. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  425. }
  426. }
  427. //endregion
  428. //region 修改总单数据信息
  429. Inbound inbound = new Inbound();
  430. inbound.setIntoId(inboundVO.getIntoId());
  431. inbound.setIntoDate(null);
  432. inbound.setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName());
  433. inbound.setIntoingQty(inboundResponse.getIntoingQty().add(inboundResponse.getIntoQty()));
  434. inbound.setIntoingAmt(inboundResponse.getIntoingAmt().add(inboundResponse.getIntoAmt()));
  435. inbound.setIntoQty(BigDecimal.ZERO);
  436. inbound.setIntoAmt(BigDecimal.ZERO);
  437. //修改
  438. inboundMapper.update(inbound,
  439. new UpdateWrapper<Inbound>().lambda()
  440. .eq(Inbound::getIntoId, UUID.fromString(inbound.getIntoId()))
  441. );
  442. //endregion
  443. //region 明细数据
  444. //根据总单id查明细
  445. List<InboundItemResponse> orderEntryItemResponsesList = inboundItemMapper.selectByCond(new InboundItemQuery().setIntoId(inbound.getIntoId()));
  446. for (InboundItemResponse inboundItemResponse : orderEntryItemResponsesList) {
  447. //region 修改采购明细数据信息
  448. if(inboundItemResponse.getFromItemId()!=null){
  449. //查询
  450. PurchaseItemResponse purchaseItemResponse = purchaseItemMapper.selectById(inboundItemResponse.getFromItemId());
  451. PurchaseItem purchaseItem = new PurchaseItem();
  452. purchaseItem.setItemId(inboundItemResponse.getFromItemId());
  453. // purchaseItem.setIntoQty(purchaseItemResponse.getIntoQty().subtract(inboundItemResponse.getIntoQty()));
  454. // purchaseItem.setIntoAmt(purchaseItemResponse.getIntoAmt().subtract(inboundItemResponse.getIntoAmt()));
  455. // purchaseItem.setIntoingQty(purchaseItemResponse.getIntoingQty().add(inboundItemResponse.getIntoQty()));
  456. // purchaseItem.setIntoingAmt(purchaseItemResponse.getIntoingAmt().add(inboundItemResponse.getIntoAmt()));
  457. purchaseItem.setIntoQty(inboundItemResponse.getIntoQty().negate());
  458. purchaseItem.setIntoAmt(inboundItemResponse.getIntoAmt().negate());
  459. purchaseItem.setIntoingQty(inboundItemResponse.getIntoQty());
  460. purchaseItem.setIntoingAmt(inboundItemResponse.getIntoAmt());
  461. //入库状态
  462. String intoStatus= this.setIntoStatus(purchaseItem.getIntoingQty(), purchaseItem.getIntoQty());
  463. purchaseItem.setIntoStatus(intoStatus);
  464. //修改
  465. int countRow = purchaseItemMapper.updateById(purchaseItem);
  466. //数量超出
  467. if (countRow == 0) {
  468. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED.getMessage());
  469. }
  470. }
  471. //endregion
  472. //region修改入库明细信息
  473. InboundItem inboundItem = new InboundItem();
  474. inboundItem.setIntoId(inbound.getIntoId())
  475. .setIntoStatus(Constant.IntoStatus.RUKUZHONG.getName())
  476. .setIntoingQty(inboundItemResponse.getIntoingQty().add(inboundItemResponse.getIntoQty()))
  477. .setIntoingAmt(inboundItemResponse.getIntoingAmt().add(inboundItemResponse.getIntoAmt()))
  478. .setIntoQty(BigDecimal.ZERO)
  479. .setIntoAmt(BigDecimal.ZERO)
  480. .setItemId(inboundItemResponse.getItemId());
  481. //修改
  482. inboundItemMapper.update(inboundItem,
  483. new UpdateWrapper<InboundItem>().lambda()
  484. .eq(InboundItem::getItemId, UUID.fromString(inboundItem.getItemId()))
  485. );
  486. //endregion
  487. }
  488. //endregion
  489. //todo 调用库存 后续写库存这里补上
  490. //region 修改库存
  491. //endregion
  492. return ResponseResultUtil.success();
  493. }
  494. /**
  495. * @desc : 入库状态通用(目前本页面)
  496. * @date : 2024/3/9 8:59
  497. * @author : 寇珊珊
  498. */
  499. public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty) {
  500. //入库状态
  501. String intoStatus = null;
  502. //已入库数量>0 入库中数量>0
  503. if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0 ) {
  504. //入库中
  505. intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
  506. }
  507. //已入库数量=0 入库中数量=0
  508. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  509. //待入库
  510. intoStatus = Constant.IntoStatus.DAIRUKU.getName();
  511. }
  512. //已入库数量>0 入库中数量=0
  513. else if (intoQty.compareTo(BigDecimal.ZERO) >0 && intoingQty.compareTo(BigDecimal.ZERO)==0) {
  514. //已入库
  515. intoStatus = Constant.IntoStatus.YIRUKU.getName();
  516. }
  517. return intoStatus;
  518. }
  519. }