Clean up budget report partial

This commit is contained in:
James Cole 2017-02-11 10:05:58 +01:00
parent aa2d78f36a
commit 5e47492318
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 78 additions and 440 deletions

View File

@ -1,193 +0,0 @@
<?php
/**
* Budget.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\Helpers\Collection;
use Illuminate\Support\Collection;
/**
*
* Class Budget
*
* @package FireflyIII\Helpers\Collection
*/
class Budget
{
/** @var Collection */
protected $budgetLines;
/** @var string */
protected $budgeted = '0';
/** @var string */
protected $left = '0';
/** @var string */
protected $overspent = '0';
/** @var string */
protected $spent = '0';
/**
*
*/
public function __construct()
{
$this->budgetLines = new Collection;
}
/**
* @param BudgetLine $budgetLine
*
* @return Budget
*/
public function addBudgetLine(BudgetLine $budgetLine): Budget
{
$this->budgetLines->push($budgetLine);
return $this;
}
/**
* @param string $add
*
* @return Budget
*/
public function addBudgeted(string $add): Budget
{
$this->budgeted = bcadd($this->budgeted, $add);
return $this;
}
/**
* @param string $add
*
* @return Budget
*/
public function addLeft(string $add): Budget
{
$this->left = bcadd($this->left, $add);
return $this;
}
/**
* @param string $add
*
* @return Budget
*/
public function addOverspent(string $add): Budget
{
$this->overspent = bcadd($this->overspent, $add);
return $this;
}
/**
* @param string $add
*
* @return Budget
*/
public function addSpent(string $add): Budget
{
$this->spent = bcadd($this->spent, $add);
return $this;
}
/**
* @return \Illuminate\Support\Collection
*/
public function getBudgetLines(): Collection
{
return $this->budgetLines;
}
/**
* @return string
*/
public function getBudgeted(): string
{
return $this->budgeted;
}
/**
* @param string $budgeted
*
* @return Budget
*/
public function setBudgeted(string $budgeted): Budget
{
$this->budgeted = $budgeted;
return $this;
}
/**
* @return string
*/
public function getLeft(): string
{
return $this->left;
}
/**
* @param string $left
*
* @return Budget
*/
public function setLeft(string $left): Budget
{
$this->left = $left;
return $this;
}
/**
* @return string
*/
public function getOverspent(): string
{
return $this->overspent;
}
/**
* @param string $overspent
*
* @return Budget
*/
public function setOverspent(string $overspent): Budget
{
$this->overspent = $overspent;
return $this;
}
/**
* @return string
*/
public function getSpent(): string
{
return $this->spent;
}
/**
* @param string $spent
*
* @return Budget
*/
public function setSpent(string $spent): Budget
{
$this->spent = $spent;
return $this;
}
}

View File

@ -1,161 +0,0 @@
<?php
/**
* BudgetLine.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\Helpers\Collection;
use FireflyIII\Models\Budget as BudgetModel;
use FireflyIII\Models\BudgetLimit;
/**
*
* Class BudgetLine
*
* @package FireflyIII\Helpers\Collection
*/
class BudgetLine
{
/** @var BudgetModel */
protected $budget;
/** @var BudgetLimit */
protected $budgetLimit;
/** @var string */
protected $budgeted = '0';
/** @var string */
protected $left = '0';
/** @var string */
protected $overspent = '0';
/** @var string */
protected $spent = '0';
/**
* @return BudgetModel
*/
public function getBudget(): BudgetModel
{
return $this->budget ?? new BudgetModel;
}
/**
* @param BudgetModel $budget
*
* @return BudgetLine
*/
public function setBudget(BudgetModel $budget): BudgetLine
{
$this->budget = $budget;
return $this;
}
/**
* @return BudgetLimit
*/
public function getBudgetLimit(): BudgetLimit
{
return $this->budgetLimit ?? new BudgetLimit;
}
/**
* @param BudgetLimit $budgetLimit
*
* @return BudgetLimit
*/
public function setBudgetLimit(BudgetLimit $budgetLimit): BudgetLine
{
$this->budgetLimit = $budgetLimit;
return $this;
}
/**
* @return string
*/
public function getBudgeted(): string
{
return $this->budgeted;
}
/**
* @param string $budgeted
*
* @return BudgetLine
*/
public function setBudgeted(string $budgeted): BudgetLine
{
$this->budgeted = $budgeted;
return $this;
}
/**
* @return string
*/
public function getLeft(): string
{
return $this->left;
}
/**
* @param string $left
*
* @return BudgetLine
*/
public function setLeft(string $left): BudgetLine
{
$this->left = $left;
return $this;
}
/**
* @return string
*/
public function getOverspent(): string
{
return $this->overspent;
}
/**
* @param string $overspent
*
* @return BudgetLine
*/
public function setOverspent(string $overspent): BudgetLine
{
$this->overspent = $overspent;
return $this;
}
/**
* @return string
*/
public function getSpent(): string
{
return $this->spent;
}
/**
* @param string $spent
*
* @return BudgetLine
*/
public function setSpent(string $spent): BudgetLine
{
$this->spent = $spent;
return $this;
}
}

View File

@ -15,8 +15,6 @@ namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use FireflyIII\Helpers\Collection\BudgetLine;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
@ -48,44 +46,63 @@ class BudgetReportHelper implements BudgetReportHelperInterface
* @param Carbon $end
* @param Collection $accounts
*
* @return BudgetCollection
* @return array
*/
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): array
{
$object = new BudgetCollection;
$set = $this->repository->getBudgets();
$array = [];
/** @var Budget $budget */
foreach ($set as $budget) {
$budgetLimits = $this->repository->getBudgetLimits($budget, $start, $end);
if ($budgetLimits->count() == 0) { // no budget limit(s) for this budget
$spent = $this->repository->spentInPeriod(new Collection([$budget]), $accounts, $start, $end);// spent for budget in time range
if ($spent > 0) {
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget)->setOverspent($spent);
$object->addOverspent($spent)->addBudgetLine($budgetLine);
if (bccomp($spent, '0') === -1) {
$line = [
'type' => 'budget',
'id' => $budget->id,
'name' => $budget->name,
'budgeted' => '0',
'spent' => $spent,
'left' => '0',
'overspent' => '0',
];
$array[] = $line;
}
continue;
}
/** @var BudgetLimit $budgetLimit */
foreach ($budgetLimits as $budgetLimit) { // one or more repetitions for budget
$data = $this->calculateExpenses($budget, $budgetLimit, $accounts);
$budgetLine = new BudgetLine;
$budgetLine->setBudget($budget)->setBudgetLimit($budgetLimit)
->setLeft($data['left'])->setSpent($data['expenses'])->setOverspent($data['overspent'])
->setBudgeted(strval($budgetLimit->amount));
$object->addBudgeted(strval($budgetLimit->amount))->addSpent($data['spent'])
->addLeft($data['left'])->addOverspent($data['overspent'])->addBudgetLine($budgetLine);
$line = [
'type' => 'budget-line',
'start' => $budgetLimit->start_date,
'end' => $budgetLimit->end_date,
'limit' => $budgetLimit->id,
'id' => $budget->id,
'name' => $budget->name,
'budgeted' => strval($budgetLimit->amount),
'spent' => $data['expenses'],
'left' => $data['left'],
'overspent' => $data['overspent'],
];
$array[] = $line;
}
}
$noBudget = $this->repository->spentInPeriodWoBudget($accounts, $start, $end); // stuff outside of budgets
$budgetLine = new BudgetLine;
$budgetLine->setOverspent($noBudget)->setSpent($noBudget);
$object->addOverspent($noBudget)->addBudgetLine($budgetLine);
$line = [
'type' => 'no-budget',
'budgeted' => '0',
'spent' => $noBudget,
'left' => '0',
'overspent' => '0',
];
$array[] = $line;
return $object;
return $array;
}
/**

View File

@ -15,7 +15,6 @@ namespace FireflyIII\Helpers\Report;
use Carbon\Carbon;
use FireflyIII\Helpers\Collection\Budget as BudgetCollection;
use Illuminate\Support\Collection;
/**
@ -31,9 +30,9 @@ interface BudgetReportHelperInterface
* @param Carbon $end
* @param Collection $accounts
*
* @return BudgetCollection
* @return array
*/
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): BudgetCollection;
public function getBudgetReport(Carbon $start, Carbon $end, Collection $accounts): array;
/**
* @param Carbon $start

View File

@ -11,26 +11,33 @@
</tr>
</thead>
<tbody>
{% for budgetLine in budgets.getBudgetLines %}
<tr>
{% set sum_budgeted = 0 %}
{% set sum_spent = 0 %}
{% set sum_left = 0 %}
{% set sum_overspent = 0 %}
{% for line in budgets %}
{% if budgetLine.getBudget.id %}
<td data-value="{{ budgetLine.getBudget.name }}">
<a href="{{ route('budgets.show',budgetLine.getBudget.id) }}">{{ budgetLine.getBudget.name }}</a>
</td>
{% else %}
{% set sum_budgeted = sum_budgeted + line.budgeted %}
{% set sum_spent = sum_spent + line.spent %}
{% set sum_left = sum_left + line.left %}
{% set sum_overspent = sum_overspent + line.overspent %}
<tr>
{% if line.type == 'no-budget' %}
<td data-value="zzzzzzz">
<em>{{ 'no_budget'|_ }}</em>
</td>
{% else %}
<td data-value="{{ line.name }}">
<a href="{{ route('budgets.show',line.id) }}">{{ line.name }}</a>
</td>
{% endif %}
{% if budgetLine.getBudgetLimit.id %}
<td class="hidden-xs" data-value="{{ budgetLine.getBudgetLimit.start_date.format('Y-m-d') }}">
<a href="{{ route('budgets.show.limit', [budgetLine.getBudget.id, budgetLine.getBudgetLimit.id]) }}">
{{ budgetLine.getBudgetLimit.start_date.formatLocalized(monthAndDayFormat) }}
{% if line.type == 'budget-line' %}
<td class="hidden-xs" data-value="{{ line.start.format('Y-m-d') }}">
<a href="{{ route('budgets.show.limit', [line.id, line.limit]) }}">
{{ line.start.formatLocalized(monthAndDayFormat) }}
&mdash;
{{ budgetLine.getBudgetLimit.end_date.formatLocalized(monthAndDayFormat) }}
{{ line.end.formatLocalized(monthAndDayFormat) }}
</a>
</td>
{% else %}
@ -38,47 +45,23 @@
</td>
{% endif %}
{% if budgetLine.getBudgetLimit.id %}
<td data-value="{{ budgetLine.getBudgetLimit.amount }}" style="text-align: right;">
{{ budgetLine.getBudgetLimit.amount|formatAmount }}
<td data-value="{{ line.budgeted }}" style="text-align: right;">
{{ line.budgeted|formatAmount }}
</td>
{% else %}
<td data-value="0" style="text-align: right;">
{{ 0|formatAmount }}
</td>
{% endif %}
<td data-value="{{ budgetLine.getSpent }}" style="text-align: right;">
{% if budgetLine.getSpent != 0 %}
{{ budgetLine.getSpent|formatAmount }}
<!-- <i class="fa fa-fw text-muted fa-info-circle firefly-info-button"
data-location="budget-spent-amount" data-budget-id="{{ budgetLine.getBudget.id }}"></i>
-->
{% endif %}
{% if budgetLine.getSpent == 0 %}
{{ budgetLine.getSpent|formatAmount }}
{% endif %}
<td data-value="{{ line.spent }}" style="text-align: right;">
{{ line.spent|formatAmount }}
</td>
<td>
{% if budgetLine.getSpent != 0 %}
{% if line.spent != 0 %}
<i class="fa fa-fw text-muted fa-info-circle firefly-info-button"
data-location="budget-spent-amount" data-budget-id="{{ budgetLine.getBudget.id }}"></i>
data-location="budget-spent-amount" data-budget-id="{{ line.id }}"></i>
{% endif %}
</td>
<td data-value="{{ budgetLine.getLeft }}" style="text-align: right;">
{% if(budgetLine.getOverspent == 0) %}
{{ budgetLine.getLeft|formatAmount }}
{% endif %}
<td data-value="{{ line.left }}" style="text-align: right;">
{{ line.left|formatAmount }}
</td>
<td data-value="{{ budgetLine.getOverspent }}" style="text-align: right;">
{% if budgetLine.getOverspent != 0 %}
{{ budgetLine.getOverspent|formatAmount }}
{% endif %}
<td data-value="{{ line.overspent }}" style="text-align: right;">
{{ line.overspent|formatAmount }}
</td>
</tr>
{% endfor %}
@ -87,18 +70,11 @@
<tr>
<td><em>{{ 'sum'|_ }}</em></td>
<td class="hidden-xs">&nbsp;</td>
<td style="text-align: right;">{{ budgets.getBudgeted|formatAmount }}</td>
<td style="text-align: right;">
{% if budgets.getSpent != 0 %}
<span class="text-danger">{{ budgets.getSpent|formatAmountPlain }}</span>
{% endif %}
{% if budgets.getSpent == 0 %}
{{ budgets.getSpent|formatAmount }}
{% endif %}
</td>
<td style="text-align: right;">{{ sum_budgeted|formatAmount }}</td>
<td style="text-align: right;">{{ sum_spent|formatAmount }}</td>
<td>&nbsp;</td>
<td style="text-align: right;">{{ budgets.getLeft|formatAmount }}</td>
<td style="text-align: right;"><span class="text-danger">{{ budgets.getOverspent|formatAmountPlain }}</span></td>
<td style="text-align: right;">{{ sum_left|formatAmount }}</td>
<td style="text-align: right;">{{ sum_overspent|formatAmount }}</td>
</tr>
</tfoot>
</table>