diff --git a/src/integration-test/java/org/openlmis/buq/web/buq/BottomUpQuantificationControllerIntegrationTest.java b/src/integration-test/java/org/openlmis/buq/web/buq/BottomUpQuantificationControllerIntegrationTest.java index e20f8c2..a9597ad 100644 --- a/src/integration-test/java/org/openlmis/buq/web/buq/BottomUpQuantificationControllerIntegrationTest.java +++ b/src/integration-test/java/org/openlmis/buq/web/buq/BottomUpQuantificationControllerIntegrationTest.java @@ -59,6 +59,7 @@ import org.openlmis.buq.builder.BottomUpQuantificationDataBuilder; import org.openlmis.buq.builder.ProgramDtoDataBuilder; import org.openlmis.buq.domain.buq.BottomUpQuantification; +import org.openlmis.buq.dto.BottomUpQuantificationGroupCostsData; import org.openlmis.buq.dto.buq.BottomUpQuantificationDto; import org.openlmis.buq.dto.referencedata.ProgramDto; import org.openlmis.buq.i18n.MessageKeys; @@ -595,14 +596,14 @@ public void shouldReturnUnauthorizedForAuditLogEndpointIfUserIsNotAuthorized() { } @Test - public void shouldReturnBottomUpQuantificationsForFinalApproval() { + public void shouldReturnBottomUpQuantificationsForFinalApprovalWithGroupCosts() { mockUserHasAtLeastOneOfFollowingRights(PermissionService.MOH_PORALG_RIGHTS); - given(bottomUpQuantificationService.getBottomUpQuantificationsForFinalApproval( + given(bottomUpQuantificationService.getBottomUpQuantificationsForFinalApprovalWithGroupCosts( any(UUID.class), any(UUID.class), any(UUID.class), any(Pageable.class))) - .willReturn(new PageImpl<>(Collections.singletonList(bottomUpQuantification))); + .willReturn(Collections.singletonList(new BottomUpQuantificationGroupCostsData())); restAssured.given() .header(HttpHeaders.AUTHORIZATION, getTokenHeader()) @@ -613,9 +614,7 @@ public void shouldReturnBottomUpQuantificationsForFinalApproval() { .get(FOR_FINAL_APPROVAL_URL) .then() .statusCode(HttpStatus.SC_OK) - .body("content", Matchers.hasSize(1)) - .body("content[0].id", Matchers.is(bottomUpQuantification.getId().toString())) - .body("content[0].status", Matchers.is(bottomUpQuantification.getStatus().toString())); + .body("content", Matchers.hasSize(1)); assertThat(RAML_ASSERT_MESSAGE, restAssured.getLastReport(), RamlMatchers.hasNoViolations()); } diff --git a/src/main/java/org/openlmis/buq/dto/BottomUpQuantificationGroupCostsData.java b/src/main/java/org/openlmis/buq/dto/BottomUpQuantificationGroupCostsData.java new file mode 100644 index 0000000..097fc25 --- /dev/null +++ b/src/main/java/org/openlmis/buq/dto/BottomUpQuantificationGroupCostsData.java @@ -0,0 +1,32 @@ +/* + * This program is part of the OpenLMIS logistics management information system platform software. + * Copyright © 2017 VillageReach + * + * This program is free software: you can redistribute it and/or modify it under the terms + * of the GNU Affero General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Affero General Public License for more details. You should have received a copy of + * the GNU Affero General Public License along with this program. If not, see + * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org. + */ + +package org.openlmis.buq.dto; + +import java.util.Map; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.openlmis.buq.dto.buq.BottomUpQuantificationDto; + +@Getter +@Setter +@NoArgsConstructor +public class BottomUpQuantificationGroupCostsData { + + private BottomUpQuantificationDto bottomUpQuantification; + private Map calculatedGroupsCosts; + +} diff --git a/src/main/java/org/openlmis/buq/service/buq/BottomUpQuantificationService.java b/src/main/java/org/openlmis/buq/service/buq/BottomUpQuantificationService.java index f94a896..4c0dd36 100644 --- a/src/main/java/org/openlmis/buq/service/buq/BottomUpQuantificationService.java +++ b/src/main/java/org/openlmis/buq/service/buq/BottomUpQuantificationService.java @@ -58,6 +58,7 @@ import org.openlmis.buq.domain.buq.Rejection; import org.openlmis.buq.domain.productgroup.ProductGroup; import org.openlmis.buq.domain.sourceoffund.SourceOfFund; +import org.openlmis.buq.dto.BottomUpQuantificationGroupCostsData; import org.openlmis.buq.dto.ResultDto; import org.openlmis.buq.dto.buq.BottomUpQuantificationDto; import org.openlmis.buq.dto.buq.BottomUpQuantificationLineItemDto; @@ -753,6 +754,48 @@ public Page getBottomUpQuantificationsForFinalApproval( pageable); } + /** + * Retrieves bottom-up quantifications that are ready for final approval + * based on the specified processing period, program, geographic zone, + * and user permissions, along with group costs data. + * + * @param programId The UUID of the program for which quantifications are retrieved. + * @param processingPeriodId The UUID of the processing period. + * @param geographicZoneId The UUID of the geographic zone. + * @param pageable object used to encapsulate the pagination related values: page, + * size and sort. + * @return A page of {@link BottomUpQuantification} objects representing quantifications ready + * for final approval. + */ + public List + getBottomUpQuantificationsForFinalApprovalWithGroupCosts( + UUID programId, + UUID processingPeriodId, + UUID geographicZoneId, + Pageable pageable) { + Page bottomUpQuantifications = + getBottomUpQuantificationsForFinalApproval(programId, processingPeriodId, + geographicZoneId, pageable); + List productGroups = productGroupRepository.findAll(); + + return bottomUpQuantifications.stream() + .map(buq -> buildBottomUpQuantificationGroupCostsData(buq, productGroups)) + .collect(Collectors.toList()); + } + + private BottomUpQuantificationGroupCostsData buildBottomUpQuantificationGroupCostsData( + BottomUpQuantification bottomUpQuantification, List productGroups) { + BottomUpQuantificationGroupCostsData bottomUpQuantificationGroupCostsData = + new BottomUpQuantificationGroupCostsData(); + bottomUpQuantificationGroupCostsData.setBottomUpQuantification( + bottomUpQuantificationDtoBuilder.buildDto(bottomUpQuantification)); + bottomUpQuantificationGroupCostsData.setCalculatedGroupsCosts( + calculateProductGroupsCost(Collections.singletonList(bottomUpQuantification), + productGroups) + ); + return bottomUpQuantificationGroupCostsData; + } + private boolean isZoneInHierarchy(UUID targetZoneId, GeographicZoneDto zone) { if (zone == null) { return false; diff --git a/src/main/java/org/openlmis/buq/web/buq/BottomUpQuantificationController.java b/src/main/java/org/openlmis/buq/web/buq/BottomUpQuantificationController.java index 382b54c..42140c7 100644 --- a/src/main/java/org/openlmis/buq/web/buq/BottomUpQuantificationController.java +++ b/src/main/java/org/openlmis/buq/web/buq/BottomUpQuantificationController.java @@ -25,6 +25,7 @@ import org.openlmis.buq.ApproveFacilityForecastingStats; import org.openlmis.buq.domain.buq.BottomUpQuantification; import org.openlmis.buq.domain.buq.Rejection; +import org.openlmis.buq.dto.BottomUpQuantificationGroupCostsData; import org.openlmis.buq.dto.buq.BottomUpQuantificationDto; import org.openlmis.buq.dto.buq.RejectionDto; import org.openlmis.buq.dto.productgroup.ProductGroupsCostData; @@ -319,31 +320,26 @@ public Page getForApproval(Pageable pageable, } /** - * Get bottom-up quantifications to approve for right supervisor. + * Get bottom-up quantifications along with group costs data to approve for right supervisor. * * @param pageable object used to encapsulate the pagination related values: page, size and sort. - * @return List of bottom-up quantifications to approve. + * @return List of bottom-up quantifications to approve along with group costs data. */ @GetMapping(value = "/forFinalApproval") @ResponseStatus(HttpStatus.OK) @ResponseBody - public Page getForFinalApproval(Pageable pageable, + public Page getForFinalApprovalWithGroupCostsData( + Pageable pageable, @RequestParam(value = PROGRAM_ID) UUID programId, @RequestParam(value = PROCESSING_PERIOD_ID) UUID processingPeriodId, @RequestParam(value = GEOGRAPHIC_ZONE_ID) UUID geographicZoneId) { permissionService.hasAtLeastOnePermission(PermissionService.MOH_PORALG_RIGHTS); - Page bottomUpQuantificationsForFinalApproval = - bottomUpQuantificationService.getBottomUpQuantificationsForFinalApproval(programId, - processingPeriodId, geographicZoneId, pageable); - - List content = bottomUpQuantificationsForFinalApproval - .getContent() - .stream() - .map(buq -> bottomUpQuantificationDtoBuilder.buildDto(buq)) - .collect(Collectors.toList()); + List content = + bottomUpQuantificationService.getBottomUpQuantificationsForFinalApprovalWithGroupCosts( + programId, processingPeriodId, geographicZoneId, pageable); return Pagination.getPage(content, pageable, - bottomUpQuantificationsForFinalApproval.getTotalElements()); + content.size()); } /** diff --git a/src/main/resources/api-definition.yaml b/src/main/resources/api-definition.yaml index 3629569..6e381be 100644 --- a/src/main/resources/api-definition.yaml +++ b/src/main/resources/api-definition.yaml @@ -24,6 +24,8 @@ schemas: - bottomUpQuantification: !include schemas/bottomUpQuantification.json - bottomUpQuantificationPage: !include schemas/bottomUpQuantificationPage.json +- bottomUpQuantificationGroupCostsData: !include schemas/bottomUpQuantificationGroupCostsData.json +- bottomUpQuantificationGroupCostsDataPage: !include schemas/bottomUpQuantificationGroupCostsDataPage.json - sourceOfFund: !include schemas/sourceOfFund.json - sourceOfFundPage: !include schemas/sourceOfFundPage.json - remark: !include schemas/remark.json @@ -695,7 +697,7 @@ resourceTypes: Keep-Alive: body: application/json: - schema: bottomUpQuantificationPage + schema: bottomUpQuantificationGroupCostsData 400: body: application/json: diff --git a/src/main/resources/schemas/bottomUpQuantificationGroupCostsData.json b/src/main/resources/schemas/bottomUpQuantificationGroupCostsData.json new file mode 100644 index 0000000..cfae564 --- /dev/null +++ b/src/main/resources/schemas/bottomUpQuantificationGroupCostsData.json @@ -0,0 +1,23 @@ +{ + "type": "object", + "$schema": "http://json-schema.org/draft-04/schema", + "title": "BottomUpQuantificationGroupCostsData", + "description": "Single bottom up quantification group costs data instance", + "properties": { + "calculatedGroupsCosts": { + "type": [ + "object", + "null" + ], + "title": "calculatedGroupsCosts" + }, + "bottomUpQuantification": { + "type": [ + "object", + "null" + ], + "$ref": "bottomUpQuantification.json", + "title": "bottomUpQuantification" + } + } +} diff --git a/src/main/resources/schemas/bottomUpQuantificationGroupCostsDataPage.json b/src/main/resources/schemas/bottomUpQuantificationGroupCostsDataPage.json new file mode 100644 index 0000000..3dbeb2d --- /dev/null +++ b/src/main/resources/schemas/bottomUpQuantificationGroupCostsDataPage.json @@ -0,0 +1,60 @@ +{ + "type": "object", + "$schema": "http://json-schema.org/draft-04/schema", + "title": "Collection", + "description": "Paginated collection", + "properties": { + "content": { + "type": "array", + "items": { + "type": "array", + "$ref": "bottomUpQuantificationGroupCostsData.json" + } + }, + "totalPages": { + "type": "integer", + "title": "totalPages" + }, + "totalElements": { + "type": "integer", + "title": "totalElements" + }, + "size": { + "type": "integer", + "title": "size" + }, + "number": { + "type": "integer", + "title": "number" + }, + "numberOfElements": { + "type": "integer", + "title": "numberOfElements" + }, + "last": { + "type": "boolean", + "title": "last" + }, + "first": { + "type": "boolean", + "title": "first" + }, + "sort?": { + "title": "sort", + "type": "array", + "items": { + "type": "object" + } + } + }, + "required": [ + "content", + "totalPages", + "totalElements", + "size", + "number", + "numberOfElements", + "first", + "last" + ] +}