Skip to content

Instantly share code, notes, and snippets.

@algorythmist
Created September 24, 2021 23:59
Show Gist options
  • Save algorythmist/31cb52b75fec0ce6fd48a10effc9010a to your computer and use it in GitHub Desktop.
Save algorythmist/31cb52b75fec0ce6fd48a10effc9010a to your computer and use it in GitHub Desktop.
public Optional<Invoice> createClientInvoice(String clientId, LocalDate date) {
List<Fee> fees = feeRepository.findByClientId(clientId);
if (fees.isEmpty()) {
return Optional.empty();
}
Optional<Contract> optionalContract = contractRepository.findByClientId(clientId);
if (optionalContract.isEmpty()) {
return Optional.empty();
}
Contract contract = optionalContract.get();
String invoiceCurrency = contract.getInvoiceCurrency().toString();
CurrencyConversion conversion = exchangeRateProvider.getCurrencyConversion(invoiceCurrency);
MonetaryAmount total =
fees.stream()
.map(fee -> conversion.apply(fee.getAmount()))
.reduce(Money.of(0, invoiceCurrency), MonetaryAmount::add);
total = total.subtract(total.multiply(contract.getDiscountPercent()).divide(BigDecimal.valueOf(100)));
total = total.with(Monetary.getDefaultRounding());
Invoice invoice = createInvoice(clientId, date, total);
return Optional.of(invoice);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment