OutboundSaleOrderService.java 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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.InboundConvert;
  14. import com.dk.mdm.infrastructure.convert.ivt.InboundItemConvert;
  15. import com.dk.mdm.infrastructure.convert.ivt.OutboundConvert;
  16. import com.dk.mdm.infrastructure.convert.ivt.OutboundItemConvert;
  17. import com.dk.mdm.mapper.common.CommonMapper;
  18. import com.dk.mdm.mapper.ivt.InboundItemMapper;
  19. import com.dk.mdm.mapper.ivt.InboundMapper;
  20. import com.dk.mdm.mapper.ivt.OutboundItemMapper;
  21. import com.dk.mdm.mapper.ivt.OutboundMapper;
  22. import com.dk.mdm.mapper.mst.CustomerMapper;
  23. import com.dk.mdm.mapper.sale.MultiOwnerMapper;
  24. import com.dk.mdm.mapper.sale.OrderItemMapper;
  25. import com.dk.mdm.mapper.sale.OrderMapper;
  26. import com.dk.mdm.model.pojo.ivt.Inbound;
  27. import com.dk.mdm.model.pojo.ivt.InboundItem;
  28. import com.dk.mdm.model.pojo.ivt.Outbound;
  29. import com.dk.mdm.model.pojo.ivt.OutboundItem;
  30. import com.dk.mdm.model.pojo.mst.Customer;
  31. import com.dk.mdm.model.pojo.pur.Purchase;
  32. import com.dk.mdm.model.pojo.pur.PurchaseItem;
  33. import com.dk.mdm.model.pojo.sale.MultiOwner;
  34. import com.dk.mdm.model.pojo.sale.Order;
  35. import com.dk.mdm.model.pojo.sale.OrderItem;
  36. import com.dk.mdm.model.query.ivt.OutboundItemQuery;
  37. import com.dk.mdm.model.query.ivt.OutboundQuery;
  38. import com.dk.mdm.model.query.sale.OrderQuery;
  39. import com.dk.mdm.model.response.ivt.InboundResponse;
  40. import com.dk.mdm.model.response.ivt.OutboundItemResponse;
  41. import com.dk.mdm.model.response.ivt.OutboundResponse;
  42. import com.dk.mdm.model.response.pur.PurchaseItemResponse;
  43. import com.dk.mdm.model.response.pur.PurchaseResponse;
  44. import com.dk.mdm.model.response.sale.OrderItemResponse;
  45. import com.dk.mdm.model.response.sale.OrderResponse;
  46. import com.dk.mdm.model.vo.ivt.InboundItemVO;
  47. import com.dk.mdm.model.vo.ivt.InboundVO;
  48. import com.dk.mdm.model.vo.ivt.OutboundItemVO;
  49. import com.dk.mdm.model.vo.ivt.OutboundVO;
  50. import com.dk.mdm.model.vo.mac.RecPayVO;
  51. import com.dk.mdm.service.common.CommonService;
  52. import com.dk.mdm.service.ivt.inventory.InventoryService;
  53. import com.dk.mdm.service.mac.AccountService;
  54. import com.dk.mdm.service.mac.ReceiptService;
  55. import org.springframework.beans.factory.annotation.Autowired;
  56. import org.springframework.stereotype.Service;
  57. import org.springframework.transaction.annotation.Transactional;
  58. import java.math.BigDecimal;
  59. import java.time.LocalDate;
  60. import java.util.HashMap;
  61. import java.util.List;
  62. import java.util.Map;
  63. import java.util.UUID;
  64. import java.util.stream.Collectors;
  65. /**
  66. * @author : 寇珊珊
  67. * @desc : 销售出库业务层
  68. * @date : 2024/3/18 15:33
  69. */
  70. @Service
  71. public class OutboundSaleOrderService extends BaseService<Outbound> {
  72. @Override
  73. public BaseMapper<Outbound> getRepository() {
  74. return outboundMapper;
  75. }
  76. @Autowired
  77. private CommonService commonService;
  78. @Autowired
  79. private OutboundMapper outboundMapper;
  80. @Autowired
  81. private OutboundConvert outboundConvert;
  82. @Autowired
  83. private OutboundItemMapper outboundItemMapper;
  84. @Autowired
  85. private OutboundItemConvert outboundItemConvert;
  86. @Autowired
  87. private OrderMapper orderMapper;
  88. @Autowired
  89. private OrderItemMapper orderItemMapper;
  90. @Autowired
  91. private CommonMapper commonMapper;
  92. @Autowired
  93. private InventoryService inventoryService;
  94. @Autowired
  95. private AccountService accountService;
  96. @Autowired
  97. private OutCommon outCommon;
  98. @Autowired
  99. private InboundMapper inboundMapper;
  100. @Autowired
  101. private InboundConvert inboundConvert;
  102. @Autowired
  103. private InboundItemMapper inboundItemMapper;
  104. @Autowired
  105. private InboundItemConvert inboundItemConvert;
  106. @Autowired
  107. private ReceiptService receiptService;
  108. /**
  109. * @desc : 删除外协品生产外协入库单
  110. * @date : 2024/5/10 10:31
  111. * @author : 寇珊珊
  112. */
  113. @Transactional(rollbackFor = {Exception.class})
  114. public void deleteOutsideGoodsInto(OutboundVO outboundVO, List<OutboundItemVO> outboundItemVOList) {
  115. if (outboundItemVOList != null && outboundItemVOList.size() > 0) {
  116. //根据出库单id分组
  117. Map<String, List<OutboundItemVO>> outboundItemVOMap = outboundItemVOList.stream().collect(Collectors.groupingBy(OutboundItemVO::getOutId));
  118. for (String str : outboundItemVOMap.keySet()) {
  119. InboundResponse inboundResponse = inboundMapper.selectByFromId(str);
  120. // 退账
  121. if (inboundResponse != null && inboundResponse.getReceivableId() != null) {
  122. accountService.reversePayable(inboundResponse.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
  123. }
  124. }
  125. //删除外协入库单
  126. //外协入库总单
  127. inboundMapper.deleteByFromId(outboundItemVOList.get(0).getOutId());
  128. //外协入库明细
  129. inboundItemMapper.deleteItemByFromId(outboundItemVOList.get(0).getOutId());
  130. }
  131. }
  132. /**
  133. * @desc : 新建外协品生产外协入库单
  134. * @date : 2024/5/10 10:31
  135. * @author : 寇珊珊
  136. */
  137. @Transactional(rollbackFor = {Exception.class})
  138. public void insertOutsideGoodsInto(OutboundVO outboundVO, List<OutboundItemVO> outboundItemVOList) {
  139. if (outboundItemVOList != null && outboundItemVOList.size() > 0) {
  140. Map<String, List<OutboundItemVO>> outboundItemVOMap = outboundItemVOList.stream().collect(Collectors.groupingBy(OutboundItemVO::getOutId));
  141. for (String str : outboundItemVOMap.keySet()) {
  142. //提取分组后的明细
  143. List<OutboundItemVO> outboundItemVOListGroup = outboundItemVOMap.get(str);
  144. //region 新建总单
  145. //已入库金额
  146. BigDecimal sumIntoAmt = outboundItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  147. //已入库数量
  148. BigDecimal sumIntoQty = outboundItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  149. //总单实体
  150. InboundVO inboundVO = new InboundVO();
  151. //获取 id/单号
  152. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OUTSOURCED.getName(), false);
  153. inboundVO.setIntoId(codeMap.get("outId").toString()).
  154. setIntoNo(codeMap.get("outNote").toString());
  155. //入库类型
  156. inboundVO.setIntoType(Constant.IntoType.OUTSOURCED.getName());
  157. //已入库
  158. inboundVO.setIntoStatus(Constant.IntoStatus.YIRUKU.getName());
  159. //供应商
  160. inboundVO.setSupId(outboundItemVOListGroup.get(0).getSupId());
  161. //入库状态等于已入库
  162. inboundVO.setIntoQty(sumIntoAmt)
  163. .setIntoAmt(sumIntoQty)
  164. .setIntoingQty(BigDecimal.ZERO)
  165. .setIntoingAmt(BigDecimal.ZERO);
  166. //来源id
  167. inboundVO.setFromId(outboundItemVOListGroup.get(0).getFromId());
  168. //来源单号
  169. inboundVO.setFromNo(outboundVO.getOutNo());
  170. //部门
  171. inboundVO.setOrgId(outboundVO.getOrgId());
  172. //员工
  173. inboundVO.setStaffId(outboundVO.getStaffId());
  174. //入库日期
  175. inboundVO.setIntoDate(LocalDate.now());
  176. //制单人
  177. inboundVO.setMakeStaff(outboundVO.getMakeStaff());
  178. //公司
  179. inboundVO.setCpId(outboundVO.getCpId());
  180. //实体转换
  181. Inbound inbound = inboundConvert.convertToPo(inboundVO);
  182. inboundMapper.insert(inbound);
  183. //endregion
  184. //region 明细
  185. Integer count = 0;
  186. for (OutboundItemVO outboundItemVO : outboundItemVOListGroup) {
  187. InboundItemVO inboundItemVO = new InboundItemVO();
  188. //总单id
  189. inboundItemVO.setIntoId(inboundVO.getIntoId());
  190. //入库类型
  191. inboundItemVO.setIntoType(inboundVO.getIntoType());
  192. //入库状态等于已入库 更新合计入库数量/金额 = 入库中数量/入库中金额
  193. inboundItemVO
  194. .setPriceInto(outboundItemVO.getPriceOut())
  195. .setIntoQty(outboundItemVO.getOutQty())
  196. .setIntoAmt(outboundItemVO.getOutQty())
  197. .setIntoingQty(BigDecimal.ZERO)
  198. .setIntoingAmt(BigDecimal.ZERO)
  199. .setCostPrice(outboundItemVO.getPriceOut())
  200. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  201. ;
  202. //入库状态
  203. inboundItemVO.setIntoStatus(inboundVO.getIntoStatus());
  204. //商品顺序
  205. inboundItemVO.setItemIndex(count);
  206. //箱(入库中)
  207. inboundItemVO.setIntoingBox(outboundItemVO.getOutingBox());
  208. //片(入库中)
  209. inboundItemVO.setIntoingPiece(outboundItemVO.getOutingPiece());
  210. //箱(已入库)
  211. inboundItemVO.setIntoBox(outboundItemVO.getOutBox());
  212. //片(已入库)
  213. inboundItemVO.setIntoPiece(outboundItemVO.getOutPiece());
  214. //入库仓库
  215. inboundItemVO.setWhId(outboundItemVO.getWhId());
  216. //企业ID
  217. inboundItemVO.setCpId(outboundItemVO.getCpId());
  218. //实体转换
  219. InboundItem inboundItem = inboundItemConvert.convertToPo(inboundItemVO);
  220. inboundItemMapper.insert(inboundItem);
  221. //用来填写商品顺序
  222. count++;
  223. inboundItemVO.setItemId(inboundItem.getItemId());
  224. }
  225. //endregion
  226. //region 入账
  227. accountService.accPayable(inboundVO.getIntoId(), Constant.InventoryDocCode.INTOBOUND.getTableName());
  228. //endregion
  229. }
  230. }
  231. }
  232. /**
  233. * @desc : 销售出库新建
  234. * @date : 2024/3/7 14:13
  235. * @author : 寇珊珊
  236. */
  237. @Transactional(rollbackFor = {Exception.class})
  238. public ResponseResultVO<?> saleOrderOutboundInsert(OutboundVO outboundVO) {
  239. //region 如果没有客户id,要新建
  240. outboundVO = outCommon.insertCustomer(outboundVO);
  241. //endregion
  242. //region 查询当前公司的系统参数 自动办理信息 并赋值
  243. Map<String, Object> map = new HashMap<>();
  244. map.put("cpId", outboundVO.getCpId());
  245. map.put("code", Constant.SystemConstant.IVT_001.getValue());
  246. //自动办理标识
  247. String flgHandleSetting = commonMapper.getSettingValue(map);
  248. //自动办理标识为1 自动办理入库
  249. if (Constant.FlgAutoHandleStringType.ONE.getValue().equals(flgHandleSetting)) {
  250. outboundVO.setFlgHandleSetting(Constant.FlgHandleSetting.TRUE.getValue());
  251. outboundVO.setFlgAutoHandle(Constant.FlgAutoHandle.TRUE.getValue());
  252. }
  253. //endregion
  254. //region 总单
  255. //获取 id/单号
  256. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.SALEORDER.getName(), false);
  257. outboundVO.setOutId(codeMap.get("outId").toString()).
  258. setOutNo(codeMap.get("outNote").toString());
  259. //出库类型
  260. outboundVO.setOutType(Constant.OutType.SALE.getName()).setOutDate(LocalDate.now());
  261. //自动入库标识
  262. if (outboundVO.getFlgAutoHandle()) {
  263. //已出库
  264. outboundVO.setOutStatus(Constant.OutStatus.YICHUKU.getName());
  265. } else {
  266. //出库中
  267. outboundVO.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  268. }
  269. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  270. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  271. outboundVO
  272. .setOutQty(outboundVO.getOutingQty())
  273. .setOutAmt(outboundVO.getOutingAmt())
  274. .setOutingQty(BigDecimal.ZERO)
  275. .setOutingAmt(BigDecimal.ZERO)
  276. ;
  277. } else {
  278. outboundVO
  279. .setOutQty(BigDecimal.ZERO)
  280. .setOutAmt(BigDecimal.ZERO)
  281. ;
  282. }
  283. //实体转换
  284. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  285. outboundMapper.insert(outbound);
  286. //endregion
  287. //region 新建多业务归属
  288. outCommon.insertMultiOwner(outboundVO);
  289. //endregion
  290. //region 销售订单
  291. if (outboundVO.getFromId() != null) {
  292. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  293. Order order = new Order();
  294. order.setOrderId(outboundVO.getFromId());
  295. //根据id查询
  296. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  297. //已出库
  298. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  299. order.setOutingQty(BigDecimal.ZERO);
  300. order.setOutingAmt(BigDecimal.ZERO);
  301. order.setOutQty(outboundVO.getOutQty());
  302. order.setOutAmt(outboundVO.getOutAmt());
  303. }
  304. //出库中
  305. else {
  306. order.setOutingQty(outboundVO.getOutingQty());
  307. order.setOutingAmt(outboundVO.getOutingAmt());
  308. order.setOutQty(BigDecimal.ZERO);
  309. order.setOutAmt(BigDecimal.ZERO);
  310. }
  311. //入库状态
  312. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  313. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  314. order.setOutStatus(outStatus);
  315. //修改
  316. int countRow = orderMapper.updateById(order);
  317. //数量超出
  318. if (countRow == 0) {
  319. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  320. }
  321. }
  322. //endregion
  323. //region 明细
  324. //校验明细
  325. if (outboundVO.getItemList().size() == 0) {
  326. throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  327. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  328. }
  329. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  330. //region 将库存需要的参数赋值
  331. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  332. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  333. outboundItemVO.setAddOrEditFlag(true);
  334. //endregion
  335. //总单id
  336. outboundItemVO.setOutId(outboundVO.getOutId());
  337. //出库类型
  338. outboundItemVO.setOutType(outboundVO.getOutType());
  339. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  340. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  341. outboundItemVO
  342. .setOutQty(outboundItemVO.getOutingQty())
  343. .setOutAmt(outboundItemVO.getOutingAmt())
  344. .setOutingQty(BigDecimal.ZERO)
  345. .setOutingAmt(BigDecimal.ZERO)
  346. .setCostPrice(outboundItemVO.getPriceOut())
  347. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  348. ;
  349. } else {
  350. outboundItemVO
  351. .setOutQty(BigDecimal.ZERO)
  352. .setOutAmt(BigDecimal.ZERO);
  353. }
  354. //入库状态
  355. outboundItemVO.setOutStatus(outboundVO.getOutStatus());
  356. //实体转换
  357. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  358. outboundItemMapper.insert(outboundItem);
  359. //明细id
  360. outboundItemVO.setItemId(outboundItem.getItemId());
  361. //endregion
  362. //region 销售明细
  363. if (outboundItemVO.getFromItemId() != null) {
  364. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  365. OrderItem orderItem = new OrderItem();
  366. orderItem.setItemId(outboundItemVO.getFromItemId());
  367. //根据id查询
  368. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  369. //已出库
  370. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  371. orderItem.setOutingQty(BigDecimal.ZERO);
  372. orderItem.setOutingAmt(BigDecimal.ZERO);
  373. orderItem.setOutQty(outboundItemVO.getOutQty());
  374. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  375. }
  376. //出库中
  377. else {
  378. orderItem.setOutingQty(outboundItemVO.getOutingQty());
  379. orderItem.setOutingAmt(outboundItemVO.getOutingAmt());
  380. orderItem.setOutQty(BigDecimal.ZERO);
  381. orderItem.setOutAmt(BigDecimal.ZERO);
  382. }
  383. //入库状态
  384. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  385. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  386. orderItem.setOutStatus(outStatus);
  387. //修改
  388. int countRow = orderItemMapper.updateById(orderItem);
  389. //数量超出
  390. if (countRow == 0) {
  391. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  392. }
  393. }
  394. //endregion
  395. }
  396. //endregion
  397. //region 已出库 应收记账
  398. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  399. accountService.accReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  400. }
  401. //endregion
  402. //region 外协品+库存
  403. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  404. //region 外协品新建外协入库单
  405. //筛选出skuId为空的 走外协品逻辑
  406. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  407. //删除外协品生产外协入库单
  408. this.deleteOutsideGoodsInto(outboundVO, outsideGoods);
  409. //新建外协品生产外协入库单
  410. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  411. //endregion
  412. //region 库存
  413. //筛选出skuId不为空的 走库存
  414. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  415. if(invList!=null && invList.size() > 0){
  416. Map<String, Object> invMap = new HashMap<>();
  417. invMap.put("outDetail", invList);
  418. inventoryService.operatingInventoryInformation(invMap);
  419. }
  420. //endregion
  421. }
  422. //endregion
  423. return ResponseResultUtil.success(outboundVO);
  424. }
  425. /**
  426. * @desc : 销售出库新建>>>收款
  427. * @date : 2024/3/7 14:13
  428. * @author : 寇珊珊
  429. */
  430. @Transactional(rollbackFor = {Exception.class})
  431. public ResponseResultVO<?> saleOrderOutboundInsertCollection(OutboundVO outboundVO) {
  432. //新建销售出库
  433. saleOrderOutboundInsert(outboundVO);
  434. //新建退款
  435. RecPayVO recPayVO = new RecPayVO();
  436. recPayVO.setObjectId(outboundVO.getObjectId());
  437. recPayVO.setOrgId(outboundVO.getOrgId());
  438. recPayVO.setStaffId(outboundVO.getStaffId());
  439. recPayVO.setAccDate(outboundVO.getAccDate());
  440. recPayVO.setMakeStaff(outboundVO.getMakeStaff());
  441. recPayVO.setRpType(outboundVO.getRpType());
  442. recPayVO.setSumAmtRec(outboundVO.getSumAmtRec());
  443. recPayVO.setItemList(outboundVO.getItemListRecPayItemVO());
  444. receiptService.insertReceipt(recPayVO);
  445. return ResponseResultUtil.success();
  446. }
  447. /**
  448. * @desc : 销售出库编辑
  449. * @date : 2024/3/25 16:25
  450. * @author : 寇珊珊
  451. */
  452. @Transactional(rollbackFor = {Exception.class})
  453. public ResponseResultVO<?> saleOutboundUpdate(OutboundVO outboundVO) {
  454. //region 小编辑
  455. if (!outboundVO.getLimitEdit()) {
  456. Outbound outbound = new Outbound();
  457. outbound.setOutId(outboundVO.getOutId());
  458. outbound.setRemarks(outboundVO.getRemarks());
  459. outbound.setAnnexPaths(outboundVO.getAnnexPaths());
  460. outboundMapper.update(outbound,
  461. new UpdateWrapper<Outbound>().lambda()
  462. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  463. );
  464. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  465. OutboundItem outboundItem = new OutboundItem();
  466. outboundItem.setItemId(outboundItemVO.getItemId());
  467. if (outboundItemVO.getRemarks() != null || outboundItemVO.getRemarks() != " ") {
  468. outboundItem.setRemarks(outboundItemVO.getRemarks());
  469. outboundItemMapper.update(outboundItem,
  470. new UpdateWrapper<OutboundItem>().lambda()
  471. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  472. );
  473. }
  474. }
  475. }
  476. //endregion
  477. //region 大编辑
  478. else {
  479. // region 更新客户信息
  480. outboundVO = outCommon.insertCustomer(outboundVO);
  481. // endregion
  482. //region 根据id查询 并且明细数量金额 求和
  483. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  484. BigDecimal sumQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  485. BigDecimal sumAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  486. //endregion
  487. //region 自动办理参数为true 已入库编辑
  488. //自动办理参数为true
  489. if (Constant.FlgHandleSetting.TRUE.getValue().equals(outboundVO.getFlgHandleSetting())) {
  490. //region 应收反记账
  491. if (outboundVO.getReceivableId() != null) {
  492. accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  493. }
  494. //endregion
  495. //region 修改明细
  496. List<OutboundItemVO> itemList = outboundVO.getItemList();
  497. for (OutboundItemVO outboundItemVO : itemList) {
  498. //根据id查询
  499. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  500. //region 编辑明细
  501. if (outboundItemVO.getItemId() != null) {
  502. //region 将库存需要的参数赋值
  503. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  504. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  505. //编辑之前的数
  506. outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
  507. outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
  508. //编辑之后的数
  509. outboundItemVO.setOutQty(outboundItemVO.getOutingQty());
  510. outboundItemVO.setOutAmt(outboundItemVO.getOutingAmt());
  511. outboundItemVO.setAddOrEditFlag(false);
  512. //endregion
  513. OutboundItem outboundItem = new OutboundItem();
  514. outboundItem.setItemId(outboundItemVO.getItemId());
  515. outboundItem.setOutQty(outboundItemVO.getOutingQty());
  516. outboundItem.setOutAmt(outboundItemVO.getOutingAmt());
  517. outboundItem.setCostPrice(outboundItemVO.getPriceOut());
  518. outboundItem.setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(6, BigDecimal.ROUND_HALF_UP));
  519. //修改
  520. outboundItemMapper.update(outboundItem,
  521. new UpdateWrapper<OutboundItem>().lambda()
  522. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  523. );
  524. }
  525. //endregion
  526. //region 新建明细
  527. else {
  528. outboundItemVO
  529. .setOutId(outboundItemVO.getOutId())
  530. .setOutQty(outboundItemVO.getOutingQty())
  531. .setOutAmt(outboundItemVO.getOutingAmt())
  532. .setOutingQty(BigDecimal.ZERO)
  533. .setOutingAmt(BigDecimal.ZERO)
  534. .setCostPrice(outboundItemVO.getPriceOut())
  535. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  536. .setOutType(Constant.OutType.SALE.getName())
  537. ;
  538. //出库状态
  539. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  540. outboundItemVO.setOutStatus(outStatus);
  541. //实体转换
  542. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  543. //新建
  544. outboundItemMapper.insert(outboundItem);
  545. outboundItemVO.setItemId(outboundItem.getItemId());
  546. //region 将库存需要的参数赋值
  547. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  548. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  549. outboundItemVO.setAddOrEditFlag(true);
  550. //endregion
  551. }
  552. //endregion
  553. //region 销售明细
  554. if (outboundItemVO.getFromItemId() != null) {
  555. //根据id查询
  556. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  557. OrderItem orderItem = new OrderItem();
  558. orderItem.setItemId(outboundItemVO.getFromItemId());
  559. orderItem.setOutQty(outboundItemResponse.getOutQty().negate().add(outboundItemVO.getOutingQty()));
  560. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate().add(outboundItemVO.getOutingAmt()));
  561. orderItem.setOutingQty(outboundItemVO.getOutQty());
  562. orderItem.setOutingAmt(outboundItemVO.getOutAmt());
  563. //出库状态
  564. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  565. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  566. orderItem.setOutStatus(outStatus);
  567. int countRow = orderItemMapper.updateById(orderItem);
  568. //数量超出
  569. if (countRow == 0) {
  570. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  571. }
  572. }
  573. //endregion
  574. }
  575. //endregion
  576. //region 删除明细
  577. BigDecimal delOutQty = BigDecimal.ZERO;
  578. BigDecimal delOutAmt = BigDecimal.ZERO;
  579. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  580. delOutQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  581. delOutAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  582. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  583. if (outboundItemVO.getItemId() != null) {
  584. //region 将库存需要的参数赋值
  585. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  586. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  587. //endregion
  588. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  589. outboundItem.setFlgValid(false);
  590. //修改
  591. outboundItemMapper.update(outboundItem,
  592. new UpdateWrapper<OutboundItem>().lambda()
  593. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  594. );
  595. }
  596. //region 销售明细
  597. if (outboundItemVO.getFromItemId() != null) {
  598. //根据id查询
  599. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  600. OrderItem orderItem = new OrderItem();
  601. orderItem.setItemId(outboundItemVO.getFromItemId());
  602. orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  603. orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  604. orderItem.setOutingQty(outboundItemVO.getOutQty());
  605. orderItem.setOutingAmt(outboundItemVO.getOutQty());
  606. //出库状态
  607. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  608. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  609. orderItem.setOutStatus(outStatus);
  610. int countRow = orderItemMapper.updateById(orderItem);
  611. //数量超出
  612. if (countRow == 0) {
  613. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  614. }
  615. }
  616. //endregion
  617. }
  618. }
  619. //endregion
  620. //region 修改出库总单
  621. Outbound outbound = new Outbound();
  622. outbound.setOutId(outboundVO.getOutId());
  623. outbound.setOutQty(sumQty);
  624. outbound.setOutAmt(sumAmt);
  625. //修改
  626. outboundMapper.update(outbound,
  627. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  628. );
  629. //endregion
  630. //region 修改销售总单
  631. if (outboundVO.getFromId() != null) {
  632. //根据id查询
  633. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  634. Order order = new Order();
  635. order.setOrderId(outboundVO.getFromId());
  636. order.setOutQty(outboundResponse.getOutQty().negate().add(sumQty).subtract(delOutQty));
  637. order.setOutAmt(outboundResponse.getOutAmt().negate().add(sumAmt).subtract(delOutAmt));
  638. order.setOutingQty(delOutQty);
  639. order.setOutingAmt(delOutAmt);
  640. //出库状态
  641. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  642. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  643. order.setOutStatus(outStatus);
  644. //修改
  645. int countRow = orderMapper.updateById(order);
  646. //数量超出
  647. if (countRow == 0) {
  648. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  649. }
  650. }
  651. //endregion
  652. //region 应收记账
  653. if (outboundVO.getReceivableId() != null) {
  654. accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  655. }
  656. //endregion
  657. //region 外协品新建外协入库单
  658. //筛选出skuId为空的 走外协品逻辑
  659. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  660. //删除外协品生产外协入库单
  661. this.deleteOutsideGoodsInto(outboundVO, outsideGoods);
  662. //新建外协品生产外协入库单
  663. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  664. List<OutboundItemVO> delOutsideGoods = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  665. //删除外协品生产外协入库单
  666. this.deleteOutsideGoodsInto(outboundVO, delOutsideGoods);
  667. //endregion
  668. //region 修改库存
  669. //筛选出skuId不为空的 走库存
  670. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  671. List<OutboundItemVO> invDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  672. if ((invList != null && invList.size() > 0) || (invDelList != null && invDelList.size() > 0)) {
  673. Map<String, Object> invMap = new HashMap<>();
  674. invMap.put("outDetail", invList);
  675. invMap.put("delOutDetail", invDelList);
  676. inventoryService.operatingInventoryInformation(invMap);
  677. }
  678. //endregion
  679. } else {
  680. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
  681. }
  682. //endregion
  683. //region 自动办理参数false 入库中编辑
  684. //自动办理标识为false 并且 自动办理参数为false 入库中
  685. if (!outboundVO.getFlgAutoHandle() && Constant.FlgHandleSetting.FALSE.getValue().equals(outboundVO.getFlgHandleSetting())) {
  686. //region 修改明细
  687. List<OutboundItemVO> itemList = outboundVO.getItemList();
  688. for (OutboundItemVO outboundItemVO : itemList) {
  689. //根据id查询 获取到还未进行修改的数据
  690. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  691. //region 编辑明细
  692. if (outboundItemVO.getItemId() != null) {
  693. OutboundItem outboundItem = new OutboundItem();
  694. outboundItem.setItemId(outboundItemResponse.getItemId());
  695. outboundItem.setOutingQty(outboundItemResponse.getOutingQty());
  696. outboundItem.setOutingAmt(outboundItemResponse.getOutingAmt());
  697. //修改
  698. outboundItemMapper.update(outboundItem,
  699. new UpdateWrapper<OutboundItem>().lambda()
  700. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  701. );
  702. }
  703. //endregion
  704. //region 新建明细
  705. else {
  706. outboundItemVO
  707. .setOutId(outboundItemVO.getOutId())
  708. .setOutQty(BigDecimal.ZERO)
  709. .setOutAmt(BigDecimal.ZERO)
  710. .setCostPrice(outboundItemVO.getPriceOut())
  711. .setCostAmt(outboundItemVO.getOutingQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  712. .setOutType(Constant.OutType.SALE.getName())
  713. ;
  714. //出库状态
  715. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  716. outboundItemVO.setOutStatus(outStatus);
  717. //实体转换
  718. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  719. //新建
  720. outboundItemMapper.insert(outboundItem);
  721. outboundItemVO.setItemId(outboundItem.getItemId());
  722. }
  723. //endregion
  724. //region 销售明细
  725. if (outboundItemVO.getFromItemId() != null) {
  726. //根据id查询
  727. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  728. OrderItem orderItem = new OrderItem();
  729. orderItem.setItemId(outboundItemVO.getFromItemId());
  730. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate().add(outboundItemVO.getOutingQty()));
  731. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate().add(outboundItemVO.getOutingAmt()));
  732. //出库状态
  733. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  734. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  735. orderItem.setOutStatus(outStatus);
  736. int countRow = orderItemMapper.updateById(orderItem);
  737. //数量超出
  738. if (countRow == 0) {
  739. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  740. }
  741. }
  742. //endregion
  743. }
  744. //endregion
  745. //region 删除明细
  746. BigDecimal delOutQty = BigDecimal.ZERO;
  747. BigDecimal delOutAmt = BigDecimal.ZERO;
  748. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  749. delOutQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  750. delOutAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  751. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  752. if (outboundItemVO.getItemId() != null) {
  753. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  754. outboundItem.setFlgValid(false);
  755. //修改
  756. outboundItemMapper.update(outboundItem,
  757. new UpdateWrapper<OutboundItem>().lambda()
  758. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  759. );
  760. }
  761. //region 销售明细
  762. if (outboundItemVO.getFromItemId() != null) {
  763. //根据id查询
  764. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  765. OrderItem orderItem = new OrderItem();
  766. orderItem.setItemId(outboundItemVO.getFromItemId());
  767. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  768. orderItem.setOutingAmt(outboundItemVO.getOutQty().negate());
  769. //出库状态
  770. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  771. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  772. orderItem.setOutStatus(outStatus);
  773. int countRow = orderItemMapper.updateById(orderItem);
  774. //数量超出
  775. if (countRow == 0) {
  776. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  777. }
  778. }
  779. //endregion
  780. }
  781. }
  782. //endregion
  783. //region 修改入库总单
  784. Outbound outbound = new Outbound();
  785. outbound.setOutId(outboundVO.getOutId());
  786. outbound.setOutingQty(sumQty);
  787. outbound.setOutingAmt(sumAmt);
  788. //修改
  789. outboundMapper.update(outbound,
  790. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  791. );
  792. //endregion
  793. //region 修改采购总单
  794. if (outboundVO.getFromId() != null) {
  795. //根据id查询
  796. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  797. Order order = new Order();
  798. order.setOrderId(outboundVO.getFromId());
  799. order.setOutingQty(outboundResponse.getOutingQty().negate().add(sumQty).subtract(delOutQty));
  800. order.setOutingAmt(outboundResponse.getOutingAmt().negate().add(sumAmt).subtract(delOutAmt));
  801. //出库状态
  802. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  803. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  804. order.setOutStatus(outStatus);
  805. //修改
  806. int countRow = orderMapper.updateById(order);
  807. //数量超出
  808. if (countRow == 0) {
  809. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  810. }
  811. }
  812. //endregion
  813. } else {
  814. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.FLG_HANDLE_SETTING_NOT_ENABLED_OUT.getMessage());
  815. }
  816. //endregion
  817. }
  818. //endregion
  819. return ResponseResultUtil.success(outboundVO);
  820. }
  821. /**
  822. * @desc : 销售出库作废
  823. * @date : 2024/3/26 9:24
  824. * @author : 寇珊珊
  825. */
  826. @Transactional(rollbackFor = {Exception.class})
  827. public ResponseResultVO<?> saleOutboundRepeal(String outId) {
  828. //region 查询总单 查询明细
  829. //根据id查询 此条出库单的数据还未更改前的数据
  830. OutboundResponse outboundResponse = outboundMapper.selectById(outId);
  831. //根据总单id查询
  832. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  833. //endregion
  834. //region 已出库状态作废
  835. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  836. //region 应收反记账
  837. if (outboundResponse.getReceivableId() != null) {
  838. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  839. }
  840. //endregion
  841. //region 修改明细
  842. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  843. //region 将库存需要的参数赋值
  844. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  845. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  846. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  847. outboundItemResponse.setOutAmt(outboundItemResponse.getOutAmt().negate());
  848. //endregion
  849. //赋值 防止作废的单据查不到明细 故注掉下面代码
  850. // OutboundItem outboundItem = new OutboundItem();
  851. // outboundItem.setItemId(outboundItemResponse.getItemId());
  852. // outboundItem.setFlgValid(false);
  853. // //修改
  854. // outboundItemMapper.update(outboundItem,
  855. // new UpdateWrapper<OutboundItem>().lambda()
  856. // .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  857. // );
  858. //region 销售明细
  859. if (outboundItemResponse.getFromItemId() != null) {
  860. //根据id查询
  861. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  862. OrderItem orderItem = new OrderItem();
  863. orderItem.setItemId(outboundItemResponse.getFromItemId());
  864. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  865. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  866. //出库状态
  867. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty(),
  868. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  869. orderItem.setOutStatus(orderOutStatus);
  870. int countRow = orderItemMapper.updateById(orderItem);
  871. //数量超出
  872. if (countRow == 0) {
  873. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  874. }
  875. }
  876. //endregion
  877. }
  878. //endregion
  879. //region 修改入库总单
  880. Outbound outbound = new Outbound();
  881. outbound.setOutId(outboundResponse.getOutId());
  882. outbound.setFlgValid(false);
  883. //修改
  884. outboundMapper.update(outbound,
  885. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  886. );
  887. //endregion
  888. //region 修改销售总单
  889. if (outboundResponse.getFromId() != null) {
  890. //根据id查询
  891. OrderResponse orderResponse = orderMapper.selectById(outboundResponse.getFromId());
  892. Order order = new Order();
  893. order.setOrderId(outboundResponse.getFromId());
  894. order.setOutQty(outboundResponse.getOutQty().negate());
  895. order.setOutAmt(outboundResponse.getOutAmt().negate());
  896. //出库状态
  897. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty(),
  898. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  899. order.setOutStatus(orderOutStatus);
  900. //修改
  901. int countRow = orderMapper.updateById(order);
  902. //数量超出
  903. if (countRow == 0) {
  904. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  905. }
  906. }
  907. //endregion
  908. //region 外协品新建外协入库单
  909. //筛选出skuId为空的 走外协品逻辑
  910. OutboundVO outboundVO = outboundConvert.convertResToVO(outboundResponse);
  911. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  912. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  913. //删除外协品生产外协入库单
  914. this.deleteOutsideGoodsInto(outboundVO, outsideGoodsVOList);
  915. //endregion
  916. //region 修改库存
  917. //筛选出skuId不为空的 走库存
  918. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  919. if(invList!=null && invList.size()>0) {
  920. Map<String, Object> map = new HashMap<>();
  921. map.put("delOutDetail", invList);
  922. inventoryService.operatingInventoryInformation(map);
  923. }
  924. //endregion
  925. }
  926. //endregion
  927. //region 出库中、待出库状态作废
  928. if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus()) ||
  929. Constant.OutStatus.DAICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  930. //region 修改明细
  931. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  932. //region 修改明细
  933. //赋值
  934. OutboundItem outboundItem = new OutboundItem();
  935. outboundItem.setItemId(outboundItemResponse.getItemId());
  936. outboundItem.setOutingQty(BigDecimal.ZERO);
  937. outboundItem.setOutingAmt(BigDecimal.ZERO);
  938. // outboundItem.setFlgValid(false);
  939. //修改
  940. outboundItemMapper.update(outboundItem,
  941. new UpdateWrapper<OutboundItem>().lambda()
  942. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  943. );
  944. //endregion
  945. //region 销售明细
  946. if (outboundItemResponse.getFromItemId() != null) {
  947. //根据id查询
  948. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  949. OrderItem orderItem = new OrderItem();
  950. orderItem.setItemId(outboundItemResponse.getFromItemId());
  951. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate());
  952. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate());
  953. //出库状态
  954. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  955. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  956. orderItem.setOutStatus(orderOutStatus);
  957. int countRow = orderItemMapper.updateById(orderItem);
  958. //数量超出
  959. if (countRow == 0) {
  960. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  961. }
  962. }
  963. //endregion
  964. }
  965. //endregion
  966. //region 修改出库总单
  967. Outbound outbound = new Outbound();
  968. outbound.setOutId(outboundResponse.getOutId());
  969. outbound.setOutingQty(BigDecimal.ZERO);
  970. outbound.setOutingAmt(BigDecimal.ZERO);
  971. outbound.setFlgValid(false);
  972. //修改
  973. outboundMapper.update(outbound,
  974. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  975. );
  976. //endregion
  977. //region 修改销售总单
  978. if (outboundResponse.getFromId() != null) {
  979. //根据id查询
  980. OrderResponse orderResponse = orderMapper.selectById(outboundResponse.getFromId());
  981. Order order = new Order();
  982. order.setOrderId(outboundResponse.getFromId());
  983. order.setOutingQty(outboundResponse.getOutingQty().negate());
  984. order.setOutingAmt(outboundResponse.getOutingAmt().negate());
  985. //出库状态
  986. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  987. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  988. order.setOutStatus(orderOutStatus);
  989. //修改
  990. int countRow = orderMapper.updateById(order);
  991. //数量超出
  992. if (countRow == 0) {
  993. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  994. }
  995. }
  996. //endregion
  997. }
  998. //endregion
  999. return ResponseResultUtil.success();
  1000. }
  1001. /**
  1002. * @desc : 销售出库办理
  1003. * @date : 2024/3/7 15:47
  1004. * @author : 寇珊珊
  1005. */
  1006. @Transactional(rollbackFor = {Exception.class})
  1007. public ResponseResultVO<?> saleOrderHandleOutbound(OutboundVO outboundVO) {
  1008. //region 根据id查询 此条入库单的数据还未更改前的数据
  1009. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1010. //endregion
  1011. //region 应收反记账
  1012. if (outboundResponse.getReceivableId() != null) {
  1013. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1014. }
  1015. //endregion
  1016. //region 编辑明细
  1017. //校验明细
  1018. if (outboundVO.getItemList().size() == 0) {
  1019. throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  1020. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  1021. }
  1022. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  1023. //明细根据id查询
  1024. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  1025. //region 校验数量是否超出
  1026. if (outboundItemVO.getOutingQty().compareTo(outboundItemResponse.getOutingQty()) > 0) {
  1027. throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getCode(),
  1028. ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getMessage());
  1029. }
  1030. //endregion
  1031. //region 编辑明细
  1032. if (outboundItemVO.getItemId() != null) {
  1033. outboundItemVO
  1034. .setOutQty(outboundItemVO.getOutingQty())
  1035. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1036. .setOutAmt(outboundItemVO.getOutingAmt())
  1037. .setOutingQty(BigDecimal.ZERO).setOutingBox(0).setOutingPiece(BigDecimal.ZERO)
  1038. .setOutingAmt(BigDecimal.ZERO)
  1039. .setCostPrice(outboundItemVO.getPriceOut())
  1040. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1041. ;
  1042. //出库状态
  1043. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1044. outboundItemVO.setOutStatus(outStatus);
  1045. //实体转换
  1046. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1047. //修改
  1048. outboundItemMapper.update(outboundItem,
  1049. new UpdateWrapper<OutboundItem>().lambda()
  1050. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1051. );
  1052. //region 将库存需要的参数赋值
  1053. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1054. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1055. //编辑之前的数
  1056. if (outboundItemResponse.getOutQty().compareTo(BigDecimal.ZERO) > 0) {
  1057. outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
  1058. outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
  1059. }
  1060. outboundItemVO.setAddOrEditFlag(true);
  1061. //endregion
  1062. }
  1063. //endregion
  1064. //region 新建明细
  1065. else {
  1066. outboundItemVO
  1067. .setOutId(outboundItemVO.getOutId())
  1068. .setOutQty(outboundItemVO.getOutingQty())
  1069. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1070. .setOutAmt(outboundItemVO.getOutingAmt())
  1071. .setCostPrice(outboundItemVO.getPriceOut())
  1072. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1073. .setOutType(Constant.OutType.SALE.getName())
  1074. .setOutingQty(BigDecimal.ZERO)
  1075. .setOutingAmt(BigDecimal.ZERO)
  1076. ;
  1077. //出库状态
  1078. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1079. outboundItemVO.setOutStatus(outStatus);
  1080. //实体转换
  1081. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1082. //新建
  1083. outboundItemMapper.insert(outboundItem);
  1084. outboundItemVO.setItemId(outboundItem.getItemId());
  1085. //region 将库存需要的参数赋值
  1086. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1087. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1088. outboundItemVO.setAddOrEditFlag(true);
  1089. //endregion
  1090. }
  1091. //endregion
  1092. //region 销售订单明细
  1093. if (outboundItemVO.getFromItemId() != null) {
  1094. //根据id查询
  1095. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1096. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1097. OrderItem orderItem = new OrderItem();
  1098. orderItem.setItemId(outboundItemVO.getFromItemId());
  1099. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  1100. orderItem.setOutingAmt(outboundItemVO.getOutAmt().negate());
  1101. orderItem.setOutQty(outboundItemVO.getOutQty());
  1102. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  1103. //出库状态
  1104. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(outboundItemVO.getOutQty()),
  1105. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1106. orderItem.setOutStatus(orderOutStatus);
  1107. //修改
  1108. int countRow = orderItemMapper.updateById(orderItem);
  1109. //数量超出
  1110. if (countRow == 0) {
  1111. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1112. }
  1113. }
  1114. //endregion
  1115. }
  1116. //endregion
  1117. //region 删除明细
  1118. BigDecimal delOutingQty = BigDecimal.ZERO;
  1119. BigDecimal delOutingAmt = BigDecimal.ZERO;
  1120. BigDecimal delOutQty = BigDecimal.ZERO;
  1121. BigDecimal delOutAmt = BigDecimal.ZERO;
  1122. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  1123. delOutingQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1124. delOutingAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1125. delOutQty = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1126. delOutAmt = outboundVO.getDeleteItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1127. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  1128. if (outboundItemVO.getItemId() != null) {
  1129. //region 将库存需要的参数赋值
  1130. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1131. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1132. //endregion
  1133. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1134. outboundItem.setFlgValid(false);
  1135. //修改
  1136. outboundItemMapper.update(outboundItem,
  1137. new UpdateWrapper<OutboundItem>().lambda()
  1138. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1139. );
  1140. }
  1141. //region 销售订单明细
  1142. if (outboundItemVO.getFromItemId() != null) {
  1143. //根据id查询
  1144. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1145. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1146. OrderItem orderItem = new OrderItem();
  1147. orderItem.setItemId(outboundItemVO.getFromItemId());
  1148. orderItem.setOutingQty(outboundItemVO.getOutingQty().negate());
  1149. orderItem.setOutingAmt(outboundItemVO.getOutingAmt().negate());
  1150. orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  1151. orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  1152. //出库状态
  1153. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(orderItem.getOutingQty()),
  1154. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1155. orderItem.setOutStatus(outStatus);
  1156. //修改
  1157. int countRow = orderItemMapper.updateById(orderItem);
  1158. //数量超出
  1159. if (countRow == 0) {
  1160. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1161. }
  1162. }
  1163. //endregion
  1164. }
  1165. }
  1166. //endregion
  1167. //region 编辑总单
  1168. BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1169. BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1170. outboundVO.setOutQty(sumOutQty);
  1171. outboundVO.setOutAmt(sumOutAmt);
  1172. outboundVO.setOutingQty(BigDecimal.ZERO);
  1173. outboundVO.setOutingAmt(BigDecimal.ZERO);
  1174. //出库状态
  1175. String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
  1176. outboundVO.setOutStatus(outStatus);
  1177. //实体转换
  1178. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  1179. //修改
  1180. outboundMapper.update(outbound,
  1181. new UpdateWrapper<Outbound>().lambda()
  1182. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1183. );
  1184. //endregion
  1185. //region 修改销售订单订单
  1186. if (outboundVO.getFromId() != null) {
  1187. //根据id查询
  1188. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  1189. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1190. Order order = new Order();
  1191. order.setOrderId(outboundVO.getFromId());
  1192. order.setOutQty(sumOutQty.subtract(delOutQty));
  1193. order.setOutAmt(sumOutAmt.subtract(delOutAmt));
  1194. order.setOutingQty((outboundResponse.getOutingQty().add(delOutingQty)).negate());
  1195. order.setOutingAmt((outboundResponse.getOutingAmt().add(delOutingAmt)).negate());
  1196. //出库状态
  1197. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().subtract(sumOutQty).add(delOutQty),
  1198. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1199. order.setOutStatus(orderOutStatus);
  1200. //修改
  1201. int countRow = orderMapper.updateById(order);
  1202. //数量超出
  1203. if (countRow == 0) {
  1204. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1205. }
  1206. }
  1207. //endregion
  1208. //region 应收记账
  1209. accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1210. //endregion
  1211. //region 库存明细处理
  1212. if(outboundVO.getItemList()!=null && outboundVO.getItemList().size()>0){
  1213. Map<String, Object> map = new HashMap<>(); // 定义map值 修改库存用
  1214. // 获取外协数据
  1215. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1216. //新建外协品生产外协入库单
  1217. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  1218. // 获取商品明细数据
  1219. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1220. // 赋值修改明细
  1221. map.put("outDetail", invList);
  1222. // 删除的商品
  1223. if(outboundVO.getDeleteItemList()!=null && outboundVO.getDeleteItemList().size()>0){
  1224. // 获取外协删除数据
  1225. List<OutboundItemVO> invsideDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1226. //删除外协品生产外协入库单
  1227. this.deleteOutsideGoodsInto(outboundVO, invsideDelList);
  1228. // 获取删除商品数据
  1229. List<OutboundItemVO> invDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1230. // 赋值删除明细
  1231. map.put("delOutDetail", invDelList);
  1232. }
  1233. // 调用修改库存信息方法
  1234. inventoryService.operatingInventoryInformation(map);
  1235. }
  1236. //endregion
  1237. return ResponseResultUtil.success(outboundVO);
  1238. }
  1239. /**
  1240. * @desc : 销售出库撤销
  1241. * @date : 2024/3/7 17:06
  1242. * @author : 寇珊珊
  1243. */
  1244. @Transactional(rollbackFor = {Exception.class})
  1245. public ResponseResultVO<?> saleOrderOutboundCancel(OutboundVO outboundVO) {
  1246. //region 查询出库总单数据信息
  1247. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1248. //endregion
  1249. //region 应收反记账
  1250. if (outboundResponse.getReceivableId() != null) {
  1251. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1252. }
  1253. //endregion
  1254. //region 修改订单数据信息
  1255. if (outboundResponse.getFromId() != null) {
  1256. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1257. Order order = new Order();
  1258. order.setOrderId(outboundResponse.getFromId());
  1259. order.setOutingQty(outboundResponse.getOutQty());
  1260. order.setOutingAmt(outboundResponse.getOutAmt());
  1261. order.setOutQty(outboundResponse.getOutQty().negate());
  1262. order.setOutAmt(outboundResponse.getOutAmt().negate());
  1263. //根据id查询
  1264. OrderResponse orderResponse = orderMapper.selectById(outboundResponse.getFromId());
  1265. //出库状态
  1266. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1267. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1268. order.setOutStatus(outStatus);
  1269. //修改
  1270. int countRow = orderMapper.updateById(order);
  1271. //数量超出
  1272. if (countRow == 0) {
  1273. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1274. }
  1275. }
  1276. //endregion
  1277. //region 修改总单数据信息
  1278. Outbound outbound = new Outbound();
  1279. outbound.setOutId(outboundResponse.getOutId());
  1280. outbound.setOutDate(null);
  1281. outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  1282. outbound.setOutQty(BigDecimal.ZERO);
  1283. outbound.setOutAmt(BigDecimal.ZERO);
  1284. outbound.setOutingQty(outboundResponse.getOutingQty().add(outboundResponse.getOutQty()));
  1285. outbound.setOutingAmt(outboundResponse.getOutingAmt().add(outboundResponse.getOutAmt()));
  1286. //修改
  1287. outboundMapper.update(outbound,
  1288. new UpdateWrapper<Outbound>().lambda()
  1289. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1290. );
  1291. //endregion
  1292. //region 明细数据
  1293. //根据总单id查明细
  1294. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1295. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1296. //region 修改销售订单明细数据信息
  1297. if (outboundItemResponse.getFromItemId() != null) {
  1298. //根据id查询
  1299. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1300. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1301. OrderItem orderItem = new OrderItem();
  1302. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1303. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  1304. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  1305. orderItem.setOutingQty(outboundItemResponse.getOutQty());
  1306. orderItem.setOutingAmt(outboundItemResponse.getOutAmt());
  1307. //出库状态
  1308. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  1309. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1310. orderItem.setOutStatus(outStatus);
  1311. //修改
  1312. int countRow = orderItemMapper.updateById(orderItem);
  1313. //数量超出
  1314. if (countRow == 0) {
  1315. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1316. }
  1317. }
  1318. //endregion
  1319. //region修改出库明细信息
  1320. OutboundItem outboundItem = new OutboundItem();
  1321. //region 将库存需要的参数赋值
  1322. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1323. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1324. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1325. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1326. //endregion
  1327. outboundItem
  1328. .setOutId(outboundResponse.getOutId())
  1329. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  1330. .setOutingQty(outboundItemResponse.getOutingQty().add(outboundItemResponse.getOutQty()))
  1331. .setOutAmt(outboundItemResponse.getOutingAmt().add(outboundItemResponse.getOutAmt()))
  1332. .setOutQty(BigDecimal.ZERO)
  1333. .setOutAmt(BigDecimal.ZERO)
  1334. .setCostPrice(BigDecimal.ZERO)
  1335. .setCostAmt(BigDecimal.ZERO)
  1336. .setItemId(outboundItemResponse.getItemId());
  1337. //修改
  1338. outboundItemMapper.update(outboundItem,
  1339. new UpdateWrapper<OutboundItem>().lambda()
  1340. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1341. );
  1342. //endregion
  1343. }
  1344. //endregion
  1345. //region 外协品新建外协入库单
  1346. //筛选出skuId为空的 走外协品逻辑
  1347. OutboundVO outsideGoodVO = outboundConvert.convertResToVO(outboundResponse);
  1348. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1349. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  1350. //删除外协品生产外协入库单
  1351. this.deleteOutsideGoodsInto(outsideGoodVO, outsideGoodsVOList);
  1352. //endregion
  1353. //region 调用库存
  1354. //筛选出skuId不为空的 走库存
  1355. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1356. if(invList!=null && invList.size()>0) {
  1357. Map<String, Object> map = new HashMap<>();
  1358. map.put("delOutDetail", invList);
  1359. inventoryService.operatingInventoryInformation(map);
  1360. }
  1361. //endregion
  1362. return ResponseResultUtil.success();
  1363. }
  1364. /**
  1365. * @desc : 出库状态通用(目前本页面)
  1366. * @date : 2024/3/9 8:59
  1367. * @author : 寇珊珊
  1368. */
  1369. @Transactional(rollbackFor = {Exception.class})
  1370. public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
  1371. //出库状态
  1372. String outStatus = null;
  1373. //已出库数量>0 出库中数量>0
  1374. if (outQty.compareTo(BigDecimal.ZERO) >= 0 && outingQty.compareTo(BigDecimal.ZERO) > 0) {
  1375. //出库中
  1376. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1377. }
  1378. //出入库数量=0 出库中数量=0
  1379. else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1380. //待出库
  1381. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1382. }
  1383. //已出库数量>0 出库中数量=0
  1384. else if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1385. //已出库
  1386. outStatus = Constant.OutStatus.YICHUKU.getName();
  1387. }
  1388. return outStatus;
  1389. }
  1390. /**
  1391. * @desc : 上游单据入库状态通用(目前本页面)
  1392. * @date : 2024/4/1 17:14
  1393. * @author : 寇珊珊
  1394. */
  1395. @Transactional(rollbackFor = {Exception.class})
  1396. public String setOutStatus(BigDecimal intoingQty, BigDecimal intoQty, BigDecimal sumQty) {
  1397. //入库状态
  1398. String outStatus = null;
  1399. //入库中+已入库 小于 总数
  1400. if (intoingQty.add(intoQty).compareTo(sumQty) < 0) {
  1401. //入库中
  1402. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1403. }
  1404. //已入库数量=0 入库中数量=0
  1405. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  1406. //待入库
  1407. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1408. }
  1409. //入库中+已入库 等于 总数
  1410. else if (intoingQty.add(intoQty).compareTo(sumQty) == 0) {
  1411. //已入库
  1412. outStatus = Constant.OutStatus.YICHUKU.getName();
  1413. }
  1414. return outStatus;
  1415. }
  1416. /**
  1417. * @desc : 获取单据信息(编辑用)
  1418. * @date : 2024/3/16 16:28
  1419. * @author : 寇珊珊
  1420. */
  1421. public ResponseResultVO<?> selectByUpdate(String id) {
  1422. Map<String, Object> dataInfo = new HashMap<>();
  1423. //总单
  1424. OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
  1425. //单据不存在
  1426. if (outboundResponse == null) {
  1427. throw new BaseBusinessException(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
  1428. ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
  1429. }
  1430. dataInfo.put("data", outboundResponse);
  1431. // 明细
  1432. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1433. dataInfo.put("dataItem", outboundItemResponseList);
  1434. return ResponseResultUtil.success(dataInfo);
  1435. }
  1436. /********************************************** 销售出库查询相关方法begin *************************************/
  1437. /**
  1438. * @desc : 一览页销售出库
  1439. * @author : 付斌
  1440. * @date : 2023/1/9 10:40
  1441. */
  1442. @Pagination
  1443. public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1444. outboundQuery.setOutType(Constant.OutType.SALE.getName());
  1445. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1446. outboundMapper.countByCond(outboundQuery));
  1447. }
  1448. /**
  1449. * @desc : 一览页销售出库明细(货物、附件)
  1450. * @author : 付斌
  1451. * @date : 2024-02-28 13:25
  1452. */
  1453. @Pagination
  1454. public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
  1455. Map<String, Object> result = new HashMap<>();
  1456. // 商品明细
  1457. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1458. result.put("outboundItem", outboundItem);
  1459. // 附件
  1460. return ResponseResultUtil.success(result);
  1461. }
  1462. /**
  1463. * @desc : 获取销售出库信息(编辑用)
  1464. * @author : 付斌
  1465. * @date : 2024-03-02 17:27
  1466. */
  1467. public ResponseResultVO<?> getOutboundForUpdate(String id) {
  1468. Map<String, Object> dataInfo = new HashMap<>();
  1469. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1470. dataInfo.put("data", outboundResponse);
  1471. // 商品明细
  1472. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEdit(id);
  1473. dataInfo.put("dataItem", outboundItemResponseList);
  1474. return ResponseResultUtil.success(dataInfo);
  1475. }
  1476. /**
  1477. * @desc : 获取销售出库信息(编辑用,适用于直接新建的出库单)
  1478. * @author : 付斌
  1479. * @date : 2024-03-02 17:27
  1480. */
  1481. public ResponseResultVO<?> getOutboundTogetherForUpdate(String id) {
  1482. Map<String, Object> dataInfo = new HashMap<>();
  1483. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1484. dataInfo.put("data", outboundResponse);
  1485. // 商品明细
  1486. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEditTogether(id);
  1487. dataInfo.put("dataItem", outboundItemResponseList);
  1488. return ResponseResultUtil.success(dataInfo);
  1489. }
  1490. /**
  1491. * @desc : 获取出库信息(新建退货用)
  1492. * @author : 付斌
  1493. * @date : 2024-03-02 17:27
  1494. */
  1495. public ResponseResultVO<?> getOutForReturn(String id) {
  1496. OutboundResponse outboundResponse = outboundMapper.selectByIdForReturn(id);
  1497. List<OutboundItemResponse> outboundItemList = outboundItemMapper.selectByCondForReturn(new OutboundItemQuery().setOutId(id));
  1498. if (outboundItemList != null && outboundItemList.size() > 0) {
  1499. // 求和
  1500. OutboundItemResponse sumEntity = outboundItemList.stream().reduce((x, y) -> {
  1501. OutboundItemResponse item = new OutboundItemResponse();
  1502. item.setOutingQty(x.getOutingQty().add(y.getOutingQty()));
  1503. item.setOutingAmt(x.getOutingAmt().add(y.getOutingAmt()));
  1504. return item;
  1505. }).get();
  1506. outboundResponse.setOutingQty(sumEntity.getOutingQty()).setOutingAmt(sumEntity.getOutingAmt());
  1507. }
  1508. Map<String, Object> dataInfo = new HashMap<>();
  1509. dataInfo.put("data", outboundResponse);
  1510. dataInfo.put("dataItem", outboundItemList);
  1511. return ResponseResultUtil.success(dataInfo);
  1512. }
  1513. /**
  1514. * @desc : 查询出库明细(货物、附件)
  1515. * @author : 付斌
  1516. * @date : 2024-02-28 13:25
  1517. */
  1518. @Pagination
  1519. public ResponseResultVO selectById(String id) {
  1520. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1521. // 商品明细
  1522. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1523. outboundResponse.setGoodsList(outboundItem);
  1524. // 附件
  1525. return ResponseResultUtil.success(outboundResponse);
  1526. }
  1527. /********************************************** 销售出库查询相关方法end *************************************/
  1528. // /**
  1529. // * @desc : 条件查询
  1530. // * @date : 2024/3/18 11:20
  1531. // * @author : 寇珊珊
  1532. // */
  1533. // @Pagination
  1534. // public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1535. // return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1536. // outboundMapper.countByCond(outboundQuery));
  1537. // }
  1538. //
  1539. // /**
  1540. // * @desc : 查询明细
  1541. // * @date : 2024/3/15 16:43
  1542. // * @author : 寇珊珊
  1543. // */
  1544. // @Pagination
  1545. // public ResponseResultVO<Map<String, Object>> selectOutBoundSaleOrderItemInfoById(String id) {
  1546. // Map<String, Object> result = new HashMap<>();
  1547. // // 商品明细
  1548. // List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1549. // result.put("itemList", outboundItemResponseList);
  1550. // // 收款
  1551. //
  1552. // // 附件
  1553. // return ResponseResultUtil.success(result);
  1554. // }
  1555. /**
  1556. * @desc : 条件查询(总单带明细)
  1557. * @author : 于继渤
  1558. * @date : 2023/1/9 10:36
  1559. */
  1560. @Pagination
  1561. public ResponseResultVO<PageList<OutboundResponse>> selectByCondDetail(OutboundQuery outboundQuery) {
  1562. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCondDetail(outboundQuery),
  1563. outboundMapper.countByCondDetail(outboundQuery));
  1564. }
  1565. }