Skip to content

Commit

Permalink
#749 - added method to get account budget by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlocker8 committed Jun 22, 2023
1 parent 04b397d commit 09c7c6d
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import org.springframework.stereotype.Service;

import java.text.DecimalFormatSymbols;
import java.text.MessageFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -175,20 +177,36 @@ public int getAmount(Transaction transaction, Account account)

public int getCurrentAccountBudget()
{
Account currentAccount = getCurrentAccount();
final Account currentAccount = getCurrentAccount();
return getBudgetForAccount(currentAccount);
}

public int getAccountBudgetByID(Integer accountID)
{
final Optional<Account> accountOptional = accountRepository.findById(accountID);
if(accountOptional.isEmpty())
{
throw new IllegalArgumentException(MessageFormat.format("No account with ID \"{0)\" found", accountID));
}

final Account account = accountOptional.get();
return getBudgetForAccount(account);
}

private int getBudgetForAccount(Account account)
{
final LocalDate endDate = DateHelper.getCurrentDate();
List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(currentAccount, endDate, FilterConfiguration.DEFAULT);
List<Transaction> transactions = transactionService.getTransactionsForAccountUntilDate(account, endDate, FilterConfiguration.DEFAULT);

int sum = 0;
for(Transaction transaction : transactions)
{
sum += getAmount(transaction, currentAccount);
sum += getAmount(transaction, account);
}

return sum;
}


public List<RepeatingModifierType> getRepeatingModifierTypes()
{
return Arrays.asList(RepeatingModifierType.values());
Expand Down

0 comments on commit 09c7c6d

Please sign in to comment.