mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Build trigger.
This commit is contained in:
@@ -186,7 +186,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
|
|||||||
$journal->transaction_type_id = $transactionType->id;
|
$journal->transaction_type_id = $transactionType->id;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Validatre & save journal
|
* Validate & save journal
|
||||||
*/
|
*/
|
||||||
$journal->validate();
|
$journal->validate();
|
||||||
$journal->save();
|
$journal->save();
|
||||||
|
@@ -20,53 +20,46 @@ class EloquentJournalTrigger
|
|||||||
*/
|
*/
|
||||||
public function store(\TransactionJournal $journal)
|
public function store(\TransactionJournal $journal)
|
||||||
{
|
{
|
||||||
// select all reminders for recurring transactions:
|
/*
|
||||||
if ($journal->transaction_type->type == 'Withdrawal') {
|
* Grab all recurring events.
|
||||||
\Log::debug('Trigger on the creation of a withdrawal');
|
*/
|
||||||
$transaction = $journal->transactions()->orderBy('amount', 'DESC')->first();
|
$set = $journal->user()->first()->recurringtransactions()->get();
|
||||||
$amount = floatval($transaction->amount);
|
$result = [];
|
||||||
$description = strtolower($journal->description);
|
/*
|
||||||
$beneficiary = strtolower($transaction->account->name);
|
* Prep vars
|
||||||
|
*/
|
||||||
|
$description = strtolower($journal->description);
|
||||||
|
|
||||||
// make an array of parts:
|
/** @var \RecurringTransaction $recurring */
|
||||||
$parts = explode(' ', $description);
|
foreach ($set as $recurring) {
|
||||||
$parts[] = $beneficiary;
|
$matches = explode(' ', $recurring->match);
|
||||||
$today = new Carbon;
|
|
||||||
$set = \RecurringTransactionReminder::
|
/*
|
||||||
leftJoin(
|
* Count the number of matches.
|
||||||
'recurring_transactions', 'recurring_transactions.id', '=', 'reminders.recurring_transaction_id'
|
*/
|
||||||
)
|
$count = 0;
|
||||||
->where('startdate', '<', $today->format('Y-m-d'))
|
foreach ($matches as $word) {
|
||||||
->where('enddate', '>', $today->format('Y-m-d'))
|
if (!(strpos($description, $word) === false)) {
|
||||||
->where('amount_min', '<=', $amount)
|
$count++;
|
||||||
->where('amount_max', '>=', $amount)->get(['reminders.*']);
|
\Log::debug('Recurring transaction #' . $recurring->id . ': word "' . $word . '" found in "' . $description . '".');
|
||||||
/** @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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$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;
|
return true;
|
||||||
|
|
||||||
|
@@ -18,14 +18,6 @@ class EloquentRecurringTrigger
|
|||||||
*/
|
*/
|
||||||
public function destroy(\RecurringTransaction $recurring)
|
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)
|
public function store(\RecurringTransaction $recurring)
|
||||||
{
|
{
|
||||||
$this->createReminders();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function 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)
|
public function subscribe(Dispatcher $events)
|
||||||
{
|
{
|
||||||
$events->listen('recurring.destroy', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@destroy');
|
// $events->listen('recurring.destroy', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@destroy');
|
||||||
$events->listen('recurring.store', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@store');
|
// $events->listen('recurring.store', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@store');
|
||||||
$events->listen('recurring.update', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@update');
|
// $events->listen('recurring.update', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@update');
|
||||||
$events->listen('recurring.check', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@createReminders');
|
// $events->listen('recurring.check', 'Firefly\Trigger\Recurring\EloquentRecurringTrigger@createReminders');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,14 +50,5 @@ class EloquentRecurringTrigger
|
|||||||
*/
|
*/
|
||||||
public function update(\RecurringTransaction $recurring)
|
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)!
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user