mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
New trigger for new journals.
This commit is contained in:
parent
77b4942691
commit
5446e85424
92
app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php
Normal file
92
app/lib/Firefly/Trigger/Journals/EloquentJournalTrigger.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace Firefly\Trigger\Journals;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Events\Dispatcher;
|
||||
|
||||
/**
|
||||
* Class EloquentJournalTrigger
|
||||
*
|
||||
* @package Firefly\Trigger\Journals
|
||||
*/
|
||||
class EloquentJournalTrigger
|
||||
{
|
||||
|
||||
/**
|
||||
* @param \TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function store(\TransactionJournal $journal)
|
||||
{
|
||||
// select all reminders for recurring transactions:
|
||||
if ($journal->transaction_type->type == 'Withdrawal') {
|
||||
$transaction = $journal->transactions()->orderBy('amount', 'DESC')->first();
|
||||
$amount = floatval($transaction->amount);
|
||||
$description = $journal->description;
|
||||
$beneficiary = $transaction->account->name;
|
||||
|
||||
// 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 */
|
||||
foreach ($set as $reminder) {
|
||||
/** @var \RecurringTransaction $RT */
|
||||
$RT = $reminder->recurring_transaction;
|
||||
$matches = explode(' ', $RT->match);
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Dispatcher $events
|
||||
*/
|
||||
public function subscribe(Dispatcher $events)
|
||||
{
|
||||
$events->listen('journals.store', 'Firefly\Trigger\Journals\EloquentJournalTrigger@store');
|
||||
$events->listen('journals.update', 'Firefly\Trigger\Journals\EloquentJournalTrigger@update');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \TransactionJournal $journal
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update(\TransactionJournal $journal)
|
||||
{
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -91,5 +91,6 @@ Event::subscribe('Firefly\Trigger\Limits\EloquentLimitTrigger');
|
||||
Event::subscribe('Firefly\Trigger\Piggybanks\EloquentPiggybankTrigger');
|
||||
Event::subscribe('Firefly\Trigger\Budgets\EloquentBudgetTrigger');
|
||||
Event::subscribe('Firefly\Trigger\Recurring\EloquentRecurringTrigger');
|
||||
Event::subscribe('Firefly\Trigger\Journals\EloquentJournalTrigger');
|
||||
|
||||
return $app;
|
||||
|
Loading…
Reference in New Issue
Block a user