Code for issue #489

This commit is contained in:
James Cole
2016-12-22 16:36:56 +01:00
parent 0feeac9160
commit 9859052c4d
11 changed files with 321 additions and 132 deletions

View File

@@ -15,10 +15,10 @@ namespace FireflyIII\Http\Controllers;
use Amount;
use Carbon\Carbon;
use Config;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Http\Requests\BudgetFormRequest;
use FireflyIII\Http\Requests\BudgetIncomeRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\Budget;
use FireflyIII\Models\LimitRepetition;
@@ -26,8 +26,6 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
use Input;
use Log;
use Navigation;
use Preferences;
use Response;
use Session;
@@ -42,6 +40,9 @@ use View;
class BudgetController extends Controller
{
/** @var BudgetRepositoryInterface */
private $repository;
/**
*
*/
@@ -55,6 +56,7 @@ class BudgetController extends Controller
function ($request, $next) {
View::share('title', trans('firefly.budgets'));
View::share('mainTitleIcon', 'fa-tasks');
$this->repository = app(BudgetRepositoryInterface::class);
return $next($request);
}
@@ -73,14 +75,8 @@ class BudgetController extends Controller
/** @var Carbon $start */
$start = session('start', Carbon::now()->startOfMonth());
/** @var Carbon $end */
$end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data;
// is custom view range?
if (session('is_custom_range') === true) {
$viewRange = 'custom';
}
$end = session('end', Carbon::now()->endOfMonth());
$viewRange = Preferences::get('viewRange', '1M')->data;
$limitRepetition = $repository->updateLimitAmount($budget, $start, $end, $viewRange, $amount);
if ($amount == 0) {
$limitRepetition = null;
@@ -173,82 +169,27 @@ class BudgetController extends Controller
}
/**
* @param BudgetRepositoryInterface $repository
* @param AccountRepositoryInterface $accountRepository
*
* @return View
*
*/
public function index(BudgetRepositoryInterface $repository, AccountRepositoryInterface $accountRepository)
public function index()
{
$repository->cleanupBudgets();
$this->repository->cleanupBudgets();
$budgets = $repository->getActiveBudgets();
$inactive = $repository->getInactiveBudgets();
$spent = '0';
$budgeted = '0';
$range = Preferences::get('viewRange', '1M')->data;
$repeatFreq = Config::get('firefly.range_to_repeat_freq.' . $range);
if (session('is_custom_range') === true) {
$repeatFreq = 'custom';
}
/** @var Carbon $start */
$start = session('start', new Carbon);
/** @var Carbon $end */
$budgets = $this->repository->getActiveBudgets();
$inactive = $this->repository->getInactiveBudgets();
$start = session('start', new Carbon);
$end = session('end', new Carbon);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$budgetIncomeTotal = Preferences::get($key, 1000)->data;
$period = Navigation::periodShow($start, $range);
$periodStart = $start->formatLocalized($this->monthAndDayFormat);
$periodEnd = $end->formatLocalized($this->monthAndDayFormat);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
$startAsString = $start->format('Y-m-d');
$endAsString = $end->format('Y-m-d');
Log::debug('Now at /budgets');
// loop the budgets:
/** @var Budget $budget */
foreach ($budgets as $budget) {
Log::debug(sprintf('Now at budget #%d ("%s")', $budget->id, $budget->name));
$budget->spent = $repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);
$allRepetitions = $repository->getAllBudgetLimitRepetitions($start, $end);
$otherRepetitions = new Collection;
/** @var LimitRepetition $repetition */
foreach ($allRepetitions as $repetition) {
if ($repetition->budget_id == $budget->id) {
if ($repetition->budgetLimit->repeat_freq == $repeatFreq
&& $repetition->startdate->format('Y-m-d') == $startAsString
&& $repetition->enddate->format('Y-m-d') == $endAsString
) {
// do something
$budget->currentRep = $repetition;
continue;
}
$otherRepetitions->push($repetition);
}
}
$budget->otherRepetitions = $otherRepetitions;
if (!is_null($budget->currentRep) && !is_null($budget->currentRep->id)) {
$budgeted = bcadd($budgeted, $budget->currentRep->amount);
}
$spent = bcadd($spent, $budget->spent);
}
$defaultCurrency = Amount::getDefaultCurrency();
$budgetInformation = $this->collectBudgetInformation($budgets, $start, $end);
$defaultCurrency = Amount::getDefaultCurrency();
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
$spent = array_sum(array_column($budgetInformation, 'spent'));
$budgeted = array_sum(array_column($budgetInformation, 'budgeted'));
return view(
'budgets.index', compact(
'periodStart', 'periodEnd',
'period', 'range', 'budgetIncomeTotal',
'defaultCurrency', 'inactive', 'budgets',
'spent', 'budgeted'
)
'budgets.index',
compact('available', 'periodStart', 'periodEnd', 'budgetInformation', 'defaultCurrency', 'inactive', 'budgets', 'spent', 'budgeted')
);
}
@@ -280,16 +221,14 @@ class BudgetController extends Controller
/**
* @return \Illuminate\Http\RedirectResponse
*/
public function postUpdateIncome()
public function postUpdateIncome(BudgetIncomeRequest $request)
{
$range = Preferences::get('viewRange', '1M')->data;
/** @var Carbon $date */
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$start = session('start', new Carbon);
$end = session('end', new Carbon);
$defaultCurrency = Amount::getDefaultCurrency();
$amount = $request->get('amount');
Preferences::set($key, intval(Input::get('amount')));
$this->repository->setAvailableBudget($defaultCurrency, $start, $end, $amount);
Preferences::mark();
return redirect(route('budgets.index'));
@@ -430,19 +369,57 @@ class BudgetController extends Controller
*/
public function updateIncome()
{
$range = Preferences::get('viewRange', '1M')->data;
$format = strval(trans('config.month_and_day'));
$start = session('start', new Carbon);
$end = session('end', new Carbon);
$defaultCurrency = Amount::getDefaultCurrency();
$available = $this->repository->getAvailableBudget($defaultCurrency, $start, $end);
/** @var Carbon $date */
$date = session('start', new Carbon);
$start = Navigation::startOfPeriod($date, $range);
$end = Navigation::endOfPeriod($start, $range);
$key = 'budgetIncomeTotal' . $start->format('Ymd') . $end->format('Ymd');
$amount = Preferences::get($key, 1000);
$displayStart = $start->formatLocalized($format);
$displayEnd = $end->formatLocalized($format);
return view('budgets.income', compact('amount', 'displayStart', 'displayEnd'));
return view('budgets.income', compact('available', 'start', 'end'));
}
/**
* @param Collection $budgets
* @param Carbon $start
* @param Carbon $end
*
* @return array
*/
private function collectBudgetInformation(Collection $budgets, Carbon $start, Carbon $end): array
{
// get account information
$accountRepository = app(AccountRepositoryInterface::class);
$accounts = $accountRepository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET, AccountType::CASH]);
$return = [];
/** @var Budget $budget */
foreach ($budgets as $budget) {
$budgetId = $budget->id;
$return[$budgetId] = [
'spent' => $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end),
'budgeted' => '0',
'currentRep' => false,
];
$allRepetitions = $this->repository->getAllBudgetLimitRepetitions($start, $end);
$otherRepetitions = new Collection;
// get all the limit repetitions relevant between start and end and examine them:
/** @var LimitRepetition $repetition */
foreach ($allRepetitions as $repetition) {
if ($repetition->budget_id == $budget->id) {
if ($repetition->startdate->isSameDay($start) && $repetition->enddate->isSameDay($end)
) {
$return[$budgetId]['currentRep'] = $repetition;
$return[$budgetId]['budgeted'] = $repetition->amount;
continue;
}
// otherwise it's just one of the many relevant repetitions:
$otherRepetitions->push($repetition);
}
}
$return[$budgetId]['otherRepetitions'] = $otherRepetitions;
}
return $return;
}
}

View File

@@ -63,6 +63,7 @@ class HomeController extends Controller
// a possible problem with the budgets.
if ($label === strval(trans('firefly.everything')) || $label === strval(trans('firefly.customRange'))) {
$isCustomRange = true;
Preferences::set('viewRange', 'custom');
Log::debug('Range is now marked as "custom".');
}

View File

@@ -0,0 +1,42 @@
<?php
/**
* BudgetIncomeRequest.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Http\Requests;
/**
* Class BudgetIncomeRequest
*
*
* @package FireflyIII\Http\Requests
*/
class BudgetIncomeRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
return auth()->check();
}
/**
* @return array
*/
public function rules()
{
return [
'amount' => 'numeric|required|min:0',
];
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* AvailableBudget.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International License.
*
* See the LICENSE file for details.
*/
declare(strict_types = 1);
namespace FireflyIII\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class AvailableBudget
*
* @package FireflyIII\Models
*/
class AvailableBudget extends Model
{
use SoftDeletes;
/** @var array */
protected $dates = ['created_at', 'updated_at', 'deleted_at', 'start_date', 'end_date'];
/** @var array */
protected $fillable = ['user_id', 'transaction_currency_id', 'amount', 'start_date', 'end_date'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function transactionCurrency()
{
return $this->belongsTo('FireflyIII\Models\TransactionCurrency');
}
/**
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo('FireflyIII\User');
}
}

View File

@@ -17,10 +17,12 @@ use Carbon\Carbon;
use FireflyIII\Events\StoredBudgetLimit;
use FireflyIII\Events\UpdatedBudgetLimit;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\LimitRepetition;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Models\TransactionType;
use FireflyIII\User;
@@ -210,6 +212,27 @@ class BudgetRepository implements BudgetRepositoryInterface
return $set;
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string
{
$amount = '0';
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
if (!is_null($availableBudget)) {
$amount = strval($availableBudget->amount);
}
return $amount;
}
/**
* This method is being used to generate the budget overview in the year/multi-year report. Its used
* in both the year/multi-year budget overview AND in the accompanying chart.
@@ -322,6 +345,33 @@ class BudgetRepository implements BudgetRepositoryInterface
return $result;
}
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return bool
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): bool
{
$availableBudget = $this->user->availableBudgets()
->where('transaction_currency_id', $currency->id)
->where('start_date', $start->format('Y-m-d'))
->where('end_date', $end->format('Y-m-d'))->first();
if (is_null($availableBudget)) {
$availableBudget = new AvailableBudget;
$availableBudget->user()->associate($this->user);
$availableBudget->transactionCurrency()->associate($currency);
$availableBudget->start_date = $start;
$availableBudget->end_date = $end;
}
$availableBudget->amount = $amount;
$availableBudget->save();
return true;
}
/**
* @param Collection $budgets
* @param Collection $accounts

View File

@@ -16,6 +16,7 @@ namespace FireflyIII\Repositories\Budget;
use Carbon\Carbon;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionCurrency;
use Illuminate\Support\Collection;
/**
@@ -90,6 +91,15 @@ interface BudgetRepositoryInterface
*/
public function getAllBudgetLimitRepetitions(Carbon $start, Carbon $end): Collection;
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
*
* @return string
*/
public function getAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end): string;
/**
*
* @param Collection $budgets
@@ -120,6 +130,16 @@ interface BudgetRepositoryInterface
*/
public function getNoBudgetPeriodReport(Collection $accounts, Carbon $start, Carbon $end): array;
/**
* @param TransactionCurrency $currency
* @param Carbon $start
* @param Carbon $end
* @param string $amount
*
* @return bool
*/
public function setAvailableBudget(TransactionCurrency $currency, Carbon $start, Carbon $end, string $amount): bool;
/**
* @param Collection $budgets
* @param Collection $accounts

View File

@@ -51,6 +51,15 @@ class User extends Authenticatable
*/
protected $table = 'users';
/**
* @return HasMany
*/
public function availableBudgets(): HasMany
{
return $this->hasMany('FireflyIII\Models\AvailableBudget');
}
/**
* @return HasMany
*/

View File

@@ -0,0 +1,44 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
/**
* Class ChangesForV430
*/
class ChangesForV430 extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('available_budgets', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->softDeletes();
$table->integer('user_id', false, true);
$table->integer('transaction_currency_id', false, true);
$table->decimal('amount', 14, 4);
$table->date('start_date');
$table->date('end_date');
$table->foreign('transaction_currency_id')->references('id')->on('transaction_currencies')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('available_budgets');
}
}

View File

@@ -1,5 +1,3 @@
/* globals $, budgeted:true, currencySymbol, budgetIncomeTotal, columnChart, budgetedMuch, budgetedPercentage, token, budgetID, repetitionID, spent, lineChart */
function drawSpentBar() {
"use strict";
if ($('.spentBar').length > 0) {
@@ -23,19 +21,19 @@ function drawBudgetedBar() {
"use strict";
if ($('.budgetedBar').length > 0) {
var budgetedMuch = budgeted > budgetIncomeTotal;
var budgetedMuch = budgeted > available;
// recalculate percentage:
var pct;
if (budgetedMuch) {
// budgeted too much.
pct = (budgetIncomeTotal / budgeted) * 100;
pct = (available / budgeted) * 100;
$('.budgetedBar .progress-bar-warning').css('width', pct + '%');
$('.budgetedBar .progress-bar-danger').css('width', (100 - pct) + '%');
$('.budgetedBar .progress-bar-info').css('width', 0);
} else {
pct = (budgeted / budgetIncomeTotal) * 100;
pct = (budgeted / available) * 100;
$('.budgetedBar .progress-bar-warning').css('width', 0);
$('.budgetedBar .progress-bar-danger').css('width', 0);
$('.budgetedBar .progress-bar-info').css('width', pct + '%');

View File

@@ -4,7 +4,8 @@
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">{{ 'close'|_ }}</span>
</button>
<h4 class="modal-title" id="myModalLabel">
{{ trans('firefly.update_budget_amount_range', {start: displayStart, end: displayEnd}) }}
{{ trans('firefly.update_budget_amount_range',
{start: start.formatLocalized(monthAndDayFormat), end: end.formatLocalized(monthAndDayFormat)}) }}
</h4>
</div>
@@ -14,7 +15,7 @@
<div class="input-group">
<div class="input-group-addon">{{ getCurrencySymbol()|raw }}</div>
<input step="any" class="form-control" id="amount" value="{{ amount.data }}" autocomplete="off" name="amount" type="number"/>
<input step="any" class="form-control" id="amount" value="{{ available }}" autocomplete="off" name="amount" type="number"/>
</div>
</div>
<div class="modal-footer">

View File

@@ -18,8 +18,8 @@
</div>
<div class="col-lg-6 col-md-4 col-sm-3" style="text-align:right;">
<small>{{ trans('firefly.available_between',{start : periodStart, end: periodEnd }) }}:
<a href="#" class="updateIncome"><span id="budgetIncomeTotal"
data-value="{{ budgetIncomeTotal }}">{{ budgetIncomeTotal|formatAmount }}</span></a>
<a href="#" class="updateIncome"><span id="available"
data-value="{{ available }}">{{ available|formatAmountPlain }}</span></a>
</small>
</div>
</div>
@@ -90,8 +90,8 @@
<div class="box-header with-border">
<h3 class="box-title">
<!-- link in header -->
{% if budget.currentRep.id %}
<a href="{{ route('budgets.show.repetition', [budget.id, budget.currentRep.id]) }}" class="budget-link"
{% if budgetInformation[budget.id]['currentRep'] %}
<a href="{{ route('budgets.show.repetition', [budget.id, budgetInformation[budget.id]['currentRep'].id]) }}" class="budget-link"
data-id="{{ budget.id }}">{{ budget.name }}</a>
{% else %}
<a href="{{ route('budgets.show',budget.id) }}" class="budget-link" data-id="{{ budget.id }}">{{ budget.name }}</a>
@@ -122,20 +122,16 @@
<div class="form-group" style="margin-bottom:0;">
<div class="input-group">
<div class="input-group-addon">{{ defaultCurrency.symbol|raw }}</div>
<input type="hidden" name="balance_currency_id" value="1"/>
<input class="form-control budgetAmount" data-original="{{ budget.currentRep.amount|number_format(0,'','') }}"
data-id="{{ budget.id }}" value="{{ budget.currentRep.amount|number_format(0,'','') }}" autocomplete="off"
<input type="hidden" name="balance_currency_id" value="{{ defaultCurrency.id }}"/>
{% if budgetInformation[budget.id]['currentRep'] %}
{% set repAmount = budgetInformation[budget.id]['currentRep'].amount %}
{% else %}
{% set repAmount = '0' %}
{% endif %}
<input class="form-control budgetAmount" data-original="{{ repAmount }}"
data-id="{{ budget.id }}" value="{{ repAmount }}" autocomplete="off"
step="1" min="0" name="amount" type="number">
</div>
<!--
<div class="small">
<ul class="list-inline">
<li>Previously budgeted:</li>
<li><a href="#">{{ 123|formatAmountPlain }}</a></li>
<li><a href="#">{{ 456|formatAmountPlain }}</a></li>
</ul>
</div>
-->
</div>
</td>
</tr>
@@ -147,21 +143,23 @@
{{ session('end').formatLocalized(monthAndDayFormat) }}
</span>
</td>
<td>{{ budget.spent|formatAmount }}</a></td>
<td>
{{ budgetInformation[budget.id]['spent']|formatAmount }}
</td>
</tr>
{% if budget.otherRepetitions.count > 0 %}
{% if budgetInformation[budget.id]['otherRepetitions'].count > 0 %}
<tr>
<td colspan="2">
<ul class="list-unstyled">
{% for other in budget.otherRepetitions %}
{% if other.id != budget.currentRep.id %}
<li>Budgeted
<a href="{{ route('budgets.show.repetition', [budget.id, other.id]) }}">{{ other.amount|formatAmountPlain }}</a>
between
{{ other.startdate.formatLocalized(monthAndDayFormat) }}
and {{ other.enddate.formatLocalized(monthAndDayFormat) }}.
</li>
{% endif %}
{% for other in budgetInformation[budget.id]['otherRepetitions'] %}
<li>
<!-- translate -->
Budgeted
<a href="{{ route('budgets.show.repetition', [budget.id, other.id]) }}">{{ other.amount|formatAmountPlain }}</a>
between
{{ other.startdate.formatLocalized(monthAndDayFormat) }}
and {{ other.enddate.formatLocalized(monthAndDayFormat) }}.
</li>
{% endfor %}
</ul>
</td>
@@ -206,7 +204,7 @@
// budgeted data:
var budgeted = {{ budgeted }};
var budgetIncomeTotal = {{ budgetIncomeTotal }};
var available = {{ available }};
</script>
<script type="text/javascript" src="js/ff/budgets/index.js"></script>