Skip to content

Firefly III: a personal finances manager

Clean up account report helper.

James Coled0c92a2244e3
diff --git a/app/Helpers/Report/AccountReportHelper.php b/app/Helpers/Report/AccountReportHelper.php
index fdb5539ed9..5cf499285c 100644
--- a/app/Helpers/Report/AccountReportHelper.php
+++ b/app/Helpers/Report/AccountReportHelper.php
@@ -14,10 +14,11 @@ declare(strict_types = 1);
namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
-use DB;
use FireflyIII\Helpers\Collection\Account as AccountCollection;
use FireflyIII\Models\Account;
use Illuminate\Support\Collection;
+use Log;
+use Steam;
/**
@@ -27,63 +28,8 @@ use Illuminate\Support\Collection;
*/
class AccountReportHelper implements AccountReportHelperInterface
{
- /**
- * @param Account $account
- * @param Collection $startSet
- * @param Collection $endSet
- * @param Collection $backupSet
- *
- * @return Account
- */
- public static function reportFilter(Account $account, Collection $startSet, Collection $endSet, Collection $backupSet)
- {
- // The balance for today always incorporates transactions made on today. So to get todays "start" balance, we sub one day.
- $account->startBalance = '0';
- $account->endBalance = '0';
- $currentStart = $startSet->filter(
- function (Account $entry) use ($account) {
- return $account->id == $entry->id;
- }
- );
-
- $currentBackup = $backupSet->filter( // grab entry from current backup as well:
- function (Account $entry) use ($account) {
- return $account->id == $entry->id;
- }
- );
-
- // first try to set from backup
- if (!is_null($currentBackup->first())) {
- $account->startBalance = $currentBackup->first()->balance;
- }
-
- // overrule with data from start
- if (!is_null($currentStart->first())) {
- $account->startBalance = $currentStart->first()->balance;
- }
-
- $currentEnd = $endSet->filter(
- function (Account $entry) use ($account) {
- return $account->id == $entry->id;
- }
- );
-
- if (!is_null($currentEnd->first())) {
- $account->endBalance = $currentEnd->first()->balance;
- }
-
- return $account;
- }
/**
- * This method generates a full report for the given period on all
- * given accounts.
- *
- * a special consideration for accounts that did exist on this exact day.
- * we also grab the balance from today just in case, to see if that changes things.
- * it's a fall back for users who (rightly so) start keeping score at the first of
- * the month and find the first report lacking / broken.
- *
* @param Carbon $start
* @param Carbon $end
* @param Collection $accounts
@@ -98,13 +44,27 @@ class AccountReportHelper implements AccountReportHelperInterface
$ids = $accounts->pluck('id')->toArray();
$yesterday = clone $start;
$yesterday->subDay();
- $startSet = $this->getSet($ids, $yesterday); // get balances for start.
- $backupSet = $this->getSet($ids, $start);
- $endSet = $this->getSet($ids, $end);
-
+ $startSet = Steam::balancesById($ids, $yesterday);
+ $backupSet = Steam::balancesById($ids, $start);
+ $endSet = Steam::balancesById($ids, $end);
+
+ Log::debug(
+ sprintf(
+ 'getAccountReport from %s to %s for %d accounts.',
+ $start->format('Y-m-d'),
+ $end->format('Y-m-d'),
+ $accounts->count()
+ )
+ );
$accounts->each(
function (Account $account) use ($startSet, $endSet, $backupSet) {
- return self::reportFilter($account, $startSet, $endSet, $backupSet);
+ $account->startBalance = $startSet[$account->id] ?? '0';
+ $account->endBalance = $endSet[$account->id] ?? '0';
+
+ // check backup set just in case:
+ if ($account->startBalance === '0' && isset($backupSet[$account->id])) {
+ $account->startBalance = $backupSet[$account->id];
+ }
}
);
@@ -121,24 +81,7 @@ class AccountReportHelper implements AccountReportHelperInterface
$object->setDifference($diff);
$object->setAccounts($accounts);
- return $object;
- }
- /**
- * @param array $ids
- * @param Carbon $date
- *
- * @return Collection
- */
- private function getSet(array $ids, Carbon $date): Collection
- {
- return Account::leftJoin('transactions', 'transactions.account_id', '=', 'accounts.id')
- ->leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id')
- ->whereIn('accounts.id', $ids)
- ->whereNull('transaction_journals.deleted_at')
- ->whereNull('transactions.deleted_at')
- ->where('transaction_journals.date', '<=', $date->format('Y-m-d'))
- ->groupBy('accounts.id')
- ->get(['accounts.id', DB::raw('SUM(transactions.amount) AS balance')]);
+ return $object;
}
}
diff --git a/app/Repositories/Account/AccountTasker.php b/app/Repositories/Account/AccountTasker.php
index 6339e12290..d7811f9ff5 100644
--- a/app/Repositories/Account/AccountTasker.php
+++ b/app/Repositories/Account/AccountTasker.php
@@ -13,6 +13,8 @@ declare(strict_types = 1);
namespace FireflyIII\Repositories\Account;
+use FireflyIII\User;
+
/**
* Class AccountTasker
*
@@ -20,5 +22,16 @@ namespace FireflyIII\Repositories\Account;
*/
class AccountTasker
{
+ /** @var User */
+ private $user;
+ /**
+ * AttachmentRepository constructor.
+ *
+ * @param User $user
+ */
+ public function __construct(User $user)
+ {
+ $this->user = $user;
+ }
}
\ No newline at end of file