@Test public void shouldCalculateDaysDifferenceUsingPreviousPeriodIfPreviousPeriodPresentButSecondPreviousPeriodIsNotPresent() throws Exception { Date authorizedDateOfPreviousLineItem = setLineItemDatesAndReturnDate(); Rnr rnr = getVirtualFacilityRnr(); RnrLineItem lineItem = rnr.getFullSupplyLineItems().get(0); ProcessingPeriod previousPeriod = new ProcessingPeriod(2l, new Date(), new Date(), 2, "previousPeriod"); when(processingScheduleService.getNPreviousPeriodsInDescOrder(rnr.getPeriod(), 2)).thenReturn(asList(previousPeriod)); when(requisitionRepository.getAuthorizedDateForPreviousLineItem(rnr, lineItem.getProductCode(), previousPeriod.getStartDate())).thenReturn(authorizedDateOfPreviousLineItem); calculationService.fillReportingDays(rnr); assertThat(lineItem.getReportingDays(), is(5)); verify(processingScheduleService).getNPreviousPeriodsInDescOrder(rnr.getPeriod(), 2); verify(requisitionRepository).getAuthorizedDateForPreviousLineItem(rnr, lineItem.getProductCode(), previousPeriod.getStartDate()); }
public void fillReportingDays(Rnr requisition) { Date startDate = requisition.getPeriod().getStartDate(); Integer numberOfMonths = requisition.getPeriod().getNumberOfMonths(); List<ProcessingPeriod> twoPreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 2); if (twoPreviousPeriods.size() != 0) { numberOfMonths = twoPreviousPeriods.get(0).getNumberOfMonths(); startDate = (numberOfMonths < 3 && twoPreviousPeriods.size() != 1) ? twoPreviousPeriods.get(1).getStartDate() : twoPreviousPeriods.get(0).getStartDate(); } for (RnrLineItem lineItem : requisition.getNonSkippedLineItems()) { Integer reportingDays = getReportingDaysBasedOnRequisition(requisition, lineItem.getProductCode(), startDate, numberOfMonths); lineItem.setReportingDays(reportingDays); } }
CalculationService { public void fillReportingDays(Rnr requisition) { Date startDate = requisition.getPeriod().getStartDate(); Integer numberOfMonths = requisition.getPeriod().getNumberOfMonths(); List<ProcessingPeriod> twoPreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 2); if (twoPreviousPeriods.size() != 0) { numberOfMonths = twoPreviousPeriods.get(0).getNumberOfMonths(); startDate = (numberOfMonths < 3 && twoPreviousPeriods.size() != 1) ? twoPreviousPeriods.get(1).getStartDate() : twoPreviousPeriods.get(0).getStartDate(); } for (RnrLineItem lineItem : requisition.getNonSkippedLineItems()) { Integer reportingDays = getReportingDaysBasedOnRequisition(requisition, lineItem.getProductCode(), startDate, numberOfMonths); lineItem.setReportingDays(reportingDays); } } }
CalculationService { public void fillReportingDays(Rnr requisition) { Date startDate = requisition.getPeriod().getStartDate(); Integer numberOfMonths = requisition.getPeriod().getNumberOfMonths(); List<ProcessingPeriod> twoPreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 2); if (twoPreviousPeriods.size() != 0) { numberOfMonths = twoPreviousPeriods.get(0).getNumberOfMonths(); startDate = (numberOfMonths < 3 && twoPreviousPeriods.size() != 1) ? twoPreviousPeriods.get(1).getStartDate() : twoPreviousPeriods.get(0).getStartDate(); } for (RnrLineItem lineItem : requisition.getNonSkippedLineItems()) { Integer reportingDays = getReportingDaysBasedOnRequisition(requisition, lineItem.getProductCode(), startDate, numberOfMonths); lineItem.setReportingDays(reportingDays); } }  }
CalculationService { public void fillReportingDays(Rnr requisition) { Date startDate = requisition.getPeriod().getStartDate(); Integer numberOfMonths = requisition.getPeriod().getNumberOfMonths(); List<ProcessingPeriod> twoPreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 2); if (twoPreviousPeriods.size() != 0) { numberOfMonths = twoPreviousPeriods.get(0).getNumberOfMonths(); startDate = (numberOfMonths < 3 && twoPreviousPeriods.size() != 1) ? twoPreviousPeriods.get(1).getStartDate() : twoPreviousPeriods.get(0).getStartDate(); } for (RnrLineItem lineItem : requisition.getNonSkippedLineItems()) { Integer reportingDays = getReportingDaysBasedOnRequisition(requisition, lineItem.getProductCode(), startDate, numberOfMonths); lineItem.setReportingDays(reportingDays); } }  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 fillReportingDays(Rnr requisition) { Date startDate = requisition.getPeriod().getStartDate(); Integer numberOfMonths = requisition.getPeriod().getNumberOfMonths(); List<ProcessingPeriod> twoPreviousPeriods = processingScheduleService.getNPreviousPeriodsInDescOrder(requisition.getPeriod(), 2); if (twoPreviousPeriods.size() != 0) { numberOfMonths = twoPreviousPeriods.get(0).getNumberOfMonths(); startDate = (numberOfMonths < 3 && twoPreviousPeriods.size() != 1) ? twoPreviousPeriods.get(1).getStartDate() : twoPreviousPeriods.get(0).getStartDate(); } for (RnrLineItem lineItem : requisition.getNonSkippedLineItems()) { Integer reportingDays = getReportingDaysBasedOnRequisition(requisition, lineItem.getProductCode(), startDate, numberOfMonths); lineItem.setReportingDays(reportingDays); } }  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; }