Files
firefly-iii/app/Support/Twig/AmountFormat.php

179 lines
5.9 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* AmountFormat.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Twig;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Account as AccountModel;
use FireflyIII\Models\TransactionCurrency;
2018-03-30 22:44:37 +02:00
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
2025-02-07 05:26:03 +01:00
use FireflyIII\Support\Facades\Amount;
use Illuminate\Support\Facades\Log;
2019-12-28 09:44:56 +01:00
use Twig\Extension\AbstractExtension;
2021-03-21 09:15:40 +01:00
use Twig\TwigFilter;
use Twig\TwigFunction;
use Override;
/**
* Contains all amount formatting routines.
*/
2019-12-28 09:44:56 +01:00
class AmountFormat extends AbstractExtension
{
#[Override]
public function getFilters(): array
{
return [
$this->formatAmount(),
$this->formatAmountPlain(),
];
}
2023-05-29 13:56:55 +02:00
protected function formatAmount(): TwigFilter
{
2021-03-21 09:15:40 +01:00
return new TwigFilter(
2023-05-29 13:56:55 +02:00
'formatAmount',
2021-03-21 09:15:40 +01:00
static function (string $string): string {
$currency = Amount::getPrimaryCurrency();
2017-06-05 07:03:32 +02:00
return Amount::formatAnything($currency, $string, true);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
2017-06-06 07:18:09 +02:00
);
}
2023-06-21 12:34:58 +02:00
protected function formatAmountPlain(): TwigFilter
2017-08-23 21:21:42 +02:00
{
2023-06-21 12:34:58 +02:00
return new TwigFilter(
'formatAmountPlain',
static function (string $string): string {
$currency = Amount::getPrimaryCurrency();
2018-07-27 05:03:37 +02:00
return Amount::formatAnything($currency, $string, false);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
2017-08-23 21:21:42 +02:00
);
}
#[Override]
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
$this->formatAmountByCode(),
];
}
2023-05-29 13:56:55 +02:00
/**
* Will format the amount by the currency related to the given account.
*
2024-01-04 07:44:52 +01:00
* TODO Remove me when v2 hits.
2023-05-29 13:56:55 +02:00
*/
2023-06-21 12:34:58 +02:00
protected function formatAmountByAccount(): TwigFunction
2023-05-29 13:56:55 +02:00
{
return new TwigFunction(
2023-06-21 12:34:58 +02:00
'formatAmountByAccount',
static function (AccountModel $account, string $amount, ?bool $coloured = null): string {
2023-12-10 06:45:59 +01:00
$coloured ??= true;
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
$currency = $accountRepos->getAccountCurrency($account) ?? Amount::getPrimaryCurrency();
2023-05-29 13:56:55 +02:00
return Amount::formatAnything($currency, $amount, $coloured);
2023-05-29 13:56:55 +02:00
},
['is_safe' => ['html']]
);
}
2017-06-06 07:18:09 +02:00
/**
* Will format the amount by the currency related to the given account.
*/
2019-12-28 09:44:56 +01:00
protected function formatAmountBySymbol(): TwigFunction
2017-06-06 07:18:09 +02:00
{
2019-12-28 09:44:56 +01:00
return new TwigFunction(
2017-11-15 10:52:29 +01:00
'formatAmountBySymbol',
2025-09-15 19:20:51 +02:00
static function (string $amount, ?string $symbol = null, ?int $decimalPlaces = null, ?bool $coloured = null): string {
if (null === $symbol) {
$message = sprintf('formatAmountBySymbol("%s", %s, %d, %s) was called without a symbol. Please browse to /flush to clear your cache.', $amount, var_export($symbol, true), $decimalPlaces, var_export($coloured, true));
Log::error($message);
2025-09-15 19:20:51 +02:00
$currency = Amount::getPrimaryCurrency();
}
if (null !== $symbol) {
$decimalPlaces ??= 2;
$coloured ??= true;
2025-09-15 19:20:51 +02:00
$currency = new TransactionCurrency();
$currency->symbol = $symbol;
$currency->decimal_places = $decimalPlaces;
}
2017-06-06 07:18:09 +02:00
return Amount::formatAnything($currency, $amount, $coloured);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
);
}
/**
2023-06-21 12:34:58 +02:00
* Will format the amount by the currency related to the given account.
*/
2023-06-21 12:34:58 +02:00
protected function formatAmountByCurrency(): TwigFunction
{
2023-06-21 12:34:58 +02:00
return new TwigFunction(
'formatAmountByCurrency',
static function (TransactionCurrency $currency, string $amount, ?bool $coloured = null): string {
2023-12-10 06:45:59 +01:00
$coloured ??= true;
return Amount::formatAnything($currency, $amount, $coloured);
2017-11-15 10:52:29 +01:00
},
['is_safe' => ['html']]
);
}
2025-05-04 17:41:26 +02:00
/**
* Use the code to format a currency.
*/
protected function formatAmountByCode(): TwigFunction
{
// formatAmountByCode
return new TwigFunction(
'formatAmountByCode',
static function (string $amount, string $code, ?bool $coloured = null): string {
$coloured ??= true;
2025-09-07 14:49:49 +02:00
try {
$currency = Amount::getTransactionCurrencyByCode($code);
} catch (FireflyException) {
Log::error(sprintf('Could not find currency with code "%s". Fallback to primary currency.', $code));
$currency = Amount::getPrimaryCurrency();
2025-05-04 17:41:26 +02:00
Log::error(sprintf('Fallback currency is "%s".', $currency->code));
}
return Amount::formatAnything($currency, $amount, $coloured);
2025-05-04 17:41:26 +02:00
},
['is_safe' => ['html']]
);
}
2017-07-07 08:09:42 +02:00
}