firefly-iii/app/controllers/BudgetController.php

137 lines
3.8 KiB
PHP
Raw Normal View History

<?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');
}
2014-07-20 13:46:10 -05:00
public function index($group = null)
{
2014-07-20 13:46:10 -05:00
$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];
}
}
}
// 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;
}
}
}
krsort($reps);
2014-07-23 01:16:04 -05:00
return View::make('budgets.index')->with('group', $group)->with('reps', $reps);
2014-07-20 13:46:10 -05:00
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;
}
}
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'))
];
2014-07-22 23:57:51 -05:00
$this->_budgets->store($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;
}
2014-07-22 23:57:51 -05:00
$str = '';
foreach ($return as $month => $set) {
2014-07-22 23:57:51 -05:00
$str .= '<h1>' . $month . '</h1>';
/** @var \TransactionJournal $tj */
$sum = 0;
foreach ($set as $tj) {
2014-07-22 23:57:51 -05:00
$str .= '#' . $tj->id . ' ' . $tj->description . ': ';
foreach ($tj->transactions as $index => $t) {
2014-07-22 23:57:51 -05:00
$str .= $t->amount . ', ';
if ($index == 0) {
$sum += $t->amount;
}
}
2014-07-22 23:57:51 -05:00
$str .= '<br>';
}
2014-07-22 23:57:51 -05:00
$str .= 'sum: ' . $sum . '<br><br>';
}
2014-07-22 23:57:51 -05:00
return $str;
}
2014-07-23 01:16:04 -05:00
}