OutboundSaleOrderService.java 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  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(outboundItemVO.getOutingQty());
  790. outboundItem.setOutingAmt(outboundItemVO.getOutingAmt());
  791. outboundItem.setWhId(outboundItemVO.getWhId());
  792. //修改
  793. outboundItemMapper.update(outboundItem,
  794. new UpdateWrapper<OutboundItem>().lambda()
  795. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  796. );
  797. }
  798. //endregion
  799. //region 新建明细
  800. else {
  801. //region 反写订单总单 数据需要 把查出来的出库中数量用别的变量存起来
  802. outboundItemVO.setUpdateOrderOutingQty(outboundItemVO.getOutingQty());
  803. outboundItemVO.setUpdateOrderOutingAmt(outboundItemVO.getOutingAmt());
  804. //endregion
  805. outboundItemVO
  806. .setOutId(outboundItemVO.getOutId())
  807. .setOutQty(BigDecimal.ZERO)
  808. .setOutAmt(BigDecimal.ZERO)
  809. .setCostPrice(outboundItemVO.getPriceOut())
  810. .setCostAmt(outboundItemVO.getOutingQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  811. .setOutType(Constant.OutType.SALE.getName())
  812. ;
  813. //出库状态
  814. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  815. outboundItemVO.setOutStatus(outStatus);
  816. //实体转换
  817. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  818. //新建
  819. outboundItemMapper.insert(outboundItem);
  820. outboundItemVO.setItemId(outboundItem.getItemId());
  821. }
  822. //endregion
  823. //region 销售明细
  824. if (outboundItemVO.getFromItemId() != null) {
  825. //根据id查询
  826. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  827. OrderItem orderItem = new OrderItem();
  828. orderItem.setItemId(outboundItemVO.getFromItemId());
  829. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate().add(outboundItemVO.getOutingQty()));
  830. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate().add(outboundItemVO.getOutingAmt()));
  831. //出库状态
  832. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  833. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  834. orderItem.setOutStatus(outStatus);
  835. int countRow = orderItemMapper.updateById(orderItem);
  836. //数量超出
  837. if (countRow == 0) {
  838. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  839. }
  840. }
  841. //endregion
  842. }
  843. //endregion
  844. //region 删除明细
  845. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  846. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  847. if (outboundItemVO.getItemId() != null) {
  848. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  849. outboundItem.setFlgValid(false);
  850. //修改
  851. outboundItemMapper.update(outboundItem,
  852. new UpdateWrapper<OutboundItem>().lambda()
  853. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  854. );
  855. }
  856. //region 销售明细
  857. if (outboundItemVO.getFromItemId() != null) {
  858. //根据id查询
  859. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  860. OrderItem orderItem = new OrderItem();
  861. orderItem.setItemId(outboundItemVO.getFromItemId());
  862. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  863. orderItem.setOutingAmt(outboundItemVO.getOutQty().negate());
  864. //出库状态
  865. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  866. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  867. orderItem.setOutStatus(outStatus);
  868. int countRow = orderItemMapper.updateById(orderItem);
  869. //数量超出
  870. if (countRow == 0) {
  871. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  872. }
  873. }
  874. //endregion
  875. }
  876. }
  877. //endregion
  878. //region 修改入库总单
  879. Outbound outbound = new Outbound();
  880. outbound.setOutId(outboundVO.getOutId());
  881. outbound.setOutingQty(sumQty);
  882. outbound.setOutingAmt(sumAmt);
  883. //修改
  884. outboundMapper.update(outbound,
  885. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  886. );
  887. //endregion
  888. //region 修改销售总单
  889. this.updateOrderMessageByEdit(outboundResponse, outboundVO.getItemList(), outboundVO.getDeleteItemList());
  890. //endregion
  891. }
  892. //endregion
  893. }
  894. //endregion
  895. return ResponseResultUtil.success(outboundVO);
  896. }
  897. /**
  898. * @desc : 编辑销售出库修改销售订单信息
  899. * @date : 2024/6/21 10:34
  900. * @author : 寇珊珊
  901. */
  902. @Transactional(rollbackFor = {Exception.class})
  903. public void updateOrderMessageByEdit(OutboundResponse outboundResponse, List<OutboundItemVO> itemList, List<OutboundItemVO> deleteItemList) {
  904. //出库明细汇总
  905. Map<String, List<OutboundItemVO>> orderOutItemVOListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  906. //出库明细删除汇总
  907. Map<String, List<OutboundItemVO>> delOrderOutItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  908. //出库明细汇总循环
  909. for (String fromId : orderOutItemVOListMap.keySet()) {
  910. //有需要删除的修改数据
  911. if(delOrderOutItemVOListMap!=null && delOrderOutItemVOListMap.keySet().size()>0){
  912. //出库明细删除汇总循环
  913. for (String delFromId : delOrderOutItemVOListMap.keySet()) {
  914. //新建出库明细来源Id = 删除出库明细来源Id
  915. if (fromId.equals(delFromId)) {
  916. //region 提取新建分组后的明细
  917. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  918. //分组后的明细 出库数量和金额总和
  919. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  920. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  921. //分组后的明细 原出库数量和金额总和
  922. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  923. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  924. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  925. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  926. //已出库
  927. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  928. updateOrderOutQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  929. updateOrderOutAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  930. }
  931. //出库中
  932. else {
  933. updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  934. updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  935. }
  936. //endregion
  937. //region 提取删除分组后的明细
  938. List<OutboundItemVO> delOrderOutItemVOListGroup = delOrderOutItemVOListMap.get(delFromId);
  939. BigDecimal orderDelOutQty = BigDecimal.ZERO;
  940. BigDecimal orderDelOutAmt = BigDecimal.ZERO;
  941. //已出库
  942. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  943. orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  944. orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  945. }
  946. //出库中
  947. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  948. orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  949. orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  950. }
  951. //endregion
  952. //region 反写订单数据
  953. //根据id查询
  954. OrderResponse orderResponse = orderMapper.selectById(fromId);
  955. Order order = new Order();
  956. order.setOrderId(fromId);
  957. //已出库
  958. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  959. order.setOutQty(updateOrderOutQty.negate().add(orderSumQty).subtract(orderDelOutQty));
  960. order.setOutAmt(updateOrderOutAmt.negate().add(orderSumAmt).subtract(orderDelOutAmt));
  961. order.setOutingQty(orderDelOutQty);
  962. order.setOutingAmt(orderDelOutAmt);
  963. //出库状态
  964. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  965. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  966. order.setOutStatus(outStatus);
  967. }
  968. //出库中
  969. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  970. order.setOutingQty(updateOrderOutingQty.negate().add(orderSumQty).subtract(orderDelOutQty));
  971. order.setOutingAmt(updateOrderOutingAmt.negate().add(orderSumQty).subtract(orderDelOutAmt));
  972. //出库状态
  973. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  974. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  975. order.setOutStatus(outStatus);
  976. }
  977. //修改
  978. int countRow = orderMapper.updateById(order);
  979. //数量超出
  980. if (countRow == 0) {
  981. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  982. }
  983. //endregion
  984. }
  985. }
  986. }
  987. //没有需要删除的修改数据
  988. else{
  989. //region 提取新建分组后的明细
  990. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  991. //分组后的明细 出库数量和金额总和
  992. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  993. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  994. //分组后的明细 原出库数量和金额总和
  995. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  996. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  997. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  998. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  999. //已出库
  1000. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1001. updateOrderOutQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1002. updateOrderOutAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1003. }
  1004. //出库中
  1005. else {
  1006. updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1007. updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1008. }
  1009. //endregion
  1010. //region 反写订单数据
  1011. //根据id查询
  1012. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1013. Order order = new Order();
  1014. order.setOrderId(fromId);
  1015. //已出库
  1016. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1017. order.setOutQty(updateOrderOutQty.negate().add(orderSumQty));
  1018. order.setOutAmt(updateOrderOutAmt.negate().add(orderSumAmt));
  1019. //出库状态
  1020. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1021. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1022. order.setOutStatus(outStatus);
  1023. }
  1024. //出库中
  1025. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  1026. order.setOutingQty(updateOrderOutingQty.negate().add(orderSumQty));
  1027. order.setOutingAmt(updateOrderOutingAmt.negate().add(orderSumQty));
  1028. //出库状态
  1029. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1030. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  1031. order.setOutStatus(outStatus);
  1032. }
  1033. //修改
  1034. int countRow = orderMapper.updateById(order);
  1035. //数量超出
  1036. if (countRow == 0) {
  1037. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1038. }
  1039. //endregion
  1040. }
  1041. }
  1042. }
  1043. /**
  1044. * @desc : 销售出库作废
  1045. * @date : 2024/3/26 9:24
  1046. * @author : 寇珊珊
  1047. */
  1048. @Transactional(rollbackFor = {Exception.class})
  1049. public ResponseResultVO<?> saleOutboundRepeal(String outId) {
  1050. //region 查询总单 查询明细
  1051. //根据id查询 此条出库单的数据还未更改前的数据
  1052. OutboundResponse outboundResponse = outboundMapper.selectById(outId);
  1053. //根据总单id查询
  1054. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1055. //endregion
  1056. //region 已出库状态作废
  1057. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1058. //region 应收反记账
  1059. if (outboundResponse.getReceivableId() != null) {
  1060. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1061. }
  1062. //endregion
  1063. //region 修改明细
  1064. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1065. //region 将库存需要的参数赋值
  1066. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1067. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1068. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty());
  1069. outboundItemResponse.setOutAmt(outboundItemResponse.getOutAmt());
  1070. //endregion
  1071. //赋值 防止作废的单据查不到明细 故注掉下面代码
  1072. // OutboundItem outboundItem = new OutboundItem();
  1073. // outboundItem.setItemId(outboundItemResponse.getItemId());
  1074. // outboundItem.setFlgValid(false);
  1075. // //修改
  1076. // outboundItemMapper.update(outboundItem,
  1077. // new UpdateWrapper<OutboundItem>().lambda()
  1078. // .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1079. // );
  1080. //region 销售明细
  1081. if (outboundItemResponse.getFromItemId() != null) {
  1082. //根据id查询
  1083. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1084. OrderItem orderItem = new OrderItem();
  1085. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1086. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  1087. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  1088. //出库状态
  1089. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty(),
  1090. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1091. orderItem.setOutStatus(orderOutStatus);
  1092. int countRow = orderItemMapper.updateById(orderItem);
  1093. //数量超出
  1094. if (countRow == 0) {
  1095. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1096. }
  1097. }
  1098. //endregion
  1099. }
  1100. //endregion
  1101. //region 修改出库总单
  1102. Outbound outbound = new Outbound();
  1103. outbound.setOutId(outboundResponse.getOutId());
  1104. outbound.setFlgValid(false);
  1105. //修改
  1106. outboundMapper.update(outbound,
  1107. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1108. );
  1109. //endregion
  1110. //region 修改销售总单
  1111. this.updateOrderMessageByRepeal(outboundResponse, outboundItemResponseList);
  1112. //endregion
  1113. //region 外协品新建外协入库单
  1114. //筛选出skuId为空的 走外协品逻辑
  1115. OutboundVO outboundVO = outboundConvert.convertResToVO(outboundResponse);
  1116. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1117. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  1118. //删除外协品生产外协入库单
  1119. this.deleteOutsideGoodsInto(outboundVO, outsideGoodsVOList);
  1120. //endregion
  1121. //region 修改库存
  1122. //筛选出skuId不为空的 走库存
  1123. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1124. if (invList != null && invList.size() > 0) {
  1125. Map<String, Object> map = new HashMap<>();
  1126. map.put("delOutDetail", invList);
  1127. inventoryService.operatingInventoryInformation(map);
  1128. }
  1129. //endregion
  1130. }
  1131. //endregion
  1132. //region 出库中、待出库状态作废
  1133. if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus()) ||
  1134. Constant.OutStatus.DAICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1135. //region 修改明细
  1136. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1137. //region 修改明细
  1138. //赋值
  1139. OutboundItem outboundItem = new OutboundItem();
  1140. outboundItem.setItemId(outboundItemResponse.getItemId());
  1141. outboundItem.setOutingQty(BigDecimal.ZERO);
  1142. outboundItem.setOutingAmt(BigDecimal.ZERO);
  1143. // outboundItem.setFlgValid(false);
  1144. //修改
  1145. outboundItemMapper.update(outboundItem,
  1146. new UpdateWrapper<OutboundItem>().lambda()
  1147. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1148. );
  1149. //endregion
  1150. //region 销售明细
  1151. if (outboundItemResponse.getFromItemId() != null) {
  1152. //根据id查询
  1153. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1154. OrderItem orderItem = new OrderItem();
  1155. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1156. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate());
  1157. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate());
  1158. //出库状态
  1159. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  1160. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  1161. orderItem.setOutStatus(orderOutStatus);
  1162. int countRow = orderItemMapper.updateById(orderItem);
  1163. //数量超出
  1164. if (countRow == 0) {
  1165. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1166. }
  1167. }
  1168. //endregion
  1169. }
  1170. //endregion
  1171. //region 修改出库总单
  1172. Outbound outbound = new Outbound();
  1173. outbound.setOutId(outboundResponse.getOutId());
  1174. outbound.setOutingQty(BigDecimal.ZERO);
  1175. outbound.setOutingAmt(BigDecimal.ZERO);
  1176. outbound.setFlgValid(false);
  1177. //修改
  1178. outboundMapper.update(outbound,
  1179. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1180. );
  1181. //endregion
  1182. //region 修改销售总单
  1183. this.updateOrderMessageByRepeal(outboundResponse, outboundItemResponseList);
  1184. //endregion
  1185. }
  1186. //endregion
  1187. return ResponseResultUtil.success();
  1188. }
  1189. /**
  1190. * @desc : 作废销售出库修改销售订单信息
  1191. * @date : 2024/6/21 10:34
  1192. * @author : 寇珊珊
  1193. */
  1194. @Transactional(rollbackFor = {Exception.class})
  1195. public void updateOrderMessageByRepeal(OutboundResponse outboundResponse, List<OutboundItemResponse> itemList) {
  1196. //出库明细汇总
  1197. Map<String, List<OutboundItemResponse>> orderOutItemListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemResponse::getFromId));
  1198. //出库明细汇总循环
  1199. for (String fromId : orderOutItemListMap.keySet()) {
  1200. //region 提取新建分组后的明细
  1201. //分组后的明细 原出库数量和金额总和
  1202. List<OutboundItemResponse> orderOutItemListGroup = orderOutItemListMap.get(fromId);
  1203. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  1204. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  1205. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  1206. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  1207. //已出库
  1208. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1209. updateOrderOutQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1210. updateOrderOutAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1211. }
  1212. //出库中
  1213. else {
  1214. updateOrderOutingQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1215. updateOrderOutingAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1216. }
  1217. //endregion
  1218. //region 反写订单数据
  1219. //根据id查询
  1220. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1221. Order order = new Order();
  1222. order.setOrderId(fromId);
  1223. //已出库
  1224. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1225. order.setOutQty(updateOrderOutQty.negate());
  1226. order.setOutAmt(updateOrderOutAmt.negate());
  1227. //出库状态
  1228. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty(),
  1229. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1230. order.setOutStatus(orderOutStatus);
  1231. }
  1232. //出库中
  1233. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  1234. order.setOutingQty(updateOrderOutingQty.negate());
  1235. order.setOutingAmt(updateOrderOutingAmt.negate());
  1236. //出库状态
  1237. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1238. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  1239. order.setOutStatus(orderOutStatus);
  1240. }
  1241. //修改
  1242. int countRow = orderMapper.updateById(order);
  1243. //数量超出
  1244. if (countRow == 0) {
  1245. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1246. }
  1247. //endregion
  1248. }
  1249. }
  1250. /**
  1251. * @desc : 销售出库办理
  1252. * @date : 2024/3/7 15:47
  1253. * @author : 寇珊珊
  1254. */
  1255. @Transactional(rollbackFor = {Exception.class})
  1256. public ResponseResultVO<?> saleOrderHandleOutbound(OutboundVO outboundVO) {
  1257. //region 根据id查询 此条入库单的数据还未更改前的数据
  1258. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1259. //endregion
  1260. //region 应收反记账
  1261. if (outboundResponse.getReceivableId() != null) {
  1262. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1263. }
  1264. //endregion
  1265. //region 编辑明细
  1266. //校验明细
  1267. if (outboundVO.getItemList().size() == 0) {
  1268. throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  1269. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  1270. }
  1271. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  1272. //明细根据id查询
  1273. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  1274. //region 校验数量是否超出
  1275. if (outboundItemVO.getOutingQty().compareTo(outboundItemResponse.getOutingQty()) > 0) {
  1276. throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getCode(),
  1277. ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getMessage());
  1278. }
  1279. //endregion
  1280. //region 反写订单总单 数据需要 把查出来的出库中数量用别的变量存起来
  1281. outboundItemVO.setUpdateOrderOutingQty(outboundItemResponse.getOutingQty());
  1282. outboundItemVO.setUpdateOrderOutingAmt(outboundItemResponse.getOutingAmt());
  1283. //endregion
  1284. //region 编辑明细
  1285. if (outboundItemVO.getItemId() != null) {
  1286. outboundItemVO
  1287. .setOutQty(outboundItemVO.getOutingQty())
  1288. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1289. .setOutAmt(outboundItemVO.getOutingAmt())
  1290. .setOutingQty(BigDecimal.ZERO).setOutingBox(0).setOutingPiece(BigDecimal.ZERO)
  1291. .setOutingAmt(BigDecimal.ZERO)
  1292. .setCostPrice(outboundItemVO.getPriceOut())
  1293. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1294. ;
  1295. //出库状态
  1296. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1297. outboundItemVO.setOutStatus(outStatus);
  1298. //实体转换
  1299. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1300. //修改
  1301. outboundItemMapper.update(outboundItem,
  1302. new UpdateWrapper<OutboundItem>().lambda()
  1303. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1304. );
  1305. //region 将库存需要的参数赋值
  1306. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1307. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1308. //编辑之前的数
  1309. if (outboundItemResponse.getOutQty().compareTo(BigDecimal.ZERO) > 0) {
  1310. outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
  1311. outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
  1312. }
  1313. outboundItemVO.setAddOrEditFlag(true);
  1314. //endregion
  1315. }
  1316. //endregion
  1317. //region 新建明细
  1318. else {
  1319. outboundItemVO
  1320. .setOutId(outboundItemVO.getOutId())
  1321. .setOutQty(outboundItemVO.getOutingQty())
  1322. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1323. .setOutAmt(outboundItemVO.getOutingAmt())
  1324. .setCostPrice(outboundItemVO.getPriceOut())
  1325. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1326. .setOutType(Constant.OutType.SALE.getName())
  1327. .setOutingQty(BigDecimal.ZERO)
  1328. .setOutingAmt(BigDecimal.ZERO)
  1329. ;
  1330. //出库状态
  1331. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1332. outboundItemVO.setOutStatus(outStatus);
  1333. //实体转换
  1334. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1335. //新建
  1336. outboundItemMapper.insert(outboundItem);
  1337. outboundItemVO.setItemId(outboundItem.getItemId());
  1338. //region 将库存需要的参数赋值
  1339. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1340. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1341. outboundItemVO.setAddOrEditFlag(true);
  1342. //endregion
  1343. }
  1344. //endregion
  1345. //region 销售订单明细
  1346. if (outboundItemVO.getFromItemId() != null) {
  1347. //根据id查询
  1348. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1349. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1350. OrderItem orderItem = new OrderItem();
  1351. orderItem.setItemId(outboundItemVO.getFromItemId());
  1352. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  1353. orderItem.setOutingAmt(outboundItemVO.getOutAmt().negate());
  1354. orderItem.setOutQty(outboundItemVO.getOutQty());
  1355. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  1356. //出库状态
  1357. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(outboundItemVO.getOutQty()),
  1358. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1359. orderItem.setOutStatus(orderOutStatus);
  1360. //修改
  1361. int countRow = orderItemMapper.updateById(orderItem);
  1362. //数量超出
  1363. if (countRow == 0) {
  1364. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1365. }
  1366. }
  1367. //endregion
  1368. }
  1369. //endregion
  1370. //region 删除明细
  1371. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  1372. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  1373. if (outboundItemVO.getItemId() != null) {
  1374. //region 将库存需要的参数赋值
  1375. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1376. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1377. //endregion
  1378. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1379. outboundItem.setFlgValid(false);
  1380. //修改
  1381. outboundItemMapper.update(outboundItem,
  1382. new UpdateWrapper<OutboundItem>().lambda()
  1383. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1384. );
  1385. }
  1386. //region 销售订单明细
  1387. if (outboundItemVO.getFromItemId() != null) {
  1388. //根据id查询
  1389. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1390. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1391. OrderItem orderItem = new OrderItem();
  1392. orderItem.setItemId(outboundItemVO.getFromItemId());
  1393. orderItem.setOutingQty(outboundItemVO.getOutingQty().negate());
  1394. orderItem.setOutingAmt(outboundItemVO.getOutingAmt().negate());
  1395. orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  1396. orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  1397. //出库状态
  1398. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(orderItem.getOutingQty()),
  1399. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1400. orderItem.setOutStatus(outStatus);
  1401. //修改
  1402. int countRow = orderItemMapper.updateById(orderItem);
  1403. //数量超出
  1404. if (countRow == 0) {
  1405. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1406. }
  1407. }
  1408. //endregion
  1409. }
  1410. }
  1411. //endregion
  1412. //region 编辑总单
  1413. BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1414. BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1415. outboundVO.setOutQty(sumOutQty);
  1416. outboundVO.setOutAmt(sumOutAmt);
  1417. outboundVO.setOutingQty(BigDecimal.ZERO);
  1418. outboundVO.setOutingAmt(BigDecimal.ZERO);
  1419. //出库状态
  1420. String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
  1421. outboundVO.setOutStatus(outStatus);
  1422. //实体转换
  1423. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  1424. //修改
  1425. outboundMapper.update(outbound,
  1426. new UpdateWrapper<Outbound>().lambda()
  1427. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1428. );
  1429. //endregion
  1430. //region 修改销售订单订单
  1431. this.updateOrderMessageByHandle(outboundVO.getItemList(), outboundVO.getDeleteItemList());
  1432. //endregion
  1433. //region 应收记账
  1434. accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1435. //endregion
  1436. //region 库存明细处理
  1437. if (outboundVO.getItemList() != null && outboundVO.getItemList().size() > 0) {
  1438. Map<String, Object> map = new HashMap<>(); // 定义map值 修改库存用
  1439. // 获取外协数据
  1440. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1441. //新建外协品生产外协入库单
  1442. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  1443. // 获取商品明细数据
  1444. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1445. // 赋值修改明细
  1446. map.put("outDetail", invList);
  1447. // 删除的商品
  1448. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  1449. // 获取外协删除数据
  1450. List<OutboundItemVO> invsideDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1451. //删除外协品生产外协入库单
  1452. this.deleteOutsideGoodsInto(outboundVO, invsideDelList);
  1453. // 获取删除商品数据
  1454. List<OutboundItemVO> invDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1455. // 赋值删除明细
  1456. map.put("delOutDetail", invDelList);
  1457. }
  1458. // 调用修改库存信息方法
  1459. inventoryService.operatingInventoryInformation(map);
  1460. }
  1461. //endregion
  1462. return ResponseResultUtil.success(outboundVO);
  1463. }
  1464. /**
  1465. * @desc : 办理销售出库修改销售订单信息
  1466. * @date : 2024/6/21 15:13
  1467. * @author : 寇珊珊
  1468. */
  1469. @Transactional(rollbackFor = {Exception.class})
  1470. public void updateOrderMessageByHandle(List<OutboundItemVO> itemList, List<OutboundItemVO> deleteItemList) {
  1471. //出库明细汇总
  1472. Map<String, List<OutboundItemVO>> orderOutItemVOListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  1473. //出库明细删除汇总
  1474. Map<String, List<OutboundItemVO>> delOrderOutItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  1475. //出库明细汇总循环
  1476. for (String fromId : orderOutItemVOListMap.keySet()) {
  1477. //有需要删除的办理数据
  1478. if (delOrderOutItemVOListMap != null && delOrderOutItemVOListMap.keySet().size() > 0) {
  1479. //出库明细删除汇总循环
  1480. for (String delFromId : delOrderOutItemVOListMap.keySet()) {
  1481. //新建出库明细来源Id = 删除出库明细来源Id
  1482. if (fromId.equals(delFromId)) {
  1483. //region 提取新建分组后的明细
  1484. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  1485. //分组后的明细 出库数量和金额总和
  1486. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1487. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1488. //分组后的明细 原出库数量和金额总和
  1489. BigDecimal updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1490. BigDecimal updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1491. //endregion
  1492. //region 提取删除分组后的明细
  1493. List<OutboundItemVO> delOrderOutItemVOListGroup = delOrderOutItemVOListMap.get(delFromId);
  1494. BigDecimal orderDelOutingQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1495. BigDecimal orderDelOutingAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1496. BigDecimal orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1497. BigDecimal orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1498. //endregion
  1499. //region 反写订单数据
  1500. //根据id查询
  1501. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1502. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1503. Order order = new Order();
  1504. order.setOrderId(fromId);
  1505. order.setOutQty(orderSumQty.subtract(orderDelOutQty));
  1506. order.setOutAmt(orderSumAmt.subtract(orderDelOutAmt));
  1507. order.setOutingQty((updateOrderOutingQty.add(orderDelOutingQty)).negate());
  1508. order.setOutingAmt((updateOrderOutingAmt.add(orderDelOutingAmt)).negate());
  1509. //出库状态
  1510. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().subtract(orderSumQty).add(orderDelOutQty),
  1511. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1512. order.setOutStatus(orderOutStatus);
  1513. //修改
  1514. int countRow = orderMapper.updateById(order);
  1515. //数量超出
  1516. if (countRow == 0) {
  1517. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1518. }
  1519. //endregion
  1520. }
  1521. }
  1522. }
  1523. //没有需要删除的办理数据
  1524. else {
  1525. //region 提取新建分组后的明细
  1526. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  1527. //分组后的明细 出库数量和金额总和
  1528. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1529. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1530. //分组后的明细 原出库数量和金额总和
  1531. BigDecimal updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1532. BigDecimal updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1533. //endregion
  1534. //region 反写订单数据
  1535. //根据id查询
  1536. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1537. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1538. Order order = new Order();
  1539. order.setOrderId(fromId);
  1540. order.setOutQty(orderSumQty);
  1541. order.setOutAmt(orderSumAmt);
  1542. order.setOutingQty(updateOrderOutingQty.negate());
  1543. order.setOutingAmt(updateOrderOutingAmt.negate());
  1544. //出库状态
  1545. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().subtract(orderSumQty),
  1546. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1547. order.setOutStatus(orderOutStatus);
  1548. //修改
  1549. int countRow = orderMapper.updateById(order);
  1550. //数量超出
  1551. if (countRow == 0) {
  1552. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1553. }
  1554. //endregion
  1555. }
  1556. }
  1557. }
  1558. /**
  1559. * @desc : 销售出库撤销
  1560. * @date : 2024/3/7 17:06
  1561. * @author : 寇珊珊
  1562. */
  1563. @Transactional(rollbackFor = {Exception.class})
  1564. public ResponseResultVO<?> saleOrderOutboundCancel(OutboundVO outboundVO) {
  1565. //region 查询出库总单数据信息
  1566. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1567. //endregion
  1568. //region根据总单id查明细
  1569. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1570. //endregion
  1571. //region 应收反记账
  1572. if (outboundResponse.getReceivableId() != null) {
  1573. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1574. }
  1575. //endregion
  1576. //region 修改订单数据信息
  1577. this.updateOrderMessageByCancel(outboundItemResponseList);
  1578. //endregion
  1579. //region 修改总单数据信息
  1580. Outbound outbound = new Outbound();
  1581. outbound.setOutId(outboundResponse.getOutId());
  1582. outbound.setOutDate(null);
  1583. outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  1584. outbound.setOutQty(BigDecimal.ZERO);
  1585. outbound.setOutAmt(BigDecimal.ZERO);
  1586. outbound.setOutingQty(outboundResponse.getOutingQty().add(outboundResponse.getOutQty()));
  1587. outbound.setOutingAmt(outboundResponse.getOutingAmt().add(outboundResponse.getOutAmt()));
  1588. //修改
  1589. outboundMapper.update(outbound,
  1590. new UpdateWrapper<Outbound>().lambda()
  1591. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1592. );
  1593. //endregion
  1594. //region 明细数据
  1595. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1596. //region 修改销售订单明细数据信息
  1597. if (outboundItemResponse.getFromItemId() != null) {
  1598. //根据id查询
  1599. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1600. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1601. OrderItem orderItem = new OrderItem();
  1602. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1603. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  1604. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  1605. orderItem.setOutingQty(outboundItemResponse.getOutQty());
  1606. orderItem.setOutingAmt(outboundItemResponse.getOutAmt());
  1607. //出库状态
  1608. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  1609. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1610. orderItem.setOutStatus(outStatus);
  1611. //修改
  1612. int countRow = orderItemMapper.updateById(orderItem);
  1613. //数量超出
  1614. if (countRow == 0) {
  1615. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1616. }
  1617. }
  1618. //endregion
  1619. //region修改出库明细信息
  1620. OutboundItem outboundItem = new OutboundItem();
  1621. //region 将库存需要的参数赋值
  1622. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1623. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1624. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1625. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1626. //endregion
  1627. outboundItem
  1628. .setOutId(outboundResponse.getOutId())
  1629. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  1630. .setOutingQty(outboundItemResponse.getOutingQty().add(outboundItemResponse.getOutQty()))
  1631. .setOutAmt(outboundItemResponse.getOutingAmt().add(outboundItemResponse.getOutAmt()))
  1632. .setOutQty(BigDecimal.ZERO)
  1633. .setOutAmt(BigDecimal.ZERO)
  1634. .setCostPrice(BigDecimal.ZERO)
  1635. .setCostAmt(BigDecimal.ZERO)
  1636. .setItemId(outboundItemResponse.getItemId());
  1637. //修改
  1638. outboundItemMapper.update(outboundItem,
  1639. new UpdateWrapper<OutboundItem>().lambda()
  1640. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1641. );
  1642. //endregion
  1643. }
  1644. //endregion
  1645. //region 外协品新建外协入库单
  1646. //筛选出skuId为空的 走外协品逻辑
  1647. OutboundVO outsideGoodVO = outboundConvert.convertResToVO(outboundResponse);
  1648. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1649. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  1650. //删除外协品生产外协入库单
  1651. this.deleteOutsideGoodsInto(outsideGoodVO, outsideGoodsVOList);
  1652. //endregion
  1653. //region 调用库存
  1654. //筛选出skuId不为空的 走库存
  1655. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1656. if (invList != null && invList.size() > 0) {
  1657. Map<String, Object> map = new HashMap<>();
  1658. map.put("delOutDetail", invList);
  1659. inventoryService.operatingInventoryInformation(map);
  1660. }
  1661. //endregion
  1662. return ResponseResultUtil.success();
  1663. }
  1664. /**
  1665. * @desc : 撤销销售出库修改销售订单信息
  1666. * @date : 2024/6/21 16:00
  1667. * @author : 寇珊珊
  1668. */
  1669. @Transactional(rollbackFor = {Exception.class})
  1670. public void updateOrderMessageByCancel(List<OutboundItemResponse> itemList) {
  1671. //出库明细汇总
  1672. Map<String, List<OutboundItemResponse>> orderOutItemListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemResponse::getFromId));
  1673. //出库明细汇总循环
  1674. for (String fromId : orderOutItemListMap.keySet()) {
  1675. //region 提取新建分组后的明细
  1676. List<OutboundItemResponse> orderOutItemListGroup = orderOutItemListMap.get(fromId);
  1677. //分组后的明细 出库数量和金额总和
  1678. BigDecimal orderSumQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1679. BigDecimal orderSumAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1680. //endregion
  1681. //region 反写订单数据
  1682. //根据id查询
  1683. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1684. Order order = new Order();
  1685. order.setOrderId(fromId);
  1686. order.setOutingQty(orderSumQty);
  1687. order.setOutingAmt(orderSumAmt);
  1688. order.setOutQty(orderSumAmt.negate());
  1689. order.setOutAmt(orderSumAmt.negate());
  1690. //出库状态
  1691. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1692. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1693. order.setOutStatus(outStatus);
  1694. //修改
  1695. int countRow = orderMapper.updateById(order);
  1696. //数量超出
  1697. if (countRow == 0) {
  1698. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1699. }
  1700. //endregion
  1701. }
  1702. }
  1703. /**
  1704. * @desc : 出库状态通用(目前本页面)
  1705. * @date : 2024/3/9 8:59
  1706. * @author : 寇珊珊
  1707. */
  1708. @Transactional(rollbackFor = {Exception.class})
  1709. public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
  1710. //出库状态
  1711. String outStatus = null;
  1712. //已出库数量>0 出库中数量>0
  1713. if (outQty.compareTo(BigDecimal.ZERO) >= 0 && outingQty.compareTo(BigDecimal.ZERO) > 0) {
  1714. //出库中
  1715. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1716. }
  1717. //出入库数量=0 出库中数量=0
  1718. else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1719. //待出库
  1720. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1721. }
  1722. //已出库数量>0 出库中数量=0
  1723. else if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1724. //已出库
  1725. outStatus = Constant.OutStatus.YICHUKU.getName();
  1726. }
  1727. return outStatus;
  1728. }
  1729. /**
  1730. * @desc : 上游单据入库状态通用(目前本页面)
  1731. * @date : 2024/4/1 17:14
  1732. * @author : 寇珊珊
  1733. */
  1734. @Transactional(rollbackFor = {Exception.class})
  1735. public String setOutStatus(BigDecimal intoingQty, BigDecimal intoQty, BigDecimal sumQty) {
  1736. //出库状态
  1737. String outStatus = null;
  1738. //出库中+已出库 小于 总数 或者 出库中数量小于等于订单总数量
  1739. if (intoingQty.compareTo(sumQty) <= 0 && intoingQty.add(intoQty).compareTo(sumQty) <= 0 && intoQty.compareTo(sumQty) < 0) {
  1740. //出库中
  1741. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1742. }
  1743. //已出库数量=0 出库中数量=0
  1744. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  1745. //待出库
  1746. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1747. }
  1748. //出库中+已出库 等于 总数 并且 出库中数量小于等于订单总数量
  1749. else if (intoingQty.compareTo(sumQty) < 0 && intoingQty.add(intoQty).compareTo(sumQty) == 0) {
  1750. //已出库
  1751. outStatus = Constant.OutStatus.YICHUKU.getName();
  1752. }
  1753. return outStatus;
  1754. }
  1755. /**
  1756. * @desc : 获取单据信息(编辑用)
  1757. * @date : 2024/3/16 16:28
  1758. * @author : 寇珊珊
  1759. */
  1760. public ResponseResultVO<?> selectByUpdate(String id) {
  1761. Map<String, Object> dataInfo = new HashMap<>();
  1762. //总单
  1763. OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
  1764. //单据不存在
  1765. if (outboundResponse == null) {
  1766. throw new BaseBusinessException(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
  1767. ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
  1768. }
  1769. dataInfo.put("data", outboundResponse);
  1770. // 明细
  1771. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1772. dataInfo.put("dataItem", outboundItemResponseList);
  1773. return ResponseResultUtil.success(dataInfo);
  1774. }
  1775. /********************************************** 销售出库查询相关方法begin *************************************/
  1776. /**
  1777. * @desc : 一览页销售出库
  1778. * @author : 付斌
  1779. * @date : 2023/1/9 10:40
  1780. */
  1781. @Pagination
  1782. public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1783. outboundQuery.setOutType(Constant.OutType.SALE.getName());
  1784. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1785. outboundMapper.countByCond(outboundQuery));
  1786. }
  1787. /**
  1788. * @desc : 一览页销售出库明细(货物、附件)
  1789. * @author : 付斌
  1790. * @date : 2024-02-28 13:25
  1791. */
  1792. @Pagination
  1793. public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
  1794. Map<String, Object> result = new HashMap<>();
  1795. // 商品明细
  1796. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1797. result.put("outboundItem", outboundItem);
  1798. // 附件
  1799. return ResponseResultUtil.success(result);
  1800. }
  1801. /**
  1802. * @desc : 获取销售出库信息(编辑用)
  1803. * @author : 付斌
  1804. * @date : 2024-03-02 17:27
  1805. */
  1806. public ResponseResultVO<?> getOutboundForUpdate(String id) {
  1807. Map<String, Object> dataInfo = new HashMap<>();
  1808. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1809. dataInfo.put("data", outboundResponse);
  1810. // 商品明细
  1811. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEdit(id);
  1812. dataInfo.put("dataItem", outboundItemResponseList);
  1813. return ResponseResultUtil.success(dataInfo);
  1814. }
  1815. /**
  1816. * @desc : 获取销售出库信息(编辑用,适用于直接新建的出库单)
  1817. * @author : 付斌
  1818. * @date : 2024-03-02 17:27
  1819. */
  1820. public ResponseResultVO<?> getOutboundTogetherForUpdate(String id) {
  1821. Map<String, Object> dataInfo = new HashMap<>();
  1822. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1823. dataInfo.put("data", outboundResponse);
  1824. // 商品明细
  1825. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEditTogether(id);
  1826. dataInfo.put("dataItem", outboundItemResponseList);
  1827. return ResponseResultUtil.success(dataInfo);
  1828. }
  1829. /**
  1830. * @desc : 获取出库信息(新建退货用)
  1831. * @author : 付斌
  1832. * @date : 2024-03-02 17:27
  1833. */
  1834. public ResponseResultVO<?> getOutForReturn(String id) {
  1835. OutboundResponse outboundResponse = outboundMapper.selectByIdForReturn(id);
  1836. List<OutboundItemResponse> outboundItemList = outboundItemMapper.selectByCondForReturn(new OutboundItemQuery().setOutId(id));
  1837. if (outboundItemList != null && outboundItemList.size() > 0) {
  1838. // 求和
  1839. OutboundItemResponse sumEntity = outboundItemList.stream().reduce((x, y) -> {
  1840. OutboundItemResponse item = new OutboundItemResponse();
  1841. item.setOutingQty(x.getOutingQty().add(y.getOutingQty()));
  1842. item.setOutingAmt(x.getOutingAmt().add(y.getOutingAmt()));
  1843. return item;
  1844. }).get();
  1845. outboundResponse.setOutingQty(sumEntity.getOutingQty()).setOutingAmt(sumEntity.getOutingAmt());
  1846. }
  1847. Map<String, Object> dataInfo = new HashMap<>();
  1848. dataInfo.put("data", outboundResponse);
  1849. dataInfo.put("dataItem", outboundItemList);
  1850. return ResponseResultUtil.success(dataInfo);
  1851. }
  1852. /**
  1853. * @desc : 查询出库明细(货物、附件)
  1854. * @author : 付斌
  1855. * @date : 2024-02-28 13:25
  1856. */
  1857. @Pagination
  1858. public ResponseResultVO selectById(String id) {
  1859. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1860. // 商品明细
  1861. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1862. outboundResponse.setGoodsList(outboundItem);
  1863. // 附件
  1864. return ResponseResultUtil.success(outboundResponse);
  1865. }
  1866. /********************************************** 销售出库查询相关方法end *************************************/
  1867. // /**
  1868. // * @desc : 条件查询
  1869. // * @date : 2024/3/18 11:20
  1870. // * @author : 寇珊珊
  1871. // */
  1872. // @Pagination
  1873. // public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1874. // return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1875. // outboundMapper.countByCond(outboundQuery));
  1876. // }
  1877. //
  1878. // /**
  1879. // * @desc : 查询明细
  1880. // * @date : 2024/3/15 16:43
  1881. // * @author : 寇珊珊
  1882. // */
  1883. // @Pagination
  1884. // public ResponseResultVO<Map<String, Object>> selectOutBoundSaleOrderItemInfoById(String id) {
  1885. // Map<String, Object> result = new HashMap<>();
  1886. // // 商品明细
  1887. // List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1888. // result.put("itemList", outboundItemResponseList);
  1889. // // 收款
  1890. //
  1891. // // 附件
  1892. // return ResponseResultUtil.success(result);
  1893. // }
  1894. /**
  1895. * @desc : 条件查询(总单带明细)
  1896. * @author : 于继渤
  1897. * @date : 2023/1/9 10:36
  1898. */
  1899. @Pagination
  1900. public ResponseResultVO<PageList<OutboundResponse>> selectByCondDetail(OutboundQuery outboundQuery) {
  1901. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCondDetail(outboundQuery),
  1902. outboundMapper.countByCondDetail(outboundQuery));
  1903. }
  1904. }