@Test public void sdpShouldNotInitiateRnrIfRnrAlreadyExists() throws Exception{ RnrLineItem rnrLineItem = make(a(defaultRnrLineItem, with(productCode, "P10"))); List<RnrLineItem> products = asList(rnrLineItem); requisition.setFullSupplyLineItems(products); requisition.setProgram(new Program()); RegimenLineItem regimenLineItem = make(a(defaultRegimenLineItem)); requisition.setRegimenLineItems(asList(regimenLineItem)); report.setProducts(products); RegimenLineItem reportRegimenLineItem = make(a(defaultRegimenLineItem, with(patientsOnTreatment, 10), with(patientsStoppedTreatment, 5))); report.setRegimens(asList(reportRegimenLineItem)); report.setPeriodId(1L); report.setEmergency(false); Long facility_id = 5L; ProgramSupported programSupported = make(a(defaultProgramSupported)); Facility facility = make(a(defaultFacility, with(facilityId, facility_id), with(programSupportedList, asList(programSupported)), with(virtualFacility, true))); when(facilityService.getOperativeFacilityByCode(DEFAULT_AGENT_CODE)).thenReturn(facility); when(programService.getValidatedProgramByCode(DEFAULT_PROGRAM_CODE)).thenReturn(new Program(PROGRAM_ID)); expectedException.expect(DataException.class); doThrow(new DataException("rnr.error")).when(restRequisitionCalculator).validateCustomPeriod(any(Facility.class), any(Program.class), any(ProcessingPeriod.class), any(Long.class)); ArrayList<ProcessingPeriod> array = new ArrayList<>(); when(requisitionService.getRequisitionsFor(any(RequisitionSearchCriteria.class), any(array.getClass()))).thenReturn(asList(new Rnr())); service.submitSdpReport(report, 1L); verify(requisitionService, never()).initiate(any(Facility.class), any(Program.class), any(Long.class), any(Boolean.class), any(ProcessingPeriod.class)); }
@Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); } }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  @Transactional Rnr submitReport(Report report, Long userId); @Transactional Rnr submitSdpReport(Report report, Long userId); @Transactional void approve(Report report, Long requisitionId, Long userId); ReplenishmentDTO getReplenishmentDetails(Long id); List<Report> getRequisitionsByFacility(String facilityCode); }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  @Transactional Rnr submitReport(Report report, Long userId); @Transactional Rnr submitSdpReport(Report report, Long userId); @Transactional void approve(Report report, Long requisitionId, Long userId); ReplenishmentDTO getReplenishmentDetails(Long id); List<Report> getRequisitionsByFacility(String facilityCode); static final boolean EMERGENCY; }