New event to create budget limits, new handler to handle said event, new routes for budget control, new routes for limit control, extended migration, extended models, extended JS. [skip-ci]

This commit is contained in:
James Cole
2014-07-20 18:24:27 +02:00
parent 0bcda34738
commit 08cbd91dd9
42 changed files with 1482 additions and 121 deletions

View File

@@ -13,7 +13,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function get()
{
return \Auth::user()->accounts()->with('accounttype')->orderBy('name','ASC')->get();
return \Auth::user()->accounts()->with('accounttype')->orderBy('name', 'ASC')->get();
}
public function getBeneficiaries()
@@ -23,7 +23,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
)
->where('account_types.description', 'Beneficiary account')->where('accounts.active', 1)
->orderBy('accounts.name','ASC')->get(['accounts.*']);
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
return $list;
}
@@ -34,7 +34,11 @@ class EloquentAccountRepository implements AccountRepositoryInterface
public function getByIds($ids)
{
return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name','ASC')->get();
if (count($ids) > 0) {
return \Auth::user()->accounts()->with('accounttype')->whereIn('id', $ids)->orderBy('name', 'ASC')->get();
} else {
return [];
}
}
public function getDefault()
@@ -42,7 +46,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
return \Auth::user()->accounts()->leftJoin('account_types', 'account_types.id', '=', 'accounts.account_type_id')
->where('account_types.description', 'Default account')
->orderBy('accounts.name','ASC')->get(['accounts.*']);
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
}
public function getActiveDefault()
@@ -60,7 +64,7 @@ class EloquentAccountRepository implements AccountRepositoryInterface
)
->where('account_types.description', 'Default account')->where('accounts.active', 1)
->orderBy('accounts.name','ASC')->get(['accounts.*']);
->orderBy('accounts.name', 'ASC')->get(['accounts.*']);
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;

View File

@@ -6,6 +6,9 @@ namespace Firefly\Storage\Budget;
interface BudgetRepositoryInterface
{
public function getAsSelectList();
public function get();
public function create($data);
public function find($id);

View File

@@ -8,7 +8,9 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
public function getAsSelectList()
{
$list = \Auth::user()->budgets()->get();
$list = \Auth::user()->budgets()->with(
['limits', 'limits.limitrepetitions']
)->orderBy('name', 'ASC')->get();
$return = [];
foreach ($list as $entry) {
$return[intval($entry->id)] = $entry->name;
@@ -16,6 +18,63 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
return $return;
}
public function create($data)
{
$budget = new \Budget;
$budget->name = $data['name'];
$budget->user()->associate(\Auth::user());
$budget->save();
// if limit, create limit (repetition itself will be picked up elsewhere).
if ($data['amount'] > 0) {
$limit = new \Limit;
$limit->budget()->associate($budget);
$startDate = new \Carbon\Carbon;
switch ($data['repeat_freq']) {
case 'daily':
$startDate->startOfDay();
break;
case 'weekly':
$startDate->startOfWeek();
break;
case 'monthly':
$startDate->startOfMonth();
break;
case 'quarterly':
$startDate->firstOfQuarter();
break;
case 'half-year':
$startDate->startOfYear();
if (intval($startDate->format('m')) >= 7) {
$startDate->addMonths(6);
}
break;
case 'yearly':
$startDate->startOfYear();
break;
}
$limit->startdate = $startDate;
$limit->amount = $data['amount'];
$limit->repeats = $data['repeats'];
$limit->repeat_freq = $data['repeat_freq'];
$limit->save();
}
return $budget;
}
public function get()
{
return \Auth::user()->budgets()->with(
['limits' => function ($q) {
$q->orderBy('limits.startdate','ASC');
}, 'limits.limitrepetitions' => function ($q) {
$q->orderBy('limit_repetitions.startdate','ASC');
}]
)->orderBy('name', 'ASC')->get();
}
public function find($id)
{

View File

@@ -0,0 +1,84 @@
<?php
/**
* Created by PhpStorm.
* User: sander
* Date: 20/07/14
* Time: 13:43
*/
namespace Firefly\Storage\Limit;
class EloquentLimitRepository implements LimitRepositoryInterface
{
public function store($data)
{
$budget = \Budget::find($data['budget_id']);
if (is_null($budget)) {
\Session::flash('error', 'No such budget.');
return new \Limit;
}
// set the date to the correct start period:
$date = new \Carbon\Carbon($data['startdate']);
switch ($data['period']) {
case 'daily':
$date->startOfDay();
break;
case 'weekly':
$date->startOfWeek();
break;
case 'monthly':
$date->startOfMonth();
break;
case 'quarterly':
$date->firstOfQuarter();
break;
case 'half-year':
if (intval($date->format('m')) >= 7) {
$date->startOfYear();
$date->addMonths(6);
} else {
$date->startOfYear();
}
break;
case 'yearly':
$date->startOfYear();
break;
}
// find existing:
$count = \Limit::
leftJoin('components', 'components.id', '=', 'limits.component_id')->where(
'components.user_id', \Auth::user()->id
)->where('startdate', $date->format('Y-m-d'))->where('component_id', $data['budget_id'])->where(
'repeat_freq', $data['period']
)->count();
if ($count > 0) {
\Session::flash('error', 'There already is an entry for these parameters.');
return new \Limit;
}
// create new limit:
$limit = new \Limit;
$limit->budget()->associate($budget);
$limit->startdate = $date;
$limit->amount = floatval($data['amount']);
$limit->repeats = isset($data['repeats']) ? intval($data['repeats']) : 0;
$limit->repeat_freq = $data['period'];
if (!$limit->save()) {
Session::flash('error', 'Could not save: ' . $limit->errors()->first());
}
return $limit;
}
public function getTJByBudgetAndDateRange(\Budget $budget, \Carbon\Carbon $start, \Carbon\Carbon $end)
{
$type = \TransactionType::where('type', 'Withdrawal')->first();
$result = $budget->transactionjournals()->after($start)->
before($end)->get();
return $result;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Firefly\Storage\Limit;
interface LimitRepositoryInterface
{
public function store($data);
public function getTJByBudgetAndDateRange(\Budget $budget, \Carbon\Carbon $start, \Carbon\Carbon $end);
}

View File

@@ -34,6 +34,11 @@ class StorageServiceProvider extends ServiceProvider
'Firefly\Storage\Component\EloquentComponentRepository'
);
$this->app->bind(
'Firefly\Storage\Limit\LimitRepositoryInterface',
'Firefly\Storage\Limit\EloquentLimitRepository'
);
$this->app->bind(
'Firefly\Storage\Budget\BudgetRepositoryInterface',
'Firefly\Storage\Budget\EloquentBudgetRepository'