From 98cff18efa6856a1a2bb452426dd8acb9569f2cf Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 18 Aug 2019 11:16:33 +0200 Subject: [PATCH] Fix email message for new transactions --- app/Events/RequestedReportOnJournals.php | 10 +- app/Handlers/Events/AutomationHandler.php | 4 +- app/Http/Controllers/HomeController.php | 2 - app/Mail/ReportNewJournalsMail.php | 31 ++++-- .../v1/emails/report-new-journals-html.twig | 97 ++++++++++++++----- .../v1/emails/report-new-journals-text.twig | 28 ++---- 6 files changed, 114 insertions(+), 58 deletions(-) diff --git a/app/Events/RequestedReportOnJournals.php b/app/Events/RequestedReportOnJournals.php index 23e5e7c26c..3cbca4ca5d 100644 --- a/app/Events/RequestedReportOnJournals.php +++ b/app/Events/RequestedReportOnJournals.php @@ -60,8 +60,8 @@ class RequestedReportOnJournals { use Dispatchable, InteractsWithSockets, SerializesModels; - /** @var Collection The journals to report on. */ - public $journals; + /** @var Collection The transaction groups to report on. */ + public $groups; /** @var int The ID of the user. */ public $userId; @@ -69,13 +69,13 @@ class RequestedReportOnJournals * Create a new event instance. * * @param int $userId - * @param Collection $journals + * @param Collection $groups */ - public function __construct(int $userId, Collection $journals) + public function __construct(int $userId, Collection $groups) { Log::debug('In event RequestedReportOnJournals.'); $this->userId = $userId; - $this->journals = $journals; + $this->groups = $groups; } /** diff --git a/app/Handlers/Events/AutomationHandler.php b/app/Handlers/Events/AutomationHandler.php index a86d1d24cf..05b4fcce6a 100644 --- a/app/Handlers/Events/AutomationHandler.php +++ b/app/Handlers/Events/AutomationHandler.php @@ -55,10 +55,10 @@ class AutomationHandler /** @var UserRepositoryInterface $repository */ $repository = app(UserRepositoryInterface::class); $user = $repository->findNull($event->userId); - if (null !== $user && 0 !== $event->journals->count()) { + if (null !== $user && 0 !== $event->groups->count()) { try { Log::debug('Trying to mail...'); - Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->journals)); + Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->groups)); // @codeCoverageIgnoreStart } catch (Exception $e) { Log::debug('Send message failed! :('); diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index f1618c2d49..3f37271b08 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -105,7 +105,6 @@ class HomeController extends Controller { $types = config('firefly.accountTypesByIdentifier.asset'); $count = $repository->count($types); - Log::channel('audit')->info('User visits homepage.'); if (0 === $count) { @@ -127,7 +126,6 @@ class HomeController extends Controller /** @var BillRepositoryInterface $billRepository */ $billRepository = app(BillRepositoryInterface::class); $billCount = $billRepository->getBills()->count(); - foreach ($accounts as $account) { /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); diff --git a/app/Mail/ReportNewJournalsMail.php b/app/Mail/ReportNewJournalsMail.php index 0ffe29868c..2d2f38319d 100644 --- a/app/Mail/ReportNewJournalsMail.php +++ b/app/Mail/ReportNewJournalsMail.php @@ -22,6 +22,8 @@ declare(strict_types=1); namespace FireflyIII\Mail; +use FireflyIII\Models\TransactionGroup; +use FireflyIII\Transformers\TransactionGroupTransformer; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; @@ -43,21 +45,24 @@ class ReportNewJournalsMail extends Mailable /** @var string IP address of user (if known) */ public $ipAddress; - /** @var Collection A collection of journals */ - public $journals; + /** @var Collection A collection of groups */ + public $groups; + + /** @var array All groups, transformed to array. */ + public $transformed; /** * ConfirmEmailChangeMail constructor. * * @param string $email * @param string $ipAddress - * @param Collection $journals + * @param Collection $groups */ - public function __construct(string $email, string $ipAddress, Collection $journals) + public function __construct(string $email, string $ipAddress, Collection $groups) { $this->email = $email; $this->ipAddress = $ipAddress; - $this->journals = $journals; + $this->groups = $groups; } /** @@ -67,13 +72,25 @@ class ReportNewJournalsMail extends Mailable */ public function build(): self { - $subject = 1 === $this->journals->count() + $subject = 1 === $this->groups->count() ? 'Firefly III has created a new transaction' : sprintf( - 'Firefly III has created new %d transactions', $this->journals->count() + 'Firefly III has created new %d transactions', $this->groups->count() ); + $this->transform(); return $this->view('emails.report-new-journals-html')->text('emails.report-new-journals-text') ->subject($subject); } + + private function transform(): void + { + /** @var TransactionGroupTransformer $transformer */ + $transformer = app(TransactionGroupTransformer::class); + + /** @var TransactionGroup $group */ + foreach ($this->groups as $group) { + $this->transformed[] = $transformer->transformObject($group); + } + } } diff --git a/resources/views/v1/emails/report-new-journals-html.twig b/resources/views/v1/emails/report-new-journals-html.twig index d4d8e0394b..66ec31b09e 100644 --- a/resources/views/v1/emails/report-new-journals-html.twig +++ b/resources/views/v1/emails/report-new-journals-html.twig @@ -1,34 +1,83 @@ {% include 'emails.header-html' %}

- {% if journals.count == 1 %} - Firefly III has created a transaction for you. + {% if transformed|length == 1 %} + Firefly III has created a transaction for you. You can find it in your Firefly III installation: {% endif %} - {% if journals.count > 1 %} - Firefly III has created {{ journals.count }} transactions for you. + {% if transformed|length > 1 %} + Firefly III has created {{ groups.count }} transactions for you. You can find them in your Firefly III installation: {% endif %}

+ +
    + {% for group in transformed %} +
  1. + {% set count = group.transactions|length %} + + {% if 1 == count %} + {% set journal = group.transactions[0] %} + {{ journal.description }}, + + {% if journal.type == 'deposit' %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount*-1 %} + ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + + {% elseif journal.type == 'transfer' %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount %} + ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + + {% else %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount %} + ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + -{% if journals.count == 1 %} -

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

    -{% endif %} + {% endif %} + + {% else %} + {{ group.group_title }} +
      + {% for journal in group.transactions %} +
    1. + {{ journal.description }}, + + {% if journal.type == 'deposit' %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount*-1 %} + ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + + {% elseif journal.type == 'transfer' %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount %} + ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + + {% else %} + + {{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }} + {% if null != journal.foreign_amount %} + ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}) + {% endif %} + -{% if journals.count > 1 %} -

      - You can find them in your Firefly III installation: -

      - -{% endif %} + {% endif %} +
    2. + {% endfor %} +
    + {% endif %} +
  2. + {% endfor %} +
{% include 'emails.footer-html' %} diff --git a/resources/views/v1/emails/report-new-journals-text.twig b/resources/views/v1/emails/report-new-journals-text.twig index c3d3527a3a..b5e45c15bc 100644 --- a/resources/views/v1/emails/report-new-journals-text.twig +++ b/resources/views/v1/emails/report-new-journals-text.twig @@ -1,25 +1,17 @@ {% include 'emails.header-text' %} -{% if journals.count == 1 %} -Firefly III has created a transaction for you. +{% if transformed|length == 1 %}Firefly III has created a transaction for you. You can find it in your Firefly III installation:{% endif %} +{% if transformed|length > 1 %}Firefly III has created {{ groups.count }} transactions for you. You can find them in your Firefly III installation:{% endif %} -{% endif %} -{% if journals.count > 1 %} -Firefly III has created {{ journals.count }} transactions for you. -{% endif %} -{% if journals.count == 1 %} -You can find in in your Firefly III installation: -{% for journal in journals %} -{{ journal.description }}: {{ route('transactions.show', journal.id) }} (AMOUNT TODO) +{% for group in transformed %} +{% set count = group.transactions|length %} +{% if 1 == count %}{% set journal = group.transactions[0] %} +- {{ journal.description }}, {% if journal.type == 'deposit' %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount*-1 %} ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% elseif journal.type == 'transfer' %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount %} ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% else %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount %} ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% endif %} + +{% else %}- {{ group.group_title }} +{% for journal in group.transactions %}-- {{ journal.description }}, {% if journal.type == 'deposit' %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount*-1 %} ({{ formatAmountBySymbol(journal.foreign_amount, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% elseif journal.type == 'transfer' %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount %} ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% else %}{{ formatAmountBySymbol(journal.amount*-1, journal.currency_symbol, journal.currency_decimal_places, false) }}{% if null != journal.foreign_amount %} ({{ formatAmountBySymbol(journal.foreign_amount*-1, journal.foreign_currency_symbol, journal.foreign_currency_decimal_places, false) }}){% endif %}{% endif %} + {% endfor %} {% endif %} - -{% if journals.count > 1 %} -You can find them in your Firefly III installation: - -{% for journal in journals %} -- {{ journal.description }}: {{ route('transactions.show', journal.id) }} (AMOUNT TODO) {% endfor %} -{% endif %} - {% include 'emails.footer-text' %}