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

@@ -3,6 +3,8 @@
namespace Firefly\Helper\Migration;
use Firefly\Helper\MigrationException;
class MigrationHelper implements MigrationHelperInterface
{
protected $path;
@@ -56,6 +58,9 @@ class MigrationHelper implements MigrationHelperInterface
// create transfers:
$this->_importTransfers();
// create limits:
$this->_importLimits();
} catch (\Firefly\Exception\FireflyException $e) {
\DB::rollBack();
@@ -75,7 +80,7 @@ class MigrationHelper implements MigrationHelperInterface
/** @var \Firefly\Storage\Account\AccountRepositoryInterface $accounts */
$accounts = \App::make('Firefly\Storage\Account\AccountRepositoryInterface');
$cash = $accounts->store(['name' => 'Cash account', 'account_type' => $cashAT, 'active' => 0]);
\Log::info('Created cash account (#'.$cash->id.')');
\Log::info('Created cash account (#' . $cash->id . ')');
$this->map['cash'] = $cash;
}
@@ -149,6 +154,39 @@ class MigrationHelper implements MigrationHelperInterface
return $components->store(['name' => $component->name, 'class' => 'Budget']);
}
protected function _importLimits()
{
\Log::info('Importing limits');
foreach ($this->JSON->limits as $entry) {
\Log::debug(
'Now at #' . $entry->id . ': EUR ' . $entry->amount . ' for month ' . $entry->date
. ' and componentID: ' . $entry->component_id
);
$budget = isset($this->map['budgets'][$entry->component_id]) ? $this->map['budgets'][$entry->component_id]
: null;
if (!is_null($budget)) {
\Log::debug('Found budget for this limit: #' . $budget->id . ', ' . $budget->name);
$limit = new \Limit;
$limit->budget()->associate($budget);
$limit->startdate = new \Carbon\Carbon($entry->date);
$limit->amount = floatval($entry->amount);
$limit->repeats = 0;
$limit->repeat_freq = 'monthly';
if (!$limit->save()) {
\Log::error('MigrationException!');
throw new MigrationException('Importing limits failed: ' . $limit->errors()->first());
}
} else {
\Log::warning('No budget for this limit!');
}
// create repeat thing should not be necessary.
}
}
protected function _importTransactions()
{

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'

View File

@@ -0,0 +1,178 @@
<?php
namespace Firefly\Trigger\Limits;
/**
* Class EloquentLimitTrigger
*
* @package Firefly\Trigger\Limits
*/
class EloquentLimitTrigger
{
public function updateLimitRepetitions()
{
if (!\Auth::check()) {
return;
}
// get budgets with limits:
$budgets = \Auth::user()->budgets()
->with(['limits', 'limits.limitrepetitions'])
->whereNotNull('limits.id')
->leftJoin('limits', 'components.id', '=', 'limits.component_id')->get(['components.*']);
// get todays date.
foreach ($budgets as $budget) {
\Log::debug(
'Now checking the ' . count($budget->limits) . ' limits in ' . $budget->name . ' (#' . $budget->id
. ').'
);
// loop limits:
foreach ($budget->limits as $limit) {
\Log::debug(
'Now at limit #' . $limit->id . ', which has ' . count($limit->limitrepetitions) . ' reps already'
);
\Log::debug(
'More: Amount: ' . $limit->amount . ', repeat: ' . $limit->repeats . ', freq: '
. $limit->repeat_freq
);
// should have a repetition, at the very least
// for the period it starts (startdate and onwards).
if (count($limit->limitrepetitions) == 0) {
\Log::debug('No reps, create one.');
// create such a repetition:
$repetition = new \LimitRepetition();
$start = clone $limit->startdate;
$end = clone $start;
// go to end:
switch ($limit->repeat_freq) {
case 'daily':
$end->addDay();
break;
case 'weekly':
$end->addWeek();
break;
case 'monthly':
$end->addMonth();
break;
case 'quarterly':
$end->addMonths(3);
break;
case 'half-year':
$end->addMonths(6);
break;
case 'yearly':
$end->addYear();
break;
}
$end->subDay();
$repetition->startdate = $start;
$repetition->enddate = $end;
$repetition->amount = $limit->amount;
$repetition->limit()->associate($limit);
\Log::debug('Created single rep for non-repeating limit, from ' . $start . ' until ' . $end);
try {
$repetition->save();
} catch (\Illuminate\Database\QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
} else {
// there are limits already, do they
// fall into the range surrounding today?
$today = new \Carbon\Carbon;
$today->addMonths(2);
if ($limit->repeats == 1 && $today >= $limit->startdate) {
/** @var \Carbon\Carbon $flowStart */
$flowStart = clone $today;
/** @var \Carbon\Carbon $flowEnd */
$flowEnd = clone $today;
switch ($limit->repeat_freq) {
case 'daily':
$flowStart->startOfDay();
$flowEnd->endOfDay();
break;
case 'weekly':
$flowStart->startOfWeek();
$flowEnd->endOfWeek();
break;
case 'monthly':
$flowStart->startOfMonth();
$flowEnd->endOfMonth();
break;
case 'quarterly':
$flowStart->firstOfQuarter();
$flowEnd->startOfMonth()->lastOfQuarter()->endOfDay();
break;
case 'half-year':
if (intval($flowStart->format('m')) >= 7) {
$flowStart->startOfYear();
$flowStart->addMonths(6);
} else {
$flowStart->startOfYear();
}
$flowEnd->endOfYear();
if (intval($start->format('m')) <= 6) {
$flowEnd->subMonths(6);
$flowEnd->subDay();
}
break;
case 'yearly':
$flowStart->startOfYear();
$flowEnd->endOfYear();
break;
}
$inRange = false;
foreach ($limit->limitrepetitions as $rep) {
if ($rep->startdate->format('dmY') == $flowStart->format('dmY')
&& $rep->enddate->format('dmY') == $flowEnd->format('dmY')
) {
// falls in current range, do nothing?
$inRange = true;
}
}
// if there is none that fall in range, create!
if ($inRange === false) {
// create (but check first)!
$count = \LimitRepetition::where('limit_id', $limit->id)->where('startdate', $flowStart)
->where('enddate', $flowEnd)->count();
if ($count == 0) {
$repetition = new \LimitRepetition;
$repetition->startdate = $flowStart;
$repetition->enddate = $flowEnd;
$repetition->amount = $limit->amount;
$repetition->limit()->associate($limit);
try {
$repetition->save();
} catch (\Illuminate\Database\QueryException $e) {
// do nothing
\Log::error($e->getMessage());
}
}
}
}
}
}
\Log::debug('Done checking the budget!');
}
}
public function subscribe(\Illuminate\Events\Dispatcher $events)
{
$events->listen('app.before', 'Firefly\Trigger\Limits\EloquentLimitTrigger@updateLimitRepetitions');
}
}
\Limit::observe(new EloquentLimitTrigger);