Merge branch 'release/3.1'

This commit is contained in:
Sander Dorigo 2014-10-11 12:11:17 +02:00
commit 81662473a6
7 changed files with 107 additions and 181 deletions

View File

@ -22,7 +22,7 @@ class TransactionController extends BaseController
* Construct a new transaction controller with two of the most often used helpers. * Construct a new transaction controller with two of the most often used helpers.
* *
* @param TJRI $repository * @param TJRI $repository
* @param TI $helper * @param TI $helper
*/ */
public function __construct(TJRI $repository, TI $helper) public function __construct(TJRI $repository, TI $helper)
{ {
@ -178,9 +178,9 @@ class TransactionController extends BaseController
* Data to properly display the edit form. * Data to properly display the edit form.
*/ */
$prefilled = [ $prefilled = [
'date' => $journal->date->format('Y-m-d'), 'date' => $journal->date->format('Y-m-d'),
'category' => '', 'category' => '',
'budget_id' => 0, 'budget_id' => 0,
'piggybank_id' => $piggyBankId 'piggybank_id' => $piggyBankId
]; ];
@ -280,10 +280,7 @@ class TransactionController extends BaseController
/* /*
* Try to store: * Try to store:
*/ */
$data['return_journal'] = true; $messageBag = $this->_helper->store($data);
$set = $this->_helper->store($data);
$journal = $set['journal'];
$messageBag = $set['messagebag'];
/* /*
* Failure! * Failure!
@ -298,13 +295,6 @@ class TransactionController extends BaseController
*/ */
Session::flash('success', 'Transaction "' . e(Input::get('description')) . '" saved!'); 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. * Redirect to original location or back to the form.
*/ */

View File

@ -34,9 +34,9 @@ class Json implements JsonInterface
$length = intval(\Input::get('length')); $length = intval(\Input::get('length'));
} }
$parameters = [ $parameters = [
'start' => intval(\Input::get('start')), 'start' => intval(\Input::get('start')),
'length' => $length, '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'))) { if (!is_null(\Input::get('columns')) && is_array(\Input::get('columns'))) {
foreach (\Input::get('columns') as $column) { foreach (\Input::get('columns') as $column) {
$parameters['columns'][] = [ $parameters['columns'][] = [
'data' => $column['data'], 'data' => $column['data'],
'name' => $column['name'], 'name' => $column['name'],
'searchable' => $column['searchable'] == 'true' ? true : false, 'searchable' => $column['searchable'] == 'true' ? true : false,
'orderable' => $column['orderable'] == 'true' ? true : false, 'orderable' => $column['orderable'] == 'true' ? true : false,
'search' => [ 'search' => [
'value' => $column['search']['value'], 'value' => $column['search']['value'],
'regex' => $column['search']['regex'] == 'true' ? true : false, 'regex' => $column['search']['regex'] == 'true' ? true : false,
] ]
@ -69,7 +69,7 @@ class Json implements JsonInterface
$columnName = $parameters['columns'][$columnIndex]['name']; $columnName = $parameters['columns'][$columnIndex]['name'];
$parameters['order'][] = [ $parameters['order'][] = [
'name' => $columnName, 'name' => $columnName,
'dir' => strtoupper($order['dir']) 'dir' => strtoupper($order['dir'])
]; ];
if ($columnName == 'to' || $columnName == 'from') { if ($columnName == 'to' || $columnName == 'from') {
$parameters['orderOnAccount'] = true; $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 * Do some sorting, counting and ordering on the query and return a nicely formatted array
* that can be used by the DataTables JQuery plugin. * that can be used by the DataTables JQuery plugin.
* *
* @param array $parameters * @param array $parameters
* @param Builder $query * @param Builder $query
* *
* @return array * @return array
@ -132,10 +132,10 @@ class Json implements JsonInterface
* Build return array: * Build return array:
*/ */
$data = [ $data = [
'draw' => $parameters['draw'], 'draw' => $parameters['draw'],
'recordsTotal' => $count, 'recordsTotal' => $count,
'recordsFiltered' => $filtered, 'recordsFiltered' => $filtered,
'data' => [], 'data' => [],
]; ];
@ -169,19 +169,20 @@ class Json implements JsonInterface
*/ */
/** @var \TransactionJournal $entry */ /** @var \TransactionJournal $entry */
foreach ($set as $entry) { foreach ($set as $entry) {
$from = $entry->transactions[0]->account; $from = $entry->transactions[0]->account;
$to = $entry->transactions[1]->account; $to = $entry->transactions[1]->account;
$budget = $entry->budgets()->first(); $budget = $entry->budgets()->first();
$category = $entry->categories()->first(); $category = $entry->categories()->first();
$arr = [ $recurring = $entry->recurringTransaction()->first();
'date' => $entry->date->format('j F Y'), $arr = [
'date' => $entry->date->format('j F Y'),
'description' => [ 'description' => [
'description' => $entry->description, 'description' => $entry->description,
'url' => route('transactions.show', $entry->id) 'url' => route('transactions.show', $entry->id)
], ],
'amount' => floatval($entry->amount), 'amount' => floatval($entry->amount),
'from' => ['name' => $from->name, 'url' => route('accounts.show', $from->id)], 'from' => ['name' => $from->name, 'url' => route('accounts.show', $from->id)],
'to' => ['name' => $to->name, 'url' => route('accounts.show', $to->id)], 'to' => ['name' => $to->name, 'url' => route('accounts.show', $to->id)],
'components' => [ 'components' => [
'budget_id' => 0, 'budget_id' => 0,
'budget_url' => '', 'budget_url' => '',
@ -190,20 +191,25 @@ class Json implements JsonInterface
'category_url' => '', 'category_url' => '',
'category_name' => '' 'category_name' => ''
], ],
'id' => [ 'id' => [
'edit' => route('transactions.edit', $entry->id), 'edit' => route('transactions.edit', $entry->id),
'delete' => route('transactions.delete', $entry->id) 'delete' => route('transactions.delete', $entry->id)
] ]
]; ];
if($budget) { if ($budget) {
$arr['components']['budget_id'] = $budget->id; $arr['components']['budget_id'] = $budget->id;
$arr['components']['budget_name'] = $budget->name; $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) { if ($category) {
$arr['components']['category_id'] = $category->id; $arr['components']['category_id'] = $category->id;
$arr['components']['category_name'] = $category->name; $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; $data['data'][] = $arr;
@ -257,14 +263,14 @@ class Json implements JsonInterface
$query->leftJoin( $query->leftJoin(
'transactions AS ' . $prefix . 't1', function ($join) use ($operator) { 'transactions AS ' . $prefix . 't1', function ($join) use ($operator) {
$join->on('t1.transaction_journal_id', '=', 'transaction_journals.id') $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: // left join second table for "to" account:
$query->leftJoin( $query->leftJoin(
'transactions AS ' . $prefix . 't2', function ($join) use ($operatorNegated) { 'transactions AS ' . $prefix . 't2', function ($join) use ($operatorNegated) {
$join->on('t2.transaction_journal_id', '=', 'transaction_journals.id') $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 * Do some sorting, counting and ordering on the query and return a nicely formatted array
* that can be used by the DataTables JQuery plugin. * that can be used by the DataTables JQuery plugin.
* *
* @param array $parameters * @param array $parameters
* @param Builder $query * @param Builder $query
* *
* @return array * @return array
@ -328,10 +334,10 @@ class Json implements JsonInterface
* Build return array: * Build return array:
*/ */
$data = [ $data = [
'draw' => $parameters['draw'], 'draw' => $parameters['draw'],
'recordsTotal' => $count, 'recordsTotal' => $count,
'recordsFiltered' => $filtered, 'recordsFiltered' => $filtered,
'data' => [], 'data' => [],
]; ];
@ -351,16 +357,16 @@ class Json implements JsonInterface
foreach ($set as $entry) { foreach ($set as $entry) {
$data['data'][] = [ $data['data'][] = [
'name' => ['name' => $entry->name,'url' => route('recurring.show',$entry->id)], 'name' => ['name' => $entry->name, 'url' => route('recurring.show', $entry->id)],
'match' => explode(' ',$entry->match), 'match' => explode(' ', $entry->match),
'amount_max' => floatval($entry->amount_max), 'amount_max' => floatval($entry->amount_max),
'amount_min' => floatval($entry->amount_min), 'amount_min' => floatval($entry->amount_min),
'date' => $entry->date->format('j F Y'), 'date' => $entry->date->format('j F Y'),
'active' => intval($entry->active), 'active' => intval($entry->active),
'automatch' => intval($entry->automatch), 'automatch' => intval($entry->automatch),
'repeat_freq' => $entry->repeat_freq, 'repeat_freq' => $entry->repeat_freq,
'id' => [ 'id' => [
'edit' => route('recurring.edit', $entry->id), 'edit' => route('recurring.edit', $entry->id),
'delete' => route('recurring.delete', $entry->id) 'delete' => route('recurring.delete', $entry->id)
] ]
]; ];

View File

@ -176,6 +176,7 @@ class Transaction implements TransactionInterface
$components = array_merge($budgetids,$catids); $components = array_merge($budgetids,$catids);
$journal->components()->sync($components); $journal->components()->sync($components);
$journal->save(); $journal->save();
if (isset($data['return_journal']) && $data['return_journal'] == true) { if (isset($data['return_journal']) && $data['return_journal'] == true) {
return $journal; return $journal;
} }
@ -469,6 +470,12 @@ class Transaction implements TransactionInterface
} }
$journal->completed = true; $journal->completed = true;
$journal->save(); $journal->save();
/*
* Trigger recurring transaction event.
*/
\Event::fire('journals.store',[$journal]);
if (isset($data['return_journal']) && $data['return_journal'] == true) { if (isset($data['return_journal']) && $data['return_journal'] == true) {
return ['journal' => $journal, 'messagebag' => $journal->errors()]; return ['journal' => $journal, 'messagebag' => $journal->errors()];
} }

View File

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

View File

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

View File

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

View File

@ -95,6 +95,9 @@ $(document).ready(function () {
if (data.category_id > 0) { if (data.category_id > 0) {
html += '<a href="' + data.category_url + '" title="' + data.category_name + '"><i class="fa fa-bar-chart fa-fw"></i></a> '; html += '<a href="' + data.category_url + '" title="' + data.category_name + '"><i class="fa fa-bar-chart fa-fw"></i></a> ';
} }
if(data.recurring_id > 0) {
html += '<a href="' + data.recurring_url + '" title="' + data.recurring_name + '"><i class="fa fa-rotate-right fa-fw"></i></a> ';
}
return html; return html;
} }
}, },