Skip to content

Firefly III: a personal finances manager

Add some debug, fix balance report bug.

James Cole8fb9577660ed
diff --git a/app/Helpers/Report/BalanceReportHelper.php b/app/Helpers/Report/BalanceReportHelper.php
index acd623a2a9..18cdd71acc 100644
--- a/app/Helpers/Report/BalanceReportHelper.php
+++ b/app/Helpers/Report/BalanceReportHelper.php
@@ -26,6 +26,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Collection;
+use Log;
/**
* Class BalanceReportHelper
@@ -59,19 +60,23 @@ class BalanceReportHelper implements BalanceReportHelperInterface
*/
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance
{
+ Log::debug('Start of balance report');
$balance = new Balance;
$header = new BalanceHeader;
$limitRepetitions = $this->budgetRepository->getAllBudgetLimitRepetitions($start, $end);
foreach ($accounts as $account) {
+ Log::debug(sprintf('Add account %s to headers.', $account->name));
$header->addAccount($account);
}
/** @var LimitRepetition $repetition */
foreach ($limitRepetitions as $repetition) {
$budget = $this->budgetRepository->find($repetition->budget_id);
- $line = $this->createBalanceLine($budget, $repetition, $accounts);
+ Log::debug(sprintf('Create balance line for budget #%d ("%s") and repetition #%d', $budget->id, $budget->name, $repetition->id));
+ $line = $this->createBalanceLine($budget, $repetition, $accounts);
$balance->addBalanceLine($line);
}
+ Log::debug('Create rest of the things.');
$noBudgetLine = $this->createNoBudgetLine($accounts, $start, $end);
$coveredByTagLine = $this->createTagsBalanceLine($accounts, $start, $end);
$leftUnbalancedLine = $this->createLeftUnbalancedLine($noBudgetLine, $coveredByTagLine);
@@ -81,9 +86,12 @@ class BalanceReportHelper implements BalanceReportHelperInterface
$balance->addBalanceLine($leftUnbalancedLine);
$balance->setBalanceHeader($header);
+ Log::debug('Clear unused budgets.');
// remove budgets without expenses from balance lines:
$balance = $this->removeUnusedBudgets($balance);
+ Log::debug('Return report.');
+
return $balance;
}
diff --git a/app/Repositories/Budget/BudgetRepository.php b/app/Repositories/Budget/BudgetRepository.php
index b672444fbd..92645343aa 100644
--- a/app/Repositories/Budget/BudgetRepository.php
+++ b/app/Repositories/Budget/BudgetRepository.php
@@ -203,9 +203,24 @@ class BudgetRepository implements BudgetRepositoryInterface
{
$query = LimitRepetition::leftJoin('budget_limits', 'limit_repetitions.budget_limit_id', '=', 'budget_limits.id')
->leftJoin('budgets', 'budgets.id', '=', 'budget_limits.budget_id')
- ->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'))
- ->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'))
- ->where('budgets.user_id', $this->user->id);
+ ->where(
+ function (Builder $q1) use ($start, $end) {
+ $q1->where(
+ function (Builder $q2) use ($start, $end) {
+ $q2->where('limit_repetitions.enddate', '>=', $start->format('Y-m-d 00:00:00'));
+ $q2->where('limit_repetitions.enddate', '<=', $end->format('Y-m-d 00:00:00'));
+ }
+ )
+ ->orWhere(
+ function (Builder $q3) use ($start, $end) {
+ $q3->where('limit_repetitions.startdate', '>=', $start->format('Y-m-d 00:00:00'));
+ $q3->where('limit_repetitions.startdate', '<=', $end->format('Y-m-d 00:00:00'));
+ }
+ );
+ }
+ )
+ ->where('budgets.user_id', $this->user->id)
+ ->whereNull('budgets.deleted_at');
$set = $query->get(['limit_repetitions.*', 'budget_limits.budget_id']);
@@ -383,10 +398,11 @@ class BudgetRepository implements BudgetRepositoryInterface
public function spentInPeriod(Collection $budgets, Collection $accounts, Carbon $start, Carbon $end): string
{
// collect amount of transaction journals, which is easy:
- $budgetIds = $budgets->pluck('id')->toArray();
+ $budgetIds = $budgets->pluck('id')->toArray();
+
$accountIds = $accounts->pluck('id')->toArray();
- Log::debug('spentInPeriod: Now in spentInPeriod for these budgets: ', $budgetIds);
+ Log::debug(sprintf('spentInPeriod: Now in spentInPeriod for these budgets (%d): ', count($budgetIds)), $budgetIds);
Log::debug('spentInPeriod: and these accounts: ', $accountIds);
Log::debug(sprintf('spentInPeriod: Start date is "%s", end date is "%s"', $start->format('Y-m-d'), $end->format('Y-m-d')));