Simplify frontpage.

This commit is contained in:
James Cole
2020-08-05 18:51:17 +02:00
parent dfa60ec703
commit 8df6356abf
4 changed files with 103 additions and 99 deletions

View File

@@ -55,43 +55,6 @@ class TransactionGroupTwig extends AbstractExtension
];
}
/**
* @return TwigFunction
*/
public function groupAmount(): TwigFunction
{
return new TwigFunction(
'groupAmount',
function (array $array): string {
$sums = $array['sums'];
$return = [];
$first = reset($array['transactions']);
$type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$colored = true;
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
/** @var array $sum */
foreach ($sums as $sum) {
$amount = $sum['amount'];
$sourceType = $first['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
$return[] = app('amount')->formatFlat($sum['currency_symbol'], (int)$sum['currency_decimal_places'], $amount, $colored);
}
$result = implode(', ', $return);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
return $result;
},
['is_safe' => ['html']]
);
}
/**
* @return TwigFunction
*/
@@ -222,12 +185,12 @@ class TransactionGroupTwig extends AbstractExtension
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int) $array['foreign_currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
@@ -246,18 +209,18 @@ class TransactionGroupTwig extends AbstractExtension
{
$type = $journal->transactionType->type;
/** @var Transaction $first */
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
$amount = $first->foreign_amount ?? '0';
$colored = true;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $first->foreignCurrency;
$amount = $first->foreign_amount ?? '0';
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
$result = app('amount')->formatFlat($currency->symbol, (int) $currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
@@ -274,17 +237,17 @@ class TransactionGroupTwig extends AbstractExtension
*/
private function normalJournalArrayAmount(array $array): string
{
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
$amount = $array['amount'] ?? '0';
$colored = true;
$sourceType = $array['source_account_type'] ?? 'invalid';
$amount = $this->signAmount($amount, $type, $sourceType);
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($array['currency_symbol'], (int)$array['currency_decimal_places'], $amount, $colored);
$result = app('amount')->formatFlat($array['currency_symbol'], (int) $array['currency_decimal_places'], $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
@@ -301,19 +264,19 @@ class TransactionGroupTwig extends AbstractExtension
*/
private function normalJournalObjectAmount(TransactionJournal $journal): string
{
$type = $journal->transactionType->type;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $journal->transactionCurrency;
$amount = $first->amount ?? '0';
$colored = true;
$type = $journal->transactionType->type;
$first = $journal->transactions()->where('amount', '<', 0)->first();
$currency = $journal->transactionCurrency;
$amount = $first->amount ?? '0';
$colored = true;
$sourceType = $first->account()->first()->accountType()->first()->type;
$amount = $this->signAmount($amount, $type, $sourceType);
if ($type === TransactionType::TRANSFER) {
$colored = false;
}
$result = app('amount')->formatFlat($currency->symbol, (int)$currency->decimal_places, $amount, $colored);
$result = app('amount')->formatFlat($currency->symbol, (int) $currency->decimal_places, $amount, $colored);
if ($type === TransactionType::TRANSFER) {
$result = sprintf('<span class="text-info">%s</span>', $result);
}
@@ -333,7 +296,14 @@ class TransactionGroupTwig extends AbstractExtension
return null !== $first->foreign_amount;
}
private function signAmount( string $amount, string $transactionType, string $sourceType ): string {
/**
* @param string $amount
* @param string $transactionType
* @param string $sourceType
* @return string
*/
private function signAmount(string $amount, string $transactionType, string $sourceType): string
{
// withdrawals stay negative
if ($transactionType !== TransactionType::WITHDRAWAL) {