OutboundSaleOrderService.java 121 KB

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