From 49de4f2200ac436e13588069eafa6db459b95744 Mon Sep 17 00:00:00 2001 From: James Cole Date: Tue, 26 Jun 2018 19:26:10 +0200 Subject: [PATCH] Expand report email. --- app/Jobs/CreateRecurringTransactions.php | 2 +- app/Models/Transaction.php | 1 + .../Twig/Extension/TransactionJournal.php | 52 +++++++++++++++---- app/Support/Twig/Journal.php | 1 + .../emails/report-new-journals-html.twig | 8 +-- .../emails/report-new-journals-text.twig | 4 +- 6 files changed, 53 insertions(+), 15 deletions(-) diff --git a/app/Jobs/CreateRecurringTransactions.php b/app/Jobs/CreateRecurringTransactions.php index 5c378811d4..65a42a6ea9 100644 --- a/app/Jobs/CreateRecurringTransactions.php +++ b/app/Jobs/CreateRecurringTransactions.php @@ -90,7 +90,7 @@ class CreateRecurringTransactions implements ShouldQueue // will now send email to users. foreach ($result as $userId => $journals) { //// random bunch to make mail. - $journals = TransactionJournal::where('user_id', $userId)->inRandomOrder()->take(1)->get(); + //$journals = TransactionJournal::where('user_id', $userId)->inRandomOrder()->take(1)->get(); event(new RequestedReportOnJournals($userId, $journals)); } diff --git a/app/Models/Transaction.php b/app/Models/Transaction.php index 2a9ffa0a69..4742467e29 100644 --- a/app/Models/Transaction.php +++ b/app/Models/Transaction.php @@ -73,6 +73,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property bool $is_split * @property int $attachmentCount * @property int $transaction_currency_id + * @property int $foreign_currency_id */ class Transaction extends Model { diff --git a/app/Support/Twig/Extension/TransactionJournal.php b/app/Support/Twig/Extension/TransactionJournal.php index c9b56a3d8b..66f05708d3 100644 --- a/app/Support/Twig/Extension/TransactionJournal.php +++ b/app/Support/Twig/Extension/TransactionJournal.php @@ -96,10 +96,51 @@ class TransactionJournal extends Twig_Extension * @return string */ public function totalAmount(JournalModel $journal): string + { + $type = $journal->transactionType->type; + $totals = $this->getTotalAmount($journal); + $array = []; + foreach ($totals as $total) { + if (TransactionType::WITHDRAWAL === $type) { + $total['amount'] = bcmul($total['amount'], '-1'); + } + $array[] = app('amount')->formatAnything($total['currency'], $total['amount']); + } + + return implode(' / ', $array); + } + + /** + * @param JournalModel $journal + * + * @return string + */ + public function totalAmountPlain(JournalModel $journal): string + { + $type = $journal->transactionType->type; + $totals = $this->getTotalAmount($journal); + $array = []; + + foreach ($totals as $total) { + if (TransactionType::WITHDRAWAL === $type) { + $total['amount'] = bcmul($total['amount'], '-1'); + } + $array[] = app('amount')->formatAnything($total['currency'], $total['amount'], false); + } + + return implode(' / ', $array); + } + + /** + * @param JournalModel $journal + * + * @return string + */ + private function getTotalAmount(JournalModel $journal): array { $transactions = $journal->transactions()->where('amount', '>', 0)->get(); $totals = []; - $type = $journal->transactionType->type; + /** @var TransactionModel $transaction */ foreach ($transactions as $transaction) { $currencyId = $transaction->transaction_currency_id; @@ -128,14 +169,7 @@ class TransactionJournal extends Twig_Extension ); } } - $array = []; - foreach ($totals as $total) { - if (TransactionType::WITHDRAWAL === $type) { - $total['amount'] = bcmul($total['amount'], '-1'); - } - $array[] = app('amount')->formatAnything($total['currency'], $total['amount']); - } - return implode(' / ', $array); + return $totals; } } diff --git a/app/Support/Twig/Journal.php b/app/Support/Twig/Journal.php index d8613a6e4f..c6502a8f72 100644 --- a/app/Support/Twig/Journal.php +++ b/app/Support/Twig/Journal.php @@ -79,6 +79,7 @@ class Journal extends Twig_Extension { $filters = [ new Twig_SimpleFilter('journalTotalAmount', [TransactionJournalExtension::class, 'totalAmount'], ['is_safe' => ['html']]), + new Twig_SimpleFilter('journalTotalAmountPlain', [TransactionJournalExtension::class, 'totalAmountPlain'], ['is_safe' => ['html']]), ]; return $filters; diff --git a/resources/views/emails/report-new-journals-html.twig b/resources/views/emails/report-new-journals-html.twig index c25899b2d7..a3022d0775 100644 --- a/resources/views/emails/report-new-journals-html.twig +++ b/resources/views/emails/report-new-journals-html.twig @@ -11,9 +11,9 @@ {% if journals.count == 1 %}

- You can find in in your Firefly III installation: + You can find it in your Firefly III installation:
{% for journal in journals %} - {{ journal.description }} + {{ journal.description }} ({{ journal|journalTotalAmount }}) {% endfor %}

{% endif %} @@ -24,7 +24,9 @@

{% endif %} diff --git a/resources/views/emails/report-new-journals-text.twig b/resources/views/emails/report-new-journals-text.twig index 780e62133a..81faa232d5 100644 --- a/resources/views/emails/report-new-journals-text.twig +++ b/resources/views/emails/report-new-journals-text.twig @@ -10,7 +10,7 @@ Firefly III has created {{ journals.count }} transactions for you. You can find in in your Firefly III installation: {% for journal in journals %} -{{ journal.description }}: {{ route('transactions.show', journal.id) }} +{{ journal.description }}: {{ route('transactions.show', journal.id) }} ({{ journal|journalTotalAmountPlain }}) {% endfor %} {% endif %} @@ -18,7 +18,7 @@ You can find in in your Firefly III installation: You can find them in your Firefly III installation: {% for journal in journals %} -- {{ journal.description }}: {{ route('transactions.show', journal.id) }} +- {{ journal.description }}: {{ route('transactions.show', journal.id) }} ({{ journal|journalTotalAmountPlain }}) {% endfor %} {% endif %}