Some new stuff for budget management. [skip ci]

This commit is contained in:
James Cole 2014-07-24 22:16:42 +02:00
parent 36901359d0
commit a40b281ea3
19 changed files with 317 additions and 156 deletions

39
app/config/firefly.php Normal file
View File

@ -0,0 +1,39 @@
<?php
return [
'index_periods' => '1D', '1W', '1M', '3M', '6M', 'custom',
'budget_periods' => 'daily', 'weekly', 'monthly', 'quarterly', 'half-year', 'yearly',
'periods_to_text' => [
'weekly' => 'A week',
'monthly' => 'A month',
'quarterly' => 'A quarter',
'half-year' => 'Six months',
'yearly' => 'A year',
],
'range_to_text' => [
'1D' => 'day',
'1W' => 'week',
'1M' => 'month',
'3M' => 'three months',
'6M' => 'half year',
'custom' => '(custom)'
],
'range_to_repeat_freq' => [
'1D' => 'weekly',
'1W' => 'weekly',
'1M' => 'monthly',
'3M' => 'quarterly',
'6M' => 'half-year',
'custom' => 'monthly'
],
'date_formats_by_period' => [
'monthly' => [
'group_date' => 'Y-m',
'display_date' => 'F Y'
],
'weekly' => [
'group_date' => 'Y-W',
'display_date' => '\W\e\e\k W, Y'
]
]
];

View File

@ -13,67 +13,57 @@ class BudgetController extends BaseController
View::share('menu', 'budgets');
}
public function index($group = null)
public function indexByDate()
{
$opts = ['date', 'budget'];
$group = in_array($group, $opts) ? $group : 'date';
switch ($group) {
case 'date':
// get a list of dates by getting all repetitions:
$budgets = $this->_budgets->get();
$reps = [];
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
$monthOrder = $rep->startdate->format('Y-m');
$month = $rep->startdate->format('F Y');
$reps[$monthOrder] = isset($reps[$monthOrder]) ? $reps[$monthOrder] : ['date' => $month];
}
}
// get a list of dates by getting all repetitions:
$budgets = $this->_budgets->get();
$reps = [];
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
if(is_null($dateFormats)) {
die('No date formats for ' . $limit->repeat_freq);
}
// put all the budgets under their respective date:
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
foreach ($limit->limitrepetitions as $rep) {
$month = $rep->startdate->format('Y-m');
$reps[$month]['limitrepetitions'][] = $rep;
}
}
foreach ($limit->limitrepetitions as $rep) {
$periodOrder = $rep->startdate->format($dateFormats['group_date']);
$period = $rep->startdate->format($dateFormats['display_date']);
$reps[$periodOrder] = isset($reps[$periodOrder]) ? $reps[$periodOrder] : ['date' => $period];
}
krsort($reps);
return View::make('budgets.index')->with('group', $group)->with('reps', $reps);
break;
case 'budget':
$budgets = $this->_budgets->get();
$today = new \Carbon\Carbon;
return View::make('budgets.index')->with('budgets', $budgets)->with('today', $today)->with(
'group', $group
);
break;
}
}
// put all the budgets under their respective date:
foreach ($budgets as $budget) {
foreach ($budget->limits as $limit) {
$dateFormats = \Config::get('firefly.date_formats_by_period.' . $limit->repeat_freq);
if(is_null($dateFormats)) {
die('No date formats for ' . $limit->repeat_freq);
}
foreach ($limit->limitrepetitions as $rep) {
$month = $rep->startdate->format($dateFormats['group_date']);
$reps[$month]['limitrepetitions'][] = $rep;
}
}
}
krsort($reps);
return View::make('budgets.indexByDate')->with('reps', $reps);
}
public function indexByBudget()
{
$budgets = $this->_budgets->get();
$today = new \Carbon\Carbon;
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today);
}
public function create()
{
$periods = [
'weekly' => 'A week',
'monthly' => 'A month',
'quarterly' => 'A quarter',
'half-year' => 'Six months',
'yearly' => 'A year',
];
$periods = \Config::get('firefly.periods_to_text');
return View::make('budgets.create')->with('periods', $periods);
}
@ -92,8 +82,15 @@ class BudgetController extends BaseController
return Redirect::route('budgets.index');
}
/**
* TODO actual view, actual content.
* @param $budgetId
*
* @return string
*/
public function show($budgetId)
{
/** @var \Budget $budget */
$budget = $this->_budgets->find($budgetId);
$list = $budget->transactionjournals()->get();
@ -102,7 +99,6 @@ class BudgetController extends BaseController
foreach ($list as $entry) {
$month = $entry->date->format('F Y');
$return[$month] = isset($return[$month]) ? $return[$month] : [];
$return[$month][] = $entry;
}

View File

@ -36,8 +36,7 @@ class ChartController extends BaseController
*/
public function homeAccount($accountId = null)
{
list($start, $end) = $this->_tk->getDateRange();
\Log::debug('Start is (cannot clone?): ' . $start);
list($start, $end) = $this->_tk->getDateRangeDates();
$current = clone $start;
$return = [];
$account = null;
@ -75,10 +74,7 @@ class ChartController extends BaseController
}
} else {
$return[0] = ['name' => $account->name, 'id' => $account->id, 'data' => []];
\Log::debug('Start is: '.$start);
\Log::debug('End is: '.$end);
while ($current <= $end) {
\Log::debug('Current: ' . $current.' is smaller or equal to ' . $end);
if ($current > $today) {
$return[0]['data'][] = [$current->timestamp * 1000, $account->predict(clone $current)];
} else {
@ -88,21 +84,6 @@ class ChartController extends BaseController
$current->addDay();
}
}
// // add an error bar as experiment:
// foreach($return as $index => $serie) {
// $err = [
// 'type' => 'errorbar',
// 'name' => $serie['name'].' pred',
// 'linkedTo' => $serie['id'],
// 'data' => []
// ];
// foreach($serie['data'] as $entry) {
// $err['data'][] = [$entry[0],10,300];
// }
// $return[] = $err;
// }
return Response::json($return);
}
@ -140,7 +121,7 @@ class ChartController extends BaseController
public function homeCategories()
{
list($start, $end) =$this->_tk->getDateRange();
list($start, $end) =$this->_tk->getDateRangeDates();
$account = null;
$result = [];
// grab all transaction journals in this period:

View File

@ -17,8 +17,8 @@ class HomeController extends BaseController
protected $_tk;
/**
* @param ARI $accounts
* @param PHI $preferences
* @param ARI $accounts
* @param PHI $preferences
* @param TJRI $journal
*/
public function __construct(ARI $accounts, PHI $preferences, TJRI $journal, Toolkit $toolkit, BRI $budgets)
@ -40,7 +40,7 @@ class HomeController extends BaseController
{
// count, maybe we need some introducing text to show:
$count = $this->_accounts->count();
list($start, $end) = $this->_tk->getDateRange();
list($start, $end) = $this->_tk->getDateRangeDates();
// get the preference for the home accounts to show:
@ -53,12 +53,14 @@ class HomeController extends BaseController
// get the budgets for this period:
$dates = $this->_tk->getDateRange();
$budgets = $this->_budgets->getWithRepetitionsInPeriod($dates[0], \Session::get('range'));
$budgets = $this->_budgets->getWithRepetitionsInPeriod($start, \Session::get('range'));
$transactions = [];
foreach ($accounts as $account) {
$transactions[] = [$this->_journal->getByAccountInDateRange($account, 15, $start, $end), $account];
$set = $this->_journal->getByAccountInDateRange($account, 15, $start, $end);
if (count($set) > 0) {
$transactions[] = [$set, $account];
}
}
if (count($transactions) % 2 == 0) {
@ -73,4 +75,10 @@ class HomeController extends BaseController
'budgets', $budgets
);
}
public function flush()
{
Cache::flush();
return Redirect::route('index');
}
}

View File

@ -19,20 +19,16 @@ class LimitController extends BaseController
public function create($budgetId = null)
{
$periods = [
'weekly' => 'A week',
'monthly' => 'A month',
'quarterly' => 'A quarter',
'half-year' => 'Six months',
'yearly' => 'A year',
$periods = \Config::get('firefly.periods_to_text');
$prefilled = [
'startdate' => Input::get('startdate') ? : date('Y-m-d'),
'repeat_freq' => Input::get('repeat_freq') ? : 'monthly'
];
$budget = $this->_budgets->find($budgetId);
$budget_id = is_null($budget) ? null : $budget->id;
$budgets = $this->_budgets->getAsSelectList();
return View::make('limits.create')->with('budgets', $budgets)->with('budget_id', $budget_id)->with(
return View::make('limits.create')->with('budgets', $budgets)->with('budget_id', $budgetId)->with(
'periods', $periods
);
)->with('prefilled', $prefilled);
}
public function edit($limitId = null)
@ -50,8 +46,9 @@ class LimitController extends BaseController
if ($limit) {
return View::make('limits.edit')->with('limit', $limit)->with('budgets', $budgets)->
with('periods',$periods);
return View::make('limits.edit')->with('limit', $limit)->with('budgets', $budgets)->with(
'periods', $periods
);
}
}
@ -60,23 +57,23 @@ class LimitController extends BaseController
{
/** @var \Limit $limit */
$limit = $this->_limits->find($limitId);
if($limit) {
if ($limit) {
$limit->startdate = new \Carbon\Carbon(Input::get('date'));
$limit->repeat_freq = Input::get('period');
$limit->repeats = !is_null(Input::get('repeats')) && Input::get('repeats') == '1' ? 1 : 0;
$limit->amount = floatval(Input::get('amount'));
if(!$limit->save()) {
Session::flash('error','Could not save new limit: ' . $limit->errors()->first());
return Redirect::route('budgets.limits.edit',$limit->id)->withInput();
if (!$limit->save()) {
Session::flash('error', 'Could not save new limit: ' . $limit->errors()->first());
return Redirect::route('budgets.limits.edit', $limit->id)->withInput();
} else {
Session::flash('success','Limit saved!');
foreach($limit->limitrepetitions()->get() as $rep) {
Session::flash('success', 'Limit saved!');
foreach ($limit->limitrepetitions()->get() as $rep) {
$rep->delete();
}
return Redirect::route('budgets.index');
}
}
return View::make('error')->with('message','No limit!');
return View::make('error')->with('message', 'No limit!');
}

View File

@ -4,11 +4,12 @@
App::before(
function ($request) {
Event::fire('app.before');
if (Auth::check()) {
$toolkit = App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$toolkit->getDateRange();
return $toolkit->getDateRange();
}
Event::fire('app.before');
}
);

View File

@ -37,9 +37,23 @@ class Toolkit implements ToolkitInterface
}
// switch $range, update range or something:
$start = \Session::has('start') ? \Session::get('start') : new \Carbon\Carbon();
$end = \Session::has('end') ? \Session::get('end') : new \Carbon\Carbon();
$today = new \Carbon\Carbon;
$start = clone $today;
$end = clone $today;
\Log::debug('Start: ' . $start.' ('.\Session::has('start').')');
\Log::debug('End: ' . $end);
// see if we have to do a prev / next thing:
$doPrev = false;
$doNext = false;
if (\Input::get('action') == 'prev') {
$doPrev = true;
}
if (\Input::get('action') == 'next') {
$doNext = true;
}
switch ($range) {
case 'custom':
// when range is custom AND input, we ignore $today
@ -53,39 +67,94 @@ class Toolkit implements ToolkitInterface
break;
case '1D':
$start->startOfDay();
$end = clone $start;
$end->endOfDay();
if ($doNext) {
$start->addDay();
$end->addDay();
}
if ($doPrev) {
$start->subDay();
$end->subDay();
}
break;
case '1W':
$start->startOfWeek();
$end = clone $start;
$end->endOfWeek();
if ($doNext) {
$start->addWeek();
$end->addWeek();
}
if ($doPrev) {
$start->subWeek();
$end->subWeek();
}
break;
case '1M':
$start->startOfMonth();
$end = clone $start;
$end->endOfMonth();
if ($doNext) {
$start->addMonth();
$end->addMonth();
}
if ($doPrev) {
$start->subMonth();
\Log::debug('1M prev. Before: ' . $end);
$end->startOfMonth()->subMonth()->endOfMonth();
\Log::debug('1M prev. After: ' . $end);
}
break;
case '3M':
$start->firstOfQuarter();
$end = clone $start;
$end->lastOfQuarter();
if ($doNext) {
$start->addMonths(3)->firstOfQuarter();
$end->addMonths(6)->lastOfQuarter();
}
if ($doPrev) {
$start->subMonths(3)->firstOfQuarter();
$end->subMonths(3)->lastOfQuarter();
}
break;
case '6M':
if (intval($today->format('m')) >= 7) {
$start->startOfYear()->addMonths(6);
$end = clone $start;
$end->endOfYear();
} else {
$start->startOfYear();
$end = clone $start;
$end->startOfYear()->addMonths(6);
}
if ($doNext) {
$start->addMonths(6);
$end->addMonths(6);
}
if ($doPrev) {
$start->subMonths(6);
$end->subMonths(6);
}
break;
}
// save in session:
\Session::put('start', $start);
\Session::put('end', $end);
\Session::put('range', $range);
if ($doPrev || $doNext) {
return \Redirect::route('index');
// and return:
return [$start, $end];
}
return;
}
public function getDateRangeDates()
{
return [\Session::get('start'), \Session::get('end')];
}
}

View File

@ -7,4 +7,6 @@ interface ToolkitInterface
{
public function getDateRange();
public function getDateRangeDates();
}

View File

@ -23,7 +23,7 @@ class EloquentBudgetRepository implements BudgetRepositoryInterface
/** @var \Firefly\Helper\Toolkit\ToolkitInterface $toolkit */
$toolkit = \App::make('Firefly\Helper\Toolkit\ToolkitInterface');
$dates = $toolkit->getDateRange();
$dates = $toolkit->getDateRangeDates();
$start = $dates[0];
$result = [];

View File

@ -4,6 +4,7 @@ Route::group(['before' => 'auth'], function () {
// home controller
Route::get('/', ['uses' => 'HomeController@index', 'as' => 'index']);
Route::get('/flush', ['uses' => 'HomeController@flush', 'as' => 'flush']);
// chart controller
Route::get('/chart/home/account/{account?}', ['uses' => 'ChartController@homeAccount', 'as' => 'chart.home']);
@ -28,7 +29,8 @@ Route::group(['before' => 'auth'], function () {
// budget controller:
Route::get('/budget/create',['uses' => 'BudgetController@create', 'as' => 'budgets.create']);
Route::get('/budgets/{group?}',['uses' => 'BudgetController@index','as' => 'budgets.index']);
Route::get('/budgets',['uses' => 'BudgetController@indexByDate','as' => 'budgets.index']);
Route::get('/budgets/budget',['uses' => 'BudgetController@indexByBudget','as' => 'budgets.index.budget']);
Route::get('/budget/show/{id}',['uses' => 'BudgetController@show', 'as' => 'budgets.show']);
// limit controller:

View File

@ -1,3 +1,23 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Budgets and limits</small>
</h1>
<p class="text-info">
These are your budgets and if set, their "limits". Firefly uses an "<a
href="http://en.wikipedia.org/wiki/Envelope_System" class="text-success">envelope system</a>" for your
budgets,
which means that for each period of time (for example a month) a virtual "envelope" can be created
containing a certain amount of money. Money spent within a budget is removed from the envelope.
</p>
<p>
<a class="btn btn-default" href ="{{route('budgets.index')}}"><span class="glyphicon glyphicon-th"></span> Group by date</a>
<a class="btn btn-default" href ="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create a limit</a>
</p>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<table class="table table-bordered table-striped">
@ -81,4 +101,5 @@
</table>
</div>
</div>
</div>
@stop

View File

@ -1,3 +1,24 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Budgets and limits</small>
</h1>
<p class="text-info">
These are your budgets and if set, their "limits". Firefly uses an "<a
href="http://en.wikipedia.org/wiki/Envelope_System" class="text-success">envelope system</a>" for your
budgets,
which means that for each period of time (for example a month) a virtual "envelope" can be created
containing a certain amount of money. Money spent within a budget is removed from the envelope.
</p>
<p>
<a class="btn btn-default" href ="{{route('budgets.index.budget')}}"><span class="glyphicon glyphicon-indent-left"></span> Group by budget</a>
<a class="btn btn-default" href ="{{route('budgets.limits.create')}}"><span class="glyphicon glyphicon-plus-sign"></span> Create a limit</a>
</p>
</div>
</div>
@foreach($reps as $date => $data)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
@ -46,4 +67,6 @@
</table>
</div>
</div>
@endforeach
@endforeach
@stop

View File

@ -1,47 +1,6 @@
@extends('layouts.default')
@section('content')
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
@if($count > 0)
<small>What's playing?</small>
@endif
</h1>
@if($count > 0)
<form role="form" class="form-horizontal">
<div class="input-group">
<?php $r = Session::get('range', '1M'); ?>
<span class="input-group-btn input-group-btn">
<button name="range" value="1D" class="btn btn-default @if($r=='1D') btn-info @endif btn-sm"
type="submit">1D
</button>
<button name="range" value="1W" class="btn btn-default @if($r=='1W') btn-info @endif btn-sm"
type="submit">1W
</button>
<button name="range" value="1M" class="btn btn-default @if($r=='1M') btn-info @endif btn-sm"
type="submit">1M
</button>
<button name="range" value="3M" class="btn btn-default @if($r=='3M') btn-info @endif btn-sm"
type="submit">3M
</button>
<button name="range" value="6M" class="btn btn-default @if($r=='6M') btn-info @endif btn-sm"
type="submit">6M
</button>
</span>
<input value="{{Session::get('start')->format('Y-m-d')}}" name="start" type="date"
style="width:15%;border-right:0;" class="form-control input-sm">
<input value="{{Session::get('end')->format('Y-m-d')}}" name="end" type="date"
style="width:15%;border-right:0;" class="form-control input-sm">
<button class="btn btn-default btn-sm @if($r=='custom') btn-info @endif" type="submit" name="range"
value="custom">Custom
</button>
</div>
</form>
@endif
</div>
</div>
@include('partials.date_nav')
@if($count == 0)
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
@ -101,7 +60,8 @@
<h5><a href="{{route('budgets.show',$budget->id)}}">{{{$budget->name}}}</a></h5>
@if($budget->count == 0)
<p>
<small><em>No budget set for this period.</em></small>
<a href="{{route('budgets.limits.create',[$budget->id])}}?startdate={{\Session::get('start')->format('Y-m-d')}}&amp;repeat_freq={{\Config::get('firefly.range_to_repeat_freq.' . \Session::get('range'))}}" class="btn btn-xs btn-default"><span class="glyphicon glyphicon-envelope"></span> Add a new envelope</a>
</p>
@else
@foreach($budget->limits as $limit)

View File

@ -3,7 +3,7 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
<small>Set a limit to a budget</small>
<small>Create an envelope for a budget</small>
</h1>
<p class="text-info">
Firefly uses an "<a href="http://en.wikipedia.org/wiki/Envelope_System" class="text-success">envelope
@ -41,7 +41,7 @@
<div class="form-group">
{{ Form::label('startdate', 'Start date', ['class' => 'col-sm-3 control-label'])}}
<div class="col-sm-9">
<input type="date" name="startdate" value="{{Input::old('startdate') ?: date('Y-m-d')}}"
<input type="date" name="startdate" value="{{Input::old('startdate') ?: $prefilled['startdate']}}"
class="form-control"/>
<span class="help-block">This date indicates when the envelope "starts". The date you select
here will correct itself to the nearest [period] you select below.</span>
@ -52,7 +52,7 @@
<label for="period" class="col-sm-3 control-label">Spending period</label>
<div class="col-sm-9">
{{Form::select('period',$periods,Input::old('period') ?: 'monthly',['class' => 'form-control'])}}
{{Form::select('period',$periods,Input::old('period') ?: $prefilled['repeat_freq'],['class' => 'form-control'])}}
<span class="help-block">How long will the envelope last? A week, a month, or even longer?</span>
</div>
</div>

View File

@ -0,0 +1,61 @@
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h1>Firefly
@if($count > 0)
<small>What's playing?</small>
@endif
</h1>
@if($count > 0)
<form role="form" method="GET">
<?php $r = Session::get('range', '1M'); ?>
<div class="row">
<div class="col-lg-2">
<button name="action" value="prev" class="btn btn-default @if($r=='1D') btn-info @endif btn-sm"
type="submit">&laquo; Previous {{Config::get('firefly.range_to_text.'.$r)}}
</button>
</div>
<div class="col-lg-3">
<div class="btn-group btn-group-sm">
<button name="range" value="1D" class="btn btn-default @if($r=='1D') btn-info @endif btn-sm"
type="submit">1D
</button>
<button name="range" value="1W" class="btn btn-default @if($r=='1W') btn-info @endif btn-sm"
type="submit">1W
</button>
<button name="range" value="1M" class="btn btn-default @if($r=='1M') btn-info @endif btn-sm"
type="submit">1M
</button>
<button name="range" value="3M" class="btn btn-default @if($r=='3M') btn-info @endif btn-sm"
type="submit">3M
</button>
<button name="range" value="6M" class="btn btn-default @if($r=='6M') btn-info @endif btn-sm"
type="submit">6M
</button>
</div>
</div>
<div class="col-lg-2">
<input value="{{Session::get('start')->format('Y-m-d')}}" name="start" type="date"
class="form-control input-sm">
</div>
<div class="col-lg-2">
<input value="{{Session::get('end')->format('Y-m-d')}}" name="end" type="date"
class="form-control input-sm">
</div>
<div class="col-lg-1">
<button class="btn btn-default btn-sm @if($r=='custom') btn-info @endif" type="submit" name="range"
value="custom">Custom
</button>
</div>
<div class="col-lg-2" style="text-align:right;">
<button name="action" value="next" class="btn btn-default @if($r=='1D') btn-info @endif btn-sm"
type="submit">&raquo; Next {{Config::get('firefly.range_to_text.'.$r)}}
</button>
</div>
</div>
</form>
@endif
</div>
</div>

1
dev Normal file
View File

@ -0,0 +1 @@
<a href="http://localhost/projects/firefly-iii/public">home</a>

1
dev.1 Normal file
View File

@ -0,0 +1 @@
<a href="http://localhost/projects/firefly-iii/public">home</a>

View File

@ -2,7 +2,7 @@
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="bootstrap/autoload.php"
colors="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"

View File

@ -1,4 +1,3 @@
var accountChart;
$(function () {