Search in sources :

Example 1 with VoucherLine

use of org.folio.rest.acq.model.VoucherLine in project mod-invoice by folio-org.

the class MockServer method handleGetVoucherLines.

private void handleGetVoucherLines(RoutingContext ctx) {
    logger.info("handleGetVoucherLines got: {}?{}", ctx.request().path(), ctx.request().query());
    String queryParam = StringUtils.trimToEmpty(ctx.request().getParam(QUERY));
    String tenant = ctx.request().getHeader(OKAPI_HEADER_TENANT);
    String voucherId = EMPTY;
    if (queryParam.contains(VOUCHER_ID)) {
        voucherId = queryParam.split(VOUCHER_ID + "==")[1];
    }
    if (queryParam.contains(BAD_QUERY)) {
        serverResponse(ctx, 400, APPLICATION_JSON, Response.Status.BAD_REQUEST.getReasonPhrase());
    } else if (queryParam.contains(ID_FOR_INTERNAL_SERVER_ERROR) || GET_VOUCHER_LINES_ERROR_TENANT.equals(tenant)) {
        serverResponse(ctx, 500, APPLICATION_JSON, Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase());
    } else {
        Supplier<List<VoucherLine>> getFromFile = () -> {
            try {
                return new JsonObject(getMockData(VOUCHER_LINES_COLLECTION)).mapTo(VoucherLineCollection.class).getVoucherLines();
            } catch (IOException e) {
                return Collections.emptyList();
            }
        };
        VoucherLineCollection voucherLineCollection = new VoucherLineCollection();
        List<VoucherLine> voucherLines = getMockEntries(VOUCHER_LINES, VoucherLine.class).orElseGet(getFromFile);
        Function<VoucherLine, String> voucherIdGetter = VoucherLine::getVoucherId;
        voucherLineCollection.setVoucherLines(filterEntriesByStringValue(voucherId, voucherLines, voucherIdGetter));
        voucherLineCollection.setTotalRecords(voucherLineCollection.getVoucherLines().size());
        JsonObject voucherLinesJson = JsonObject.mapFrom(voucherLineCollection);
        logger.info(voucherLinesJson.encodePrettily());
        addServerRqRsData(HttpMethod.GET, VOUCHER_LINES, voucherLinesJson);
        serverResponse(ctx, 200, APPLICATION_JSON, voucherLinesJson.encode());
    }
}
Also used : VoucherLine(org.folio.rest.acq.model.VoucherLine) Function(java.util.function.Function) VoucherLineCollection(org.folio.rest.acq.model.VoucherLineCollection) JsonObject(io.vertx.core.json.JsonObject) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) IOException(java.io.IOException)

Example 2 with VoucherLine

use of org.folio.rest.acq.model.VoucherLine in project mod-invoice by folio-org.

the class MockServer method handleGetVoucherLineById.

private void handleGetVoucherLineById(RoutingContext ctx) {
    logger.info("handleGetVoucherLinesById got: GET " + ctx.request().path());
    String id = ctx.request().getParam(AbstractHelper.ID);
    logger.info("id: " + id);
    try {
        String filePath = String.format(MOCK_DATA_PATH_PATTERN, VOUCHER_LINES_MOCK_DATA_PATH, id);
        JsonObject voucherLine = new JsonObject(getMockData(filePath));
        // validate content against schema
        VoucherLine voucherSchema = voucherLine.mapTo(VoucherLine.class);
        voucherSchema.setId(id);
        voucherLine = JsonObject.mapFrom(voucherSchema);
        addServerRqRsData(HttpMethod.GET, VOUCHER_LINES, voucherLine);
        serverResponse(ctx, 200, APPLICATION_JSON, voucherLine.encodePrettily());
    } catch (IOException e) {
        ctx.response().setStatusCode(404).end(id);
    }
}
Also used : VoucherLine(org.folio.rest.acq.model.VoucherLine) JsonObject(io.vertx.core.json.JsonObject) IOException(java.io.IOException)

Example 3 with VoucherLine

use of org.folio.rest.acq.model.VoucherLine in project mod-invoice by folio-org.

the class VoucherLinesRetrieveServiceTest method positiveGetInvoiceMapTest.

@Test
public void positiveGetInvoiceMapTest() throws IOException, ExecutionException, InterruptedException {
    VoucherLinesRetrieveService service = new VoucherLinesRetrieveService(okapiHeaders, context, "en");
    JsonObject vouchersList = new JsonObject(getMockData(VOUCHERS_LIST_PATH));
    List<Voucher> vouchers = vouchersList.getJsonArray("vouchers").stream().map(obj -> ((JsonObject) obj).mapTo(Voucher.class)).collect(toList());
    vouchers.remove(1);
    VoucherCollection voucherCollection = new VoucherCollection();
    voucherCollection.setVouchers(vouchers);
    CompletableFuture<Map<String, List<VoucherLine>>> future = service.getVoucherLinesMap(voucherCollection);
    Map<String, List<VoucherLine>> lineMap = future.get();
    Assertions.assertEquals(3, lineMap.get("a9b99f8a-7100-47f2-9903-6293d44a9905").size());
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) VoucherLineCollection(org.folio.rest.acq.model.VoucherLineCollection) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) ApiTestSuite.mockPort(org.folio.ApiTestSuite.mockPort) Context(io.vertx.core.Context) ApiTestBase(org.folio.rest.impl.ApiTestBase) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) VoucherLine(org.folio.rest.acq.model.VoucherLine) Voucher(org.folio.rest.jaxrs.model.Voucher) Map(java.util.Map) Assertions(org.junit.jupiter.api.Assertions) JsonObject(io.vertx.core.json.JsonObject) OKAPI_URL(org.folio.rest.RestConstants.OKAPI_URL) VoucherCollection(org.folio.rest.jaxrs.model.VoucherCollection) VoucherLine(org.folio.rest.acq.model.VoucherLine) JsonObject(io.vertx.core.json.JsonObject) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Voucher(org.folio.rest.jaxrs.model.Voucher) Test(org.junit.jupiter.api.Test)

Example 4 with VoucherLine

use of org.folio.rest.acq.model.VoucherLine in project mod-invoice by folio-org.

the class BatchVoucherGenerateService method buildBatchedVoucher.

private BatchedVoucher buildBatchedVoucher(Voucher voucher, Map<String, List<VoucherLine>> mapVoucherLines, Map<String, Invoice> mapInvoices, Map<String, List<InvoiceLine>> invoiceLinesMap, Map<String, Organization> vendorsMap) {
    BatchedVoucher batchedVoucher = new BatchedVoucher();
    batchedVoucher.setVoucherNumber(voucher.getVoucherNumber());
    batchedVoucher.setAccountingCode(voucher.getAccountingCode());
    batchedVoucher.setVoucherDate(voucher.getVoucherDate());
    batchedVoucher.setType(BatchedVoucher.Type.fromValue(voucher.getType().value()));
    batchedVoucher.setAmount(voucher.getAmount());
    batchedVoucher.setSystemCurrency(voucher.getSystemCurrency());
    batchedVoucher.setInvoiceCurrency(voucher.getInvoiceCurrency());
    batchedVoucher.setExchangeRate(voucher.getExchangeRate());
    batchedVoucher.setStatus(BatchedVoucher.Status.fromValue(voucher.getStatus().value()));
    Optional.ofNullable(voucher.getEnclosureNeeded()).ifPresentOrElse(batchedVoucher::setEnclosureNeeded, () -> batchedVoucher.setEnclosureNeeded(false));
    Optional.ofNullable(voucher.getAccountNo()).ifPresent(batchedVoucher::setAccountNo);
    Invoice invoice = mapInvoices.get(voucher.getInvoiceId());
    batchedVoucher.setFolioInvoiceNo(invoice.getFolioInvoiceNo());
    batchedVoucher.setVendorInvoiceNo(invoice.getVendorInvoiceNo());
    batchedVoucher.setInvoiceDate(invoice.getInvoiceDate());
    batchedVoucher.setInvoiceTerms(invoice.getPaymentTerms());
    batchedVoucher.setInvoiceNote(invoice.getNote());
    Organization organization = vendorsMap.get(invoice.getVendorId());
    batchedVoucher.setVendorName(organization.getName());
    List<Adjustment> adjustments = new ArrayList<>();
    List<InvoiceLine> invoiceLines = invoiceLinesMap.get(invoice.getId());
    List<String> invoiceAdjustmentIds = invoice.getAdjustments().stream().map(Adjustment::getId).collect(Collectors.toList());
    if (invoiceLines != null && !invoiceLines.isEmpty()) {
        for (InvoiceLine invoiceLine : invoiceLines) {
            adjustments.addAll(getInvoiceLineAdjustments(invoiceLine, invoiceAdjustmentIds, voucher.getExchangeRate()));
        }
    }
    List<Adjustment> invoiceAdjustmentToExport = invoice.getAdjustments().stream().filter(Adjustment::getExportToAccounting).map(adjustment -> calculateTotalAmount(adjustment, invoice.getSubTotal(), voucher.getExchangeRate())).collect(Collectors.toList());
    adjustments.addAll(invoiceAdjustmentToExport);
    batchedVoucher.setAdjustments(adjustments);
    List<Address> addresses = organization.getAddresses();
    if (addresses != null && !addresses.isEmpty()) {
        Address primaryAddress = addresses.stream().filter(a -> a.getIsPrimary() != null && a.getIsPrimary()).findFirst().orElse(addresses.get(0));
        batchedVoucher.setVendorAddress(addressConverter.convert(primaryAddress));
    }
    if (Objects.nonNull(voucher.getDisbursementNumber())) {
        batchedVoucher.setDisbursementNumber(voucher.getDisbursementNumber());
        batchedVoucher.setDisbursementDate(voucher.getDisbursementDate());
    }
    batchedVoucher.setDisbursementAmount(voucher.getDisbursementAmount());
    batchedVoucher.withBatchedVoucherLines(buildBatchedVoucherLines(voucher.getId(), mapVoucherLines));
    return batchedVoucher;
}
Also used : AddressConverter(org.folio.converters.AddressConverter) BatchVoucherGenerationException(org.folio.exceptions.BatchVoucherGenerationException) VoucherService(org.folio.rest.impl.VoucherService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) SpringContextUtil(org.folio.spring.SpringContextUtil) ArrayList(java.util.ArrayList) VoucherLine(org.folio.rest.acq.model.VoucherLine) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) BatchGroupHelper(org.folio.rest.impl.BatchGroupHelper) VoucherLinesRetrieveService(org.folio.services.VoucherLinesRetrieveService) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) JsonObject(io.vertx.core.json.JsonObject) VendorRetrieveService(org.folio.services.VendorRetrieveService) Vertx(io.vertx.core.Vertx) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) org.folio.rest.jaxrs.model(org.folio.rest.jaxrs.model) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FundDistribution(org.folio.rest.acq.model.FundDistribution) Address(org.folio.rest.acq.model.Address) Organization(org.folio.rest.acq.model.Organization) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) Optional(java.util.Optional) Organization(org.folio.rest.acq.model.Organization) Address(org.folio.rest.acq.model.Address) ArrayList(java.util.ArrayList)

Example 5 with VoucherLine

use of org.folio.rest.acq.model.VoucherLine in project mod-invoice by folio-org.

the class BatchVoucherGenerateService method buildBatchVoucher.

private CompletableFuture<BatchVoucher> buildBatchVoucher(BatchVoucherExport batchVoucherExport, VoucherCollection voucherCollection, Map<String, List<VoucherLine>> voucherLinesMap, Map<String, Invoice> invoiceMap, Map<String, List<InvoiceLine>> invoiceLinesMap, RequestContext requestContext) {
    List<Invoice> invoices = new ArrayList<>(invoiceMap.values());
    return vendorRetrieveService.getVendorsMap(invoices, requestContext).thenCombine(batchGroupHelper.getBatchGroup(batchVoucherExport.getBatchGroupId()), (vendorsMap, batchGroup) -> {
        BatchVoucher batchVoucher = new BatchVoucher();
        batchVoucher.setStart(batchVoucherExport.getStart());
        batchVoucher.setEnd(batchVoucherExport.getStart());
        List<BatchedVoucher> batchedVouchers = voucherCollection.getVouchers().stream().map(voucher -> buildBatchedVoucher(voucher, voucherLinesMap, invoiceMap, invoiceLinesMap, vendorsMap)).collect(toList());
        batchVoucher.setTotalRecords(batchedVouchers.size());
        batchVoucher.withBatchedVouchers(batchedVouchers);
        batchVoucher.setCreated(new Date());
        batchVoucher.setBatchGroup(batchGroup.getName());
        return batchVoucher;
    });
}
Also used : AddressConverter(org.folio.converters.AddressConverter) BatchVoucherGenerationException(org.folio.exceptions.BatchVoucherGenerationException) VoucherService(org.folio.rest.impl.VoucherService) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) CompletableFuture(java.util.concurrent.CompletableFuture) Context(io.vertx.core.Context) SpringContextUtil(org.folio.spring.SpringContextUtil) ArrayList(java.util.ArrayList) VoucherLine(org.folio.rest.acq.model.VoucherLine) InvoiceRetrieveService(org.folio.services.InvoiceRetrieveService) CompletableFuture.allOf(java.util.concurrent.CompletableFuture.allOf) BatchGroupHelper(org.folio.rest.impl.BatchGroupHelper) VoucherLinesRetrieveService(org.folio.services.VoucherLinesRetrieveService) Map(java.util.Map) RequestContext(org.folio.rest.core.models.RequestContext) JsonObject(io.vertx.core.json.JsonObject) VendorRetrieveService(org.folio.services.VendorRetrieveService) Vertx(io.vertx.core.Vertx) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) org.folio.rest.jaxrs.model(org.folio.rest.jaxrs.model) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FundDistribution(org.folio.rest.acq.model.FundDistribution) Address(org.folio.rest.acq.model.Address) Organization(org.folio.rest.acq.model.Organization) InvoiceLinesRetrieveService(org.folio.services.InvoiceLinesRetrieveService) Optional(java.util.Optional) ArrayList(java.util.ArrayList) Date(java.util.Date)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)6 VoucherLine (org.folio.rest.acq.model.VoucherLine)6 List (java.util.List)5 Collectors.toList (java.util.stream.Collectors.toList)5 Context (io.vertx.core.Context)4 Vertx (io.vertx.core.Vertx)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 IOException (java.io.IOException)3 Date (java.util.Date)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 CompletableFuture.allOf (java.util.concurrent.CompletableFuture.allOf)3 Collectors (java.util.stream.Collectors)3 AddressConverter (org.folio.converters.AddressConverter)3 BatchVoucherGenerationException (org.folio.exceptions.BatchVoucherGenerationException)3 Address (org.folio.rest.acq.model.Address)3 FundDistribution (org.folio.rest.acq.model.FundDistribution)3 Organization (org.folio.rest.acq.model.Organization)3