Skip to content

Commit

Permalink
[MODORDERS-1020] - Adjust Restricted Location + Fund validation rules (
Browse files Browse the repository at this point in the history
…#835)

[MODORDERS-1020] - Adjust Restricted Location + Fund validation rules
  • Loading branch information
imerabishvili authored Feb 8, 2024
1 parent 5f4a125 commit a0cd116
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public enum ErrorCodes {
PO_LINE_HAS_RELATED_APPROVED_INVOICE_ERROR("poLineHasRelatedApprovedInvoice", "Composite POL related invoice lines contains lines which has status APPROVED for the current fiscal year, invoice line ids: %s"),
MULTIPLE_FISCAL_YEARS("multipleFiscalYears", "Order line fund distributions have active budgets in multiple fiscal years."),
INSTANCE_INVALID_PRODUCT_ID_ERROR("instanceInvalidProductIdError", "Instance connection could not be changed, the chosen instance contains an invalid Product ID."),
FUND_LOCATION_RESTRICTION_VIOLATION("fundLocationRestrictionViolation", "One of the funds is restricted to be used for one of the locations."),
FUND_LOCATION_RESTRICTION_VIOLATION("fundLocationRestrictionViolation", "One of the locations is restricted to be used by all funds."),
ENCUMBRANCES_FOR_RE_ENCUMBER_NOT_FOUND("encumbrancesForReEncumberNotFound", "The encumbrances were correctly created during the rollover or have already been updated."),
CLAIMING_CONFIG_INVALID("claimingConfigInvalid", "Claiming interval should be set and greater than 0 if claiming is active"),
TEMPLATE_NAME_ALREADY_EXISTS("templateNameNotUnique", "Template name already exists");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.folio.rest.jaxrs.model.CompositePurchaseOrder.WorkflowStatus.PENDING;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -129,24 +128,37 @@ public Future<Void> checkFundLocationRestrictions(List<CompositePoLine> poLines,
}

private Future<Void> validateLocationRestrictions(CompositePoLine poLine, List<Fund> funds) {
List<String> polLocationIds = poLine.getLocations().stream().map(Location::getLocationId).toList();
for (Fund fund : funds) {
if (Boolean.TRUE.equals(fund.getRestrictByLocations()) && !CollectionUtils.containsAll(fund.getLocationIds(), polLocationIds)) {
String poLineId = poLine.getId();
String fundId = fund.getId();
Collection<String> restrictedLocations = CollectionUtils.subtract(polLocationIds, fund.getLocationIds());
logger.error("For POL {} fund {} is restricted to be used for locations {}", poLineId, fundId, restrictedLocations);
List<Parameter> parameters = List.of(
new Parameter().withKey("poLineId").withValue(poLineId),
new Parameter().withKey("poLineNumber").withValue(poLine.getPoLineNumber()),
new Parameter().withKey("fundId").withValue(fundId),
new Parameter().withKey("fundCode").withValue(fund.getCode()),
new Parameter().withKey("restrictedLocations").withValue(restrictedLocations.toString())
);
return Future.failedFuture(new HttpException(422, ErrorCodes.FUND_LOCATION_RESTRICTION_VIOLATION, parameters));
}
// TODO: will be removed in scope of https://folio-org.atlassian.net/browse/MODORDERS-981
// as we'll obtain funds once before the processing and check on emptiness
if (CollectionUtils.isEmpty(funds)) {
logger.info("No funds found for PO Line {}, skipping fund-location restrictions check.", poLine.getId());
return Future.succeededFuture();
}

List<String> restrictedLocations = poLine.getLocations()
.stream()
.map(Location::getLocationId)
.filter(locId -> isRestrictedByAllFunds(funds, locId))
.toList();

if (restrictedLocations.isEmpty()) {
return Future.succeededFuture();
}
return Future.succeededFuture();

String poLineId = poLine.getId();
logger.error("For POL {} locations {} are restricted to be used by all funds", poLineId, restrictedLocations);
List<Parameter> parameters = List.of(
new Parameter().withKey("poLineId").withValue(poLineId),
new Parameter().withKey("poLineNumber").withValue(poLine.getPoLineNumber()),
new Parameter().withKey("restrictedLocations").withValue(restrictedLocations.toString())
);
return Future.failedFuture(new HttpException(422, ErrorCodes.FUND_LOCATION_RESTRICTION_VIOLATION, parameters));
}

private boolean isRestrictedByAllFunds(List<Fund> funds, String locationId) {
return funds
.stream()
.allMatch(fund -> Boolean.TRUE.equals(fund.getRestrictByLocations()) && !fund.getLocationIds().contains(locationId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void testCheckFundLocationRestrictions(VertxTestContext vertxTestContext)

// given
List<String> fundIds = List.of("F1", "F2");
List<String> locationIds = List.of("L1", "L2", "L3");
List<String> locationIds = List.of("L1", "L2", "L3", "L4", "L5", "L6");
CompositePoLine poLine = new CompositePoLine()
.withId("ID")
.withPoLineNumber("number")
Expand All @@ -59,8 +59,8 @@ public void testCheckFundLocationRestrictions(VertxTestContext vertxTestContext)
);
Mockito.when(fundService.getFunds(fundIds, requestContext)).thenReturn(
Future.succeededFuture(List.of(
new Fund().withId("F1").withCode("FC").withRestrictByLocations(true).withLocationIds(List.of("L1", "L2", "L3", "L4")),
new Fund().withId("F2").withCode("FC").withRestrictByLocations(true).withLocationIds(List.of("L2"))
new Fund().withId("F1").withCode("FC").withRestrictByLocations(true).withLocationIds(List.of("L1", "L2", "L3")),
new Fund().withId("F2").withCode("FC").withRestrictByLocations(true).withLocationIds(List.of("L2", "L3", "L4"))
))
);

Expand All @@ -76,9 +76,7 @@ public void testCheckFundLocationRestrictions(VertxTestContext vertxTestContext)
List<Parameter> expectedParameters = List.of(
new Parameter().withKey("poLineId").withValue(poLine.getId()),
new Parameter().withKey("poLineNumber").withValue(poLine.getPoLineNumber()),
new Parameter().withKey("fundId").withValue("F2"),
new Parameter().withKey("fundCode").withValue("FC"),
new Parameter().withKey("restrictedLocations").withValue("[L1, L3]")
new Parameter().withKey("restrictedLocations").withValue("[L5, L6]")
);
assertEquals(FUND_LOCATION_RESTRICTION_VIOLATION.toError().withParameters(expectedParameters), exception.getError());
vertxTestContext.completeNow();
Expand Down

0 comments on commit a0cd116

Please sign in to comment.