use of org.estatio.module.lease.dom.invoicing.ssrs.InvoiceItemAttributesVM in project estatio by estatio.
the class InvoiceCalculationService method createInvoiceItems.
/**
* Calculates an invoice item with the difference between the already
* invoiced and calculated value.
*/
void createInvoiceItems(final LeaseTerm leaseTerm, final InvoiceCalculationParameters parameters, final List<CalculationResult> results) {
for (CalculationResult result : results) {
// values on a normal run
if (result.value().compareTo(BigDecimal.ZERO) != 0 || parameters.invoiceRunType().equals(InvoiceRunType.RETRO_RUN)) {
final LocalDateInterval invoicingInterval = result.invoicingInterval().asLocalDateInterval();
BigDecimal invoicedValue = invoiceItemForLeaseRepository.invoicedValue(leaseTerm, invoicingInterval);
BigDecimal newValue = result.value().subtract(invoicedValue);
//
LocalDateInterval calculationInterval = result.effectiveInterval();
LocalDateInterval effectiveInterval = calculationInterval;
Boolean adjustment = false;
if (newValue.compareTo(BigDecimal.ZERO) != 0) {
if (invoicedValue.compareTo(BigDecimal.ZERO) != 0) {
// Has been invoiced before
if (invoiceItemForLeaseRepository.findByLeaseTermAndEffectiveInterval(leaseTerm, calculationInterval).size() > 0) {
// this exact period has been invoiced before so it is an adjusment
adjustment = true;
} else {
// there is new calculated amount which is caused by tinkering the dates
effectiveInterval = attemptToCalculateRightSideLeftover(invoicingInterval, calculationInterval);
}
}
InvoiceItemForLease invoiceItem = invoiceItemForLeaseRepository.createUnapprovedInvoiceItem(leaseTerm, invoicingInterval, calculationInterval, effectiveInterval, parameters.invoiceDueDate(), interactionId);
invoiceItem.setNetAmount(newValue);
invoiceItem.setQuantity(BigDecimal.ONE);
LeaseItem leaseItem = leaseTerm.getLeaseItem();
Charge charge = leaseItem.getCharge();
invoiceItem.setCharge(charge);
invoiceItem.setTax(leaseItem.getEffectiveTax());
final InvoiceItemAttributesVM vm = new InvoiceItemAttributesVM(invoiceItem);
final String description = fragmentRenderService.render(vm, "description");
invoiceItem.setDescription(description);
invoiceItem.verify();
invoiceItem.setAdjustment(adjustment);
}
}
}
}
Aggregations