mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Clean up some money formatting routines.
This commit is contained in:
@@ -33,96 +33,6 @@ 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 || bccomp(bcmul($journalSum, '-1'), $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
|
||||
*/
|
||||
public function formatBudgetPerspective(): Twig_SimpleFunction
|
||||
{
|
||||
return new Twig_SimpleFunction(
|
||||
'formatBudgetPerspective', function (TransactionJournal $journal, ModelBudget $budget) {
|
||||
|
||||
$cache = new CacheProperties;
|
||||
$cache->addProperty('formatBudgetPerspective');
|
||||
$cache->addProperty($journal->id);
|
||||
$cache->addProperty($budget->id);
|
||||
|
||||
if ($cache->has()) {
|
||||
return $cache->get();
|
||||
}
|
||||
|
||||
// get the account amount:
|
||||
$transactions = $journal->transactions()->where('transactions.amount', '<', 0)->get(['transactions.*']);
|
||||
$amount = '0';
|
||||
foreach ($transactions as $transaction) {
|
||||
$currentBudget = $transaction->budgets->first();
|
||||
if (!is_null($currentBudget) && $currentBudget->id === $budget->id) {
|
||||
$amount = bcadd($amount, strval($transaction->amount));
|
||||
}
|
||||
}
|
||||
if ($amount === '0') {
|
||||
$formatted = Amount::formatJournal($journal);
|
||||
$cache->store($formatted);
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
|
||||
$formatted = Amount::format($amount, true) . ' (' . Amount::formatJournal($journal) . ')';
|
||||
$cache->store($formatted);
|
||||
|
||||
return $formatted;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Twig_SimpleFunction
|
||||
@@ -178,8 +88,6 @@ class Journal extends Twig_Extension
|
||||
$functions = [
|
||||
$this->getSourceAccount(),
|
||||
$this->getDestinationAccount(),
|
||||
$this->formatAccountPerspective(),
|
||||
$this->formatBudgetPerspective(),
|
||||
$this->journalBudgets(),
|
||||
$this->journalCategories(),
|
||||
];
|
||||
|
Reference in New Issue
Block a user