use of org.folio.rest.acq.model.Organization 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;
}
use of org.folio.rest.acq.model.Organization in project mod-invoice by folio-org.
the class InvoiceHelper method approveInvoice.
/**
* Handles transition of given invoice to {@link Invoice.Status#APPROVED} status.
* Transition triggers if the current {@link Invoice.Status} is {@link Invoice.Status#REVIEWED} or {@link Invoice.Status#OPEN}
* and exist at least one {@link InvoiceLine} associated with this invoice
*
* @param invoice {@link Invoice}to be approved
* @return CompletableFuture that indicates when transition is completed
*/
private CompletableFuture<Void> approveInvoice(Invoice invoice, List<InvoiceLine> lines) {
invoice.setApprovalDate(new Date());
invoice.setApprovedBy(invoice.getMetadata().getUpdatedByUserId());
RequestContext requestContext = new RequestContext(ctx, okapiHeaders);
return configurationService.getConfigurationsEntries(requestContext, SYSTEM_CONFIG_QUERY, VOUCHER_NUMBER_PREFIX_CONFIG_QUERY).thenCompose(ok -> updateInvoiceLinesWithEncumbrances(lines, requestContext)).thenCompose(v -> invoiceLineService.persistInvoiceLines(lines, requestContext)).thenCompose(v -> vendorService.getVendor(invoice.getVendorId(), requestContext)).thenAccept(organization -> validateBeforeApproval(organization, invoice, lines)).thenCompose(aVoid -> getInvoiceWorkflowDataHolders(invoice, lines, requestContext)).thenCompose(holders -> budgetExpenseClassService.checkExpenseClasses(holders, requestContext)).thenCompose(holders -> pendingPaymentWorkflowService.handlePendingPaymentsCreation(holders, requestContext)).thenCompose(v -> prepareVoucher(invoice)).thenCompose(voucher -> updateVoucherWithSystemCurrency(voucher, lines)).thenCompose(voucher -> voucherCommandService.updateVoucherWithExchangeRate(voucher, invoice, requestContext)).thenCompose(voucher -> getAllFundDistributions(lines, invoice).thenCompose(fundDistributions -> handleVoucherWithLines(fundDistributions, voucher)));
}
Aggregations