Skip to content

Firefly III: a personal finances manager

Add last activity column

James Cole3f77c845ca85
diff --git a/app/Transformers/V2/AccountTransformer.php b/app/Transformers/V2/AccountTransformer.php
index aef4ba7fe8..6b5808a324 100644
--- a/app/Transformers/V2/AccountTransformer.php
+++ b/app/Transformers/V2/AccountTransformer.php
@@ -29,9 +29,11 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
+use Illuminate\Support\Facades\DB;
/**
* Class AccountTransformer
@@ -39,6 +41,7 @@ use Illuminate\Support\Collection;
class AccountTransformer extends AbstractTransformer
{
private array $accountMeta;
+ private array $lastActivity;
private array $accountTypes;
private array $balances;
private array $convertedBalances;
@@ -50,9 +53,11 @@ class AccountTransformer extends AbstractTransformer
*/
public function collectMetaData(Collection $objects): Collection
{
+ // TODO separate methods
$this->currencies = [];
$this->accountMeta = [];
$this->accountTypes = [];
+ $this->lastActivity = [];
$this->balances = app('steam')->balancesByAccounts($objects, $this->getDate());
$this->convertedBalances = app('steam')->balancesByAccountsConverted($objects, $this->getDate());
@@ -62,6 +67,7 @@ class AccountTransformer extends AbstractTransformer
// get currencies:
$accountIds = $objects->pluck('id')->toArray();
+ // TODO move query to repository
$meta = AccountMeta::whereIn('account_id', $accountIds)
->whereIn('name', ['currency_id', 'account_role', 'account_number'])
->get(['account_meta.id', 'account_meta.account_id', 'account_meta.name', 'account_meta.data'])
@@ -79,6 +85,7 @@ class AccountTransformer extends AbstractTransformer
}
// get account types:
// select accounts.id, account_types.type from account_types left join accounts on accounts.account_type_id = account_types.id;
+ // TODO move query to repository
$accountTypes = AccountType::leftJoin('accounts', 'accounts.account_type_id', '=', 'account_types.id')
->whereIn('accounts.id', $accountIds)
->get(['accounts.id', 'account_types.type'])
@@ -89,6 +96,16 @@ class AccountTransformer extends AbstractTransformer
$this->accountTypes[$row->id] = (string)config(sprintf('firefly.shortNamesByFullName.%s', $row->type));
}
+ // get last activity
+ // TODO move query to repository
+ $array = Transaction::whereIn('account_id',$accountIds)
+ ->leftJoin('transaction_journals', 'transaction_journals.id', 'transactions.transaction_journal_id')
+ ->groupBy('transactions.account_id')
+ ->get(['transactions.account_id', DB::raw('MAX(transaction_journals.date) as date_max')])->toArray();
+ foreach($array as $row){
+ $this->lastActivity[(int)$row['account_id']] = Carbon::parse($row['date_max'], config('app.timezone'));
+ }
+
// TODO needs separate method.
/** @var null|array $sort */
$sort = $this->parameters->get('sort');
@@ -189,6 +206,7 @@ class AccountTransformer extends AbstractTransformer
'current_balance_date' => $this->getDate()->endOfDay()->toAtomString(),
// more meta
+ 'last_activity' => array_key_exists($id, $this->lastActivity) ? $this->lastActivity[$id]->toAtomString() : null,
// 'notes' => $this->repository->getNoteText($account),
// 'monthly_payment_date' => $monthlyPaymentDate,
diff --git a/resources/assets/v2/pages/accounts/index.js b/resources/assets/v2/pages/accounts/index.js
index 9a4da9963b..3bdd2a7ae0 100644
--- a/resources/assets/v2/pages/accounts/index.js
+++ b/resources/assets/v2/pages/accounts/index.js
@@ -93,13 +93,14 @@ let index = function () {
active: current.attributes.active,
name: current.attributes.name,
type: current.attributes.type,
- // role: current.attributes.account_role,
+ role: current.attributes.account_role,
iban: null === current.attributes.iban ? '' : current.attributes.iban.match(/.{1,4}/g).join(' '),
account_number: null === current.attributes.account_number ? '' : current.attributes.account_number,
current_balance: current.attributes.current_balance,
currency_code: current.attributes.currency_code,
native_current_balance: current.attributes.native_current_balance,
native_currency_code: current.attributes.native_currency_code,
+ last_activity: null === current.attributes.last_activity ? '' : format(new Date(current.attributes.last_activity),'P'),
};
this.accounts.push(account);
}
diff --git a/resources/views/v2/accounts/index.blade.php b/resources/views/v2/accounts/index.blade.php
index 4a1780cbc2..befa1f6387 100644
--- a/resources/views/v2/accounts/index.blade.php
+++ b/resources/views/v2/accounts/index.blade.php
@@ -124,8 +124,10 @@
<td>
<span x-text="formatMoney(account.current_balance, account.currency_code)"></span>
</td>
- <td>TODO</td>
- <td>TODO</td>
+ <td>
+ <span x-text="account.last_activity"></span>
+ </td>
+ <td>TODO 2 </td>
<td>&nbsp;</td>
</tr>
</template>