From c2dfbcba1025e947bf9b3288f6b444e628adac37 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Mar 2018 14:09:44 +0100 Subject: [PATCH] Improve view for transactions. --- .../Controllers/TransactionController.php | 34 ++++- .../Models/TransactionJournalTrait.php | 85 ------------ app/Support/Twig/Extension/Transaction.php | 12 +- resources/views/transactions/show.twig | 130 ++++++++++-------- 4 files changed, 103 insertions(+), 158 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index d6be82467f..e5aea753ad 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -33,10 +33,12 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\Journal\JournalTaskerInterface; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; use FireflyIII\Support\CacheProperties; +use FireflyIII\Transformers\TransactionTransformer; use Illuminate\Http\Request; use Illuminate\Support\Collection; use Log; use Preferences; +use Symfony\Component\HttpFoundation\ParameterBag; use View; /** @@ -177,6 +179,7 @@ class TransactionController extends Controller * @param JournalTaskerInterface $tasker * @param LinkTypeRepositoryInterface $linkTypeRepository * + * @throws FireflyException * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View */ public function show(TransactionJournal $journal, JournalTaskerInterface $tasker, LinkTypeRepositoryInterface $linkTypeRepository) @@ -184,15 +187,32 @@ class TransactionController extends Controller if ($this->isOpeningBalance($journal)) { return $this->redirectToAccount($journal); } - if (TransactionType::RECONCILIATION === $journal->transactionType->type) { + $transactionType = $journal->transactionType->type; + if (TransactionType::RECONCILIATION === $transactionType) { return redirect(route('accounts.reconcile.show', [$journal->id])); // @codeCoverageIgnore } - $linkTypes = $linkTypeRepository->get(); - $links = $linkTypeRepository->getLinks($journal); - $events = $tasker->getPiggyBankEvents($journal); - $transactions = $tasker->getTransactionsOverview($journal); - $what = strtolower($journal->transaction_type_type ?? $journal->transactionType->type); - $subTitle = trans('firefly.' . $what) . ' "' . $journal->description . '"'; + $linkTypes = $linkTypeRepository->get(); + $links = $linkTypeRepository->getLinks($journal); + + // get transactions using the collector: + // needs a lot of extra data to match the journal collector. Or just expand that one. + // collect transactions using the journal collector + $collector = app(JournalCollectorInterface::class); + $collector->setUser(auth()->user()); + $collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation(); + // filter on specific journals. + $collector->setJournals(new Collection([$journal])); + $set = $collector->getJournals(); + $transactions = []; + $transformer = new TransactionTransformer(new ParameterBag); + /** @var Transaction $transaction */ + foreach ($set as $transaction) { + $transactions[] = $transformer->transform($transaction); + } + + $events = $tasker->getPiggyBankEvents($journal); + $what = strtolower($transactionType); + $subTitle = trans('firefly.' . $what) . ' "' . $journal->description . '"'; return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links')); } diff --git a/app/Support/Models/TransactionJournalTrait.php b/app/Support/Models/TransactionJournalTrait.php index a782e669a1..943d5d9568 100644 --- a/app/Support/Models/TransactionJournalTrait.php +++ b/app/Support/Models/TransactionJournalTrait.php @@ -24,11 +24,9 @@ namespace FireflyIII\Support\Models; use Carbon\Carbon; use FireflyIII\Models\Transaction; -use FireflyIII\Models\TransactionJournalMeta; use FireflyIII\Models\TransactionType; use FireflyIII\Support\CacheProperties; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\Collection; @@ -63,16 +61,6 @@ trait TransactionJournalTrait return false; } - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - abstract public function budgets(): BelongsToMany; - - /** - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - abstract public function categories(): BelongsToMany; - /** * @deprecated * @return Collection @@ -117,79 +105,6 @@ trait TransactionJournalTrait return $list; } - /** - * @deprecated - * @param string $name - * - * @return string - */ - abstract public function getMeta(string $name); - - /** - * - * @return bool - */ - abstract public function isDeposit(): bool; - - /** - * @return bool - */ - abstract public function isOpeningBalance(): bool; - - /** - * @return bool - */ - abstract public function isTransfer(): bool; - - /** - * @return bool - */ - abstract public function isWithdrawal(): bool; - - /** - * @return \Illuminate\Database\Eloquent\Relations\HasMany - */ - abstract public function piggyBankEvents(): HasMany; - - /** - * @deprecated - * @return int - */ - public function piggyBankId(): int - { - if ($this->piggyBankEvents()->count() > 0) { - return $this->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id; - } - - return 0; - } - - /** - * @deprecated - * @return Transaction - */ - public function positiveTransaction(): Transaction - { - return $this->transactions()->where('amount', '>', 0)->first(); - } - - /** - * Save the model to the database. - * - * @param array $options - * - * @return bool - */ - abstract public function save(array $options = []): bool; - - /** - * @param string $name - * @param $value - * - * @return TransactionJournalMeta - */ - abstract public function setMeta(string $name, $value): TransactionJournalMeta; - /** * @deprecated * @return Collection diff --git a/app/Support/Twig/Extension/Transaction.php b/app/Support/Twig/Extension/Transaction.php index 2d8bb0d66b..9e8c7bb071 100644 --- a/app/Support/Twig/Extension/Transaction.php +++ b/app/Support/Twig/Extension/Transaction.php @@ -101,17 +101,15 @@ class Transaction extends Twig_Extension public function amountArray(array $transaction): string { // first display amount: - $amount = TransactionType::WITHDRAWAL === $transaction['journal_type'] ? $transaction['source_amount'] - : $transaction['destination_amount']; + $amount = strval($transaction['amount']); $fakeCurrency = new TransactionCurrency; - $fakeCurrency->decimal_places = $transaction['transaction_currency_dp']; - $fakeCurrency->symbol = $transaction['transaction_currency_symbol']; + $fakeCurrency->decimal_places = $transaction['currency_dp']; + $fakeCurrency->symbol = $transaction['currency_symbol']; $string = app('amount')->formatAnything($fakeCurrency, $amount, true); // then display (if present) the foreign amount: - if (null !== $transaction['foreign_source_amount']) { - $amount = TransactionType::WITHDRAWAL === $transaction['journal_type'] ? $transaction['foreign_source_amount'] - : $transaction['foreign_destination_amount']; + if (null !== $transaction['foreign_amount']) { + $amount = strval($transaction['foreign_amount']); $fakeCurrency = new TransactionCurrency; $fakeCurrency->decimal_places = $transaction['foreign_currency_dp']; $fakeCurrency->symbol = $transaction['foreign_currency_symbol']; diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig index d0cd6c494e..0867e62dcb 100644 --- a/resources/views/transactions/show.twig +++ b/resources/views/transactions/show.twig @@ -88,10 +88,13 @@ {{ journal|journalTotalAmount }} {# if more transactions, list each one: #} - {% if transactions|length > 1 %} - ({% for transaction in transactions %}{{ transaction|transactionArrayAmount }}{% if loop.index != loop.length %}, {% endif %}{% endfor %}) - {% endif %} + {% if transactions|length > 2 %} + ({% for transaction in transactions %} + {{ transaction|transactionArrayAmount }} + {% if loop.index != loop.length %}, {% endif %} + {% endfor %}) + {% endif %} @@ -366,64 +369,73 @@

{{ 'transactions'|_ }}

- - - - - - - - - - - - - - - {% for transaction in transactions %} +
+
{{ trans('list.source_account') }}{{ trans('list.destination_account') }}{{ trans('list.amount') }}
+ - - - - - - - - + + + + + + - {% endfor %} - -
- {% if transaction.source_account_type == 'Cash account' %} - ({{ 'cash'|_ }}) - {% else %} - {{ transaction.source_account_name }} - {% endif %} - - - {% if transaction.destination_account_type == 'Cash account' %} - ({{ 'cash'|_ }}) - {% else %} - {{ transaction.destination_account_name }} - {% endif %} - - - {{ transaction|transactionArrayAmount }} - {{ trans('list.source_account') }}{{ trans('list.destination_account') }}{{ trans('list.amount') }}
+ + + + {% set maxIdentifier = ((transactions|length) / 2) -1 %} + {% for x in 0..maxIdentifier %} + + {# loop each transaction in the array.#} + {% for transaction in transactions %} + {% if + ((transaction.type == 'Withdrawal') and transaction.identifier == x and transaction.amount < 0) + or + ((transaction.type == 'Deposit' or transaction.type == 'Transfer') and transaction.identifier == x and transaction.amount > 0) + %} + + {% if transaction.description == "" %} + {{ journal.description }} + {% else %} + {{ transaction.description }} + {% endif %} + + + {% if transaction.source_type == 'Cash account' %} + ({{ 'cash'|_ }}) + {% else %} + {{ transaction.source_name }} + {% endif %} + + + {% if transaction.destination_type == 'Cash account' %} + ({{ 'cash'|_ }}) + {% else %} + {{ transaction.destination_name }} + {% endif %} + + + + {{ transaction|transactionArrayAmount }} + + + {% if transaction.budget_id %} + {{ transaction.budget_name }} + {% endif %} + + + {% if transaction.category_id %} + {{ transaction.category_name }} + {% endif %} + + {% endif %} + {% endfor %} + + + {% endfor %} + + +