OutboundSaleOrderService.java 85 KB

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