OutboundSaleOrderService.java 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  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. if (outboundVO.getFromId() != null) {
  415. //赋值 (这里重写了更新方法,数量在更新方法中有数据库原始数量+本次数量)
  416. Order order = new Order();
  417. order.setOrderId(outboundVO.getFromId());
  418. //根据id查询
  419. OrderResponse orderResponse = orderMapper.selectById(outboundVO.getFromId());
  420. //已出库
  421. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  422. order.setOutingQty(BigDecimal.ZERO);
  423. order.setOutingAmt(BigDecimal.ZERO);
  424. order.setOutQty(outboundVO.getOutQty());
  425. order.setOutAmt(outboundVO.getOutAmt());
  426. }
  427. //出库中
  428. else {
  429. order.setOutingQty(outboundVO.getOutingQty());
  430. order.setOutingAmt(outboundVO.getOutingAmt());
  431. order.setOutQty(BigDecimal.ZERO);
  432. order.setOutAmt(BigDecimal.ZERO);
  433. }
  434. //入库状态
  435. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  436. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  437. order.setOutStatus(outStatus);
  438. //修改
  439. int countRow = orderMapper.updateById(order);
  440. //数量超出
  441. if (countRow == 0) {
  442. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  443. }
  444. }
  445. //endregion
  446. BigDecimal factAmt = BigDecimal.ZERO; //总单的实际金额
  447. //region 明细
  448. //校验明细
  449. if (outboundVO.getItemList().size() == 0) {
  450. throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  451. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  452. }
  453. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  454. outboundItemVO.setItemId(null);
  455. //region 将库存需要的参数赋值
  456. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  457. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  458. outboundItemVO.setAddOrEditFlag(true);
  459. //endregion
  460. //总单id
  461. outboundItemVO.setOutId(outboundVO.getOutId());
  462. //出库类型
  463. outboundItemVO.setOutType(outboundVO.getOutType());
  464. //出库状态等于已出库 更新合计出库数量/金额 = 出库中数量/出库中金额
  465. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  466. outboundItemVO
  467. .setOutQty(outboundItemVO.getOutingQty())
  468. .setOutAmt(outboundItemVO.getOutingAmt())
  469. .setOutingQty(BigDecimal.ZERO)
  470. .setOutingAmt(BigDecimal.ZERO)
  471. .setCostPrice(outboundItemVO.getPriceOut())
  472. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP)) ;
  473. } else {
  474. outboundItemVO
  475. .setOutQty(BigDecimal.ZERO)
  476. .setOutAmt(BigDecimal.ZERO);
  477. }
  478. factAmt = factAmt.add(outboundItemVO.getFactAmt()); //把明细的 实际金额相加就是总单的
  479. //出库状态
  480. outboundItemVO.setOutStatus(outboundVO.getOutStatus());
  481. //实体转换
  482. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  483. outboundItemMapper.insert(outboundItem);
  484. //明细id
  485. outboundItemVO.setItemId(outboundItem.getItemId());
  486. //endregion
  487. //region 销售明细
  488. if (outboundItemVO.getFromItemId() != null) {
  489. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  490. OrderItem orderItem = new OrderItem();
  491. orderItem.setItemId(outboundItemVO.getFromItemId());
  492. //根据id查询
  493. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  494. //已出库
  495. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  496. orderItem.setOutingQty(BigDecimal.ZERO);
  497. orderItem.setOutingAmt(BigDecimal.ZERO);
  498. orderItem.setOutQty(outboundItemVO.getOutQty());
  499. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  500. }
  501. //出库中
  502. else {
  503. orderItem.setOutingQty(outboundItemVO.getOutingQty());
  504. orderItem.setOutingAmt(outboundItemVO.getOutingAmt());
  505. orderItem.setOutQty(BigDecimal.ZERO);
  506. orderItem.setOutAmt(BigDecimal.ZERO);
  507. }
  508. //出库状态
  509. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  510. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  511. orderItem.setOutStatus(outStatus);
  512. //修改
  513. int countRow = orderItemMapper.updateById(orderItem);
  514. //数量超出
  515. if (countRow == 0) {
  516. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  517. }
  518. }
  519. //endregion
  520. }
  521. //endregion
  522. Outbound outboundup = new Outbound();
  523. outboundup.setOutId(outbound.getOutId()).setFactAmt(factAmt);
  524. //反写总单实际金额
  525. outboundMapper.update(outboundup,
  526. new UpdateWrapper<Outbound>().lambda()
  527. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  528. );
  529. //region 已出库 应收记账
  530. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  531. accountService.accReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  532. }
  533. //endregion
  534. //region 外协品+库存
  535. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  536. //region 外协品新建外协入库单
  537. //筛选出skuId为空的 走外协品逻辑
  538. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  539. for (int i = 0; i < outsideGoods.size(); i++) {
  540. outsideGoods.get(i).setPriceOut(outsideGoods.get(i).getCostPrice() == null ? BigDecimal.ZERO : outsideGoods.get(i).getCostPrice());
  541. }
  542. //删除外协品生产外协入库单
  543. this.deleteOutsideGoodsInto(outboundVO, outsideGoods);
  544. //新建外协品生产外协入库单
  545. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  546. //endregion
  547. //region 库存
  548. //筛选出skuId不为空的 走库存
  549. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  550. if (invList != null && invList.size() > 0) {
  551. Map<String, Object> invMap = new HashMap<>();
  552. invMap.put("outDetail", invList);
  553. inventoryService.operatingInventoryInformation(invMap);
  554. }
  555. //endregion
  556. }
  557. //endregion
  558. return ResponseResultUtil.success(outboundVO);
  559. }
  560. /**
  561. * @desc : 销售出库编辑
  562. * @date : 2024/3/25 16:25
  563. * @author : 寇珊珊
  564. */
  565. @Transactional(rollbackFor = {Exception.class})
  566. public ResponseResultVO<?> saleOutboundUpdate(OutboundVO outboundVO) {
  567. //region 小编辑
  568. if (!outboundVO.getLimitEdit()) {
  569. Outbound outbound = new Outbound();
  570. outbound.setOutId(outboundVO.getOutId());
  571. outbound.setRemarks(outboundVO.getRemarks());
  572. outbound.setAnnexPaths(outboundVO.getAnnexPaths());
  573. outboundMapper.update(outbound,
  574. new UpdateWrapper<Outbound>().lambda()
  575. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  576. );
  577. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  578. OutboundItem outboundItem = new OutboundItem();
  579. outboundItem.setItemId(outboundItemVO.getItemId());
  580. if (outboundItemVO.getRemarks() != null || outboundItemVO.getRemarks() != " ") {
  581. outboundItem.setRemarks(outboundItemVO.getRemarks());
  582. outboundItemMapper.update(outboundItem,
  583. new UpdateWrapper<OutboundItem>().lambda()
  584. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  585. );
  586. }
  587. }
  588. }
  589. //endregion
  590. //region 大编辑
  591. else {
  592. // region 更新客户信息
  593. // outboundVO = outCommon.insertCustomer(outboundVO);
  594. // endregion
  595. //region 根据id查询 并且明细数量金额 求和
  596. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  597. //根据总单id查询
  598. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  599. BigDecimal sumQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  600. BigDecimal sumAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  601. //endregion
  602. //region 已入库编辑
  603. if (Constant.OutStatus.YICHUKU.getName().equals(outboundVO.getOutStatus())) {
  604. //region 应收反记账
  605. if (outboundVO.getReceivableId() != null) {
  606. accountService.reverseReceivable(outboundVO.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  607. }
  608. //endregion
  609. //region 修改明细
  610. List<OutboundItemVO> itemList = outboundVO.getItemList();
  611. for (OutboundItemVO outboundItemVO : itemList) {
  612. //根据id查询
  613. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  614. //region 反写订单总单 数据需要 把查出来的已出库数量用别的变量存起来
  615. outboundItemVO.setUpdateOrderOutQty(outboundItemResponse.getOutQty());
  616. outboundItemVO.setUpdateOrderOutAmt(outboundItemResponse.getOutAmt());
  617. //endregion
  618. //region 编辑明细
  619. if (outboundItemVO.getItemId() != null) {
  620. //region 将库存需要的参数赋值
  621. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  622. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  623. //编辑之前的数
  624. outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
  625. outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
  626. //编辑之后的数
  627. outboundItemVO.setOutQty(outboundItemVO.getOutingQty());
  628. outboundItemVO.setOutAmt(outboundItemVO.getOutingAmt());
  629. outboundItemVO.setAddOrEditFlag(false);
  630. //endregion
  631. OutboundItem outboundItem = new OutboundItem();
  632. outboundItem.setItemId(outboundItemVO.getItemId());
  633. outboundItem.setOutQty(outboundItemVO.getOutingQty());
  634. outboundItem.setOutAmt(outboundItemVO.getOutingAmt());
  635. outboundItem.setCostPrice(outboundItemVO.getPriceOut());
  636. outboundItem.setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(6, BigDecimal.ROUND_HALF_UP));
  637. //修改
  638. outboundItemMapper.update(outboundItem,
  639. new UpdateWrapper<OutboundItem>().lambda()
  640. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  641. );
  642. }
  643. //endregion
  644. //region 新建明细
  645. else {
  646. outboundItemVO
  647. .setOutId(outboundItemVO.getOutId())
  648. .setOutQty(outboundItemVO.getOutingQty())
  649. .setOutAmt(outboundItemVO.getOutingAmt())
  650. .setOutingQty(BigDecimal.ZERO)
  651. .setOutingAmt(BigDecimal.ZERO)
  652. .setCostPrice(outboundItemVO.getPriceOut())
  653. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  654. .setOutType(Constant.OutType.SALE.getName())
  655. ;
  656. //出库状态
  657. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  658. outboundItemVO.setOutStatus(outStatus);
  659. //实体转换
  660. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  661. //新建
  662. outboundItemMapper.insert(outboundItem);
  663. outboundItemVO.setItemId(outboundItem.getItemId());
  664. //region 将库存需要的参数赋值
  665. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  666. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  667. outboundItemVO.setAddOrEditFlag(true);
  668. //endregion
  669. }
  670. //endregion
  671. //region 销售明细
  672. if (outboundItemVO.getFromItemId() != null) {
  673. //根据id查询
  674. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  675. OrderItem orderItem = new OrderItem();
  676. orderItem.setItemId(outboundItemVO.getFromItemId());
  677. orderItem.setOutQty(outboundItemResponse.getOutQty().negate().add(outboundItemVO.getOutingQty()));
  678. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate().add(outboundItemVO.getOutingAmt()));
  679. orderItem.setOutingQty(outboundItemVO.getOutQty());
  680. orderItem.setOutingAmt(outboundItemVO.getOutAmt());
  681. //出库状态
  682. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  683. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  684. orderItem.setOutStatus(outStatus);
  685. int countRow = orderItemMapper.updateById(orderItem);
  686. //数量超出
  687. if (countRow == 0) {
  688. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  689. }
  690. }
  691. //endregion
  692. }
  693. //endregion
  694. //region 删除明细
  695. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  696. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  697. if (outboundItemVO.getItemId() != null) {
  698. //region 将库存需要的参数赋值
  699. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  700. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  701. //endregion
  702. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  703. outboundItem.setFlgValid(false);
  704. //修改
  705. outboundItemMapper.update(outboundItem,
  706. new UpdateWrapper<OutboundItem>().lambda()
  707. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  708. );
  709. }
  710. //region 销售明细
  711. if (outboundItemVO.getFromItemId() != null) {
  712. //根据id查询
  713. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  714. OrderItem orderItem = new OrderItem();
  715. orderItem.setItemId(outboundItemVO.getFromItemId());
  716. orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  717. orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  718. orderItem.setOutingQty(outboundItemVO.getOutQty());
  719. orderItem.setOutingAmt(outboundItemVO.getOutQty());
  720. //出库状态
  721. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  722. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  723. orderItem.setOutStatus(outStatus);
  724. int countRow = orderItemMapper.updateById(orderItem);
  725. //数量超出
  726. if (countRow == 0) {
  727. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  728. }
  729. }
  730. //endregion
  731. }
  732. }
  733. //endregion
  734. //region 修改出库总单
  735. Outbound outbound = new Outbound();
  736. outbound.setOutId(outboundVO.getOutId());
  737. outbound.setOutQty(sumQty);
  738. outbound.setOutAmt(sumAmt);
  739. outbound.setAnnexPaths(outboundVO.getAnnexPaths());
  740. //修改
  741. outboundMapper.update(outbound,
  742. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  743. );
  744. //endregion
  745. //region 修改销售总单
  746. this.updateOrderMessageByEdit(outboundResponse, outboundVO.getItemList(), outboundVO.getDeleteItemList());
  747. //endregion
  748. //region 应收记账
  749. if (outboundVO.getReceivableId() != null) {
  750. accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  751. }
  752. //endregion
  753. //region 外协品新建外协入库单
  754. //筛选出skuId为空的 走外协品逻辑
  755. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  756. //删除外协品生产外协入库单
  757. this.deleteOutsideGoodsInto(outboundVO, outsideGoods);
  758. //新建外协品生产外协入库单
  759. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  760. if (outboundVO.getDeleteItemList() != null) {
  761. List<OutboundItemVO> delOutsideGoods = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  762. //删除外协品生产外协入库单
  763. this.deleteOutsideGoodsInto(outboundVO, delOutsideGoods);
  764. }
  765. //endregion
  766. //region 修改库存
  767. //筛选出skuId不为空的 走库存
  768. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  769. List<OutboundItemVO> invDelList = null;
  770. if (outboundVO.getDeleteItemList() != null) {
  771. invDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  772. }
  773. if ((invList != null && invList.size() > 0) || (invDelList != null && invDelList.size() > 0)) {
  774. Map<String, Object> invMap = new HashMap<>();
  775. invMap.put("outDetail", invList);
  776. invMap.put("delOutDetail", invDelList);
  777. inventoryService.operatingInventoryInformation(invMap);
  778. }
  779. //endregion
  780. }
  781. //endregion
  782. //region 入库中编辑
  783. if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundVO.getOutStatus()) ||
  784. Constant.OutStatus.DAICHUKU.getName().equals(outboundVO.getOutStatus())) {
  785. //region 修改明细
  786. List<OutboundItemVO> itemList = outboundVO.getItemList();
  787. for (OutboundItemVO outboundItemVO : itemList) {
  788. //根据id查询 获取到还未进行修改的数据
  789. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  790. //region 编辑明细
  791. if (outboundItemVO.getItemId() != null) {
  792. //region 反写订单总单 数据需要 把查出来的出库中数量用别的变量存起来
  793. outboundItemVO.setUpdateOrderOutingQty(outboundItemResponse.getOutingQty());
  794. outboundItemVO.setUpdateOrderOutingAmt(outboundItemResponse.getOutingAmt());
  795. //endregion
  796. OutboundItem outboundItem = new OutboundItem();
  797. outboundItem.setItemId(outboundItemResponse.getItemId());
  798. outboundItem.setOutingQty(outboundItemVO.getOutingQty());
  799. outboundItem.setOutingAmt(outboundItemVO.getOutingAmt());
  800. outboundItem.setWhId(outboundItemVO.getWhId());
  801. //修改
  802. outboundItemMapper.update(outboundItem,
  803. new UpdateWrapper<OutboundItem>().lambda()
  804. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  805. );
  806. }
  807. //endregion
  808. //region 新建明细
  809. else {
  810. //region 反写订单总单 数据需要 把查出来的出库中数量用别的变量存起来
  811. outboundItemVO.setUpdateOrderOutingQty(outboundItemVO.getOutingQty());
  812. outboundItemVO.setUpdateOrderOutingAmt(outboundItemVO.getOutingAmt());
  813. //endregion
  814. outboundItemVO
  815. .setOutId(outboundItemVO.getOutId())
  816. .setOutQty(BigDecimal.ZERO)
  817. .setOutAmt(BigDecimal.ZERO)
  818. .setCostPrice(outboundItemVO.getPriceOut())
  819. .setCostAmt(outboundItemVO.getOutingQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  820. .setOutType(Constant.OutType.SALE.getName())
  821. ;
  822. //出库状态
  823. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  824. outboundItemVO.setOutStatus(outStatus);
  825. //实体转换
  826. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  827. //新建
  828. outboundItemMapper.insert(outboundItem);
  829. outboundItemVO.setItemId(outboundItem.getItemId());
  830. }
  831. //endregion
  832. //region 销售明细
  833. if (outboundItemVO.getFromItemId() != null) {
  834. //根据id查询
  835. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  836. OrderItem orderItem = new OrderItem();
  837. orderItem.setItemId(outboundItemVO.getFromItemId());
  838. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate().add(outboundItemVO.getOutingQty()));
  839. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate().add(outboundItemVO.getOutingAmt()));
  840. //出库状态
  841. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  842. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  843. orderItem.setOutStatus(outStatus);
  844. int countRow = orderItemMapper.updateById(orderItem);
  845. //数量超出
  846. if (countRow == 0) {
  847. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  848. }
  849. }
  850. //endregion
  851. }
  852. //endregion
  853. //region 删除明细
  854. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  855. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  856. if (outboundItemVO.getItemId() != null) {
  857. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  858. outboundItem.setFlgValid(false);
  859. //修改
  860. outboundItemMapper.update(outboundItem,
  861. new UpdateWrapper<OutboundItem>().lambda()
  862. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  863. );
  864. }
  865. //region 销售明细
  866. if (outboundItemVO.getFromItemId() != null) {
  867. //根据id查询
  868. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  869. OrderItem orderItem = new OrderItem();
  870. orderItem.setItemId(outboundItemVO.getFromItemId());
  871. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  872. orderItem.setOutingAmt(outboundItemVO.getOutQty().negate());
  873. //出库状态
  874. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  875. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  876. orderItem.setOutStatus(outStatus);
  877. int countRow = orderItemMapper.updateById(orderItem);
  878. //数量超出
  879. if (countRow == 0) {
  880. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  881. }
  882. }
  883. //endregion
  884. }
  885. }
  886. //endregion
  887. //region 修改入库总单
  888. Outbound outbound = new Outbound();
  889. outbound.setOutId(outboundVO.getOutId());
  890. outbound.setOutingQty(sumQty);
  891. outbound.setOutingAmt(sumAmt);
  892. outbound.setAnnexPaths(outboundVO.getAnnexPaths());
  893. //修改
  894. outboundMapper.update(outbound,
  895. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  896. );
  897. //endregion
  898. //region 修改销售总单
  899. this.updateOrderMessageByEdit(outboundResponse, outboundVO.getItemList(), outboundVO.getDeleteItemList());
  900. //endregion
  901. }
  902. //endregion
  903. }
  904. //endregion
  905. return ResponseResultUtil.success(outboundVO);
  906. }
  907. /**
  908. * @desc : 编辑销售出库修改销售订单信息
  909. * @date : 2024/6/21 10:34
  910. * @author : 寇珊珊
  911. */
  912. @Transactional(rollbackFor = {Exception.class})
  913. public void updateOrderMessageByEdit(OutboundResponse outboundResponse, List<OutboundItemVO> itemList, List<OutboundItemVO> deleteItemList) {
  914. //出库明细汇总
  915. Map<String, List<OutboundItemVO>> orderOutItemVOListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  916. //出库明细删除汇总
  917. Map<String, List<OutboundItemVO>> delOrderOutItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  918. //出库明细汇总循环
  919. for (String fromId : orderOutItemVOListMap.keySet()) {
  920. //有需要删除的修改数据
  921. if(delOrderOutItemVOListMap!=null && delOrderOutItemVOListMap.keySet().size()>0){
  922. //出库明细删除汇总循环
  923. for (String delFromId : delOrderOutItemVOListMap.keySet()) {
  924. //新建出库明细来源Id = 删除出库明细来源Id
  925. if (fromId.equals(delFromId)) {
  926. //region 提取新建分组后的明细
  927. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  928. //分组后的明细 出库数量和金额总和
  929. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  930. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  931. //分组后的明细 原出库数量和金额总和
  932. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  933. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  934. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  935. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  936. //已出库
  937. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  938. updateOrderOutQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  939. updateOrderOutAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  940. }
  941. //出库中
  942. else {
  943. updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  944. updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  945. }
  946. //endregion
  947. //region 提取删除分组后的明细
  948. List<OutboundItemVO> delOrderOutItemVOListGroup = delOrderOutItemVOListMap.get(delFromId);
  949. BigDecimal orderDelOutQty = BigDecimal.ZERO;
  950. BigDecimal orderDelOutAmt = BigDecimal.ZERO;
  951. //已出库
  952. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  953. orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  954. orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  955. }
  956. //出库中
  957. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  958. orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  959. orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  960. }
  961. //endregion
  962. //region 反写订单数据
  963. //根据id查询
  964. OrderResponse orderResponse = orderMapper.selectById(fromId);
  965. Order order = new Order();
  966. order.setOrderId(fromId);
  967. //已出库
  968. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  969. order.setOutQty(updateOrderOutQty.negate().add(orderSumQty).subtract(orderDelOutQty));
  970. order.setOutAmt(updateOrderOutAmt.negate().add(orderSumAmt).subtract(orderDelOutAmt));
  971. order.setOutingQty(orderDelOutQty);
  972. order.setOutingAmt(orderDelOutAmt);
  973. //出库状态
  974. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  975. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  976. order.setOutStatus(outStatus);
  977. }
  978. //出库中
  979. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  980. order.setOutingQty(updateOrderOutingQty.negate().add(orderSumQty).subtract(orderDelOutQty));
  981. order.setOutingAmt(updateOrderOutingAmt.negate().add(orderSumQty).subtract(orderDelOutAmt));
  982. //出库状态
  983. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  984. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  985. order.setOutStatus(outStatus);
  986. }
  987. //修改
  988. int countRow = orderMapper.updateById(order);
  989. //数量超出
  990. if (countRow == 0) {
  991. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  992. }
  993. //endregion
  994. }
  995. }
  996. }
  997. //没有需要删除的修改数据
  998. else{
  999. //region 提取新建分组后的明细
  1000. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  1001. //分组后的明细 出库数量和金额总和
  1002. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1003. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1004. //分组后的明细 原出库数量和金额总和
  1005. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  1006. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  1007. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  1008. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  1009. //已出库
  1010. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1011. updateOrderOutQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1012. updateOrderOutAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1013. }
  1014. //出库中
  1015. else {
  1016. updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1017. updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1018. }
  1019. //endregion
  1020. //region 反写订单数据
  1021. //根据id查询
  1022. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1023. Order order = new Order();
  1024. order.setOrderId(fromId);
  1025. //已出库
  1026. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1027. order.setOutQty(updateOrderOutQty.negate().add(orderSumQty));
  1028. order.setOutAmt(updateOrderOutAmt.negate().add(orderSumAmt));
  1029. //出库状态
  1030. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1031. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1032. order.setOutStatus(outStatus);
  1033. }
  1034. //出库中
  1035. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  1036. order.setOutingQty(updateOrderOutingQty.negate().add(orderSumQty));
  1037. order.setOutingAmt(updateOrderOutingAmt.negate().add(orderSumQty));
  1038. //出库状态
  1039. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1040. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  1041. order.setOutStatus(outStatus);
  1042. }
  1043. //修改
  1044. int countRow = orderMapper.updateById(order);
  1045. //数量超出
  1046. if (countRow == 0) {
  1047. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1048. }
  1049. //endregion
  1050. }
  1051. }
  1052. }
  1053. /**
  1054. * @desc : 销售出库作废
  1055. * @date : 2024/3/26 9:24
  1056. * @author : 寇珊珊
  1057. */
  1058. @Transactional(rollbackFor = {Exception.class})
  1059. public ResponseResultVO<?> saleOutboundRepeal(String outId) {
  1060. //region 查询总单 查询明细
  1061. //根据id查询 此条出库单的数据还未更改前的数据
  1062. OutboundResponse outboundResponse = outboundMapper.selectById(outId);
  1063. //根据总单id查询
  1064. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1065. //endregion
  1066. //region 已出库状态作废
  1067. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1068. //region 应收反记账
  1069. if (outboundResponse.getReceivableId() != null) {
  1070. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1071. }
  1072. //endregion
  1073. //region 修改明细
  1074. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1075. //region 将库存需要的参数赋值
  1076. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1077. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1078. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty());
  1079. outboundItemResponse.setOutAmt(outboundItemResponse.getOutAmt());
  1080. //endregion
  1081. //赋值 防止作废的单据查不到明细 故注掉下面代码
  1082. // OutboundItem outboundItem = new OutboundItem();
  1083. // outboundItem.setItemId(outboundItemResponse.getItemId());
  1084. // outboundItem.setFlgValid(false);
  1085. // //修改
  1086. // outboundItemMapper.update(outboundItem,
  1087. // new UpdateWrapper<OutboundItem>().lambda()
  1088. // .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1089. // );
  1090. //region 销售明细
  1091. if (outboundItemResponse.getFromItemId() != null) {
  1092. //根据id查询
  1093. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1094. OrderItem orderItem = new OrderItem();
  1095. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1096. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  1097. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  1098. //出库状态
  1099. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty(),
  1100. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1101. orderItem.setOutStatus(orderOutStatus);
  1102. int countRow = orderItemMapper.updateById(orderItem);
  1103. //数量超出
  1104. if (countRow == 0) {
  1105. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1106. }
  1107. }
  1108. //endregion
  1109. }
  1110. //endregion
  1111. //region 修改出库总单
  1112. Outbound outbound = new Outbound();
  1113. outbound.setOutId(outboundResponse.getOutId());
  1114. outbound.setFlgValid(false);
  1115. //修改
  1116. outboundMapper.update(outbound,
  1117. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1118. );
  1119. //endregion
  1120. //region 修改销售总单
  1121. this.updateOrderMessageByRepeal(outboundResponse, outboundItemResponseList);
  1122. //endregion
  1123. //region 外协品新建外协入库单
  1124. //筛选出skuId为空的 走外协品逻辑
  1125. OutboundVO outboundVO = outboundConvert.convertResToVO(outboundResponse);
  1126. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1127. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  1128. //删除外协品生产外协入库单
  1129. this.deleteOutsideGoodsInto(outboundVO, outsideGoodsVOList);
  1130. //endregion
  1131. //region 修改库存
  1132. //筛选出skuId不为空的 走库存
  1133. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1134. if (invList != null && invList.size() > 0) {
  1135. Map<String, Object> map = new HashMap<>();
  1136. map.put("delOutDetail", invList);
  1137. inventoryService.operatingInventoryInformation(map);
  1138. }
  1139. //endregion
  1140. }
  1141. //endregion
  1142. //region 出库中、待出库状态作废
  1143. if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus()) ||
  1144. Constant.OutStatus.DAICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1145. //region 修改明细
  1146. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1147. //region 修改明细
  1148. //赋值
  1149. OutboundItem outboundItem = new OutboundItem();
  1150. outboundItem.setItemId(outboundItemResponse.getItemId());
  1151. outboundItem.setOutingQty(BigDecimal.ZERO);
  1152. outboundItem.setOutingAmt(BigDecimal.ZERO);
  1153. // outboundItem.setFlgValid(false);
  1154. //修改
  1155. outboundItemMapper.update(outboundItem,
  1156. new UpdateWrapper<OutboundItem>().lambda()
  1157. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1158. );
  1159. //endregion
  1160. //region 销售明细
  1161. if (outboundItemResponse.getFromItemId() != null) {
  1162. //根据id查询
  1163. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1164. OrderItem orderItem = new OrderItem();
  1165. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1166. orderItem.setOutingQty(outboundItemResponse.getOutingQty().negate());
  1167. orderItem.setOutingAmt(outboundItemResponse.getOutingAmt().negate());
  1168. //出库状态
  1169. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  1170. orderItemResponse.getOutQty(), orderItemResponse.getItemQty());
  1171. orderItem.setOutStatus(orderOutStatus);
  1172. int countRow = orderItemMapper.updateById(orderItem);
  1173. //数量超出
  1174. if (countRow == 0) {
  1175. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1176. }
  1177. }
  1178. //endregion
  1179. }
  1180. //endregion
  1181. //region 修改出库总单
  1182. Outbound outbound = new Outbound();
  1183. outbound.setOutId(outboundResponse.getOutId());
  1184. outbound.setOutingQty(BigDecimal.ZERO);
  1185. outbound.setOutingAmt(BigDecimal.ZERO);
  1186. outbound.setFlgValid(false);
  1187. //修改
  1188. outboundMapper.update(outbound,
  1189. new UpdateWrapper<Outbound>().lambda().eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1190. );
  1191. //endregion
  1192. //region 修改销售总单
  1193. this.updateOrderMessageByRepeal(outboundResponse, outboundItemResponseList);
  1194. //endregion
  1195. }
  1196. //endregion
  1197. return ResponseResultUtil.success();
  1198. }
  1199. /**
  1200. * @desc : 作废销售出库修改销售订单信息
  1201. * @date : 2024/6/21 10:34
  1202. * @author : 寇珊珊
  1203. */
  1204. @Transactional(rollbackFor = {Exception.class})
  1205. public void updateOrderMessageByRepeal(OutboundResponse outboundResponse, List<OutboundItemResponse> itemList) {
  1206. //出库明细汇总
  1207. Map<String, List<OutboundItemResponse>> orderOutItemListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemResponse::getFromId));
  1208. //出库明细汇总循环
  1209. for (String fromId : orderOutItemListMap.keySet()) {
  1210. //region 提取新建分组后的明细
  1211. //分组后的明细 原出库数量和金额总和
  1212. List<OutboundItemResponse> orderOutItemListGroup = orderOutItemListMap.get(fromId);
  1213. BigDecimal updateOrderOutQty = BigDecimal.ZERO;
  1214. BigDecimal updateOrderOutAmt = BigDecimal.ZERO;
  1215. BigDecimal updateOrderOutingQty = BigDecimal.ZERO;
  1216. BigDecimal updateOrderOutingAmt = BigDecimal.ZERO;
  1217. //已出库
  1218. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1219. updateOrderOutQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1220. updateOrderOutAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1221. }
  1222. //出库中
  1223. else {
  1224. updateOrderOutingQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1225. updateOrderOutingAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1226. }
  1227. //endregion
  1228. //region 反写订单数据
  1229. //根据id查询
  1230. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1231. Order order = new Order();
  1232. order.setOrderId(fromId);
  1233. //已出库
  1234. if (Constant.OutStatus.YICHUKU.getName().equals(outboundResponse.getOutStatus())) {
  1235. order.setOutQty(updateOrderOutQty.negate());
  1236. order.setOutAmt(updateOrderOutAmt.negate());
  1237. //出库状态
  1238. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty(),
  1239. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1240. order.setOutStatus(orderOutStatus);
  1241. }
  1242. //出库中
  1243. else if (Constant.OutStatus.CHUKUZHONG.getName().equals(outboundResponse.getOutStatus())) {
  1244. order.setOutingQty(updateOrderOutingQty.negate());
  1245. order.setOutingAmt(updateOrderOutingAmt.negate());
  1246. //出库状态
  1247. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1248. orderResponse.getOutQty(), orderResponse.getSumQuantity());
  1249. order.setOutStatus(orderOutStatus);
  1250. }
  1251. //修改
  1252. int countRow = orderMapper.updateById(order);
  1253. //数量超出
  1254. if (countRow == 0) {
  1255. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1256. }
  1257. //endregion
  1258. }
  1259. }
  1260. /**
  1261. * @desc : 销售出库办理
  1262. * @date : 2024/3/7 15:47
  1263. * @author : 寇珊珊
  1264. */
  1265. @Transactional(rollbackFor = {Exception.class})
  1266. public ResponseResultVO<?> saleOrderHandleOutbound(OutboundVO outboundVO) {
  1267. //region 根据id查询 此条入库单的数据还未更改前的数据
  1268. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1269. //endregion
  1270. //region 应收反记账
  1271. if (outboundResponse.getReceivableId() != null) {
  1272. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1273. }
  1274. //endregion
  1275. //region 编辑明细
  1276. //校验明细
  1277. if (outboundVO.getItemList().size() == 0) {
  1278. throw new BaseBusinessException(ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getCode(),
  1279. ErrorCodeEnum.OUTBOUND_ITEM_NOT_EXIST.getMessage());
  1280. }
  1281. for (OutboundItemVO outboundItemVO : outboundVO.getItemList()) {
  1282. //明细根据id查询
  1283. OutboundItemResponse outboundItemResponse = outboundItemMapper.selectById(outboundItemVO.getItemId());
  1284. //region 校验数量是否超出
  1285. if (outboundItemVO.getOutingQty().compareTo(outboundItemResponse.getOutingQty()) > 0) {
  1286. throw new BaseBusinessException(ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getCode(),
  1287. ErrorCodeEnum.CANNOT_EXCEED_THE_QUANTITYIN_THE_WAREHOUSE.getMessage());
  1288. }
  1289. //endregion
  1290. //region 反写订单总单 数据需要 把查出来的出库中数量用别的变量存起来
  1291. outboundItemVO.setUpdateOrderOutingQty(outboundItemResponse.getOutingQty());
  1292. outboundItemVO.setUpdateOrderOutingAmt(outboundItemResponse.getOutingAmt());
  1293. //endregion
  1294. //region 编辑明细
  1295. if (outboundItemVO.getItemId() != null) {
  1296. outboundItemVO
  1297. .setOutQty(outboundItemVO.getOutingQty())
  1298. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1299. .setOutAmt(outboundItemVO.getOutingAmt())
  1300. .setOutingQty(BigDecimal.ZERO).setOutingBox(0).setOutingPiece(BigDecimal.ZERO)
  1301. .setOutingAmt(BigDecimal.ZERO)
  1302. .setCostPrice(outboundItemVO.getPriceOut())
  1303. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1304. ;
  1305. //出库状态
  1306. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1307. outboundItemVO.setOutStatus(outStatus);
  1308. //实体转换
  1309. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1310. //修改
  1311. outboundItemMapper.update(outboundItem,
  1312. new UpdateWrapper<OutboundItem>().lambda()
  1313. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1314. );
  1315. //region 将库存需要的参数赋值
  1316. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1317. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1318. //编辑之前的数
  1319. if (outboundItemResponse.getOutQty().compareTo(BigDecimal.ZERO) > 0) {
  1320. outboundItemVO.setQtyBeforeUpdate(outboundItemResponse.getOutQty());
  1321. outboundItemVO.setAmtBeforeUpdate(outboundItemResponse.getOutAmt());
  1322. }
  1323. outboundItemVO.setAddOrEditFlag(true);
  1324. //endregion
  1325. }
  1326. //endregion
  1327. //region 新建明细
  1328. else {
  1329. outboundItemVO
  1330. .setOutId(outboundItemVO.getOutId())
  1331. .setOutQty(outboundItemVO.getOutingQty())
  1332. .setOutBox(outboundItemVO.getOutingBox()).setOutPiece(outboundItemVO.getOutingPiece())
  1333. .setOutAmt(outboundItemVO.getOutingAmt())
  1334. .setCostPrice(outboundItemVO.getPriceOut())
  1335. .setCostAmt(outboundItemVO.getOutQty().multiply(outboundItemVO.getPriceOut()).setScale(2, BigDecimal.ROUND_HALF_UP))
  1336. .setOutType(Constant.OutType.SALE.getName())
  1337. .setOutingQty(BigDecimal.ZERO)
  1338. .setOutingAmt(BigDecimal.ZERO)
  1339. ;
  1340. //出库状态
  1341. String outStatus = this.setOutStatus(outboundItemVO.getOutingQty(), outboundItemVO.getOutQty());
  1342. outboundItemVO.setOutStatus(outStatus);
  1343. //实体转换
  1344. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1345. //新建
  1346. outboundItemMapper.insert(outboundItem);
  1347. outboundItemVO.setItemId(outboundItem.getItemId());
  1348. //region 将库存需要的参数赋值
  1349. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1350. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1351. outboundItemVO.setAddOrEditFlag(true);
  1352. //endregion
  1353. }
  1354. //endregion
  1355. //region 销售订单明细
  1356. if (outboundItemVO.getFromItemId() != null) {
  1357. //根据id查询
  1358. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1359. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1360. OrderItem orderItem = new OrderItem();
  1361. orderItem.setItemId(outboundItemVO.getFromItemId());
  1362. orderItem.setOutingQty(outboundItemVO.getOutQty().negate());
  1363. orderItem.setOutingAmt(outboundItemVO.getOutAmt().negate());
  1364. orderItem.setOutQty(outboundItemVO.getOutQty());
  1365. orderItem.setOutAmt(outboundItemVO.getOutAmt());
  1366. //出库状态
  1367. String orderOutStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(outboundItemVO.getOutQty()),
  1368. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1369. orderItem.setOutStatus(orderOutStatus);
  1370. //修改
  1371. int countRow = orderItemMapper.updateById(orderItem);
  1372. //数量超出
  1373. if (countRow == 0) {
  1374. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1375. }
  1376. }
  1377. //endregion
  1378. }
  1379. //endregion
  1380. //region 删除明细
  1381. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  1382. for (OutboundItemVO outboundItemVO : outboundVO.getDeleteItemList()) {
  1383. if (outboundItemVO.getItemId() != null) {
  1384. //region 将库存需要的参数赋值
  1385. outboundItemVO.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1386. outboundItemVO.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1387. //endregion
  1388. OutboundItem outboundItem = outboundItemConvert.convertToPo(outboundItemVO);
  1389. outboundItem.setFlgValid(false);
  1390. //修改
  1391. outboundItemMapper.update(outboundItem,
  1392. new UpdateWrapper<OutboundItem>().lambda()
  1393. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1394. );
  1395. }
  1396. //region 销售订单明细
  1397. if (outboundItemVO.getFromItemId() != null) {
  1398. //根据id查询
  1399. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemVO.getFromItemId());
  1400. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1401. OrderItem orderItem = new OrderItem();
  1402. orderItem.setItemId(outboundItemVO.getFromItemId());
  1403. orderItem.setOutingQty(outboundItemVO.getOutingQty().negate());
  1404. orderItem.setOutingAmt(outboundItemVO.getOutingAmt().negate());
  1405. orderItem.setOutQty(outboundItemVO.getOutQty().negate());
  1406. orderItem.setOutAmt(outboundItemVO.getOutAmt().negate());
  1407. //出库状态
  1408. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().subtract(orderItem.getOutingQty()),
  1409. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1410. orderItem.setOutStatus(outStatus);
  1411. //修改
  1412. int countRow = orderItemMapper.updateById(orderItem);
  1413. //数量超出
  1414. if (countRow == 0) {
  1415. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1416. }
  1417. }
  1418. //endregion
  1419. }
  1420. }
  1421. //endregion
  1422. //region 编辑总单
  1423. BigDecimal sumOutQty = outboundVO.getItemList().stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1424. BigDecimal sumOutAmt = outboundVO.getItemList().stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1425. outboundVO.setOutQty(sumOutQty);
  1426. outboundVO.setOutAmt(sumOutAmt);
  1427. outboundVO.setOutingQty(BigDecimal.ZERO);
  1428. outboundVO.setOutingAmt(BigDecimal.ZERO);
  1429. //出库状态
  1430. String outStatus = this.setOutStatus(outboundVO.getOutingQty(), outboundVO.getOutQty());
  1431. outboundVO.setOutStatus(outStatus);
  1432. //实体转换
  1433. Outbound outbound = outboundConvert.convertToPo(outboundVO);
  1434. //修改
  1435. outboundMapper.update(outbound,
  1436. new UpdateWrapper<Outbound>().lambda()
  1437. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1438. );
  1439. //endregion
  1440. //region 修改销售订单订单
  1441. this.updateOrderMessageByHandle(outboundVO.getItemList(), outboundVO.getDeleteItemList());
  1442. //endregion
  1443. //region 应收记账
  1444. accountService.accReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1445. //endregion
  1446. //region 库存明细处理
  1447. if (outboundVO.getItemList() != null && outboundVO.getItemList().size() > 0) {
  1448. Map<String, Object> map = new HashMap<>(); // 定义map值 修改库存用
  1449. // 获取外协数据
  1450. List<OutboundItemVO> outsideGoods = outboundVO.getItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1451. //新建外协品生产外协入库单
  1452. this.insertOutsideGoodsInto(outboundVO, outsideGoods);
  1453. // 获取商品明细数据
  1454. List<OutboundItemVO> invList = outboundVO.getItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1455. // 赋值修改明细
  1456. map.put("outDetail", invList);
  1457. // 删除的商品
  1458. if (outboundVO.getDeleteItemList() != null && outboundVO.getDeleteItemList().size() > 0) {
  1459. // 获取外协删除数据
  1460. List<OutboundItemVO> invsideDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1461. //删除外协品生产外协入库单
  1462. this.deleteOutsideGoodsInto(outboundVO, invsideDelList);
  1463. // 获取删除商品数据
  1464. List<OutboundItemVO> invDelList = outboundVO.getDeleteItemList().stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1465. // 赋值删除明细
  1466. map.put("delOutDetail", invDelList);
  1467. }
  1468. // 调用修改库存信息方法
  1469. inventoryService.operatingInventoryInformation(map);
  1470. }
  1471. //endregion
  1472. return ResponseResultUtil.success(outboundVO);
  1473. }
  1474. /**
  1475. * @desc : 办理销售出库修改销售订单信息
  1476. * @date : 2024/6/21 15:13
  1477. * @author : 寇珊珊
  1478. */
  1479. @Transactional(rollbackFor = {Exception.class})
  1480. public void updateOrderMessageByHandle(List<OutboundItemVO> itemList, List<OutboundItemVO> deleteItemList) {
  1481. //出库明细汇总
  1482. Map<String, List<OutboundItemVO>> orderOutItemVOListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  1483. //出库明细删除汇总
  1484. Map<String, List<OutboundItemVO>> delOrderOutItemVOListMap = deleteItemList.stream().collect(Collectors.groupingBy(OutboundItemVO::getFromId));
  1485. //出库明细汇总循环
  1486. for (String fromId : orderOutItemVOListMap.keySet()) {
  1487. //有需要删除的办理数据
  1488. if (delOrderOutItemVOListMap != null && delOrderOutItemVOListMap.keySet().size() > 0) {
  1489. //出库明细删除汇总循环
  1490. for (String delFromId : delOrderOutItemVOListMap.keySet()) {
  1491. //新建出库明细来源Id = 删除出库明细来源Id
  1492. if (fromId.equals(delFromId)) {
  1493. //region 提取新建分组后的明细
  1494. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  1495. //分组后的明细 出库数量和金额总和
  1496. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1497. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1498. //分组后的明细 原出库数量和金额总和
  1499. BigDecimal updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1500. BigDecimal updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1501. //endregion
  1502. //region 提取删除分组后的明细
  1503. List<OutboundItemVO> delOrderOutItemVOListGroup = delOrderOutItemVOListMap.get(delFromId);
  1504. BigDecimal orderDelOutingQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1505. BigDecimal orderDelOutingAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1506. BigDecimal orderDelOutQty = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1507. BigDecimal orderDelOutAmt = delOrderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1508. //endregion
  1509. //region 反写订单数据
  1510. //根据id查询
  1511. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1512. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1513. Order order = new Order();
  1514. order.setOrderId(fromId);
  1515. order.setOutQty(orderSumQty.subtract(orderDelOutQty));
  1516. order.setOutAmt(orderSumAmt.subtract(orderDelOutAmt));
  1517. order.setOutingQty((updateOrderOutingQty.add(orderDelOutingQty)).negate());
  1518. order.setOutingAmt((updateOrderOutingAmt.add(orderDelOutingAmt)).negate());
  1519. //出库状态
  1520. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().subtract(orderSumQty).add(orderDelOutQty),
  1521. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1522. order.setOutStatus(orderOutStatus);
  1523. //修改
  1524. int countRow = orderMapper.updateById(order);
  1525. //数量超出
  1526. if (countRow == 0) {
  1527. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1528. }
  1529. //endregion
  1530. }
  1531. }
  1532. }
  1533. //没有需要删除的办理数据
  1534. else {
  1535. //region 提取新建分组后的明细
  1536. List<OutboundItemVO> orderOutItemVOListGroup = orderOutItemVOListMap.get(fromId);
  1537. //分组后的明细 出库数量和金额总和
  1538. BigDecimal orderSumQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1539. BigDecimal orderSumAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1540. //分组后的明细 原出库数量和金额总和
  1541. BigDecimal updateOrderOutingQty = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1542. BigDecimal updateOrderOutingAmt = orderOutItemVOListGroup.stream().map(OutboundItemVO::getUpdateOrderOutingAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1543. //endregion
  1544. //region 反写订单数据
  1545. //根据id查询
  1546. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1547. //赋值(这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1548. Order order = new Order();
  1549. order.setOrderId(fromId);
  1550. order.setOutQty(orderSumQty);
  1551. order.setOutAmt(orderSumAmt);
  1552. order.setOutingQty(updateOrderOutingQty.negate());
  1553. order.setOutingAmt(updateOrderOutingAmt.negate());
  1554. //出库状态
  1555. String orderOutStatus = this.setOutStatus(orderResponse.getOutingQty().subtract(orderSumQty),
  1556. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1557. order.setOutStatus(orderOutStatus);
  1558. //修改
  1559. int countRow = orderMapper.updateById(order);
  1560. //数量超出
  1561. if (countRow == 0) {
  1562. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1563. }
  1564. //endregion
  1565. }
  1566. }
  1567. }
  1568. /**
  1569. * @desc : 销售出库撤销
  1570. * @date : 2024/3/7 17:06
  1571. * @author : 寇珊珊
  1572. */
  1573. @Transactional(rollbackFor = {Exception.class})
  1574. public ResponseResultVO<?> saleOrderOutboundCancel(OutboundVO outboundVO) {
  1575. //region 查询出库总单数据信息
  1576. OutboundResponse outboundResponse = outboundMapper.selectById(outboundVO.getOutId());
  1577. //endregion
  1578. //region根据总单id查明细
  1579. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1580. //endregion
  1581. //region 应收反记账
  1582. if (outboundResponse.getReceivableId() != null) {
  1583. accountService.reverseReceivable(outboundResponse.getOutId(), Constant.InventoryDocCode.OUTBOUND.getTableName());
  1584. }
  1585. //endregion
  1586. //region 修改订单数据信息
  1587. this.updateOrderMessageByCancel(outboundItemResponseList);
  1588. //endregion
  1589. //region 修改总单数据信息
  1590. Outbound outbound = new Outbound();
  1591. outbound.setOutId(outboundResponse.getOutId());
  1592. outbound.setOutDate(null);
  1593. outbound.setOutStatus(Constant.OutStatus.CHUKUZHONG.getName());
  1594. outbound.setOutQty(BigDecimal.ZERO);
  1595. outbound.setOutAmt(BigDecimal.ZERO);
  1596. outbound.setOutingQty(outboundResponse.getOutingQty().add(outboundResponse.getOutQty()));
  1597. outbound.setOutingAmt(outboundResponse.getOutingAmt().add(outboundResponse.getOutAmt()));
  1598. //修改
  1599. outboundMapper.update(outbound,
  1600. new UpdateWrapper<Outbound>().lambda()
  1601. .eq(Outbound::getOutId, UUID.fromString(outbound.getOutId()))
  1602. );
  1603. //endregion
  1604. //region 明细数据
  1605. for (OutboundItemResponse outboundItemResponse : outboundItemResponseList) {
  1606. //region 修改销售订单明细数据信息
  1607. if (outboundItemResponse.getFromItemId() != null) {
  1608. //根据id查询
  1609. OrderItemResponse orderItemResponse = orderItemMapper.selectById(outboundItemResponse.getFromItemId());
  1610. //赋值 (这里重写了更新方法,数量在更新方法中有数据库院士数量+本次数量)
  1611. OrderItem orderItem = new OrderItem();
  1612. orderItem.setItemId(outboundItemResponse.getFromItemId());
  1613. orderItem.setOutQty(outboundItemResponse.getOutQty().negate());
  1614. orderItem.setOutAmt(outboundItemResponse.getOutAmt().negate());
  1615. orderItem.setOutingQty(outboundItemResponse.getOutQty());
  1616. orderItem.setOutingAmt(outboundItemResponse.getOutAmt());
  1617. //出库状态
  1618. String outStatus = this.setOutStatus(orderItemResponse.getOutingQty().add(orderItem.getOutingQty()),
  1619. orderItemResponse.getOutQty().add(orderItem.getOutQty()), orderItemResponse.getItemQty());
  1620. orderItem.setOutStatus(outStatus);
  1621. //修改
  1622. int countRow = orderItemMapper.updateById(orderItem);
  1623. //数量超出
  1624. if (countRow == 0) {
  1625. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1626. }
  1627. }
  1628. //endregion
  1629. //region修改出库明细信息
  1630. OutboundItem outboundItem = new OutboundItem();
  1631. //region 将库存需要的参数赋值
  1632. outboundItemResponse.setInventoryType(Constant.InventoryType.OUTBOUND.getName());
  1633. outboundItemResponse.setInventoryDocCode(Constant.InventoryDocCode.SALE_ORDER.getValue());
  1634. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1635. outboundItemResponse.setOutQty(outboundItemResponse.getOutQty().negate());
  1636. //endregion
  1637. outboundItem
  1638. .setOutId(outboundResponse.getOutId())
  1639. .setOutStatus(Constant.OutStatus.CHUKUZHONG.getName())
  1640. .setOutingQty(outboundItemResponse.getOutingQty().add(outboundItemResponse.getOutQty()))
  1641. .setOutAmt(outboundItemResponse.getOutingAmt().add(outboundItemResponse.getOutAmt()))
  1642. .setOutQty(BigDecimal.ZERO)
  1643. .setOutAmt(BigDecimal.ZERO)
  1644. .setCostPrice(BigDecimal.ZERO)
  1645. .setCostAmt(BigDecimal.ZERO)
  1646. .setItemId(outboundItemResponse.getItemId());
  1647. //修改
  1648. outboundItemMapper.update(outboundItem,
  1649. new UpdateWrapper<OutboundItem>().lambda()
  1650. .eq(OutboundItem::getItemId, UUID.fromString(outboundItem.getItemId()))
  1651. );
  1652. //endregion
  1653. }
  1654. //endregion
  1655. //region 外协品新建外协入库单
  1656. //筛选出skuId为空的 走外协品逻辑
  1657. OutboundVO outsideGoodVO = outboundConvert.convertResToVO(outboundResponse);
  1658. List<OutboundItemResponse> outsideGoods = outboundItemResponseList.stream().filter(it -> it.getSkuId() == null).collect(Collectors.toList());
  1659. List<OutboundItemVO> outsideGoodsVOList = outboundItemConvert.convertResListToVOList(outsideGoods);
  1660. //删除外协品生产外协入库单
  1661. this.deleteOutsideGoodsInto(outsideGoodVO, outsideGoodsVOList);
  1662. //endregion
  1663. //region 调用库存
  1664. //筛选出skuId不为空的 走库存
  1665. List<OutboundItemResponse> invList = outboundItemResponseList.stream().filter(it -> it.getSkuId() != null).collect(Collectors.toList());
  1666. if (invList != null && invList.size() > 0) {
  1667. Map<String, Object> map = new HashMap<>();
  1668. map.put("delOutDetail", invList);
  1669. inventoryService.operatingInventoryInformation(map);
  1670. }
  1671. //endregion
  1672. return ResponseResultUtil.success();
  1673. }
  1674. /**
  1675. * @desc : 撤销销售出库修改销售订单信息
  1676. * @date : 2024/6/21 16:00
  1677. * @author : 寇珊珊
  1678. */
  1679. @Transactional(rollbackFor = {Exception.class})
  1680. public void updateOrderMessageByCancel(List<OutboundItemResponse> itemList) {
  1681. //出库明细汇总
  1682. Map<String, List<OutboundItemResponse>> orderOutItemListMap = itemList.stream().collect(Collectors.groupingBy(OutboundItemResponse::getFromId));
  1683. //出库明细汇总循环
  1684. for (String fromId : orderOutItemListMap.keySet()) {
  1685. //region 提取新建分组后的明细
  1686. List<OutboundItemResponse> orderOutItemListGroup = orderOutItemListMap.get(fromId);
  1687. //分组后的明细 出库数量和金额总和
  1688. BigDecimal orderSumQty = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutQty).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(6, BigDecimal.ROUND_HALF_UP);
  1689. BigDecimal orderSumAmt = orderOutItemListGroup.stream().map(OutboundItemResponse::getOutAmt).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
  1690. //endregion
  1691. //region 反写订单数据
  1692. //根据id查询
  1693. OrderResponse orderResponse = orderMapper.selectById(fromId);
  1694. Order order = new Order();
  1695. order.setOrderId(fromId);
  1696. order.setOutingQty(orderSumQty);
  1697. order.setOutingAmt(orderSumAmt);
  1698. order.setOutQty(orderSumAmt.negate());
  1699. order.setOutAmt(orderSumAmt.negate());
  1700. //出库状态
  1701. String outStatus = this.setOutStatus(orderResponse.getOutingQty().add(order.getOutingQty()),
  1702. orderResponse.getOutQty().add(order.getOutQty()), orderResponse.getSumQuantity());
  1703. order.setOutStatus(outStatus);
  1704. //修改
  1705. int countRow = orderMapper.updateById(order);
  1706. //数量超出
  1707. if (countRow == 0) {
  1708. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVENTORY_QUANTITY_EXCEEDED_OUTBOUND.getMessage());
  1709. }
  1710. //endregion
  1711. }
  1712. }
  1713. /**
  1714. * @desc : 出库状态通用(目前本页面)
  1715. * @date : 2024/3/9 8:59
  1716. * @author : 寇珊珊
  1717. */
  1718. @Transactional(rollbackFor = {Exception.class})
  1719. public String setOutStatus(BigDecimal outingQty, BigDecimal outQty) {
  1720. //出库状态
  1721. String outStatus = null;
  1722. //已出库数量>0 出库中数量>0
  1723. if (outQty.compareTo(BigDecimal.ZERO) >= 0 && outingQty.compareTo(BigDecimal.ZERO) > 0) {
  1724. //出库中
  1725. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1726. }
  1727. //出入库数量=0 出库中数量=0
  1728. else if (outQty.compareTo(BigDecimal.ZERO) == 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1729. //待出库
  1730. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1731. }
  1732. //已出库数量>0 出库中数量=0
  1733. else if (outQty.compareTo(BigDecimal.ZERO) > 0 && outingQty.compareTo(BigDecimal.ZERO) == 0) {
  1734. //已出库
  1735. outStatus = Constant.OutStatus.YICHUKU.getName();
  1736. }
  1737. return outStatus;
  1738. }
  1739. /**
  1740. * @desc : 上游单据入库状态通用(目前本页面)
  1741. * @date : 2024/4/1 17:14
  1742. * @author : 寇珊珊
  1743. */
  1744. @Transactional(rollbackFor = {Exception.class})
  1745. public String setOutStatus(BigDecimal intoingQty, BigDecimal intoQty, BigDecimal sumQty) {
  1746. //出库状态
  1747. String outStatus = null;
  1748. //出库中+已出库 小于 总数 或者 出库中数量小于等于订单总数量
  1749. if (intoingQty.compareTo(sumQty) <= 0 && intoingQty.add(intoQty).compareTo(sumQty) <= 0 && intoQty.compareTo(sumQty) < 0) {
  1750. //出库中
  1751. outStatus = Constant.OutStatus.CHUKUZHONG.getName();
  1752. }
  1753. //已出库数量=0 出库中数量=0
  1754. else if (intoQty.compareTo(BigDecimal.ZERO) == 0 && intoingQty.compareTo(BigDecimal.ZERO) == 0) {
  1755. //待出库
  1756. outStatus = Constant.OutStatus.DAICHUKU.getName();
  1757. }
  1758. //出库中+已出库 等于 总数 并且 出库中数量小于等于订单总数量
  1759. else if (intoingQty.compareTo(sumQty) < 0 && intoingQty.add(intoQty).compareTo(sumQty) == 0) {
  1760. //已出库
  1761. outStatus = Constant.OutStatus.YICHUKU.getName();
  1762. }
  1763. return outStatus;
  1764. }
  1765. /**
  1766. * @desc : 获取单据信息(编辑用)
  1767. * @date : 2024/3/16 16:28
  1768. * @author : 寇珊珊
  1769. */
  1770. public ResponseResultVO<?> selectByUpdate(String id) {
  1771. Map<String, Object> dataInfo = new HashMap<>();
  1772. //总单
  1773. OutboundResponse outboundResponse = outboundMapper.selectMessageByOtherQuery(new OutboundQuery().setOutId(id).setOutStatus(Constant.OutStatus.CHUKUZHONG.getName()));
  1774. //单据不存在
  1775. if (outboundResponse == null) {
  1776. throw new BaseBusinessException(ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getCode(),
  1777. ErrorCodeEnum.THERE_ORDER_IS_NOT_CAN_OUTBOUND_QUANTITY.getMessage());
  1778. }
  1779. dataInfo.put("data", outboundResponse);
  1780. // 明细
  1781. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(outboundResponse.getOutId()));
  1782. dataInfo.put("dataItem", outboundItemResponseList);
  1783. return ResponseResultUtil.success(dataInfo);
  1784. }
  1785. /********************************************** 销售出库查询相关方法begin *************************************/
  1786. /**
  1787. * @desc : 一览页销售出库
  1788. * @author : 付斌
  1789. * @date : 2023/1/9 10:40
  1790. */
  1791. @Pagination
  1792. public ResponseResultVO<PageList<OutboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1793. outboundQuery.setOutType(Constant.OutType.SALE.getName());
  1794. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1795. outboundMapper.countByCond(outboundQuery));
  1796. }
  1797. /**
  1798. * @desc : 一览页销售出库明细(货物、附件)
  1799. * @author : 付斌
  1800. * @date : 2024-02-28 13:25
  1801. */
  1802. @Pagination
  1803. public ResponseResultVO<Map<String, Object>> selectOutboundInfoById(String id) {
  1804. Map<String, Object> result = new HashMap<>();
  1805. // 商品明细
  1806. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1807. result.put("outboundItem", outboundItem);
  1808. // 附件
  1809. return ResponseResultUtil.success(result);
  1810. }
  1811. /**
  1812. * @desc : 获取销售出库信息(编辑用)
  1813. * @author : 付斌
  1814. * @date : 2024-03-02 17:27
  1815. */
  1816. public ResponseResultVO<?> getOutboundForUpdate(String id) {
  1817. Map<String, Object> dataInfo = new HashMap<>();
  1818. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1819. dataInfo.put("data", outboundResponse);
  1820. // 商品明细
  1821. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEdit(id);
  1822. dataInfo.put("dataItem", outboundItemResponseList);
  1823. return ResponseResultUtil.success(dataInfo);
  1824. }
  1825. /**
  1826. * @desc : 获取销售出库信息(编辑用,适用于直接新建的出库单)
  1827. * @author : 付斌
  1828. * @date : 2024-03-02 17:27
  1829. */
  1830. public ResponseResultVO<?> getOutboundTogetherForUpdate(String id) {
  1831. Map<String, Object> dataInfo = new HashMap<>();
  1832. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1833. dataInfo.put("data", outboundResponse);
  1834. // 商品明细
  1835. List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCondForOutEditTogether(id);
  1836. dataInfo.put("dataItem", outboundItemResponseList);
  1837. return ResponseResultUtil.success(dataInfo);
  1838. }
  1839. /**
  1840. * @desc : 获取出库信息(新建退货用)
  1841. * @author : 付斌
  1842. * @date : 2024-03-02 17:27
  1843. */
  1844. public ResponseResultVO<?> getOutForReturn(String id) {
  1845. OutboundResponse outboundResponse = outboundMapper.selectByIdForReturn(id);
  1846. List<OutboundItemResponse> outboundItemList = outboundItemMapper.selectByCondForReturn(new OutboundItemQuery().setOutId(id));
  1847. if (outboundItemList != null && outboundItemList.size() > 0) {
  1848. // 求和
  1849. OutboundItemResponse sumEntity = outboundItemList.stream().reduce((x, y) -> {
  1850. OutboundItemResponse item = new OutboundItemResponse();
  1851. item.setOutingQty(x.getOutingQty().add(y.getOutingQty()));
  1852. item.setOutingAmt(x.getOutingAmt().add(y.getOutingAmt()));
  1853. return item;
  1854. }).get();
  1855. outboundResponse.setOutingQty(sumEntity.getOutingQty()).setOutingAmt(sumEntity.getOutingAmt());
  1856. }
  1857. Map<String, Object> dataInfo = new HashMap<>();
  1858. dataInfo.put("data", outboundResponse);
  1859. dataInfo.put("dataItem", outboundItemList);
  1860. return ResponseResultUtil.success(dataInfo);
  1861. }
  1862. /**
  1863. * @desc : 查询出库明细(货物、附件)
  1864. * @author : 付斌
  1865. * @date : 2024-02-28 13:25
  1866. */
  1867. @Pagination
  1868. public ResponseResultVO selectById(String id) {
  1869. OutboundResponse outboundResponse = outboundMapper.selectById(id);
  1870. // 商品明细
  1871. List<OutboundItemResponse> outboundItem = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1872. outboundResponse.setGoodsList(outboundItem);
  1873. // 附件
  1874. return ResponseResultUtil.success(outboundResponse);
  1875. }
  1876. /********************************************** 销售出库查询相关方法end *************************************/
  1877. // /**
  1878. // * @desc : 条件查询
  1879. // * @date : 2024/3/18 11:20
  1880. // * @author : 寇珊珊
  1881. // */
  1882. // @Pagination
  1883. // public ResponseResultVO<PageList<InboundResponse>> selectByCond(OutboundQuery outboundQuery) {
  1884. // return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCond(outboundQuery),
  1885. // outboundMapper.countByCond(outboundQuery));
  1886. // }
  1887. //
  1888. // /**
  1889. // * @desc : 查询明细
  1890. // * @date : 2024/3/15 16:43
  1891. // * @author : 寇珊珊
  1892. // */
  1893. // @Pagination
  1894. // public ResponseResultVO<Map<String, Object>> selectOutBoundSaleOrderItemInfoById(String id) {
  1895. // Map<String, Object> result = new HashMap<>();
  1896. // // 商品明细
  1897. // List<OutboundItemResponse> outboundItemResponseList = outboundItemMapper.selectByCond(new OutboundItemQuery().setOutId(id));
  1898. // result.put("itemList", outboundItemResponseList);
  1899. // // 收款
  1900. //
  1901. // // 附件
  1902. // return ResponseResultUtil.success(result);
  1903. // }
  1904. /**
  1905. * @desc : 条件查询(总单带明细)
  1906. * @author : 于继渤
  1907. * @date : 2023/1/9 10:36
  1908. */
  1909. @Pagination
  1910. public ResponseResultVO<PageList<OutboundResponse>> selectByCondDetail(OutboundQuery outboundQuery) {
  1911. return super.mergeListWithCount(outboundQuery, outboundMapper.selectByCondDetail(outboundQuery),
  1912. outboundMapper.countByCondDetail(outboundQuery));
  1913. }
  1914. }