Skip to content

Firefly III: a personal finances manager

Send mail message when cron has hit.

James Cole7591f3fa2949
diff --git a/app/Events/RequestedReportOnJournals.php b/app/Events/RequestedReportOnJournals.php
new file mode 100644
index 0000000000..9956ee62fd
--- /dev/null
+++ b/app/Events/RequestedReportOnJournals.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace FireflyIII\Events;
+
+use Illuminate\Broadcasting\InteractsWithSockets;
+use Illuminate\Broadcasting\PrivateChannel;
+use Illuminate\Foundation\Events\Dispatchable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Collection;
+use Log;
+
+/**
+ * Class RequestedReportOnJournals
+ */
+class RequestedReportOnJournals
+{
+ use Dispatchable, InteractsWithSockets, SerializesModels;
+
+ /** @var Collection */
+ public $journals;
+ /** @var int */
+ public $userId;
+
+ /**
+ * Create a new event instance.
+ *
+ * @return void
+ */
+ public function __construct(int $userId, Collection $journals)
+ {
+ Log::debug('In event RequestedReportOnJournals.');
+ $this->userId = $userId;
+ $this->journals = $journals;
+ }
+
+ /**
+ * Get the channels the event should broadcast on.
+ *
+ * @return \Illuminate\Broadcasting\Channel|array
+ */
+ public function broadcastOn()
+ {
+ return new PrivateChannel('channel-name');
+ }
+}
diff --git a/app/Handlers/Events/AutomationHandler.php b/app/Handlers/Events/AutomationHandler.php
new file mode 100644
index 0000000000..697371afcf
--- /dev/null
+++ b/app/Handlers/Events/AutomationHandler.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * AutomationHandler.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+declare(strict_types=1);
+
+namespace FireflyIII\Handlers\Events;
+
+use Exception;
+use FireflyIII\Events\RequestedReportOnJournals;
+use FireflyIII\Mail\ReportNewJournalsMail;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
+use Log;
+use Mail;
+
+/**
+ * Class AutomationHandler
+ */
+class AutomationHandler
+{
+
+ /**
+ * @param RequestedReportOnJournals $event
+ *
+ * @return bool
+ */
+ public function reportJournals(RequestedReportOnJournals $event): bool
+ {
+ Log::debug('In reportJournals.');
+ /** @var UserRepositoryInterface $repository */
+ $repository = app(UserRepositoryInterface::class);
+ $user = $repository->findNull($event->userId);
+ if (null === $user) {
+ Log::debug('User is NULL');
+ return true;
+ }
+ if ($event->journals->count() === 0) {
+ Log::debug('No journals.');
+ return true;
+ }
+
+ try {
+ Log::debug('Trying to mail...');
+ Mail::to($user->email)->send(new ReportNewJournalsMail($user->email, '127.0.0.1', $event->journals));
+ // @codeCoverageIgnoreStart
+ } catch (Exception $e) {
+ Log::error($e->getMessage());
+ }
+ Log::debug('Done!');
+
+ // @codeCoverageIgnoreEnd
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/app/Handlers/Events/UserEventHandler.php b/app/Handlers/Events/UserEventHandler.php
index c0b6acaa5d..64ef4a83fc 100644
--- a/app/Handlers/Events/UserEventHandler.php
+++ b/app/Handlers/Events/UserEventHandler.php
@@ -113,7 +113,7 @@ class UserEventHandler
*
* @return bool
*/
- function demoUserBackToEnglish(Login $event): bool
+ public function demoUserBackToEnglish(Login $event): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
diff --git a/app/Jobs/CreateRecurringTransactions.php b/app/Jobs/CreateRecurringTransactions.php
index af2f210ebe..45cfe72936 100644
--- a/app/Jobs/CreateRecurringTransactions.php
+++ b/app/Jobs/CreateRecurringTransactions.php
@@ -3,11 +3,14 @@
namespace FireflyIII\Jobs;
use Carbon\Carbon;
+use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Models\Recurrence;
use FireflyIII\Models\RecurrenceRepetition;
use FireflyIII\Models\RecurrenceTransaction;
+use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
+use FireflyIII\Repositories\User\UserRepositoryInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
@@ -29,6 +32,8 @@ class CreateRecurringTransactions implements ShouldQueue
private $journalRepository;
/** @var RecurringRepositoryInterface */
private $repository;
+ /** @var UserRepositoryInterface */
+ private $userRepository;
/**
* Create a new job instance.
@@ -41,6 +46,7 @@ class CreateRecurringTransactions implements ShouldQueue
$this->date = $date;
$this->repository = app(RecurringRepositoryInterface::class);
$this->journalRepository = app(JournalRepositoryInterface::class);
+ $this->userRepository = app(UserRepositoryInterface::class);
}
/**
@@ -76,14 +82,16 @@ class CreateRecurringTransactions implements ShouldQueue
$this->journalRepository->setUser($recurrence->user);
Log::debug(sprintf('Now at recurrence #%d', $recurrence->id));
$created = $this->handleRepetitions($recurrence);
- Log::debug(sprintf('Done with recurrence #%c', $recurrence->id));
-
+ Log::debug(sprintf('Done with recurrence #%d', $recurrence->id));
$result[$recurrence->user_id] = $result[$recurrence->user_id]->merge($created);
}
+ Log::debug('Now running report thing.');
// will now send email to users.
- foreach($result as $userId => $journals) {
- $this->sendReport($userId, $journals);
+ foreach ($result as $userId => $journals) {
+ // random bunch to make mail.
+ $journals = TransactionJournal::where('user_id', $userId)->inRandomOrder()->take(1)->get();
+ event(new RequestedReportOnJournals($userId, $journals));
}
Log::debug('Done with handle()');
diff --git a/app/Mail/ReportNewJournalsMail.php b/app/Mail/ReportNewJournalsMail.php
new file mode 100644
index 0000000000..d1f1d2445e
--- /dev/null
+++ b/app/Mail/ReportNewJournalsMail.php
@@ -0,0 +1,71 @@
+<?php
+/**
+ * ReportNewJournalsMail.php
+ * Copyright (c) 2018 [email protected]
+ *
+ * This file is part of Firefly III.
+ *
+ * Firefly III is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Firefly III is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace FireflyIII\Mail;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Mail\Mailable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Support\Collection;
+
+/**
+ * Class ReportNewJournalsMail.
+ *
+ * Sends a list of newly created journals to the user.
+ */
+class ReportNewJournalsMail extends Mailable
+{
+ use Queueable, SerializesModels;
+
+ /** @var string Email address of the user */
+ public $email;
+ /** @var string IP address of user (if known) */
+ public $ipAddress;
+
+ /** @var Collection A collection of journals */
+ public $journals;
+
+ /**
+ * ConfirmEmailChangeMail constructor.
+ *
+ * @param string $email
+ * @param string $ipAddress
+ * @param Collection $journals
+ */
+ public function __construct(string $email, string $ipAddress, Collection $journals)
+ {
+ $this->email = $email;
+ $this->ipAddress = $ipAddress;
+ $this->journals = $journals;
+ }
+
+ /**
+ * Build the message.
+ *
+ * @return $this
+ */
+ public function build(): self
+ {
+ return $this->view('emails.report-new-journals-html')->text('emails.report-new-journals-text')
+ ->subject('Firefly III has created new transactions');
+ }
+}
diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php
index 9e0a82dcc5..ec5891f6f3 100644
--- a/app/Providers/EventServiceProvider.php
+++ b/app/Providers/EventServiceProvider.php
@@ -26,6 +26,7 @@ use Exception;
use FireflyIII\Events\AdminRequestedTestMessage;
use FireflyIII\Events\RegisteredUser;
use FireflyIII\Events\RequestedNewPassword;
+use FireflyIII\Events\RequestedReportOnJournals;
use FireflyIII\Events\RequestedVersionCheckStatus;
use FireflyIII\Events\StoredTransactionJournal;
use FireflyIII\Events\UpdatedTransactionJournal;
@@ -70,6 +71,9 @@ class EventServiceProvider extends ServiceProvider
RequestedVersionCheckStatus::class => [
'FireflyIII\Handlers\Events\VersionCheckEventHandler@checkForUpdates',
],
+ RequestedReportOnJournals::class => [
+ 'FireflyIII\Handlers\Events\AutomationHandler@reportJournals',
+ ],
// is a User related event.
RequestedNewPassword::class => [
diff --git a/resources/views/emails/report-new-journals-html.twig b/resources/views/emails/report-new-journals-html.twig
new file mode 100644
index 0000000000..c25899b2d7
--- /dev/null
+++ b/resources/views/emails/report-new-journals-html.twig
@@ -0,0 +1,32 @@
+{% include 'emails.header-html' %}
+<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
+ {% if journals.count == 1 %}
+ Firefly III has created a transaction for you.
+ {% endif %}
+ {% if journals.count > 1 %}
+ Firefly III has created {{ journals.count }} transactions for you.
+ {% endif %}
+</p>
+
+
+{% if journals.count == 1 %}
+ <p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
+ You can find in in your Firefly III installation:
+ {% for journal in journals %}
+ <a href="{{ route('transactions.show', journal.id) }}">{{ journal.description }}</a>
+ {% endfor %}
+ </p>
+{% endif %}
+
+{% if journals.count > 1 %}
+ <p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
+ You can find them in your Firefly III installation:
+ </p>
+ <ul>
+ {% for journal in journals %}
+ <li><a href="{{ route('transactions.show', journal.id) }}">{{ journal.description }}</a></li>
+ {% endfor %}
+ </ul>
+{% endif %}
+
+{% include 'emails.footer-html' %}
diff --git a/resources/views/emails/report-new-journals-text.twig b/resources/views/emails/report-new-journals-text.twig
new file mode 100644
index 0000000000..780e62133a
--- /dev/null
+++ b/resources/views/emails/report-new-journals-text.twig
@@ -0,0 +1,25 @@
+{% include 'emails.header-text' %}
+{% if journals.count == 1 %}
+Firefly III has created a transaction for you.
+
+{% 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) }}
+{% 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) }}
+{% endfor %}
+{% endif %}
+
+{% include 'emails.footer-text' %}