Skip to content
Permalink
Browse files
fixed total balance for teller
  • Loading branch information
mgeiss committed Sep 20, 2017
1 parent f2756be commit 050d214d715383938c40c696c9477620b4bef51f
Showing 1 changed file with 18 additions and 9 deletions.
@@ -15,7 +15,9 @@
*/
package io.mifos.teller.service.internal.service;

import io.mifos.accounting.api.v1.domain.AccountEntry;
import io.mifos.accounting.api.v1.domain.AccountEntryPage;
import io.mifos.accounting.api.v1.domain.AccountType;
import io.mifos.core.lang.DateConverter;
import io.mifos.teller.api.v1.domain.Teller;
import io.mifos.teller.api.v1.domain.TellerBalanceSheet;
@@ -70,20 +72,27 @@ public TellerBalanceSheet getBalance(final String tellerCode) {
optionalTellerEntity.ifPresent(tellerEntity -> {

final String accountIdentifier = tellerEntity.getTellerAccountIdentifier();
final LocalDateTime lastModifiedOn =
tellerEntity.getLastModifiedOn() != null ? tellerEntity.getLastModifiedOn() : LocalDateTime.now(Clock.systemUTC());
final LocalDate startDate = lastModifiedOn.toLocalDate();
final LocalDate endDate = lastModifiedOn.toLocalDate();
final LocalDate startDate = tellerEntity.getLastOpenedOn().toLocalDate();
final LocalDate endDate = LocalDate.now(Clock.systemUTC());
final String dateRange =
DateConverter.toIsoString(startDate) + ".." + DateConverter.toIsoString(endDate);

final List<TellerEntry> tellerEntries = this.fetchTellerEntries(accountIdentifier, dateRange, 0);

tellerBalanceSheet.setDay(startDate.format(DateTimeFormatter.BASIC_ISO_DATE));
tellerBalanceSheet.setBalance(
BigDecimal.valueOf(tellerEntries.stream().mapToDouble(value -> value.getAmount().doubleValue()).sum())
);
tellerBalanceSheet.setEntries(tellerEntries);
tellerBalanceSheet.setDay(startDate.format(DateTimeFormatter.BASIC_ISO_DATE));
final double sumDebits = tellerEntries
.stream()
.filter(tellerEntry -> tellerEntry.getType().equals(AccountEntry.Type.DEBIT.name()))
.mapToDouble(tellerEntry -> tellerEntry.getAmount().doubleValue())
.sum();

final double sumCredits = tellerEntries
.stream()
.filter(tellerEntry -> tellerEntry.getType().equals(AccountEntry.Type.CREDIT.name()))
.mapToDouble(tellerEntry -> tellerEntry.getAmount().doubleValue())
.sum();

tellerBalanceSheet.setBalance(BigDecimal.valueOf(sumDebits - sumCredits));
});
return tellerBalanceSheet;
}

0 comments on commit 050d214

Please sign in to comment.