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.
*
* @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.
*/

View File

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

View File

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

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

View File

@ -95,6 +95,9 @@ $(document).ready(function () {
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> ';
}
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;
}
},