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;
}
Aggregations