From dd70fbad3f05644551129671c0b191ac9d69b668 Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sat, 11 Oct 2014 09:20:59 +0200 Subject: [PATCH 1/5] Cleanup old code. --- app/lib/Firefly/Helper/Controllers/Transaction.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/lib/Firefly/Helper/Controllers/Transaction.php b/app/lib/Firefly/Helper/Controllers/Transaction.php index 09ebe92014..f36edb861a 100644 --- a/app/lib/Firefly/Helper/Controllers/Transaction.php +++ b/app/lib/Firefly/Helper/Controllers/Transaction.php @@ -176,6 +176,7 @@ class Transaction implements TransactionInterface $components = array_merge($budgetids,$catids); $journal->components()->sync($components); $journal->save(); + if (isset($data['return_journal']) && $data['return_journal'] == true) { return $journal; } @@ -469,6 +470,12 @@ class Transaction implements TransactionInterface } $journal->completed = true; $journal->save(); + + /* + * Trigger recurring transaction event. + */ + \Event::fire('journals.store',[$journal]); + if (isset($data['return_journal']) && $data['return_journal'] == true) { return ['journal' => $journal, 'messagebag' => $journal->errors()]; } From b3eef4f40b859120904ce13efbf7eb082ed9ba0c Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sat, 11 Oct 2014 09:21:14 +0200 Subject: [PATCH 2/5] Cleanup. --- app/controllers/TransactionController.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/app/controllers/TransactionController.php b/app/controllers/TransactionController.php index 798ca809ae..e3ed758ed3 100644 --- a/app/controllers/TransactionController.php +++ b/app/controllers/TransactionController.php @@ -22,7 +22,7 @@ class TransactionController extends BaseController * Construct a new transaction controller with two of the most often used helpers. * * @param TJRI $repository - * @param TI $helper + * @param TI $helper */ public function __construct(TJRI $repository, TI $helper) { @@ -178,9 +178,9 @@ class TransactionController extends BaseController * Data to properly display the edit form. */ $prefilled = [ - 'date' => $journal->date->format('Y-m-d'), - 'category' => '', - 'budget_id' => 0, + 'date' => $journal->date->format('Y-m-d'), + 'category' => '', + 'budget_id' => 0, 'piggybank_id' => $piggyBankId ]; @@ -280,10 +280,7 @@ class TransactionController extends BaseController /* * Try to store: */ - $data['return_journal'] = true; - $set = $this->_helper->store($data); - $journal = $set['journal']; - $messageBag = $set['messagebag']; + $messageBag = $this->_helper->store($data); /* * Failure! @@ -298,13 +295,6 @@ class TransactionController extends BaseController */ Session::flash('success', 'Transaction "' . e(Input::get('description')) . '" saved!'); - /* - * Trigger something that will search for possibly matching recurring transactions. - * This only works for expenses. However, at this point we have no idea what the latest - * transaction is. We'll have to find it, and when the user creates a lot of them it might - * get lost somewhere. - */ - /* * Redirect to original location or back to the form. */ From 2eac9081ead6cc72827b642872c7ac580deb2da7 Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sat, 11 Oct 2014 09:21:28 +0200 Subject: [PATCH 3/5] Build trigger. --- .../EloquentTransactionJournalRepository.php | 2 +- .../Journals/EloquentJournalTrigger.php | 81 +++++++++---------- .../Recurring/EloquentRecurringTrigger.php | 81 +------------------ 3 files changed, 42 insertions(+), 122 deletions(-) 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 From 223ad16616ecedf1f6677f851cbdfecabb945b97 Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sat, 11 Oct 2014 09:54:20 +0200 Subject: [PATCH 4/5] Expand JSON. --- app/lib/Firefly/Helper/Controllers/Json.php | 94 +++++++++++---------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/app/lib/Firefly/Helper/Controllers/Json.php b/app/lib/Firefly/Helper/Controllers/Json.php index 91476fab0d..a74a954d92 100644 --- a/app/lib/Firefly/Helper/Controllers/Json.php +++ b/app/lib/Firefly/Helper/Controllers/Json.php @@ -34,9 +34,9 @@ class Json implements JsonInterface $length = intval(\Input::get('length')); } $parameters = [ - 'start' => intval(\Input::get('start')), + 'start' => intval(\Input::get('start')), 'length' => $length, - 'draw' => intval(\Input::get('draw')), + 'draw' => intval(\Input::get('draw')), ]; @@ -46,11 +46,11 @@ class Json implements JsonInterface if (!is_null(\Input::get('columns')) && is_array(\Input::get('columns'))) { foreach (\Input::get('columns') as $column) { $parameters['columns'][] = [ - 'data' => $column['data'], - 'name' => $column['name'], + 'data' => $column['data'], + 'name' => $column['name'], 'searchable' => $column['searchable'] == 'true' ? true : false, - 'orderable' => $column['orderable'] == 'true' ? true : false, - 'search' => [ + 'orderable' => $column['orderable'] == 'true' ? true : false, + 'search' => [ 'value' => $column['search']['value'], 'regex' => $column['search']['regex'] == 'true' ? true : false, ] @@ -69,7 +69,7 @@ class Json implements JsonInterface $columnName = $parameters['columns'][$columnIndex]['name']; $parameters['order'][] = [ 'name' => $columnName, - 'dir' => strtoupper($order['dir']) + 'dir' => strtoupper($order['dir']) ]; if ($columnName == 'to' || $columnName == 'from') { $parameters['orderOnAccount'] = true; @@ -97,7 +97,7 @@ class Json implements JsonInterface * Do some sorting, counting and ordering on the query and return a nicely formatted array * that can be used by the DataTables JQuery plugin. * - * @param array $parameters + * @param array $parameters * @param Builder $query * * @return array @@ -132,10 +132,10 @@ class Json implements JsonInterface * Build return array: */ $data = [ - 'draw' => $parameters['draw'], - 'recordsTotal' => $count, + 'draw' => $parameters['draw'], + 'recordsTotal' => $count, 'recordsFiltered' => $filtered, - 'data' => [], + 'data' => [], ]; @@ -169,19 +169,20 @@ class Json implements JsonInterface */ /** @var \TransactionJournal $entry */ foreach ($set as $entry) { - $from = $entry->transactions[0]->account; - $to = $entry->transactions[1]->account; - $budget = $entry->budgets()->first(); - $category = $entry->categories()->first(); - $arr = [ - 'date' => $entry->date->format('j F Y'), + $from = $entry->transactions[0]->account; + $to = $entry->transactions[1]->account; + $budget = $entry->budgets()->first(); + $category = $entry->categories()->first(); + $recurring = $entry->recurringTransaction()->first(); + $arr = [ + 'date' => $entry->date->format('j F Y'), 'description' => [ 'description' => $entry->description, - 'url' => route('transactions.show', $entry->id) + 'url' => route('transactions.show', $entry->id) ], - 'amount' => floatval($entry->amount), - 'from' => ['name' => $from->name, 'url' => route('accounts.show', $from->id)], - 'to' => ['name' => $to->name, 'url' => route('accounts.show', $to->id)], + 'amount' => floatval($entry->amount), + 'from' => ['name' => $from->name, 'url' => route('accounts.show', $from->id)], + 'to' => ['name' => $to->name, 'url' => route('accounts.show', $to->id)], 'components' => [ 'budget_id' => 0, 'budget_url' => '', @@ -190,20 +191,25 @@ class Json implements JsonInterface 'category_url' => '', 'category_name' => '' ], - 'id' => [ - 'edit' => route('transactions.edit', $entry->id), + 'id' => [ + 'edit' => route('transactions.edit', $entry->id), 'delete' => route('transactions.delete', $entry->id) ] ]; - if($budget) { - $arr['components']['budget_id'] = $budget->id; + if ($budget) { + $arr['components']['budget_id'] = $budget->id; $arr['components']['budget_name'] = $budget->name; - $arr['components']['budget_url'] = route('budgets.show',$budget->id); + $arr['components']['budget_url'] = route('budgets.show', $budget->id); } - if($category) { - $arr['components']['category_id'] = $category->id; + if ($category) { + $arr['components']['category_id'] = $category->id; $arr['components']['category_name'] = $category->name; - $arr['components']['category_url'] = route('categories.show',$category->id); + $arr['components']['category_url'] = route('categories.show', $category->id); + } + if ($recurring) { + $arr['components']['recurring_id'] = $recurring->id; + $arr['components']['recurring_name'] = e($recurring->name); + $arr['components']['recurring_url'] = route('recurring.show', $recurring->id); } $data['data'][] = $arr; @@ -257,14 +263,14 @@ class Json implements JsonInterface $query->leftJoin( 'transactions AS ' . $prefix . 't1', function ($join) use ($operator) { $join->on('t1.transaction_journal_id', '=', 'transaction_journals.id') - ->on('t1.amount', $operator, \DB::Raw(0)); + ->on('t1.amount', $operator, \DB::Raw(0)); } ); // left join second table for "to" account: $query->leftJoin( 'transactions AS ' . $prefix . 't2', function ($join) use ($operatorNegated) { $join->on('t2.transaction_journal_id', '=', 'transaction_journals.id') - ->on('t2.amount', $operatorNegated, \DB::Raw(0)); + ->on('t2.amount', $operatorNegated, \DB::Raw(0)); } ); @@ -293,7 +299,7 @@ class Json implements JsonInterface * Do some sorting, counting and ordering on the query and return a nicely formatted array * that can be used by the DataTables JQuery plugin. * - * @param array $parameters + * @param array $parameters * @param Builder $query * * @return array @@ -328,10 +334,10 @@ class Json implements JsonInterface * Build return array: */ $data = [ - 'draw' => $parameters['draw'], - 'recordsTotal' => $count, + 'draw' => $parameters['draw'], + 'recordsTotal' => $count, 'recordsFiltered' => $filtered, - 'data' => [], + 'data' => [], ]; @@ -351,16 +357,16 @@ class Json implements JsonInterface foreach ($set as $entry) { $data['data'][] = [ - 'name' => ['name' => $entry->name,'url' => route('recurring.show',$entry->id)], - 'match' => explode(' ',$entry->match), - 'amount_max' => floatval($entry->amount_max), - 'amount_min' => floatval($entry->amount_min), - 'date' => $entry->date->format('j F Y'), - 'active' => intval($entry->active), - 'automatch' => intval($entry->automatch), + 'name' => ['name' => $entry->name, 'url' => route('recurring.show', $entry->id)], + 'match' => explode(' ', $entry->match), + 'amount_max' => floatval($entry->amount_max), + 'amount_min' => floatval($entry->amount_min), + 'date' => $entry->date->format('j F Y'), + 'active' => intval($entry->active), + 'automatch' => intval($entry->automatch), 'repeat_freq' => $entry->repeat_freq, - 'id' => [ - 'edit' => route('recurring.edit', $entry->id), + 'id' => [ + 'edit' => route('recurring.edit', $entry->id), 'delete' => route('recurring.delete', $entry->id) ] ]; From a53550537f194368d34ad3b058b7377e36d5df85 Mon Sep 17 00:00:00 2001 From: Sander Dorigo Date: Sat, 11 Oct 2014 09:54:26 +0200 Subject: [PATCH 5/5] Expand JS --- public/assets/javascript/firefly/transactions.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/public/assets/javascript/firefly/transactions.js b/public/assets/javascript/firefly/transactions.js index f674ca8222..98ca8a33ed 100644 --- a/public/assets/javascript/firefly/transactions.js +++ b/public/assets/javascript/firefly/transactions.js @@ -95,6 +95,9 @@ $(document).ready(function () { if (data.category_id > 0) { html += ' '; } + if(data.recurring_id > 0) { + html += ' '; + } return html; } },