| | |
| | | currentMaterialMap.put(key, stat); |
| | | } |
| | | |
| | | // 收集所有需要查询库存的物料(本期有出入库的 + 上一期存在但本期没有出入库的) |
| | | List<String> matnrBatchList = new java.util.ArrayList<>(); |
| | | for (MaterialInOutStatDTO stat : materialStats) { |
| | | String matnr = stat.getMatnr(); |
| | | String batch = stat.getBatch() != null ? stat.getBatch() : ""; |
| | | matnrBatchList.add(matnr + "_" + batch); |
| | | } |
| | | if (previousSettle != null && !previousDetailMap.isEmpty()) { |
| | | for (Map.Entry<String, PreviousSettleEndingQtyDTO> entry : previousDetailMap.entrySet()) { |
| | | String key = entry.getKey(); |
| | | if (!currentMaterialMap.containsKey(key)) { |
| | | PreviousSettleEndingQtyDTO previousDetail = entry.getValue(); |
| | | String matnr = previousDetail.getMatnr(); |
| | | String batch = previousDetail.getBatch() != null ? previousDetail.getBatch() : ""; |
| | | String matnrBatchKey = matnr + "_" + batch; |
| | | if (!matnrBatchList.contains(matnrBatchKey)) { |
| | | matnrBatchList.add(matnrBatchKey); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | // 批量查询库存 |
| | | Map<String, BigDecimal> stockQtyMap = new HashMap<>(); |
| | | if (!matnrBatchList.isEmpty()) { |
| | | List<com.zy.asrs.entity.result.StockQtyDTO> stockQtyList = manLocDetlService.queryStockAnfmeBatch(matnrBatchList); |
| | | if (stockQtyList != null) { |
| | | for (com.zy.asrs.entity.result.StockQtyDTO stockQtyDTO : stockQtyList) { |
| | | String key = stockQtyDTO.getMatnr() + "_" + (stockQtyDTO.getBatch() != null ? stockQtyDTO.getBatch() : ""); |
| | | stockQtyMap.put(key, stockQtyDTO.getStockQty() != null ? stockQtyDTO.getStockQty() : BigDecimal.ZERO); |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 收集所有明细记录,用于批量插入 |
| | | List<MonthlySettleDetail> detailList = new java.util.ArrayList<>(); |
| | | |
| | |
| | | // 3. 计算库存相关数量 |
| | | BigDecimal beginningQty = getBeginningQty(matnr, batch, brand, previousEndingQtyMap); |
| | | BigDecimal endingQty = calculateEndingQty(beginningQty, inQty, outQty); |
| | | // 从批量查询的Map中获取库存 |
| | | String stockKey = matnr + "_" + batch; |
| | | BigDecimal stockQtyDecimal = stockQtyMap.getOrDefault(stockKey, BigDecimal.ZERO); |
| | | BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty); |
| | | // 差异数量 = 期末库存 - 期初库存(期末大于期初时为正数) |
| | | BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty); |
| | | |
| | | // 4. 创建明细记录(暂不插入) |
| | | MonthlySettleDetail detail = buildMonthlySettleDetail( |
| | | monthlySettle.getId(), settleNo, matnr, batch, maktx, brand, |
| | | beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty |
| | | beginningQty, inQty, outQty, endingQty, diffQty |
| | | ); |
| | | detail.setIsDeleted(0); // 未删除 |
| | | detailList.add(detail); |
| | |
| | | BigDecimal beginningQty = previousEndingQty; |
| | | // 期末库存 = 期初 + 入库 - 出库 = 期初(因为本期没有出入库) |
| | | BigDecimal endingQty = beginningQty; |
| | | // 从批量查询的Map中获取库存 |
| | | String stockKey = matnr + "_" + batch; |
| | | BigDecimal stockQtyDecimal = stockQtyMap.getOrDefault(stockKey, BigDecimal.ZERO); |
| | | // 计算差异 |
| | | BigDecimal diffQty = calculateDiffQty(stockQtyDecimal, endingQty); |
| | | // 差异数量 = 期初库存 - 期末库存(期初大于期末时为正数) |
| | | BigDecimal diffQty = calculateDiffQty(beginningQty, endingQty); |
| | | |
| | | // 创建明细记录(暂不插入) |
| | | MonthlySettleDetail detail = buildMonthlySettleDetail( |
| | | monthlySettle.getId(), settleNo, matnr, batch, maktx, brand, |
| | | beginningQty, inQty, outQty, endingQty, stockQtyDecimal, diffQty |
| | | beginningQty, inQty, outQty, endingQty, diffQty |
| | | ); |
| | | detail.setIsDeleted(0); // 未删除 |
| | | detailList.add(detail); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 计算差异数量(实际库存-期末库存) |
| | | * 计算差异数量(期末库存-期初库存) |
| | | * 期末大于期初时为正数,表示库存增加 |
| | | */ |
| | | private BigDecimal calculateDiffQty(BigDecimal stockQty, BigDecimal endingQty) { |
| | | return stockQty.subtract(endingQty); |
| | | private BigDecimal calculateDiffQty(BigDecimal beginningQty, BigDecimal endingQty) { |
| | | return endingQty.subtract(beginningQty); |
| | | } |
| | | |
| | | /** |
| | |
| | | private MonthlySettleDetail buildMonthlySettleDetail( |
| | | Long settleId, String settleNo, String matnr, String batch, String maktx, String brand, |
| | | BigDecimal beginningQty, BigDecimal inQty, BigDecimal outQty, BigDecimal endingQty, |
| | | BigDecimal stockQty, BigDecimal diffQty) { |
| | | BigDecimal diffQty) { |
| | | MonthlySettleDetail detail = new MonthlySettleDetail(); |
| | | // 基本信息 |
| | | detail.setSettleId(settleId); |
| | |
| | | detail.setInQty(inQty); |
| | | detail.setOutQty(outQty); |
| | | detail.setEndingQty(endingQty); |
| | | detail.setStockQty(stockQty); |
| | | // stock_qty 字段已废弃,实际库存等于期末库存,不再单独存储 |
| | | // detail.setStockQty(endingQty); |
| | | detail.setDiffQty(diffQty); |
| | | // 时间信息 |
| | | detail.setCreateTime(new Date()); |