Skip to content

Firefly III: a personal finances manager

Build trigger.

Sander Dorigo2eac9081ead6
diff --git a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
index 6814003f1a..2f2751d873 100644
--- a/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
+++ b/app/lib/Firefly/Storage/TransactionJournal/EloquentTransactionJournalRepository.php
@@ -186,7 +186,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$journal->transaction_type_id = $transactionType->id;
/*
- * Validatre & save journal
+ * Validate & save journal
*/
$journal->validate();
$journal->save();
diff --git a/app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php b/app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php
index f2e48ea93d..38d2030a31 100644
--- a/app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php
+++ b/app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php
@@ -20,53 +20,46 @@ class EloquentJournalTrigger
*/
public function store(\TransactionJournal $journal)
{
- // select all reminders for recurring transactions:
- if ($journal->transaction_type->type == 'Withdrawal') {
- \Log::debug('Trigger on the creation of a withdrawal');
- $transaction = $journal->transactions()->orderBy('amount', 'DESC')->first();
- $amount = floatval($transaction->amount);
- $description = strtolower($journal->description);
- $beneficiary = strtolower($transaction->account->name);
+ /*
+ * Grab all recurring events.
+ */
+ $set = $journal->user()->first()->recurringtransactions()->get();
+ $result = [];
+ /*
+ * Prep vars
+ */
+ $description = strtolower($journal->description);
- // make an array of parts:
- $parts = explode(' ', $description);
- $parts[] = $beneficiary;
- $today = new Carbon;
- $set = \RecurringTransactionReminder::
- leftJoin(
- 'recurring_transactions', 'recurring_transactions.id', '=', 'reminders.recurring_transaction_id'
- )
- ->where('startdate', '<', $today->format('Y-m-d'))
- ->where('enddate', '>', $today->format('Y-m-d'))
- ->where('amount_min', '<=', $amount)
- ->where('amount_max', '>=', $amount)->get(['reminders.*']);
- /** @var \RecurringTransctionReminder $reminder */
- \Log::debug('Have these parts to search for: ' . join('/',$parts));
- \Log::debug('Found ' . count($set).' possible matching recurring transactions');
- foreach ($set as $index => $reminder) {
- /** @var \RecurringTransaction $RT */
- $RT = $reminder->recurring_transaction;
- $matches = explode(' ', strtolower($RT->match));
- \Log::debug($index.': ' . join('/',$matches));
- $matchCount = 0;
- foreach ($parts as $part) {
- if (in_array($part, $matches)) {
- $matchCount++;
- }
- }
- if ($matchCount >= count($matches)) {
- // we have a match!
- \Log::debug(
- 'Match between new journal "' . join('/', $parts) . '" and RT ' . join('/', $matches) . '.'
- );
- $journal->recurringTransaction()->associate($RT);
- $journal->save();
- // also update the reminder.
- $reminder->active = 0;
- $reminder->save();
- return true;
+ /** @var \RecurringTransaction $recurring */
+ foreach ($set as $recurring) {
+ $matches = explode(' ', $recurring->match);
+
+ /*
+ * Count the number of matches.
+ */
+ $count = 0;
+ foreach ($matches as $word) {
+ if (!(strpos($description, $word) === false)) {
+ $count++;
+ \Log::debug('Recurring transaction #' . $recurring->id . ': word "' . $word . '" found in "' . $description . '".');
}
}
+ $result[$recurring->id] = $count;
+ }
+ /*
+ * The one with the highest value is the winrar!
+ */
+ $index = array_search(max($result), $result);
+
+ /*
+ * Find the recurring transaction:
+ */
+ if (count($result[$index]) > 0) {
+ $winner = $journal->user()->first()->recurringtransactions()->find($index);
+ if ($winner) {
+ $journal->recurringTransaction()->associate($winner);
+ $journal->save();
+ }
}
return true;
diff --git a/app/lib/Firefly/Trigger/Recurring/EloquentRecurringTrigger.php b/app/lib/Firefly/Trigger/Recurring/EloquentRecurringTrigger.php
index a71f7cb69a..0223091ee5 100644
--- a/app/lib/Firefly/Trigger/Recurring/EloquentRecurringTrigger.php
+++ b/app/lib/Firefly/Trigger/Recurring/EloquentRecurringTrigger.php
@@ -18,14 +18,6 @@ class EloquentRecurringTrigger
*/
public function destroy(\RecurringTransaction $recurring)
{
- $reminders = $recurring->recurringtransactionreminders()->get();
- /** @var \RecurringTransactionReminder $reminder */
- foreach ($reminders as $reminder) {
- $reminder->delete();
- }
-
- return true;
-
}
/**
@@ -33,67 +25,11 @@ class EloquentRecurringTrigger
*/
public function store(\RecurringTransaction $recurring)
{
- $this->createReminders();
}
public function createReminders()
{
- $entries = \Auth::user()->recurringtransactions()->where('active', 1)->get();
-
- // for each entry, check for existing reminders during their period:
- /** @var \RecurringTransaction $entry */
- foreach ($entries as $entry) {
-
- $start = clone $entry->date;
- $end = clone $entry->date;
- switch ($entry->repeat_freq) {
- case 'weekly':
- $start->startOfWeek();
- $end->endOfWeek();
- break;
- case 'monthly':
- $start->startOfMonth();
- $end->endOfMonth();
- break;
- case 'quarterly':
- $start->firstOfQuarter();
- $end->lastOfQuarter();
- break;
- case 'half-year':
- // start of half-year:
- if (intval($start->format('m')) >= 7) {
- $start->startOfYear();
- $start->addMonths(6);
- } else {
- $start->startOfYear();
- }
- $end = clone $start;
- $end->addMonths(6);
- break;
- case 'yearly':
- $start->startOfYear();
- $end->endOfYear();
- break;
- }
- // check if exists.
- $count = $entry->reminders()->where('startdate', $start->format('Y-m-d'))->where(
- 'enddate', $end->format('Y-m-d')
- )->count();
- if ($count == 0) {
- // create reminder:
- $reminder = new \RecurringTransactionReminder;
- $reminder->recurringtransaction()->associate($entry);
- $reminder->startdate = $start;
- $reminder->enddate = $end;
- $reminder->active = 1;
- $reminder->user()->associate(\Auth::user());
- $reminder->save();
- }
-
-
- }
-
}
/**
@@ -103,10 +39,10 @@ class EloquentRecurringTrigger
*/
public function subscribe(Dispatcher $events)
{
- $events->listen('recurring.destroy', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@destroy');
- $events->listen('recurring.store', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@store');
- $events->listen('recurring.update', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@update');
- $events->listen('recurring.check', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@createReminders');
+// $events->listen('recurring.destroy', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@destroy');
+// $events->listen('recurring.store', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@store');
+// $events->listen('recurring.update', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@update');
+// $events->listen('recurring.check', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@createReminders');
}
/**
@@ -114,14 +50,5 @@ class EloquentRecurringTrigger
*/
public function update(\RecurringTransaction $recurring)
{
- // remove old active reminders
- $reminders = $recurring->reminders()->validOnOrAfter(new Carbon)->get();
- foreach ($reminders as $r) {
- $r->delete();
- }
- $this->createReminders();
- // create new reminder for the current period.
-
- // and now create new one(s)!
}
}
\ No newline at end of file