@Test public void shouldGetPreviousTwoNormalizedConsumptionsFor1MonthInPreviousPeriodAndShouldTrackFromLast5Periods() throws Exception { Rnr requisition = getVirtualFacilityRnr(); String productCode = "Code1"; requisition.setFullSupplyLineItems(asList(make(a(defaultRnrLineItem, with(RnrLineItemBuilder.productCode, productCode))))); Date trackingStartDate = DateTime.now().minusMonths(2).toDate(); ProcessingPeriod previousPeriod = new ProcessingPeriod(2l, new Date(), new Date(), 1, "previousPeriod"); ProcessingPeriod secondLastPeriod = new ProcessingPeriod(3l, new Date(), new Date(), 1, "secondLastPeriod"); ProcessingPeriod thirdLastPeriod = new ProcessingPeriod(4l, new Date(), new Date(), 1, "thirdLastPeriod"); ProcessingPeriod fourthLastPeriod = new ProcessingPeriod(5l, new Date(), new Date(), 1, "fourthLastPeriod"); ProcessingPeriod fifthLastPeriod = new ProcessingPeriod(6l, trackingStartDate, new Date(), 1, "fifthLastPeriod"); when(processingScheduleService.findM(requisition.getPeriod())).thenReturn(1); when(processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 5)).thenReturn(asList(previousPeriod, secondLastPeriod, thirdLastPeriod, fourthLastPeriod, fifthLastPeriod)); RnrLineItem rnrLineItem = new RnrLineItem(); rnrLineItem.setNormalizedConsumption(4); RnrLineItem rnrLineItem2 = new RnrLineItem(); rnrLineItem2.setNormalizedConsumption(5); List<RnrLineItem> rnrLineItems = asList(rnrLineItem, rnrLineItem2); when(requisitionRepository.getAuthorizedRegularUnSkippedLineItems(productCode, requisition, 2, trackingStartDate)).thenReturn(rnrLineItems); Rnr previousRnr = make(a(defaultRequisition, with(period, make(a(defaultProcessingPeriod, with(numberOfMonths, 3)))))); ProgramRnrTemplate programTemplate = new ProgramRnrTemplate(); RegimenTemplate regimenTemplate = new RegimenTemplate(); when(requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), previousPeriod)).thenReturn(previousRnr); doNothing().when(requisition).setFieldsAccordingToTemplateFrom(previousRnr, programTemplate, regimenTemplate); calculationService.fillFieldsForInitiatedRequisition(requisition, programTemplate, regimenTemplate); verify(processingScheduleService).getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 5); verify(requisitionRepository).getAuthorizedRegularUnSkippedLineItems(productCode, requisition, 2, trackingStartDate); assertThat(requisition.getFullSupplyLineItems().get(0).getPreviousNormalizedConsumptions(), is(asList(4, 5))); }
public void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate) { List<ProcessingPeriod> fivePreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), MAX_NUMBER_OF_PERIODS_TO_TRACK); if (fivePreviousPeriods.size() == 0) { requisition.setFieldsAccordingToTemplateFrom(null, rnrTemplate, regimenTemplate); fillPreviousNCsInLineItems(requisition, requisition.getPeriod().getNumberOfMonths(), requisition.getPeriod().getStartDate()); return; } Rnr previousRequisition = requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), fivePreviousPeriods.get(0)); requisition.setFieldsAccordingToTemplateFrom(previousRequisition, rnrTemplate, regimenTemplate); Integer numberOfMonths = fivePreviousPeriods.get(0).getNumberOfMonths(); Date trackingDate = fivePreviousPeriods.get(0).getStartDate(); int lastPeriodIndex = MAX_NUMBER_OF_PERIODS_TO_TRACK - 1; int secondPeriodIndex = 1; if (numberOfMonths == 1) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, lastPeriodIndex); } else if (numberOfMonths == 2) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, secondPeriodIndex); } fillPreviousNCsInLineItems(requisition, numberOfMonths, trackingDate); }
CalculationService { public void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate) { List<ProcessingPeriod> fivePreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), MAX_NUMBER_OF_PERIODS_TO_TRACK); if (fivePreviousPeriods.size() == 0) { requisition.setFieldsAccordingToTemplateFrom(null, rnrTemplate, regimenTemplate); fillPreviousNCsInLineItems(requisition, requisition.getPeriod().getNumberOfMonths(), requisition.getPeriod().getStartDate()); return; } Rnr previousRequisition = requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), fivePreviousPeriods.get(0)); requisition.setFieldsAccordingToTemplateFrom(previousRequisition, rnrTemplate, regimenTemplate); Integer numberOfMonths = fivePreviousPeriods.get(0).getNumberOfMonths(); Date trackingDate = fivePreviousPeriods.get(0).getStartDate(); int lastPeriodIndex = MAX_NUMBER_OF_PERIODS_TO_TRACK - 1; int secondPeriodIndex = 1; if (numberOfMonths == 1) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, lastPeriodIndex); } else if (numberOfMonths == 2) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, secondPeriodIndex); } fillPreviousNCsInLineItems(requisition, numberOfMonths, trackingDate); } }
CalculationService { public void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate) { List<ProcessingPeriod> fivePreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), MAX_NUMBER_OF_PERIODS_TO_TRACK); if (fivePreviousPeriods.size() == 0) { requisition.setFieldsAccordingToTemplateFrom(null, rnrTemplate, regimenTemplate); fillPreviousNCsInLineItems(requisition, requisition.getPeriod().getNumberOfMonths(), requisition.getPeriod().getStartDate()); return; } Rnr previousRequisition = requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), fivePreviousPeriods.get(0)); requisition.setFieldsAccordingToTemplateFrom(previousRequisition, rnrTemplate, regimenTemplate); Integer numberOfMonths = fivePreviousPeriods.get(0).getNumberOfMonths(); Date trackingDate = fivePreviousPeriods.get(0).getStartDate(); int lastPeriodIndex = MAX_NUMBER_OF_PERIODS_TO_TRACK - 1; int secondPeriodIndex = 1; if (numberOfMonths == 1) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, lastPeriodIndex); } else if (numberOfMonths == 2) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, secondPeriodIndex); } fillPreviousNCsInLineItems(requisition, numberOfMonths, trackingDate); }  }
CalculationService { public void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate) { List<ProcessingPeriod> fivePreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), MAX_NUMBER_OF_PERIODS_TO_TRACK); if (fivePreviousPeriods.size() == 0) { requisition.setFieldsAccordingToTemplateFrom(null, rnrTemplate, regimenTemplate); fillPreviousNCsInLineItems(requisition, requisition.getPeriod().getNumberOfMonths(), requisition.getPeriod().getStartDate()); return; } Rnr previousRequisition = requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), fivePreviousPeriods.get(0)); requisition.setFieldsAccordingToTemplateFrom(previousRequisition, rnrTemplate, regimenTemplate); Integer numberOfMonths = fivePreviousPeriods.get(0).getNumberOfMonths(); Date trackingDate = fivePreviousPeriods.get(0).getStartDate(); int lastPeriodIndex = MAX_NUMBER_OF_PERIODS_TO_TRACK - 1; int secondPeriodIndex = 1; if (numberOfMonths == 1) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, lastPeriodIndex); } else if (numberOfMonths == 2) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, secondPeriodIndex); } fillPreviousNCsInLineItems(requisition, numberOfMonths, trackingDate); }  void perform(Rnr requisition, ProgramRnrTemplate template); void fillReportingDays(Rnr requisition); void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate); void copySkippedFieldFromPreviousPeriod(Rnr requisition); }
CalculationService { public void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate) { List<ProcessingPeriod> fivePreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), MAX_NUMBER_OF_PERIODS_TO_TRACK); if (fivePreviousPeriods.size() == 0) { requisition.setFieldsAccordingToTemplateFrom(null, rnrTemplate, regimenTemplate); fillPreviousNCsInLineItems(requisition, requisition.getPeriod().getNumberOfMonths(), requisition.getPeriod().getStartDate()); return; } Rnr previousRequisition = requisitionRepository.getRegularRequisitionWithLineItems(requisition.getFacility(), requisition.getProgram(), fivePreviousPeriods.get(0)); requisition.setFieldsAccordingToTemplateFrom(previousRequisition, rnrTemplate, regimenTemplate); Integer numberOfMonths = fivePreviousPeriods.get(0).getNumberOfMonths(); Date trackingDate = fivePreviousPeriods.get(0).getStartDate(); int lastPeriodIndex = MAX_NUMBER_OF_PERIODS_TO_TRACK - 1; int secondPeriodIndex = 1; if (numberOfMonths == 1) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, lastPeriodIndex); } else if (numberOfMonths == 2) { trackingDate = getStartDateForNthPreviousPeriod(fivePreviousPeriods, secondPeriodIndex); } fillPreviousNCsInLineItems(requisition, numberOfMonths, trackingDate); }  void perform(Rnr requisition, ProgramRnrTemplate template); void fillReportingDays(Rnr requisition); void fillFieldsForInitiatedRequisition(Rnr requisition, ProgramRnrTemplate rnrTemplate, RegimenTemplate regimenTemplate); void copySkippedFieldFromPreviousPeriod(Rnr requisition); static final int MILLI_SECONDS_IN_ONE_DAY; static final int MAX_NUMBER_OF_PERIODS_TO_TRACK; }