| | |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import zy.cloud.wms.common.model.BillDto; |
| | | import zy.cloud.wms.common.model.MatnrDto; |
| | | import zy.cloud.wms.common.model.OrderStoDto; |
| | | import zy.cloud.wms.common.service.MainService; |
| | | import zy.cloud.wms.common.service.asrs.AsrsService; |
| | | import zy.cloud.wms.common.service.erp.ErpService; |
| | | import zy.cloud.wms.common.utils.VersionUtils; |
| | | import zy.cloud.wms.manager.entity.*; |
| | | import zy.cloud.wms.manager.entity.param.*; |
| | | import zy.cloud.wms.manager.entity.result.StoPrintVo; |
| | | import zy.cloud.wms.manager.service.*; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Created by vincent on 2021/2/25 |
| | |
| | | @Autowired |
| | | private MatService matService; |
| | | @Autowired |
| | | private AsrsService asrsService; |
| | | private ErpService erpService; |
| | | @Autowired |
| | | private CheckService checkService; |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R stockPakin(PakinParam param, Long userId) { |
| | | Node node = nodeService.selectByUuid(param.getNodeId()); |
| | | if (node == null) { |
| | | node = nodeService.selectById(param.getNodeId()); |
| | | } |
| | | if (node == null) { |
| | | return R.error("货位不存在"); |
| | | } |
| | | if (Cools.isEmpty(param.getMats())) { |
| | | return R.error("入库物料不能为空"); |
| | | } |
| | | Date now = new Date(); |
| | | for (MatnrDto dto : param.getMats()) { |
| | | Mat mat = matService.selectByMatnr(dto.getMatnr()); |
| | | if (mat == null) { |
| | | throw new CoolException("物料数据错误,请联系管理员"); |
| | | } |
| | | LocDetl locDetl = locDetlService.getLocDetl(node.getId(), dto.getMatnr()); |
| | | if (locDetl == null) { |
| | | // 更新货位库存明细 |
| | | locDetl = new LocDetl(); |
| | | locDetl.setLocNo(node.getName()); |
| | | locDetl.setNodeId(node.getId()); |
| | | locDetl.setZpallet(""); |
| | | locDetl.setAnfme(dto.getCount()); |
| | | VersionUtils.setLocDetl(locDetl, mat); |
| | | // locDetl.setDocId(param.getDocType().longValue()); |
| | | // locDetl.setDocNum(docNum); |
| | | locDetl.setStatus(1); |
| | | locDetl.setCreateBy(userId); |
| | | locDetl.setCreateTime(now); |
| | | locDetl.setUpdateBy(userId); |
| | | locDetl.setUpdateTime(now); |
| | | boolean insert = locDetlService.insert(locDetl); |
| | | if (!insert) { |
| | | throw new CoolException("新增库存明细档失败"); |
| | | } |
| | | } else { |
| | | if (!locDetlService.incrementStock(node.getId(), dto.getMatnr(), dto.getCount())) { |
| | | throw new CoolException("新增库存明细档失败"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 上报给erp |
| | | List<BillDto> dtos = new ArrayList<>(); |
| | | for (MatnrDto dto : param.getMats()) { |
| | | BillDto billDto = new BillDto(); |
| | | billDto.setMatnr(dto.getMatnr()); |
| | | billDto.setQty(dto.getCount()); |
| | | dtos.add(billDto); |
| | | } |
| | | Integer docId = Optional.ofNullable(param.getDocType()).orElse(34); |
| | | String docNum = String.valueOf(snowflakeIdWorker.nextId()); |
| | | switch (docId) { |
| | | case 34: // 采购单 |
| | | docNum = "PU-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | break; |
| | | case 45: // 销售退货单 |
| | | docNum = "SR-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | if (!erpService.uploadBill(dtos, docId, docNum)) { |
| | | throw new CoolException("无法上报至erp"); |
| | | } |
| | | return R.ok("入库成功"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | |
| | | return R.error("物料不存在"); |
| | | } |
| | | Date now = new Date(); |
| | | String docNum = String.valueOf(snowflakeIdWorker.nextId()); |
| | | switch (param.getDocType()) { |
| | | case 34: // 采购单 |
| | | docNum = "PU-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | break; |
| | | case 45: // 销售退货单 |
| | | docNum = "SR-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | break; |
| | | default: |
| | | break; |
| | | } |
| | | for (Comb comb : combs) { |
| | | // 改变组托档状态 |
| | | comb.setIoStatus(4); |
| | |
| | | locDetl.setZpallet(param.getZpallet()); |
| | | locDetl.setAnfme(comb.getAnfme()); |
| | | VersionUtils.setLocDetl(locDetl, comb); |
| | | locDetl.setDocId(param.getDocType().longValue()); |
| | | locDetl.setDocNum(docNum); |
| | | locDetl.setStatus(1); |
| | | locDetl.setCreateBy(userId); |
| | | locDetl.setCreateTime(now); |
| | |
| | | } |
| | | |
| | | } |
| | | |
| | | // 上报给erp |
| | | List<BillDto> dtos = new ArrayList<>(); |
| | | for (Comb comb : combs) { |
| | | BillDto billDto = new BillDto(); |
| | | billDto.setMatnr(comb.getMatnr()); |
| | | billDto.setQty(comb.getAnfme()); |
| | | dtos.add(billDto); |
| | | } |
| | | if (!erpService.uploadBill(dtos, param.getDocType(), docNum)) { |
| | | throw new CoolException("无法上报至erp"); |
| | | } |
| | | |
| | | return R.ok(); |
| | | } |
| | | |
| | |
| | | List<Pakout> pakouts = pakoutService.selectList(new EntityWrapper<Pakout>() |
| | | .eq("doc_num", param.getNumber()) |
| | | .in("wrk_sts", 1, 2)); |
| | | // 修改库存数量 |
| | | for (Pakout pakout : pakouts) { |
| | | LocDetl locDetl = locDetlService.getLocDetl(pakout.getNodeId(), pakout.getMatnr()); |
| | | // 缺货 or 立库 |
| | | if (locDetl == null) { |
| | | // 缺货 |
| | | if (pakout.getLocNo().equals("缺货")) { |
| | | Double count = Optional.ofNullable(locDetlService.selectCountByMatnr(pakout.getMatnr())).orElse(0.0D); |
| | | if (count < pakout.getAnfme()) { |
| | | throw new CoolException(pakout.getMaktx()+"缺货"); |
| | | } |
| | | Date now = new Date(); |
| | | // 查找推荐货位是否库存足够 |
| | | List<LocDetl> locDetls = locDetlService.findOfSort(pakout.getMatnr()); |
| | | double issued = Optional.ofNullable(pakout.getAnfme()).orElse(0.0D) ; |
| | | for (LocDetl locDetl1 : locDetls) { |
| | | if (issued > 0) { |
| | | // 保存出库通知单 |
| | | Pakout newPakout = new Pakout(); |
| | | newPakout.setWrkSts(3L); |
| | | newPakout.setAnfme(issued>=locDetl1.getAnfme()?locDetl1.getAnfme():issued); |
| | | newPakout.setZpallet(locDetl1.getZpallet()); |
| | | newPakout.setLocNo(locDetl1.getLocNo()); |
| | | newPakout.setNodeId(locDetl1.getNodeId()); |
| | | newPakout.setWrkNo(String.valueOf(snowflakeIdWorker.nextId())); |
| | | VersionUtils.setPakout(newPakout, locDetl1); |
| | | newPakout.setDocId(pakout.getDocId()); // 单据类型 |
| | | newPakout.setDocNum(pakout.getDocNum()); // 单据编号 |
| | | newPakout.setCreateTime(now); |
| | | newPakout.setUpdateTime(now); |
| | | newPakout.setStatus(1); |
| | | if (!pakoutService.insert(newPakout)) { |
| | | throw new CoolException("保存出库通知单失败"); |
| | | } |
| | | if (issued>=locDetl1.getAnfme()) { |
| | | // 删除库存明细 |
| | | if (!locDetlService.removeStock(locDetl1.getNodeId(), newPakout.getMatnr())) { |
| | | throw new CoolException("删除库存明细失败"); |
| | | } |
| | | } else { |
| | | // 修改库存明细数量 |
| | | if (!locDetlService.reduceStock(locDetl1.getNodeId(), newPakout.getMatnr(), issued)) { |
| | | throw new CoolException("修改库存明细数量失败"); |
| | | } |
| | | } |
| | | // 剩余待出数量递减 |
| | | issued = issued - locDetl1.getAnfme(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | continue; |
| | | } |
| | | // 修改库存数量 |
| | | if (pakout.getAnfme()>=locDetl.getAnfme()) { |
| | | // 删除库存明细 |
| | | if (!locDetlService.removeStock(locDetl.getNodeId(), pakout.getMatnr())) { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 修改出库单状态 |
| | | if (!pakoutService.stockOutComplete(param.getWrkNo(), param.getNumber(), userId)) { |
| | | return R.error("保存数据失败"); |
| | |
| | | if (!custOrderService.updateStatus(param.getNumber(), 4)) { |
| | | throw new CoolException("修改单据状态失败"); |
| | | } |
| | | // 采购退货单 |
| | | // if (param.getNumber().startsWith("PR-")) { |
| | | // erp单据上传 todo |
| | | // Long docId = pakouts.get(0).getDocId(); |
| | | // if (null != docId) { |
| | | // List<BillDto> dtos = new ArrayList<>(); |
| | | // for (Pakout pakout : pakouts) { |
| | | // BillDto billDto = new BillDto(); |
| | | // billDto.setMatnr(pakout.getMatnr()); |
| | | // billDto.setQty(pakout.getAnfme()); |
| | | // dtos.add(billDto); |
| | | // } |
| | | // if (!erpService.uploadBill(dtos, docId.intValue() , pakouts.get(0).getDocNum())) { |
| | | // throw new CoolException("无法上报至erp"); |
| | | // } |
| | | // } |
| | | |
| | | // } |
| | | return R.ok(); |
| | | } |
| | | |
| | |
| | | DateUtils.convert(now), // 单据日期[非空] |
| | | param.getCustomerTypeId(), // 客户编号 |
| | | null, // 经手人编号[空] |
| | | null, |
| | | dto.getMatnr(), // 商品编号 |
| | | dto.getCount(), // 商品数量 |
| | | dto.getCount(), // 总数量 |
| | | Cools.isEmpty(mat.getName())?0.0D:Double.parseDouble(mat.getName()), // 商品单价 |
| | | null, // 商品明细行号 |
| | | null, // 商品备注[空] |
| | | 1, // 状态 |
| | | now, // 添加时间 |
| | |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R stockTransfer(String number, Long userId) { |
| | | List<CustOrder> custOrders = custOrderService.selectList(new EntityWrapper<CustOrder>().eq("number", number).eq("status", 1)); |
| | | boolean asrs = false; |
| | | List<StockTransferParam> params = new ArrayList<>(); |
| | | for (CustOrder custOrder : custOrders) { |
| | | Double count = locDetlService.selectCountByMatnr(custOrder.getUserCode()); |
| | | if (count == null) { |
| | | count = 0.0D; |
| | | } |
| | | if (count < custOrder.getQty()) { |
| | | if (!asrs) { asrs = true; } |
| | | StockTransferParam param = new StockTransferParam(); |
| | | param.setMatnr(custOrder.getUserCode()); |
| | | param.setAnfme(custOrder.getQty() - count); |
| | | params.add(param); |
| | | } |
| | | public R stockCheck(StoCheckParam param, Long userId) { |
| | | if (Cools.isEmpty(param.getLocNo())) { |
| | | return R.error("请选择货位"); |
| | | } |
| | | if (asrs) { |
| | | if (asrsService.stockTransfer(params)) { |
| | | for (StockTransferParam param : params) { |
| | | // 修改销售订单待出数量 |
| | | if (!custOrderService.updateQty(number, param.getMatnr(), param.getAnfme())) { |
| | | throw new CoolException("修改销售订单待出数量失败"); |
| | | Node node = nodeService.selectByUuid(param.getLocNo()); |
| | | if (Cools.isEmpty(node)) { |
| | | return R.error("货位异常,请联系管理员"); |
| | | } |
| | | // 准备数据 |
| | | Date now = new Date(); |
| | | List<LocDetl> locDetls = locDetlService.selectList(new EntityWrapper<LocDetl>().eq("node_id", node.getId())); |
| | | List<String> locDetlMatnrs = new ArrayList<>(); |
| | | locDetls.forEach(locDetl -> locDetlMatnrs.add(locDetl.getMatnr())); |
| | | Iterator<LocDetl> detlIterator = locDetls.iterator(); |
| | | List<PakinMatDto> dtos = param.getDtos(); |
| | | |
| | | // 1.盈亏 修改数量 |
| | | while (detlIterator.hasNext()){ |
| | | LocDetl locDetl = detlIterator.next(); |
| | | for (PakinMatDto dto : dtos) { |
| | | if (locDetl.getMatnr().equals(dto.getMatnr())) { |
| | | if (!locDetl.getAnfme().equals(dto.getCount())) { |
| | | Check check = new Check( |
| | | String.valueOf(snowflakeIdWorker.nextId()), // 盘点编号 |
| | | node.getId(), // 盘点货位 |
| | | node.getUuid(), // 货位编号 |
| | | node.getName(), // 货位名称 |
| | | locDetl.getMatnr(), // 商品编号 |
| | | locDetl.getMaktx(), // 商品名称 |
| | | locDetl.getUnit(), // 单位 |
| | | null, // 单据类型 |
| | | null, // 单据编号 |
| | | locDetl.getAnfme(), // 账面数量 |
| | | dto.getCount(), // 实盘数量 |
| | | dto.getCount() - locDetl.getAnfme(), // 差值 |
| | | locDetl.getAnfme() < dto.getCount() ? 1 : 2, // 盈亏 |
| | | 1, // 状态 |
| | | userId, // 添加人员 |
| | | now, // 添加时间 |
| | | userId, // 修改人员 |
| | | now, // 修改时间 |
| | | null // 备注 |
| | | ); |
| | | if (!checkService.insert(check)) { |
| | | throw new CoolException("保存盘点记录失败"); |
| | | } |
| | | } |
| | | detlIterator.remove(); |
| | | break; |
| | | } |
| | | return R.ok("库存调拨成功"); |
| | | } else { |
| | | return R.error("立库调拨失败,请联系管理员"); |
| | | } |
| | | } |
| | | return R.error("暂不缺货,不需要立库调拨"); |
| | | |
| | | // 2.盘亏 删除库存 |
| | | for (LocDetl locDetl : locDetls) { |
| | | Check check = new Check( |
| | | String.valueOf(snowflakeIdWorker.nextId()), // 盘点编号 |
| | | node.getId(), // 盘点货位 |
| | | node.getUuid(), // 货位编号 |
| | | node.getName(), // 货位名称 |
| | | locDetl.getMatnr(), // 商品编号 |
| | | locDetl.getMaktx(), // 商品名称 |
| | | locDetl.getUnit(), // 单位 |
| | | null, // 单据类型 |
| | | null, // 单据编号 |
| | | locDetl.getAnfme(), // 账面数量 |
| | | 0D, // 实盘数量 |
| | | 0D - locDetl.getAnfme(), // 差值 |
| | | 2, // 盈亏 |
| | | 1, // 状态 |
| | | userId, // 添加人员 |
| | | now, // 添加时间 |
| | | userId, // 修改人员 |
| | | now, // 修改时间 |
| | | null // 备注 |
| | | ); |
| | | if (!checkService.insert(check)) { |
| | | throw new CoolException("保存盘点记录失败"); |
| | | } |
| | | |
| | | } |
| | | |
| | | // 3.盘盈 新增库存 |
| | | for (PakinMatDto dto : dtos) { |
| | | if (!locDetlMatnrs.contains(dto.getMatnr())) { |
| | | Mat mat = matService.selectByMatnr(dto.getMatnr()); |
| | | Check check = new Check( |
| | | String.valueOf(snowflakeIdWorker.nextId()), // 盘点编号 |
| | | node.getId(), // 盘点货位 |
| | | node.getUuid(), // 货位编号 |
| | | node.getName(), // 货位名称 |
| | | mat.getMatnr(), // 商品编号 |
| | | mat.getMaktx(), // 商品名称 |
| | | mat.getUnit(), // 单位 |
| | | null, // 单据类型 |
| | | null, // 单据编号 |
| | | 0D, // 账面数量 |
| | | dto.getCount(), // 实盘数量 |
| | | dto.getCount() - 0D, // 差值 |
| | | 1, // 盈亏 |
| | | 1, // 状态 |
| | | userId, // 添加人员 |
| | | now, // 添加时间 |
| | | userId, // 修改人员 |
| | | now, // 修改时间 |
| | | null // 备注 |
| | | ); |
| | | if (!checkService.insert(check)) { |
| | | throw new CoolException("保存盘点记录失败"); |
| | | } |
| | | |
| | | } |
| | | } |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public R stockCheckAuditing(Long checkId, Long userId) { |
| | | Check check = checkService.selectById(checkId); |
| | | if (null == check) { |
| | | return R.error("盘点记录已失效"); |
| | | } |
| | | Date now = new Date(); |
| | | // 1.修改库存数量 |
| | | if (check.getOriQty() != 0 && check.getCekQty() != 0) { |
| | | // 更新库存明细 |
| | | if (check.getSettle() == 1) { |
| | | if (!locDetlService.incrementStock(check.getNodeId(), check.getMatnr(), Math.abs(check.getDiffQty()))) { |
| | | throw new CoolException("修改库存明细数量失败"); |
| | | } |
| | | } else if (check.getSettle() == 2) { |
| | | if (!locDetlService.reduceStock(check.getNodeId(), check.getMatnr(), Math.abs(check.getDiffQty()))) { |
| | | throw new CoolException("修改库存明细数量失败"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 2.删除库存 |
| | | if (check.getOriQty() != 0 && check.getCekQty() == 0) { |
| | | if (!locDetlService.delete(new EntityWrapper<LocDetl>().eq("node_id", check.getNodeId()).eq("matnr", check.getMatnr()))) { |
| | | throw new CoolException("删除库存明细数量失败"); |
| | | } |
| | | } |
| | | |
| | | // 3.新增库存 |
| | | if (check.getOriQty() == 0 && check.getCekQty() != 0) { |
| | | Mat mat = matService.selectByMatnr(check.getMatnr()); |
| | | if (mat == null) { |
| | | throw new CoolException("商品数据异常,请联系管理员"); |
| | | } |
| | | // 更新货位库存明细 |
| | | LocDetl locDetl = new LocDetl(); |
| | | locDetl.setLocNo(check.getLocNo()); |
| | | locDetl.setNodeId(check.getNodeId()); |
| | | // locDetl.setZpallet(param.getZpallet()); |
| | | locDetl.setAnfme(check.getCekQty()); |
| | | VersionUtils.setLocDetl(locDetl, mat); |
| | | locDetl.setStatus(1); |
| | | locDetl.setCreateBy(userId); |
| | | locDetl.setCreateTime(now); |
| | | locDetl.setUpdateBy(userId); |
| | | locDetl.setUpdateTime(now); |
| | | if (!locDetlService.insert(locDetl)) { |
| | | throw new CoolException("新增库存明细失败"); |
| | | } |
| | | } |
| | | |
| | | // 修改盘点记录 |
| | | String docNum = null; |
| | | Long docId = null; |
| | | switch (check.getSettle()) { |
| | | case 1: // 盘盈 |
| | | docNum = "CS-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | docId = 14L; // 报溢单 |
| | | break; |
| | | case 2: // 盘亏 |
| | | docNum = "CD-" + String.valueOf(snowflakeIdWorker.nextId()).substring(0, 15); |
| | | docId = 9L; // 报损单 |
| | | break; |
| | | default: |
| | | throw new CoolException("盘点记录盈亏情况异常"); |
| | | } |
| | | check.setStatus(2); |
| | | check.setDocId(docId); |
| | | check.setDocNum(docNum); |
| | | check.setUpdateBy(userId); |
| | | check.setUpdateTime(now); |
| | | if (!checkService.updateById(check)) { |
| | | throw new CoolException("修改盘点记录状态失败"); |
| | | } |
| | | |
| | | // 上报给erp |
| | | List<BillDto> dtos = new ArrayList<>(); |
| | | BillDto billDto = new BillDto(); |
| | | billDto.setMatnr(check.getMatnr()); |
| | | billDto.setQty(Math.abs(check.getDiffQty())); |
| | | dtos.add(billDto); |
| | | if (!erpService.uploadBill(dtos, docId.intValue(), docNum)) { |
| | | throw new CoolException("无法上报至erp"); |
| | | } |
| | | |
| | | return R.ok(); |
| | | } |
| | | |
| | | } |