OutboundSaleOrderService.java 101 KB

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