@Test public void shouldCalculateAverageMonthlyConsumptionWithContinuedStockOutCorrectly() throws LMISException { StockCard stockCard = new StockCard(); stockCard.setProduct(getRandomProduct()); stockCard.setStockOnHand(200); stockService.stockRepository.createOrUpdate(stockCard); createMovementItem(ISSUE, 100, stockCard, new Date(), lastForthMonthDate, false); createMovementItem(ISSUE, 100, stockCard, new Date(), lastForthMonthDate, false); createMovementItem(RECEIVE, 400, stockCard, new Date(), lastSecondMonthDate, false); createMovementItem(ISSUE, 100, stockCard, new Date(), lastSecondMonthDate, false); createMovementItem(ISSUE, 100, stockCard, new Date(), lastFirstMonthDate, false); float averageMonthlyConsumption = stockService.calculateAverageMonthlyConsumption(stockCard); assertThat(averageMonthlyConsumption, is(100F)); }
protected float calculateAverageMonthlyConsumption(StockCard stockCard) { Date firstPeriodBegin; try { firstPeriodBegin = queryFirstPeriodBegin(stockCard); } catch (LMISException e) { new LMISException(e, "StockService:calculateAverage").reportToFabric(); return -1; } if (firstPeriodBegin == null) { return -1; } List<Long> issuePerMonths = new ArrayList<>(); Period period = Period.of(today()); int periodQuantity = DateUtil.calculateDateMonthOffset(firstPeriodBegin, period.getBegin().toDate()); for (int i = 0; i < periodQuantity; i++) { period = period.previous(); Long totalIssuesEachMonth = calculateTotalIssuesPerPeriod(stockCard, period); if (hasStockOutTotoalIssue(totalIssuesEachMonth)) { continue; } issuePerMonths.add(totalIssuesEachMonth); if (issuePerMonths.size() == HIGH_STOCK_CALCULATE_MONTH_QUANTITY) { break; } } if (issuePerMonths.size() < 1) { return -1; } return getTotalIssues(issuePerMonths) * 1f / issuePerMonths.size(); }
StockService { protected float calculateAverageMonthlyConsumption(StockCard stockCard) { Date firstPeriodBegin; try { firstPeriodBegin = queryFirstPeriodBegin(stockCard); } catch (LMISException e) { new LMISException(e, "StockService:calculateAverage").reportToFabric(); return -1; } if (firstPeriodBegin == null) { return -1; } List<Long> issuePerMonths = new ArrayList<>(); Period period = Period.of(today()); int periodQuantity = DateUtil.calculateDateMonthOffset(firstPeriodBegin, period.getBegin().toDate()); for (int i = 0; i < periodQuantity; i++) { period = period.previous(); Long totalIssuesEachMonth = calculateTotalIssuesPerPeriod(stockCard, period); if (hasStockOutTotoalIssue(totalIssuesEachMonth)) { continue; } issuePerMonths.add(totalIssuesEachMonth); if (issuePerMonths.size() == HIGH_STOCK_CALCULATE_MONTH_QUANTITY) { break; } } if (issuePerMonths.size() < 1) { return -1; } return getTotalIssues(issuePerMonths) * 1f / issuePerMonths.size(); } }
StockService { protected float calculateAverageMonthlyConsumption(StockCard stockCard) { Date firstPeriodBegin; try { firstPeriodBegin = queryFirstPeriodBegin(stockCard); } catch (LMISException e) { new LMISException(e, "StockService:calculateAverage").reportToFabric(); return -1; } if (firstPeriodBegin == null) { return -1; } List<Long> issuePerMonths = new ArrayList<>(); Period period = Period.of(today()); int periodQuantity = DateUtil.calculateDateMonthOffset(firstPeriodBegin, period.getBegin().toDate()); for (int i = 0; i < periodQuantity; i++) { period = period.previous(); Long totalIssuesEachMonth = calculateTotalIssuesPerPeriod(stockCard, period); if (hasStockOutTotoalIssue(totalIssuesEachMonth)) { continue; } issuePerMonths.add(totalIssuesEachMonth); if (issuePerMonths.size() == HIGH_STOCK_CALCULATE_MONTH_QUANTITY) { break; } } if (issuePerMonths.size() < 1) { return -1; } return getTotalIssues(issuePerMonths) * 1f / issuePerMonths.size(); } StockService(); }
StockService { protected float calculateAverageMonthlyConsumption(StockCard stockCard) { Date firstPeriodBegin; try { firstPeriodBegin = queryFirstPeriodBegin(stockCard); } catch (LMISException e) { new LMISException(e, "StockService:calculateAverage").reportToFabric(); return -1; } if (firstPeriodBegin == null) { return -1; } List<Long> issuePerMonths = new ArrayList<>(); Period period = Period.of(today()); int periodQuantity = DateUtil.calculateDateMonthOffset(firstPeriodBegin, period.getBegin().toDate()); for (int i = 0; i < periodQuantity; i++) { period = period.previous(); Long totalIssuesEachMonth = calculateTotalIssuesPerPeriod(stockCard, period); if (hasStockOutTotoalIssue(totalIssuesEachMonth)) { continue; } issuePerMonths.add(totalIssuesEachMonth); if (issuePerMonths.size() == HIGH_STOCK_CALCULATE_MONTH_QUANTITY) { break; } } if (issuePerMonths.size() < 1) { return -1; } return getTotalIssues(issuePerMonths) * 1f / issuePerMonths.size(); } StockService(); void monthlyUpdateAvgMonthlyConsumption(); void immediatelyUpdateAvgMonthlyConsumption(); }
StockService { protected float calculateAverageMonthlyConsumption(StockCard stockCard) { Date firstPeriodBegin; try { firstPeriodBegin = queryFirstPeriodBegin(stockCard); } catch (LMISException e) { new LMISException(e, "StockService:calculateAverage").reportToFabric(); return -1; } if (firstPeriodBegin == null) { return -1; } List<Long> issuePerMonths = new ArrayList<>(); Period period = Period.of(today()); int periodQuantity = DateUtil.calculateDateMonthOffset(firstPeriodBegin, period.getBegin().toDate()); for (int i = 0; i < periodQuantity; i++) { period = period.previous(); Long totalIssuesEachMonth = calculateTotalIssuesPerPeriod(stockCard, period); if (hasStockOutTotoalIssue(totalIssuesEachMonth)) { continue; } issuePerMonths.add(totalIssuesEachMonth); if (issuePerMonths.size() == HIGH_STOCK_CALCULATE_MONTH_QUANTITY) { break; } } if (issuePerMonths.size() < 1) { return -1; } return getTotalIssues(issuePerMonths) * 1f / issuePerMonths.size(); } StockService(); void monthlyUpdateAvgMonthlyConsumption(); void immediatelyUpdateAvgMonthlyConsumption();  }