Search in sources :

Example 1 with Address

use of org.folio.rest.acq.model.Address 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)

Aggregations

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