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

@@ -0,0 +1,97 @@
<?php
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
class BudgetController extends BaseController
{
protected $_budgets;
public function __construct(BRI $budgets)
{
$this->_budgets = $budgets;
View::share('menu', 'budgets');
}
public function index()
{
$budgets = $this->_budgets->get();
$today = new \Carbon\Carbon;
return View::make('budgets.index')->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',
];
return View::make('budgets.create')->with('periods', $periods);
}
public function store()
{
$data = [
'name' => Input::get('name'),
'amount' => floatval(Input::get('amount')),
'repeat_freq' => Input::get('period'),
'repeats' => intval(Input::get('repeats'))
];
$budget = $this->_budgets->create($data);
Session::flash('success', 'Budget created!');
return Redirect::route('budgets.index');
}
public function show($budgetId)
{
$budget = $this->_budgets->find($budgetId);
$list = $budget->transactionjournals()->get();
$return = [];
/** @var \TransactionJournal $entry */
foreach ($list as $entry) {
$month = $entry->date->format('F Y');
$return[$month] = isset($return[$month]) ? $return[$month] : [];
$return[$month][] = $entry;
}
foreach ($return as $month => $set) {
echo '<h1>' . $month . '</h1>';
/** @var \TransactionJournal $tj */
$sum = 0;
foreach ($set as $tj) {
echo '#' . $tj->id . ' ' . $tj->description . ': ';
foreach ($tj->transactions as $index => $t) {
echo $t->amount . ', ';
if ($index == 0) {
$sum += $t->amount;
}
}
echo '<br>';
}
echo 'sum: ' . $sum . '<br><br>';
}
exit;
return View::make('budgets.show');
}
}

View File

@@ -23,6 +23,9 @@ class HomeController extends BaseController
$this->_preferences = $preferences;
$this->_journal = $journal;
View::share('menu', 'home');
}
/**
@@ -30,10 +33,28 @@ class HomeController extends BaseController
*/
public function index()
{
// get the accounts to display on the home screen:
// count, maybe we need some introductionary text to show:
$count = $this->_accounts->count();
// get the preference for the home accounts to show:
$frontpage = $this->_preferences->get('frontpageAccounts', []);
$accounts = $this->_accounts->getByIds($frontpage->data);
$transactions = [];
foreach($accounts as $account) {
$transactions[] = [$this->_journal->getByAccount($account,15),$account];
}
if(count($transactions) % 2 == 0) {
$transactions = array_chunk($transactions, 2);
} elseif(count($transactions) == 1) {
$transactions = array_chunk($transactions, 3);
} else {
$transactions = array_chunk($transactions, 3);
}
// build the home screen:
return View::make('index')->with('count', $count);
return View::make('index')->with('count', $count)->with('transactions',$transactions);
}
}

View File

@@ -0,0 +1,49 @@
<?php
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
use Firefly\Storage\Limit\LimitRepositoryInterface as LRI;
class LimitController extends BaseController
{
protected $_budgets;
protected $_limits;
public function __construct(BRI $budgets, LRI $limits)
{
$this->_budgets = $budgets;
$this->_limits = $limits;
View::share('menu', 'budgets');
}
public function create($budgetId = null)
{
$periods = [
'weekly' => 'A week',
'monthly' => 'A month',
'quarterly' => 'A quarter',
'half-year' => 'Six months',
'yearly' => 'A year',
];
$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(
'periods', $periods
);
}
public function store()
{
// find a limit with these properties, as we might already have one:
$limit = $this->_limits->store(Input::all());
if($limit->id) {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.limits.create')->withInput();
}
}
}

View File

@@ -1,5 +1,6 @@
<?php
use Carbon\Carbon as Carbon;
use Firefly\Helper\Migration\MigrationHelperInterface as MHI;
/**
@@ -40,10 +41,123 @@ class MigrationController extends BaseController
exit();
}
}
echo '<a href="'.route('index').'">home</a>';
echo '<a href="' . route('index') . '">home</a>';
exit();
}
public function limit()
{
$user = User::find(1);
$budgets = [];
// new budget
for ($i = 0; $i < 7; $i++) {
$budget = new Budget();
$budget->user()->associate($user);
$budget->name = 'Some budget #' . rand(1, 2000);
$budget->save();
$budgets[] = $budget;
}
// create a non-repeating limit for this week:
$today = new Carbon('01-07-2014');
$limit = new Limit;
$limit->budget()->associate($budgets[0]);
$limit->amount = 100;
$limit->startdate = $today;
$limit->amount = 100;
$limit->repeats = 0;
$limit->repeat_freq = 'weekly';
var_dump($limit->save());
var_dump($limit->errors()->all());
// create a repeating daily limit:
$day = new Limit;
$day->budget()->associate($budgets[1]);
$day->amount = 100;
$day->startdate = $today;
$day->amount = 100;
$day->repeats = 1;
$day->repeat_freq = 'daily';
$day->save();
// repeating weekly limit.
$week = new Limit;
$week->budget()->associate($budgets[2]);
$week->amount = 100;
$week->startdate = $today;
$week->amount = 100;
$week->repeats = 1;
$week->repeat_freq = 'weekly';
$week->save();
// repeating monthly limit
$month = new Limit;
$month->budget()->associate($budgets[3]);
$month->amount = 100;
$month->startdate = $today;
$month->amount = 100;
$month->repeats = 1;
$month->repeat_freq = 'monthly';
$month->save();
// quarter
$quarter = new Limit;
$quarter->budget()->associate($budgets[4]);
$quarter->amount = 100;
$quarter->startdate = $today;
$quarter->amount = 100;
$quarter->repeats = 1;
$quarter->repeat_freq = 'quarterly';
$quarter->save();
// six months
$six = new Limit;
$six->budget()->associate($budgets[5]);
$six->amount = 100;
$six->startdate = $today;
$six->amount = 100;
$six->repeats = 1;
$six->repeat_freq = 'half-year';
$six->save();
// year
$yearly = new Limit;
$yearly->budget()->associate($budgets[6]);
$yearly->amount = 100;
$yearly->startdate = $today;
$yearly->amount = 100;
$yearly->repeats = 1;
$yearly->repeat_freq = 'yearly';
$yearly->save();
// create a repeating weekly limit:
// create a repeating monthly limit:
foreach ($budgets as $budget) {
echo '#' . $budget->id . ': ' . $budget->name . ':<br />';
foreach ($budget->limits()->get() as $limit) {
echo '&nbsp;&nbsp;Limit #' . $limit->id . ', amount: ' . $limit->amount . ', start: '
. $limit->startdate->format('D d-m-Y') . ', repeats: '
. $limit->repeats . ', repeat_freq: ' . $limit->repeat_freq . '<br />';
foreach ($limit->limitrepetitions()->get() as $rep) {
echo '&nbsp;&nbsp;&nbsp;&nbsp;rep: #' . $rep->id . ', from ' . $rep->startdate->format('D d-m-Y')
. ' to '
. $rep->enddate->format('D d-m-Y') . '<br>';
}
}
}
return '';
}
/**
* @return \Illuminate\View\View
*/