Cleanup in budgets.

This commit is contained in:
James Cole 2015-09-27 17:44:49 +02:00
parent 49d4705014
commit 866bc2f3bd
4 changed files with 59 additions and 41 deletions

View File

@ -8,6 +8,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition; use FireflyIII\Models\LimitRepetition;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Input; use Input;
use Navigation;
use Preferences; use Preferences;
use Response; use Response;
use Session; use Session;
@ -136,24 +137,27 @@ class BudgetController extends Controller
*/ */
public function index(BudgetRepositoryInterface $repository) public function index(BudgetRepositoryInterface $repository)
{ {
$budgets = $repository->getActiveBudgets(); $budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets(); $inactive = $repository->getInactiveBudgets();
$spent = '0'; $spent = '0';
$budgeted = '0'; $budgeted = '0';
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod(Session::get('start', new Carbon), $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$budgetIncomeTotal = Preferences::get($key, 1000)->data;
$period = Navigation::periodShow($start, $range);
bcscale(2); bcscale(2);
/** /**
* Do some cleanup: * Do some cleanup:
*/ */
$repository->cleanupBudgets(); $repository->cleanupBudgets();
// loop the budgets: // loop the budgets:
/** @var Budget $budget */ /** @var Budget $budget */
foreach ($budgets as $budget) { foreach ($budgets as $budget) {
$date = Session::get('start', Carbon::now()->startOfMonth()); $budget->spent = $repository->balanceInPeriod($budget, $start, $end);
$end = Session::get('end', Carbon::now()->endOfMonth()); $budget->currentRep = $repository->getCurrentRepetition($budget, $start, $end);
$budget->spent = $repository->balanceInPeriod($budget, $date, $end);
$budget->currentRep = $repository->getCurrentRepetition($budget, $date);
if ($budget->currentRep) { if ($budget->currentRep) {
$budgeted = bcadd($budgeted, $budget->currentRep->amount); $budgeted = bcadd($budgeted, $budget->currentRep->amount);
} }
@ -161,13 +165,12 @@ class BudgetController extends Controller
} }
$dateAsString = Session::get('start', Carbon::now()->startOfMonth())->format('FY');
$budgetIncomeTotal = Preferences::get('budgetIncomeTotal' . $dateAsString, 1000)->data; $budgetMaximum = Preferences::get('budgetMaximum', 1000)->data;
$budgetMaximum = Preferences::get('budgetMaximum', 1000)->data; $defaultCurrency = Amount::getDefaultCurrency();
$defaultCurrency = Amount::getDefaultCurrency();
return view( return view(
'budgets.index', compact('budgetMaximum', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted') 'budgets.index', compact('budgetMaximum','period', 'range', 'budgetIncomeTotal', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted')
); );
} }
@ -178,8 +181,9 @@ class BudgetController extends Controller
*/ */
public function noBudget(BudgetRepositoryInterface $repository) public function noBudget(BudgetRepositoryInterface $repository)
{ {
$start = Session::get('start', Carbon::now()->startOfMonth()); $range = Preferences::get('viewRange', '1M')->data;
$end = Session::get('end', Carbon::now()->startOfMonth()); $start = Navigation::startOfPeriod(Session::get('start', new Carbon), $range);
$end = Navigation::endOfPeriod($start, $range);
$list = $repository->getWithoutBudget($start, $end); $list = $repository->getWithoutBudget($start, $end);
$subTitle = trans( $subTitle = trans(
'firefly.without_budget_between', 'firefly.without_budget_between',
@ -194,9 +198,12 @@ class BudgetController extends Controller
*/ */
public function postUpdateIncome() public function postUpdateIncome()
{ {
$range = Preferences::get('viewRange', '1M')->data;
$start = Navigation::startOfPeriod(Session::get('start', new Carbon), $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$date = Session::get('start', Carbon::now()->startOfMonth())->format('FY'); Preferences::set($key, intval(Input::get('amount')));
Preferences::set('budgetIncomeTotal' . $date, intval(Input::get('amount')));
Preferences::mark(); Preferences::mark();
return redirect(route('budgets.index')); return redirect(route('budgets.index'));
@ -297,8 +304,11 @@ class BudgetController extends Controller
*/ */
public function updateIncome() public function updateIncome()
{ {
$date = Session::get('start', Carbon::now()->startOfMonth())->format('FY'); $range = Preferences::get('viewRange', '1M')->data;
$amount = Preferences::get('budgetIncomeTotal' . $date, 1000); $start = Navigation::startOfPeriod(Session::get('start', new Carbon), $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$amount = Preferences::get($key, 1000);
return view('budgets.income', compact('amount')); return view('budgets.income', compact('amount'));
} }

View File

@ -122,20 +122,26 @@ class BudgetRepository extends ComponentRepository implements BudgetRepositoryIn
/** /**
* @param Budget $budget * @param Budget $budget
* @param Carbon $date * @param Carbon $start
* @param Carbon $end
* *
* @return LimitRepetition|null * @return LimitRepetition|null
*/ */
public function getCurrentRepetition(Budget $budget, Carbon $date) public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end)
{ {
$cache = new CacheProperties; $cache = new CacheProperties;
$cache->addProperty($budget->id); $cache->addProperty($budget->id);
$cache->addProperty($date); $cache->addProperty($start);
$cache->addProperty($end);
$cache->addProperty('getCurrentRepetition'); $cache->addProperty('getCurrentRepetition');
if ($cache->has()) { if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore return $cache->get(); // @codeCoverageIgnore
} }
$data = $budget->limitrepetitions()->where('limit_repetitions.startdate', $date)->first(['limit_repetitions.*']); $data = $budget->limitrepetitions()
->where('limit_repetitions.startdate', $start)
->where('limit_repetitions.enddate', $end)
->first(['limit_repetitions.*']);
$cache->store($data); $cache->store($data);
return $data; return $data;

View File

@ -65,11 +65,12 @@ interface BudgetRepositoryInterface
/** /**
* @param Budget $budget * @param Budget $budget
* @param Carbon $date * @param Carbon $start
* @param Carbon $end
* *
* @return LimitRepetition|null * @return LimitRepetition|null
*/ */
public function getCurrentRepetition(Budget $budget, Carbon $date); public function getCurrentRepetition(Budget $budget, Carbon $start, Carbon $end);
/** /**
* @param Budget $budget * @param Budget $budget

View File

@ -23,7 +23,7 @@
<small>{{ 'budgeted'|_ }}: <span id="budgetedAmount" class="text-success">{{ budgeted|formatAmountPlain }}</span></small> <small>{{ 'budgeted'|_ }}: <span id="budgetedAmount" class="text-success">{{ budgeted|formatAmountPlain }}</span></small>
</div> </div>
<div class="col-lg-6 col-md-4 col-sm-3" style="text-align:right;"> <div class="col-lg-6 col-md-4 col-sm-3" style="text-align:right;">
<small>{{ trans('firefly.availableIn',{date : Session.get('start').formatLocalized(monthFormat) }) }}: <small>{{ trans('firefly.availableIn',{date : period }) }}:
<a href="#" class="updateIncome"><span id="budgetIncomeTotal" <a href="#" class="updateIncome"><span id="budgetIncomeTotal"
data-value="{{ budgetIncomeTotal }}">{{ budgetIncomeTotal|formatAmount }}</span></a> data-value="{{ budgetIncomeTotal }}">{{ budgetIncomeTotal|formatAmount }}</span></a>
</small> </small>
@ -82,6 +82,21 @@
</p> </p>
</div> </div>
</div> </div>
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'createBudget'|_ }}</h3>
<!-- ACTIONS MENU -->
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
<a href="{{ route('budgets.create') }}" class="btn btn-success pull-right">{{ 'createBudget'|_ }}</a>
</div>
</div>
</div> </div>
</div> </div>
@ -137,21 +152,7 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
<div class="col-lg-3 col-sm-4 col-md-6">
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'createBudget'|_ }}</h3>
<!-- ACTIONS MENU -->
<div class="box-tools pull-right">
<button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
</div>
</div>
<div class="box-body">
<a href="{{ route('budgets.create') }}" class="btn btn-success pull-right">{{ 'createBudget'|_ }}</a>
</div>
</div>
</div>
{% if inactive|length > 0 %} {% if inactive|length > 0 %}
<div class="col-lg-3 col-sm-4 col-md-6"> <div class="col-lg-3 col-sm-4 col-md-6">
<div class="box"> <div class="box">