OutboundSaleOrderService.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. package com.dk.mdm.service.ivt.outbound;
  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.OutboundConvert;
  14. import com.dk.mdm.infrastructure.convert.ivt.OutboundItemConvert;
  15. import com.dk.mdm.mapper.ivt.OutboundItemMapper;
  16. import com.dk.mdm.mapper.ivt.OutboundMapper;
  17. import com.dk.mdm.mapper.sale.OrderItemMapper;
  18. import com.dk.mdm.mapper.sale.OrderMapper;
  19. import com.dk.mdm.model.pojo.ivt.Outbound;
  20. import com.dk.mdm.model.pojo.ivt.OutboundItem;
  21. import com.dk.mdm.model.pojo.sale.Order;
  22. import com.dk.mdm.model.pojo.sale.OrderItem;
  23. import com.dk.mdm.model.query.ivt.OutboundItemQuery;
  24. import com.dk.mdm.model.query.ivt.OutboundQuery;
  25. import com.dk.mdm.model.response.ivt.InboundResponse;
  26. import com.dk.mdm.model.response.ivt.OutboundItemResponse;
  27. import com.dk.mdm.model.response.ivt.OutboundResponse;
  28. import com.dk.mdm.model.response.sale.OrderItemResponse;
  29. import com.dk.mdm.model.response.sale.OrderResponse;
  30. import com.dk.mdm.model.vo.ivt.OutboundItemVO;
  31. import com.dk.mdm.model.vo.ivt.OutboundVO;
  32. import com.dk.mdm.service.common.CommonService;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.math.BigDecimal;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.UUID;
  41. /**
  42. * @desc : 销售出库业务层
  43. * @date : 2024/3/18 15:33
  44. * @author : 寇珊珊
  45. */
  46. @Service
  47. public class OutboundSaleOrderService extends BaseService<Outbound> {
  48. @Override
  49. public BaseMapper<Outbound> getRepository() {
  50. return outboundMapper;
  51. }
  52. @Autowired
  53. private CommonService commonService;
  54. @Autowired
  55. private OutboundMapper outboundMapper;
  56. @Autowired
  57. private OutboundConvert outboundConvert;
  58. @Autowired
  59. private OutboundItemMapper outboundItemMapper;
  60. @Autowired
  61. private OutboundItemConvert outboundItemConvert;
  62. @Autowired
  63. private OrderMapper orderMapper;
  64. @Autowired
  65. private OrderItemMapper orderItemMapper;
  66. /**
  67. * @desc : 销售出库新建
  68. * @date : 2024/3/7 14:13
  69. * @author : 寇珊珊
  70. */
  71. @Transactional(rollbackFor = {Exception.class})
  72. public ResponseResultVO<?> saleOrderOutboundInsert(OutboundVO outboundVO) {
  73. //region 总单
  74. //获取 id/单号
  75. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.SALEORDER.getName(), false);
  76. outboundVO.setOutId(codeMap.get("outId").toString()).
  77. setOutNo(codeMap.get("outNote").toString());
  78. //出库类型
  79. outboundVO.setOutType(Constant.OutType.SALE.getName());
  80. //自动入库标识
  81. if (outboundVO.getAutomaticFlg()) {
  82. //已出库
  83. outboundVO.setOutStatus(Constant.OutStatus.YICHUKU.getName());
  84. } else {
  85. //出库中
  86. outboundVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  87. }
  88. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  89. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  90. outboundVO
  91. .setOutQty(outboundVO.getOutingQty())
  92. .setOutAmt(outboundVO.getOutingAmt())
  93. .setOutingQty(BigDecimal.ZERO)
  94. .setOutingAmt(BigDecimal.ZERO)
  95. ;
  96. } else {
  97. outboundVO
  98. .setOutQty(BigDecimal.ZERO)
  99. .setOutAmt(BigDecimal.ZERO)
  100. ;
  101. }
  102. //实体转换
  103. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  104. outboundMapper.insert(outbound);
  105. //endregion
  106. //region 销售退货
  107. if (outboundVO.getFromId() != null) {
  108. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  109. Order order = new Order();
  110. order.setOrderId(outboundVO.getFromId());
  111. //根据id查询
  112. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  113. //已出库
  114. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())){
  115. //第一次已出库
  116. if(orderResponse.getOutingQty().compareTo(BigDecimal.ZERO)==0){
  117. order.setOutingQty(BigDecimal.ZERO);
  118. order.setOutingAmt(BigDecimal.ZERO);
  119. }
  120. //多次已出库
  121. else{
  122. order.setOutingQty(outboundVO.getOutQty().negate());
  123. order.setOutingAmt(outboundVO.getOutAmt().negate());
  124. }
  125. order.setOutQty(outboundVO.getOutQty());
  126. order.setOutAmt(outboundVO.getOutAmt());
  127. }
  128. //出库中
  129. else{
  130. order.setOutingQty(outboundVO.getOutingQty());
  131. order.setOutingAmt(outboundVO.getOutingAmt());
  132. order.setOutQty(BigDecimal.ZERO);
  133. order.setOutAmt(BigDecimal.ZERO);
  134. }
  135. //入库状态
  136. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  137. order.setOutStatus(outStatus);
  138. //修改
  139. int countRow = orderMapper.updateById(order);
  140. //数量超出
  141. if (countRow == 0) {
  142. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  143. }
  144. }
  145. //endregion
  146. //region 明细
  147. //校验明细
  148. if (outboundVO.getItemList().size() == 0) {
  149. return ResponseResultUtil.error(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  150. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  151. }
  152. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  153. //总单id
  154. outboundItemVO.setOutId(outboundVO.getOutId());
  155. //出库类型
  156. outboundItemVO.setOutType(outboundVO.getOutType());
  157. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  158. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  159. outboundItemVO
  160. .setOutQty(outboundItemVO.getOutingQty())
  161. .setOutAmt(outboundItemVO.getOutingAmt())
  162. .setOutingQty(BigDecimal.ZERO)
  163. .setOutingAmt(BigDecimal.ZERO)
  164. .setCostPrice(outboundItemVO.getPriceOut())
  165. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  166. ;
  167. } else {
  168. outboundItemVO
  169. .setOutQty(BigDecimal.ZERO)
  170. .setOutAmt(BigDecimal.ZERO);
  171. }
  172. //入库状态
  173. outboundItemVO.setOutStatus(outboundItemVO.getOutStatus());
  174. //实体转换
  175. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  176. outboundItemMapper.insert(outboundItem);
  177. //endregion
  178. //region 销售退货明细
  179. if (outboundItemVO.getFromItemId() != null) {
  180. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  181. OrderItem orderItem = new OrderItem();
  182. orderItem.setItemId(outboundItemVO.getFromItemId());
  183. //根据id查询
  184. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  185. //已出库
  186. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())){
  187. if(orderItemResponse.getOutingQty().compareTo(BigDecimal.ZERO)==0){
  188. orderItem.setOutingQty(BigDecimal.ZERO);
  189. orderItem.setOutingAmt(BigDecimal.ZERO);
  190. }
  191. //多次已出库
  192. else{
  193. orderItem.setOutingQty(outboundVO.getOutQty().negate());
  194. orderItem.setOutingAmt(outboundVO.getOutAmt().negate());
  195. }
  196. orderItem.setOutQty(outboundVO.getOutQty());
  197. orderItem.setOutAmt(outboundVO.getOutAmt());
  198. }
  199. //出库中
  200. else{
  201. orderItem.setOutingQty(outboundVO.getOutingQty());
  202. orderItem.setOutingAmt(outboundVO.getOutingAmt());
  203. orderItem.setOutQty(BigDecimal.ZERO);
  204. orderItem.setOutAmt(BigDecimal.ZERO);
  205. }
  206. //入库状态
  207. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  208. orderItem.setOutStatus(outStatus);
  209. //修改
  210. int countRow = orderItemMapper.updateById(orderItem);
  211. //数量超出
  212. if (countRow == 0) {
  213. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  214. }
  215. }
  216. //endregion
  217. }
  218. //endregion
  219. //todo 如果是已出库 调用库存 后续写库存这里补上
  220. //region 库存
  221. //endregion
  222. return ResponseResultUtil.success(outboundVO);
  223. }
  224. /**
  225. * @desc : 销售出库办理
  226. * @date : 2024/3/7 15:47
  227. * @author : 寇珊珊
  228. */
  229. @Transactional(rollbackFor = {Exception.class})
  230. public ResponseResultVO<?> saleOrderHandleOutbound(OutboundVO outboundVO) {
  231. //region 编辑明细
  232. //校验明细
  233. if (outboundVO.getItemList().size() == 0) {
  234. return ResponseResultUtil.error(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  235. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  236. }
  237. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  238. if (outboundItemVO.getItemId() != null) {
  239. //查询原单
  240. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  241. //编辑明细
  242. outboundItemVO
  243. .setOutQty(outboundItemResponse.getOutQty().add(outboundItemVO.getOutingQty()))
  244. .setOutAmt(outboundItemResponse.getOutAmt().add(outboundItemVO.getOutingAmt()))
  245. .setOutingQty(outboundItemResponse.getOutingQty().subtract(outboundItemVO.getOutingQty()))
  246. .setOutAmt(outboundItemResponse.getOutingAmt().subtract(outboundItemVO.getOutingAmt()))
  247. .setCostPrice(outboundItemVO.getPriceOut())
  248. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  249. ;
  250. //出库状态
  251. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  252. outboundItemVO.setOutStatus(outStatus);
  253. //实体转换
  254. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  255. //修改
  256. outboundItemMapper.update(outboundItem,
  257. new UpdateWrapper<OutboundItem>().lambda()
  258. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  259. );
  260. }
  261. //endregion
  262. //region 新建明细
  263. else {
  264. outboundItemVO
  265. .setOutId(outboundItemVO.getOutId())
  266. .setOutQty(outboundItemVO.getOutQty())
  267. .setOutAmt(outboundItemVO.getOutAmt())
  268. .setCostPrice(outboundItemVO.getPriceOut())
  269. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  270. .setOutType(Constant.OutType.SALE.getName());
  271. //入库状态
  272. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  273. outboundItemVO.setOutStatus(outStatus);
  274. //实体转换
  275. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  276. //新建
  277. outboundItemMapper.insert(outboundItem);
  278. }
  279. //endregion
  280. //region 销售订单明细
  281. if (outboundItemVO.getFromItemId() != null) {
  282. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  283. OrderItem orderItem = new OrderItem();
  284. orderItem.setItemId(outboundItemVO.getFromItemId());
  285. orderItem.setOutingQty(outboundItemVO.getOutingQty());
  286. orderItem.setOutingAmt(outboundItemVO.getOutingAmt());
  287. orderItem.setOutQty(outboundItemVO.getOutQty());
  288. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  289. //根据id查询
  290. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  291. //出库状态
  292. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  293. orderItem.setOutStatus(outStatus);
  294. //修改
  295. int countRow = orderItemMapper.updateById(orderItem);
  296. //数量超出
  297. if (countRow == 0) {
  298. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  299. }
  300. }
  301. //endregion
  302. }
  303. //endregion
  304. //region 删除明细 todo 2024年3月20日13:17:33 这里可能用不到
  305. // BigDecimal sumDelOutQty = BigDecimal.ZERO;
  306. // BigDecimal sumDelOutAmt = BigDecimal.ZERO;
  307. // BigDecimal sumDelOutingQty = BigDecimal.ZERO;
  308. // BigDecimal sumDelOutingAmt = BigDecimal.ZERO;
  309. // if (outboundVO.getDeleteItemList() != null) {
  310. // sumDelOutQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  311. // sumDelOutAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  312. // sumDelOutingQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  313. // sumDelOutingAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  314. // for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  315. // if (outboundItemVO.getItemId() != null) {
  316. // OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  317. // outboundItem.setFlgValid(false);
  318. // //修改
  319. // outboundItemMapper.update(outboundItem,
  320. // new UpdateWrapper<OutboundItem>().lambda()
  321. // .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  322. // );
  323. // }
  324. // //region 销售退货明细
  325. // if (outboundItemVO.getFromItemId() != null) {
  326. // //region 销售退货订单明细
  327. // //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  328. // OrderItem orderItem = new OrderItem();
  329. // orderItem.setItemId(outboundItemVO.getFromItemId());
  330. // orderItem.setOutingQty(outboundItemVO.getOutingQty().negate());
  331. // orderItem.setOutingAmt(outboundItemVO.getOutingAmt().negate());
  332. // orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  333. // orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  334. // //根据id查询
  335. // OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  336. // //出库状态
  337. // String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  338. // orderItem.setOutStatus(outStatus);
  339. // //修改
  340. // int countRow = orderItemMapper.updateById(orderItem);
  341. // //数量超出
  342. // if (countRow == 0) {
  343. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  344. // }
  345. // //endregion
  346. // }
  347. // //endregion
  348. // }
  349. // }
  350. //endregion
  351. //region 编辑总单
  352. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  353. BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  354. BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  355. // BigDecimal sumOutingQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  356. // BigDecimal sumOutingAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  357. outboundVO.setOutQty(sumOutQty);
  358. outboundVO.setOutAmt(sumOutAmt);
  359. outboundVO.setOutingQty(outboundResponse.getOutingQty().subtract(sumOutQty));
  360. outboundVO.setOutingAmt(outboundResponse.getOutingAmt().subtract(sumOutAmt));
  361. //出库状态
  362. String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
  363. outboundVO.setOutStatus(outStatus);
  364. //实体转换
  365. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  366. //修改
  367. outboundMapper.update(outbound,
  368. new UpdateWrapper<Outbound>().lambda()
  369. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  370. );
  371. //endregion
  372. //region 修改销售退货订单
  373. if (outboundVO.getFromId() != null) {
  374. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  375. Order order = new Order();
  376. order.setOrderId(outboundVO.getFromId());
  377. //region todo 2024年3月20日13:18:14 这里可能不用
  378. // order.setOutingQty(sumOutQty.subtract(sumDelOutQty));
  379. // order.setOutingAmt(sumOutAmt.subtract(sumDelOutAmt));
  380. // order.setOutQty((sumOutingQty.add(sumDelOutingQty)).negate());
  381. // order.setOutAmt((sumOutingAmt.add(sumDelOutingAmt)).negate());
  382. //endregion
  383. order.setOutingQty(sumOutQty);
  384. order.setOutingAmt(sumOutAmt);
  385. order.setOutQty(sumOutQty.negate());
  386. order.setOutAmt(sumOutAmt.negate());
  387. //根据id查询
  388. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  389. //出库状态
  390. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  391. order.setOutStatus(orderOutStatus);
  392. //修改
  393. int countRow = orderMapper.updateById(order);
  394. //数量超出
  395. if (countRow == 0) {
  396. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  397. }
  398. }
  399. //endregion
  400. //todo 如果是已出库 调用库存 后续写库存这里补上
  401. //region 修改库存
  402. //endregion
  403. return ResponseResultUtil.success(outboundVO);
  404. }
  405. /**
  406. * @desc : 销售出库撤销
  407. * @date : 2024/3/7 17:06
  408. * @author : 寇珊珊
  409. */
  410. @Transactional(rollbackFor = {Exception.class})
  411. public ResponseResultVO<?> saleOrderOutboundCancel(OutboundVO outboundVO) {
  412. //region 查询出库总单数据信息
  413. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  414. //endregion
  415. //region 修改订单数据信息
  416. if (outboundResponse.getFromId() != null) {
  417. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  418. Order order = new Order();
  419. order.setOrderId(outboundVO.getFromId());
  420. order.setOutingQty(outboundVO.getOutingQty());
  421. order.setOutingAmt(outboundVO.getOutingAmt());
  422. order.setOutQty(outboundVO.getOutQty().negate());
  423. order.setOutAmt(outboundVO.getOutAmt().negate());
  424. //根据id查询
  425. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  426. //出库状态
  427. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()), orderResponse.getOutQty().add(order.getOutQty()));
  428. order.setOutStatus(outStatus);
  429. //修改
  430. int countRow = orderMapper.updateById(order);
  431. //数量超出
  432. if (countRow == 0) {
  433. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  434. }
  435. }
  436. //endregion
  437. //region 修改总单数据信息
  438. Outbound outbound = new Outbound();
  439. outbound.setOutId(outboundVO.getOutId());
  440. outbound.setOutDate(null);
  441. outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  442. outbound.setOutQty(BigDecimal.ZERO);
  443. outbound.setOutAmt(BigDecimal.ZERO);
  444. //修改
  445. outboundMapper.update(outbound,
  446. new UpdateWrapper<Outbound>().lambda()
  447. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  448. );
  449. //endregion
  450. //region 明细数据
  451. //根据总单id查明细
  452. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundVO.getOutId()));
  453. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  454. //region 修改销售退货明细数据信息
  455. if (outboundItemResponse.getFromItemId() != null) {
  456. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  457. OrderItem orderItem = new OrderItem();
  458. orderItem.setItemId(outboundItemResponse.getFromItemId());
  459. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  460. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  461. orderItem.setOutingQty(outboundItemResponse.getOutingQty());
  462. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt());
  463. //根据id查询
  464. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  465. //出库状态
  466. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()), orderItemResponse.getOutQty().add(orderItem.getOutQty()));
  467. orderItem.setOutStatus(outStatus);
  468. //修改
  469. int countRow = orderItemMapper.updateById(orderItem);
  470. //数量超出
  471. if (countRow == 0) {
  472. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  473. }
  474. }
  475. //endregion
  476. //region修改出库明细信息
  477. OutboundItem outboundItem = new OutboundItem();
  478. outboundItem
  479. .setOutId(outboundResponse.getOutId())
  480. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  481. .setOutingQty(outboundItemResponse.getOutingQty().add(outboundItemResponse.getOutQty()))
  482. .setOutAmt(outboundItemResponse.getOutingAmt().add(outboundItemResponse.getOutAmt()))
  483. .setOutQty(BigDecimal.ZERO)
  484. .setOutAmt(BigDecimal.ZERO)
  485. .setCostPrice(BigDecimal.ZERO)
  486. .setCostAmt(BigDecimal.ZERO)
  487. .setItemId(outboundItemResponse.getItemId());
  488. //修改
  489. outboundItemMapper.update(outboundItem,
  490. new UpdateWrapper<OutboundItem>().lambda()
  491. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  492. );
  493. //endregion
  494. }
  495. //endregion
  496. //todo 调用库存 后续写库存这里补上
  497. //region 修改库存
  498. //endregion
  499. return ResponseResultUtil.success();
  500. }
  501. /**
  502. * @desc : 入库状态通用(目前本页面)
  503. * @date : 2024/3/9 8:59
  504. * @author : 寇珊珊
  505. */
  506. public String setIntoStatus(BigDecimal intoingQty, BigDecimal intoQty) {
  507. //入库状态
  508. String intoStatus = null;
  509. //已出库数量>0 出库中数量>0
  510. if (intoQty.compareTo(BigDecimal.ZERO) >= 0 && intoingQty.compareTo(BigDecimal.ZERO) > 0) {
  511. //出库中
  512. intoStatus = Constant.IntoStatus.RUKUZHONG.getName();
  513. }
  514. //已出库数量=0 出库中数量=0
  515. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  516. //待入库
  517. intoStatus = Constant.IntoStatus.DAIRUKU.getName();
  518. }
  519. //已出库数量>0 出库中数量=0
  520. else if (intoQty.compareTo(BigDecimal.ZERO) > 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  521. //已出库
  522. intoStatus = Constant.IntoStatus.YIRUKU.getName();
  523. }
  524. return intoStatus;
  525. }
  526. /**
  527. * @desc : 出库状态通用(目前本页面)
  528. * @date : 2024/3/9 8:59
  529. * @author : 寇珊珊
  530. */
  531. public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
  532. //出库状态
  533. String outStatus = null;
  534. //已出库数量>0 出库中数量>0
  535. if (outQty.compareTo(BigDecimal.ZERO) >= 0 && outingQty.compareTo(BigDecimal.ZERO) > 0) {
  536. //出库中
  537. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  538. }
  539. //出入库数量=0 出库中数量=0
  540. else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  541. //待出库
  542. outStatus = Constant.OutStatus.DAICHUKU.getName();
  543. }
  544. //已出库数量>0 出库中数量=0
  545. else if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  546. //已出库
  547. outStatus = Constant.OutStatus.YICHUKU.getName();
  548. }
  549. return outStatus;
  550. }
  551. /**
  552. * @desc : 获取单据信息(编辑用)
  553. * @date : 2024/3/16 16:28
  554. * @author : 寇珊珊
  555. */
  556. public ResponseResultVO<?> selectByUpdate(String id) {
  557. Map<String, Object> dataInfo = new HashMap<>();
  558. //总单
  559. OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
  560. //单据不存在
  561. if (outboundResponse == null) {
  562. return ResponseResultUtil.error(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
  563. ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
  564. }
  565. dataInfo.put("data", outboundResponse);
  566. // 明细
  567. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  568. dataInfo.put("dataItem", outboundItemResponseList);
  569. return ResponseResultUtil.success(dataInfo);
  570. }
  571. /********************************************** 销售出库查询相关方法begin *************************************/
  572. /**
  573. * @desc : 一览页销售出库
  574. * @author : 付斌
  575. * @date : 2023/1/9 10:40
  576. */
  577. @Pagination
  578. public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  579. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  580. outboundMapper.countByCond(outboundQuery));
  581. }
  582. /**
  583. * @desc : 一览页销售出库明细(货物、附件)
  584. * @author : 付斌
  585. * @date : 2024-02-28 13:25
  586. */
  587. @Pagination
  588. public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
  589. Map<String, Object> result = new HashMap<>();
  590. // 商品明细
  591. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  592. result.put("outboundItem", outboundItem);
  593. // 附件
  594. return ResponseResultUtil.success(result);
  595. }
  596. /**
  597. * @desc : 获取销售出库信息(编辑用)
  598. * @author : 付斌
  599. * @date : 2024-03-02 17:27
  600. */
  601. public ResponseResultVO<?> getOutboundForUpdate(String id) {
  602. Map<String, Object> dataInfo = new HashMap<>();
  603. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  604. dataInfo.put("data", outboundResponse);
  605. // 商品明细
  606. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEdit(id);
  607. dataInfo.put("dataItem", outboundItemResponseList);
  608. return ResponseResultUtil.success(dataInfo);
  609. }
  610. /**
  611. * @desc : 查询出库明细(货物、附件)
  612. * @author : 付斌
  613. * @date : 2024-02-28 13:25
  614. */
  615. @Pagination
  616. public ResponseResultVO selectById(String id) {
  617. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  618. // 商品明细
  619. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  620. outboundResponse.setGoodsList(outboundItem);
  621. // 附件
  622. return ResponseResultUtil.success(outboundResponse);
  623. }
  624. /********************************************** 销售出库查询相关方法end *************************************/
  625. // /**
  626. // * @desc : 条件查询
  627. // * @date : 2024/3/18 11:20
  628. // * @author : 寇珊珊
  629. // */
  630. // @Pagination
  631. // public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  632. // return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  633. // outboundMapper.countByCond(outboundQuery));
  634. // }
  635. //
  636. // /**
  637. // * @desc : 查询明细
  638. // * @date : 2024/3/15 16:43
  639. // * @author : 寇珊珊
  640. // */
  641. // @Pagination
  642. // public ResponseResultVO<Map<String, Object>> selectOutBoundSaleOrderItemInfoById(String id) {
  643. // Map<String, Object> result = new HashMap<>();
  644. // // 商品明细
  645. // List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  646. // result.put("itemList", outboundItemResponseList);
  647. // // 收款
  648. //
  649. // // 附件
  650. // return ResponseResultUtil.success(result);
  651. // }
  652. }