Build trigger.

This commit is contained in:
Sander Dorigo 2014-10-11 09:21:28 +02:00
parent b3eef4f40b
commit 2eac9081ea
3 changed files with 42 additions and 122 deletions

View File

@ -186,7 +186,7 @@ class EloquentTransactionJournalRepository implements TransactionJournalReposito
$journal->transaction_type_id = $transactionType->id;
/*
* Validatre & save journal
* Validate & save journal
*/
$journal->validate();
$journal->save();

View File

@ -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;

View File

@ -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)!
}
}