Better number formatting.

This commit is contained in:
James Cole 2016-05-17 19:23:23 +02:00
parent ce78c8993f
commit b0a5b53abb
4 changed files with 57 additions and 31 deletions

View File

@ -158,9 +158,12 @@ class HomeController extends Controller
$name = $route->getName();
$methods = $route->getMethods();
$search = ['{account}', '{what}', '{rule}', '{tj}'];
$replace = [1, 'asset', 1, 1];
$url = str_replace($search, $replace, $route->getUri());
if (!is_null($name) && in_array('GET', $methods) && !$this->startsWithAny($ignore, $name)) {
echo $name . '<br>';
echo '<a href="/' . $url . '" title="' . $name . '">' . $name . '</a><br>';
}
}

View File

@ -22,6 +22,56 @@ use Twig_SimpleFunction;
class Journal extends Twig_Extension
{
/**
* @return Twig_SimpleFunction
*/
public function formatAccountPerspective(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatAccountPerspective', function (TransactionJournal $journal, Account $account) {
$cache = new CacheProperties;
$cache->addProperty('formatAccountPerspective');
$cache->addProperty($journal->id);
$cache->addProperty($account->id);
if ($cache->has()) {
return $cache->get();
}
// get the account amount:
$transactions = $journal->transactions()->where('transactions.account_id', $account->id)->get(['transactions.*']);
$amount = '0';
foreach ($transactions as $transaction) {
$amount = bcadd($amount, strval($transaction->amount));
}
if ($journal->isTransfer()) {
$amount = bcmul($amount, '-1');
}
// check if this sum is the same as the journal:
$journalSum = TransactionJournal::amount($journal);
$full = Amount::formatJournal($journal);
if (bccomp($journalSum, $amount) === 0) {
$cache->store($full);
return $full;
}
$formatted = Amount::format($amount, true);
if ($journal->isTransfer()) {
$formatted = '<span class="text-info">' . Amount::format($amount) . '</span>';
}
$str = $formatted . ' (' . $full . ')';
$cache->store($str);
return $str;
}
);
}
/**
* @return Twig_SimpleFunction
*/
@ -46,33 +96,6 @@ class Journal extends Twig_Extension
);
}
/**
* @return Twig_SimpleFunction
*/
public function formatPerspective(): Twig_SimpleFunction
{
return new Twig_SimpleFunction(
'formatPerspective', function (TransactionJournal $journal, Account $account) {
// get the account amount:
$transactions = $journal->transactions()->where('transactions.account_id', $account->id)->get(['transactions.*']);
$amount = '0';
foreach ($transactions as $transaction) {
$amount = bcadd($amount, strval($transaction->amount));
}
if ($journal->isWithdrawal()) {
$amount = bcmul($amount, '-1');
}
$formatted = Amount::format($amount, true);
return $formatted . ' (' . Amount::formatJournal($journal) . ')';
}
);
}
/**
* @return Twig_SimpleFunction
*/
@ -126,7 +149,7 @@ class Journal extends Twig_Extension
$functions = [
$this->getSourceAccount(),
$this->getDestinationAccount(),
$this->formatPerspective(),
$this->formatAccountPerspective(),
$this->formatBudgetPerspective(),
$this->journalBudgets(),
$this->journalCategories(),

View File

@ -62,7 +62,7 @@
{% endif %}
{% if accountPerspective %}
{{ formatPerspective(journal, accountPerspective)|raw }}
{{ formatAccountPerspective(journal, accountPerspective)|raw }}
{% endif %}
{% if budgetPerspective %}

View File

@ -42,7 +42,7 @@
{% if not accountPerspective %}
{{ journal|formatJournal }}
{% else %}
{{ formatPerspective(journal, accountPerspective)|raw }}
{{ formatAccountPerspective(journal, accountPerspective)|raw }}
{% endif %}
</td>
<td class="hidden-sm hidden-xs">