Created
September 24, 2021 23:59
-
-
Save algorythmist/31cb52b75fec0ce6fd48a10effc9010a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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