OutboundSaleOrderService.java 84 KB

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