2014-07-20 11:24:27 -05:00
|
|
|
<?php
|
|
|
|
|
2014-07-27 13:29:58 -05:00
|
|
|
use Firefly\Helper\Controllers\BudgetInterface as BI;
|
2014-07-20 11:24:27 -05:00
|
|
|
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
|
2014-07-27 13:29:58 -05:00
|
|
|
use Carbon\Carbon;
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* Class BudgetController
|
|
|
|
*/
|
2014-07-20 11:24:27 -05:00
|
|
|
class BudgetController extends BaseController
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $_budgets;
|
2014-07-27 13:29:58 -05:00
|
|
|
protected $_repository;
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @param BRI $budgets
|
|
|
|
*/
|
2014-07-27 13:29:58 -05:00
|
|
|
public function __construct(BI $budgets, BRI $repository)
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
|
|
|
$this->_budgets = $budgets;
|
2014-07-27 13:29:58 -05:00
|
|
|
$this->_repository = $repository;
|
2014-07-20 11:24:27 -05:00
|
|
|
View::share('menu', 'budgets');
|
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-27 13:29:58 -05:00
|
|
|
public function create()
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
2014-07-27 13:29:58 -05:00
|
|
|
$periods = \Config::get('firefly.periods_to_text');
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-27 13:29:58 -05:00
|
|
|
return View::make('budgets.create')->with('periods', $periods);
|
2014-07-20 11:24:27 -05:00
|
|
|
}
|
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\View\View
|
|
|
|
*/
|
2014-07-24 15:16:42 -05:00
|
|
|
public function indexByBudget()
|
2014-07-20 11:24:27 -05:00
|
|
|
{
|
2014-07-27 13:29:58 -05:00
|
|
|
$budgets = $this->_repository->get();
|
|
|
|
$today = new Carbon;
|
|
|
|
|
2014-07-24 15:16:42 -05:00
|
|
|
return View::make('budgets.indexByBudget')->with('budgets', $budgets)->with('today', $today);
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-24 15:16:42 -05:00
|
|
|
}
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-25 06:02:01 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\View\View
|
2014-07-27 13:29:58 -05:00
|
|
|
* @throws Firefly\Exception\FireflyException
|
2014-07-25 06:02:01 -05:00
|
|
|
*/
|
2014-07-27 13:29:58 -05:00
|
|
|
public function indexByDate()
|
2014-07-24 15:16:42 -05:00
|
|
|
{
|
2014-07-27 13:29:58 -05:00
|
|
|
// get a list of dates by getting all repetitions:
|
|
|
|
$set = $this->_repository->get();
|
|
|
|
$budgets = $this->_budgets->organizeByDate($set);
|
2014-07-20 11:24:27 -05:00
|
|
|
|
2014-07-27 13:29:58 -05:00
|
|
|
return View::make('budgets.indexByDate')->with('budgets', $budgets);
|
2014-07-20 11:24:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-24 15:16:42 -05:00
|
|
|
/**
|
|
|
|
* TODO actual view, actual content.
|
2014-07-25 06:02:01 -05:00
|
|
|
*
|
2014-07-24 15:16:42 -05:00
|
|
|
* @param $budgetId
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-07-20 11:24:27 -05:00
|
|
|
public function show($budgetId)
|
|
|
|
{
|
2014-07-24 15:16:42 -05:00
|
|
|
/** @var \Budget $budget */
|
2014-07-20 11:24:27 -05:00
|
|
|
$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 = '';
|
2014-07-20 11:24:27 -05:00
|
|
|
|
|
|
|
foreach ($return as $month => $set) {
|
2014-07-22 23:57:51 -05:00
|
|
|
$str .= '<h1>' . $month . '</h1>';
|
2014-07-20 11:24:27 -05:00
|
|
|
/** @var \TransactionJournal $tj */
|
|
|
|
$sum = 0;
|
|
|
|
foreach ($set as $tj) {
|
2014-07-22 23:57:51 -05:00
|
|
|
$str .= '#' . $tj->id . ' ' . $tj->description . ': ';
|
2014-07-20 11:24:27 -05:00
|
|
|
|
|
|
|
foreach ($tj->transactions as $index => $t) {
|
2014-07-22 23:57:51 -05:00
|
|
|
$str .= $t->amount . ', ';
|
2014-07-20 11:24:27 -05:00
|
|
|
if ($index == 0) {
|
|
|
|
$sum += $t->amount;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2014-07-22 23:57:51 -05:00
|
|
|
$str .= '<br>';
|
2014-07-20 11:24:27 -05:00
|
|
|
|
|
|
|
}
|
2014-07-22 23:57:51 -05:00
|
|
|
$str .= 'sum: ' . $sum . '<br><br>';
|
2014-07-20 11:24:27 -05:00
|
|
|
}
|
|
|
|
|
2014-07-22 23:57:51 -05:00
|
|
|
return $str;
|
2014-07-20 11:24:27 -05:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-27 13:29:58 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function store()
|
|
|
|
{
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'name' => Input::get('name'),
|
|
|
|
'amount' => floatval(Input::get('amount')),
|
|
|
|
'repeat_freq' => Input::get('period'),
|
|
|
|
'repeats' => intval(Input::get('repeats'))
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->_budgets->store($data);
|
|
|
|
Session::flash('success', 'Budget created!');
|
|
|
|
|
|
|
|
return Redirect::route('budgets.index');
|
|
|
|
}
|
|
|
|
|
2014-07-23 01:16:04 -05:00
|
|
|
|
2014-07-20 11:24:27 -05:00
|
|
|
}
|