OutboundSaleOrderService.java 121 KB

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