OutboundSaleOrderService.java 110 KB

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