Update for balance box in report.

This commit is contained in:
James Cole 2019-08-16 17:54:38 +02:00
parent 070f46c755
commit 02db333d46
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 190 additions and 83 deletions

View File

@ -23,9 +23,7 @@ declare(strict_types=1);
namespace FireflyIII\Factory;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionGroup;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
/**
@ -60,10 +58,8 @@ class TransactionGroupFactory
$collection = $this->journalFactory->create($data);
$title = $data['group_title'] ?? null;
$title = '' === $title ? null : $title;
/** @var TransactionJournal $first */
$first = $collection->first();
$group = new TransactionGroup;
$group->user()->associate($first->user);
$group = new TransactionGroup;
$group->user()->associate($this->user);
$group->title = $title;
$group->save();

View File

@ -143,6 +143,9 @@ class TransactionJournalFactory
if (null !== $journal) {
$collection->push($journal);
}
if(null === $journal) {
Log::error('The createJournal() method returned NULL. This may indicate an error.');
}
}
return $collection;
@ -247,6 +250,7 @@ class TransactionJournalFactory
$destinationAccount = $this->getAccount($type->type, 'destination', (int)$row['destination_id'], $row['destination_name']);
// @codeCoverageIgnoreStart
} catch (FireflyException $e) {
Log::error('Could not validate source or destination.');
Log::error($e->getMessage());
return null;

View File

@ -27,7 +27,11 @@ use FireflyIII\Helpers\Collection\Balance;
use FireflyIII\Helpers\Collection\BalanceEntry;
use FireflyIII\Helpers\Collection\BalanceHeader;
use FireflyIII\Helpers\Collection\BalanceLine;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\Budget;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use Illuminate\Support\Collection;
use Log;
@ -65,11 +69,91 @@ class BalanceReportHelper implements BalanceReportHelperInterface
* @param Carbon $start
* @param Carbon $end
*
* @return Balance
* @return array
*/
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array
{
Log::debug('Start of balance report');
$report = [
'budgets' => [],
'accounts' => [],
];
/** @var Account $account */
foreach ($accounts as $account) {
$report['accounts'][$account->id] = [
'id' => $account->id,
'name' => $account->name,
'iban' => $account->iban,
'sum' => '0',
];
}
$budgets = $this->budgetRepository->getBudgets();
// per budget, dan per balance line
// of als het in een balance line valt dan daaronder en anders niet
// kruistabel vullen?
/** @var Budget $budget */
foreach ($budgets as $budget) {
$budgetId = $budget->id;
$report['budgets'][$budgetId] = [
'budget_id' => $budgetId,
'budget_name' => $budget->name,
'spent' => [], // per account
'sums' => [], // per currency
];
$spent = [];
/** @var GroupCollectorInterface $collector */
$collector = app(GroupCollectorInterface::class);
$journals = $collector->setRange($start, $end)->setSourceAccounts($accounts)->setTypes([TransactionType::WITHDRAWAL])->setBudget($budget)
->getExtractedJournals();
/** @var array $journal */
foreach ($journals as $journal) {
$sourceAccount = $journal['source_account_id'];
$currencyId = $journal['currency_id'];
$spent[$sourceAccount] = $spent[$sourceAccount] ?? [
'source_account_id' => $sourceAccount,
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
'spent' => '0',
];
$spent[$sourceAccount]['spent'] = bcadd($spent[$sourceAccount]['spent'], $journal['amount']);
// also fix sum:
$report['sums'][$budgetId][$currencyId] = $report['sums'][$budgetId][$currencyId] ?? [
'sum' => '0',
'currency_id' => $journal['currency_id'],
'currency_code' => $journal['currency_code'],
'currency_name' => $journal['currency_name'],
'currency_symbol' => $journal['currency_symbol'],
'currency_decimal_places' => $journal['currency_decimal_places'],
];
$report['sums'][$budgetId][$currencyId]['sum'] = bcadd($report['sums'][$budgetId][$currencyId]['sum'], $journal['amount']);
$report['accounts'][$sourceAccount]['sum'] = bcadd($report['accounts'][$sourceAccount]['sum'], $journal['amount']);
// add currency info for account sum
$report['accounts'][$sourceAccount]['currency_id'] = $journal['currency_id'];
$report['accounts'][$sourceAccount]['currency_code'] = $journal['currency_code'];
$report['accounts'][$sourceAccount]['currency_name'] = $journal['currency_name'];
$report['accounts'][$sourceAccount]['currency_symbol'] = $journal['currency_symbol'];
$report['accounts'][$sourceAccount]['currency_decimal_places'] = $journal['currency_decimal_places'];
}
$report['budgets'][$budgetId]['spent'] = $spent;
// get transactions in budget
}
return $report;
// do sums:
echo '<pre>';
print_r($report);
exit;
$balance = new Balance;
$header = new BalanceHeader;
$budgetLimits = $this->budgetRepository->getAllBudgetLimits($start, $end);

View File

@ -38,7 +38,7 @@ interface BalanceReportHelperInterface
* @param Carbon $start
* @param Carbon $end
*
* @return Balance
* @return array
*/
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): Balance;
public function getBalanceReport(Collection $accounts, Carbon $start, Carbon $end): array;
}

View File

@ -47,7 +47,6 @@ class BalanceController extends Controller
*/
public function general(Collection $accounts, Carbon $start, Carbon $end)
{
// chart properties for cache:
$cache = new CacheProperties;
$cache->addProperty($start);
@ -55,17 +54,19 @@ class BalanceController extends Controller
$cache->addProperty('balance-report');
$cache->addProperty($accounts->pluck('id')->toArray());
if ($cache->has()) {
return $cache->get(); // @codeCoverageIgnore
//return $cache->get(); // @codeCoverageIgnore
}
$helper = app(BalanceReportHelperInterface::class);
$balance = $helper->getBalanceReport($accounts, $start, $end);
try {
$result = view('reports.partials.balance', compact('balance'))->render();
$report = $helper->getBalanceReport($accounts, $start, $end);
// TODO no budget.
// TODO sum over account.
// try {
$result = view('reports.partials.balance', compact('report'))->render();
// @codeCoverageIgnoreStart
} catch (Throwable $e) {
Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
$result = 'Could not render view.';
}
// } catch (Throwable $e) {
// Log::debug(sprintf('Could not render reports.partials.balance: %s', $e->getMessage()));
// $result = 'Could not render view.';
// }
// @codeCoverageIgnoreEnd
$cache->store($result);

View File

@ -171,31 +171,60 @@ class CategoryController extends Controller
/** @var CategoryRepositoryInterface $repository */
$repository = app(CategoryRepositoryInterface::class);
$categories = $repository->getCategories();
$report = [];
$report = [
'categories' => [],
'sums' => [],
];
/** @var Category $category */
foreach ($categories as $category) {
$spent = $repository->spentInPeriod($category, $accounts, $start, $end);
$earned = $repository->earnedInPeriod($category, $accounts, $start, $end);
$currencies = array_keys($spent) + array_keys($earned);
if (0 === count($spent) && 0 === count($earned)) {
continue;
}
$currencies = array_unique(array_merge(array_keys($spent), array_keys($earned)));
foreach ($currencies as $code) {
$currencyInfo = $spent[$code] ?? $earned[$code];
$report[$category->id] = [
'name' => sprintf('%s (%s)', $category->name, $code),
'spent' => round($spent[$code]['spent'] ?? '0', $currencyInfo['currency_decimal_places']),
'earned' => round($earned[$code]['earned'] ?? '0', $currencyInfo['currency_decimal_places']),
$currencyInfo = $spent[$code] ?? $earned[$code];
$key = sprintf('%s-%s', $category->id, $code);
$report['categories'][$key] = [
'name' => $category->name,
'spent' => $spent[$code]['spent'] ?? '0',
'earned' => $earned[$code]['earned'] ?? '0',
'id' => $category->id,
'currency_id' => $currencyInfo['currency_id'],
'currency_code' => $currencyInfo['currency_code'],
'currency_symbol' => $currencyInfo['currency_symbol'],
'cyrrency_decimal_places' => $currencyInfo['currency_decimal_places'],
'currency_name' => $currencyInfo['currency_name'],
'currency_decimal_places' => $currencyInfo['currency_decimal_places'],
];
}
}
$sum = [];
foreach ($report as $categoryId => $row) {
$sum[$categoryId] = (float)$row['spent'];
/**
* @var string $categoryId
* @var array $row
*/
foreach ($report['categories'] as $categoryId => $row) {
$sum[$categoryId] = (float)$row['spent'];
}
array_multisort($sum, SORT_ASC, $report['categories']);
// get sums:
foreach ($report['categories'] as $entry) {
$currencyId = $entry['currency_id'];
$report['sums'][$currencyId] = $report['sums'][$currencyId] ?? [
'spent' => '0',
'earned' => '0',
'currency_id' => $entry['currency_id'],
'currency_code' => $entry['currency_code'],
'currency_symbol' => $entry['currency_symbol'],
'currency_name' => $entry['currency_name'],
'cyrrency_decimal_places' => $entry['currency_decimal_places'],
];
$report['sums'][$currencyId]['spent'] = bcadd($report['sums'][$currencyId]['spent'], $entry['spent']);
$report['sums'][$currencyId]['earned'] = bcadd($report['sums'][$currencyId]['earned'], $entry['earned']);
}
array_multisort($sum, SORT_ASC, $report);
// @codeCoverageIgnoreStart
try {

View File

@ -74,7 +74,6 @@ class ExpenseController extends Controller
* @param Carbon $end
*
* @return string
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function budget(Collection $accounts, Collection $expense, Carbon $start, Carbon $end): string
{

View File

@ -1,59 +1,45 @@
<table class="table table-hover">
<thead>
<tr>
<th colspan="2">{{ 'budgets'|_ }}</th>
{% for account in balance.getBalanceHeader.getAccounts %}
<th class="hidden-xs" style="text-align: right;"><a href="{{ route('accounts.show',account.id) }}">{{ account.name }}</a></th>
<th>{{ 'budgets'|_ }}</th>
{% for account in report.accounts %}
<th class="hidden-xs" style="text-align: right;"><a href="{{ route('accounts.show',account.id) }}" title="{{ account.iban|default(account.name) }}">{{ account.name }}</a></th>
{% endfor %}
<th style="text-align: right;">
{{ 'leftInBudget'|_ }}
</th>
<th style="text-align: right;">{{ 'sum'|_ }}</th>
</tr>
</thead>
<tbody>
{% for balanceLine in balance.getBalanceLines %}
<tr>
{% if balanceLine.getBudget.id %}
<td>
<a href="{{ route('budgets.show',balanceLine.getBudget.id) }}">{{ balanceLine.getTitle }}</a>
{% if balanceLine.getStartdate and balanceLine.getEnddate %}
<span class="small" class="hidden-xs"><br>
{{ balanceLine.getStartdate.formatLocalized(monthAndDayFormat) }}
&mdash;
{{ balanceLine.getEnddate.formatLocalized(monthAndDayFormat) }}
</span>
{% endif %}
</td>
<td style="text-align: right;">
{% if(balanceLine.getBudgetLimit.amount) %}
{{ balanceLine.getBudgetLimit.amount|formatAmount }}
{% else %}
{{ '0'|formatAmount }}
{% endif %}
</td>
{% else %}
<td colspan="2">{{ balanceLine.getTitle }}</td>
{% endif %}
{% for balanceEntry in balanceLine.getBalanceEntries %}
<td class="hidden-xs" style="text-align: right;">
{% if balanceEntry.getSpent != 0 %}
<span class="text-danger">{{ (balanceEntry.getSpent)|formatAmountPlain }}</span>
<i class="fa fa-fw text-muted fa-info-circle firefly-info-button" data-location="balance-amount"
data-account-id="{{ balanceEntry.getAccount.id }}"
data-budget-id="{{ balanceLine.getBudget.id }}" data-role="{{ balanceLine.getRole }}"></i>
{% endif %}
{% if balanceEntry.getLeft != 0 %}
<span class="text-success" style="text-align: right;">{{ (balanceEntry.getLeft)|formatAmountPlain }}</span>
{% endif %}
</td>
{% endfor %}
{% for budget in report.budgets %}
<tr>
<td>
<a href="{{ route('budgets.show', [budget.budget_id]) }}">{{ budget.budget_name }}</a>
</td>
{% for account in report.accounts %}
{% if budget.spent[account.id] %}
<td style="text-align: right;">
{{ balanceLine.leftOfRepetition|formatAmount }}
{{ formatAmountBySymbol(budget.spent[account.id].spent, budget.spent[account.id].currency_symbol, budget.spent[account.id].currency_decimal_places) }}
</td>
</tr>
{% else %}
<td>
&nbsp;
</td>
{% endif %}
{% endfor %}
<td style="text-align: right;">
{% for sum in report.sums[budget.budget_id] %}
{{ formatAmountBySymbol(sum.sum, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
<tfoot>
<tr>
<td><em>{{ 'sum'|_ }}</em></td>
{% for account in report.accounts %}
<td style="text-align: right;">{{ formatAmountBySymbol(account.sum, account.currency_symbol, account.currency_decimal_places) }}</td>
{% endfor %}
</tr>
</tfoot>
</table>

View File

@ -9,11 +9,7 @@
</thead>
<tbody>
{% set sumSpent = 0 %}
{% set sumEarned = 0 %}
{% for index, category in report %}
{% set sumSpent = sumSpent + category.spent %}
{% set sumEarned = sumEarned + category.earned %}
{% for index, category in report.categories %}
{% if loop.index > listLength %}
<tr class="overListLength">
{% else %}
@ -35,11 +31,23 @@
<tfoot>
{% if report|length > listLength %}
<tr>
<td colspan="3" class="active">
<td colspan="4" class="active">
<a href="#" class="listLengthTrigger">{{ trans('firefly.show_full_list',{number:incomeTopLength}) }}</a>
</td>
</tr>
{% endif %}
{% for sum in report.sums %}
<tr>
<td><em>{{ 'sum'|_ }} ({{ sum.currency_name }})</em></td>
<td style="text-align: right;">
{{ formatAmountBySymbol(sum.spent, sum.currency_symbol, sum.currency_decimal_places) }}
</td>
<td style="text-align: right;">
{{ formatAmountBySymbol(sum.earned, sum.currency_symbol, sum.currency_decimal_places) }}
</td>
<td></td>
</tr>
{% endfor %}
{#
<tr>
<td><em>{{ 'sum'|_ }}</em></td>