mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-15 08:35:00 +00:00
Optimize queries for statistics.
This commit is contained in:
@@ -29,10 +29,10 @@ use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
||||
use FireflyIII\Support\Facades\Amount;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Override;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
use Override;
|
||||
|
||||
/**
|
||||
* Contains all amount formatting routines.
|
||||
@@ -48,6 +48,17 @@ class AmountFormat extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
$this->formatAmountByAccount(),
|
||||
$this->formatAmountBySymbol(),
|
||||
$this->formatAmountByCurrency(),
|
||||
$this->formatAmountByCode(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function formatAmount(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
@@ -61,30 +72,6 @@ class AmountFormat extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
protected function formatAmountPlain(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'formatAmountPlain',
|
||||
static function (string $string): string {
|
||||
$currency = Amount::getPrimaryCurrency();
|
||||
|
||||
return Amount::formatAnything($currency, $string, false);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
$this->formatAmountByAccount(),
|
||||
$this->formatAmountBySymbol(),
|
||||
$this->formatAmountByCurrency(),
|
||||
$this->formatAmountByCode(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Will format the amount by the currency related to the given account.
|
||||
*
|
||||
@@ -107,50 +94,6 @@ class AmountFormat extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will format the amount by the currency related to the given account.
|
||||
*/
|
||||
protected function formatAmountBySymbol(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatAmountBySymbol',
|
||||
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);
|
||||
$currency = Amount::getPrimaryCurrency();
|
||||
}
|
||||
if (null !== $symbol) {
|
||||
$decimalPlaces ??= 2;
|
||||
$coloured ??= true;
|
||||
$currency = new TransactionCurrency();
|
||||
$currency->symbol = $symbol;
|
||||
$currency->decimal_places = $decimalPlaces;
|
||||
}
|
||||
|
||||
return Amount::formatAnything($currency, $amount, $coloured);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will format the amount by the currency related to the given account.
|
||||
*/
|
||||
protected function formatAmountByCurrency(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatAmountByCurrency',
|
||||
static function (TransactionCurrency $currency, string $amount, ?bool $coloured = null): string {
|
||||
$coloured ??= true;
|
||||
|
||||
return Amount::formatAnything($currency, $amount, $coloured);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the code to format a currency.
|
||||
*/
|
||||
@@ -175,4 +118,61 @@ class AmountFormat extends AbstractExtension
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will format the amount by the currency related to the given account.
|
||||
*/
|
||||
protected function formatAmountByCurrency(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatAmountByCurrency',
|
||||
static function (TransactionCurrency $currency, string $amount, ?bool $coloured = null): string {
|
||||
$coloured ??= true;
|
||||
|
||||
return Amount::formatAnything($currency, $amount, $coloured);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will format the amount by the currency related to the given account.
|
||||
*/
|
||||
protected function formatAmountBySymbol(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatAmountBySymbol',
|
||||
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);
|
||||
$currency = Amount::getPrimaryCurrency();
|
||||
}
|
||||
if (null !== $symbol) {
|
||||
$decimalPlaces ??= 2;
|
||||
$coloured ??= true;
|
||||
$currency = new TransactionCurrency();
|
||||
$currency->symbol = $symbol;
|
||||
$currency->decimal_places = $decimalPlaces;
|
||||
}
|
||||
|
||||
return Amount::formatAnything($currency, $amount, $coloured);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
protected function formatAmountPlain(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'formatAmountPlain',
|
||||
static function (string $string): string {
|
||||
$currency = Amount::getPrimaryCurrency();
|
||||
|
||||
return Amount::formatAnything($currency, $string, false);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -37,7 +37,6 @@ use Override;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
use function Safe\parse_url;
|
||||
|
||||
/**
|
||||
@@ -57,144 +56,6 @@ class General extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Show account balance. Only used on the front page of Firefly III.
|
||||
*/
|
||||
protected function balance(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'balance',
|
||||
static function (?Account $account): string {
|
||||
if (!$account instanceof Account) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
/** @var Carbon $date */
|
||||
$date = session('end', today(config('app.timezone'))->endOfMonth());
|
||||
Log::debug(sprintf('twig balance: Call finalAccountBalance with date/time "%s"', $date->toIso8601String()));
|
||||
$info = Steam::finalAccountBalance($account, $date);
|
||||
$currency = Steam::getAccountCurrency($account);
|
||||
$primary = Amount::getPrimaryCurrency();
|
||||
$convertToPrimary = Amount::convertToPrimary();
|
||||
$usePrimary = $convertToPrimary && $primary->id !== $currency->id;
|
||||
$currency ??= $primary;
|
||||
$strings = [];
|
||||
foreach ($info as $key => $balance) {
|
||||
if ('balance' === $key) {
|
||||
// balance in account currency.
|
||||
if (!$usePrimary) {
|
||||
$strings[] = app('amount')->formatAnything($currency, $balance, false);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if ('pc_balance' === $key) {
|
||||
// balance in primary currency.
|
||||
if ($usePrimary) {
|
||||
$strings[] = app('amount')->formatAnything($primary, $balance, false);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
// for multi currency accounts.
|
||||
if ($usePrimary && $key !== $primary->code) {
|
||||
$strings[] = app('amount')->formatAnything(Amount::getTransactionCurrencyByCode($key), $balance, false);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(', ', $strings);
|
||||
// return app('steam')->balance($account, $date);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to convert 1024 to 1kb etc.
|
||||
*/
|
||||
protected function formatFilesize(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'filesize',
|
||||
static function (int $size): string {
|
||||
// less than one GB, more than one MB
|
||||
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
|
||||
return round($size / (1024 * 1024), 2).' MB';
|
||||
}
|
||||
|
||||
// less than one MB
|
||||
if ($size < (1024 * 1024)) {
|
||||
return round($size / 1024, 2).' KB';
|
||||
}
|
||||
|
||||
return $size.' bytes';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show icon with attachment.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function mimeIcon(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'mimeIcon',
|
||||
static fn (string $string): string => match ($string) {
|
||||
'application/pdf' => 'fa-file-pdf-o',
|
||||
'image/webp', 'image/png', 'image/jpeg', 'image/svg+xml', 'image/heic', 'image/heic-sequence', 'application/vnd.oasis.opendocument.image' => 'fa-file-image-o',
|
||||
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'application/x-iwork-pages-sffpages', 'application/vnd.sun.xml.writer', 'application/vnd.sun.xml.writer.template', 'application/vnd.sun.xml.writer.global', 'application/vnd.stardivision.writer', 'application/vnd.stardivision.writer-global', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.text-template', 'application/vnd.oasis.opendocument.text-web', 'application/vnd.oasis.opendocument.text-master' => 'fa-file-word-o',
|
||||
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.sun.xml.calc', 'application/vnd.sun.xml.calc.template', 'application/vnd.stardivision.calc', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.spreadsheet-template' => 'fa-file-excel-o',
|
||||
'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.presentationml.template', 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'application/vnd.sun.xml.impress', 'application/vnd.sun.xml.impress.template', 'application/vnd.stardivision.impress', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.oasis.opendocument.presentation-template' => 'fa-file-powerpoint-o',
|
||||
'application/vnd.sun.xml.draw', 'application/vnd.sun.xml.draw.template', 'application/vnd.stardivision.draw', 'application/vnd.oasis.opendocument.chart' => 'fa-paint-brush',
|
||||
'application/vnd.oasis.opendocument.graphics', 'application/vnd.oasis.opendocument.graphics-template', 'application/vnd.sun.xml.math', 'application/vnd.stardivision.math', 'application/vnd.oasis.opendocument.formula', 'application/vnd.oasis.opendocument.database' => 'fa-calculator',
|
||||
default => 'fa-file-o',
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
protected function markdown(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'markdown',
|
||||
static function (string $text): string {
|
||||
$converter = new GithubFlavoredMarkdownConverter(
|
||||
[
|
||||
'allow_unsafe_links' => false,
|
||||
'max_nesting_level' => 5,
|
||||
'html_input' => 'escape',
|
||||
]
|
||||
);
|
||||
|
||||
return (string)$converter->convert($text);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show URL host name
|
||||
*/
|
||||
protected function phpHostName(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'phphost',
|
||||
static function (string $string): string {
|
||||
$proto = parse_url($string, PHP_URL_SCHEME);
|
||||
$host = parse_url($string, PHP_URL_HOST);
|
||||
if (is_array($host)) {
|
||||
$host = implode(' ', $host);
|
||||
}
|
||||
if (is_array($proto)) {
|
||||
$proto = implode(' ', $proto);
|
||||
}
|
||||
|
||||
return e(sprintf('%s://%s', $proto, $host));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[Override]
|
||||
public function getFunctions(): array
|
||||
{
|
||||
@@ -212,38 +73,6 @@ class General extends AbstractExtension
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic example thing for some views.
|
||||
*/
|
||||
protected function phpdate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'phpdate',
|
||||
static fn (string $str): string => date($str)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when the current route matches the given argument
|
||||
* exactly.
|
||||
*/
|
||||
protected function activeRouteStrict(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRouteStrict',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
|
||||
if (\Route::getCurrentRoute()->getName() === $route) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
@@ -275,7 +104,7 @@ class General extends AbstractExtension
|
||||
'activeRoutePartialObjectType',
|
||||
static function ($context): string {
|
||||
[, $route, $objectType] = func_get_args();
|
||||
$activeObjectType = $context['objectType'] ?? false;
|
||||
$activeObjectType = $context['objectType'] ?? false;
|
||||
|
||||
if ($objectType === $activeObjectType
|
||||
&& false !== stripos(
|
||||
@@ -291,6 +120,193 @@ class General extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "active" when the current route matches the given argument
|
||||
* exactly.
|
||||
*/
|
||||
protected function activeRouteStrict(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'activeRouteStrict',
|
||||
static function (): string {
|
||||
$args = func_get_args();
|
||||
$route = $args[0]; // name of the route.
|
||||
|
||||
if (\Route::getCurrentRoute()->getName() === $route) {
|
||||
return 'active';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show account balance. Only used on the front page of Firefly III.
|
||||
*/
|
||||
protected function balance(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'balance',
|
||||
static function (?Account $account): string {
|
||||
if (!$account instanceof Account) {
|
||||
return '0';
|
||||
}
|
||||
|
||||
/** @var Carbon $date */
|
||||
$date = session('end', today(config('app.timezone'))->endOfMonth());
|
||||
Log::debug(sprintf('twig balance: Call finalAccountBalance with date/time "%s"', $date->toIso8601String()));
|
||||
$info = Steam::finalAccountBalance($account, $date);
|
||||
$currency = Steam::getAccountCurrency($account);
|
||||
$primary = Amount::getPrimaryCurrency();
|
||||
$convertToPrimary = Amount::convertToPrimary();
|
||||
$usePrimary = $convertToPrimary && $primary->id !== $currency->id;
|
||||
$currency ??= $primary;
|
||||
$strings = [];
|
||||
foreach ($info as $key => $balance) {
|
||||
if ('balance' === $key) {
|
||||
// balance in account currency.
|
||||
if (!$usePrimary) {
|
||||
$strings[] = app('amount')->formatAnything($currency, $balance, false);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if ('pc_balance' === $key) {
|
||||
// balance in primary currency.
|
||||
if ($usePrimary) {
|
||||
$strings[] = app('amount')->formatAnything($primary, $balance, false);
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
// for multi currency accounts.
|
||||
if ($usePrimary && $key !== $primary->code) {
|
||||
$strings[] = app('amount')->formatAnything(Amount::getTransactionCurrencyByCode($key), $balance, false);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(', ', $strings);
|
||||
// return app('steam')->balance($account, $date);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function carbonize(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'carbonize',
|
||||
static fn(string $date): Carbon => new Carbon($date, config('app.timezone'))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a string as a thing by converting it to a Carbon first.
|
||||
*/
|
||||
protected function formatDate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatDate',
|
||||
static function (string $date, string $format): string {
|
||||
$carbon = new Carbon($date);
|
||||
|
||||
return $carbon->isoFormat($format);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to convert 1024 to 1kb etc.
|
||||
*/
|
||||
protected function formatFilesize(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'filesize',
|
||||
static function (int $size): string {
|
||||
// less than one GB, more than one MB
|
||||
if ($size < (1024 * 1024 * 2014) && $size >= (1024 * 1024)) {
|
||||
return round($size / (1024 * 1024), 2) . ' MB';
|
||||
}
|
||||
|
||||
// less than one MB
|
||||
if ($size < (1024 * 1024)) {
|
||||
return round($size / 1024, 2) . ' KB';
|
||||
}
|
||||
|
||||
return $size . ' bytes';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Remove me when v2 hits.
|
||||
*/
|
||||
protected function getMetaField(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'accountGetMetaField',
|
||||
static function (Account $account, string $field): string {
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$result = $repository->getMetaValue($account, $field);
|
||||
if (null === $result) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRootSearchOperator(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'getRootSearchOperator',
|
||||
static function (string $operator): string {
|
||||
$result = OperatorQuerySearch::getRootOperator($operator);
|
||||
|
||||
return str_replace('-', 'not_', $result);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return true if the user is of role X.
|
||||
*/
|
||||
protected function hasRole(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'hasRole',
|
||||
static function (string $role): bool {
|
||||
$repository = app(UserRepositoryInterface::class);
|
||||
if ($repository->hasRole(auth()->user(), $role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function markdown(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'markdown',
|
||||
static function (string $text): string {
|
||||
$converter = new GithubFlavoredMarkdownConverter(
|
||||
[
|
||||
'allow_unsafe_links' => false,
|
||||
'max_nesting_level' => 5,
|
||||
'html_input' => 'escape',
|
||||
]
|
||||
);
|
||||
|
||||
return (string)$converter->convert($text);
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return "menu-open" when a part of the route matches the argument.
|
||||
* ie. "accounts" will match "accounts.index".
|
||||
@@ -313,75 +329,58 @@ class General extends AbstractExtension
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a string as a thing by converting it to a Carbon first.
|
||||
* Show icon with attachment.
|
||||
*
|
||||
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
|
||||
*/
|
||||
protected function formatDate(): TwigFunction
|
||||
protected function mimeIcon(): TwigFilter
|
||||
{
|
||||
return new TwigFunction(
|
||||
'formatDate',
|
||||
static function (string $date, string $format): string {
|
||||
$carbon = new Carbon($date);
|
||||
return new TwigFilter(
|
||||
'mimeIcon',
|
||||
static fn(string $string): string => match ($string) {
|
||||
'application/pdf' => 'fa-file-pdf-o',
|
||||
'image/webp', 'image/png', 'image/jpeg', 'image/svg+xml', 'image/heic', 'image/heic-sequence', 'application/vnd.oasis.opendocument.image' => 'fa-file-image-o',
|
||||
'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 'application/x-iwork-pages-sffpages', 'application/vnd.sun.xml.writer', 'application/vnd.sun.xml.writer.template', 'application/vnd.sun.xml.writer.global', 'application/vnd.stardivision.writer', 'application/vnd.stardivision.writer-global', 'application/vnd.oasis.opendocument.text', 'application/vnd.oasis.opendocument.text-template', 'application/vnd.oasis.opendocument.text-web', 'application/vnd.oasis.opendocument.text-master' => 'fa-file-word-o',
|
||||
'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 'application/vnd.sun.xml.calc', 'application/vnd.sun.xml.calc.template', 'application/vnd.stardivision.calc', 'application/vnd.oasis.opendocument.spreadsheet', 'application/vnd.oasis.opendocument.spreadsheet-template' => 'fa-file-excel-o',
|
||||
'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'application/vnd.openxmlformats-officedocument.presentationml.template', 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 'application/vnd.sun.xml.impress', 'application/vnd.sun.xml.impress.template', 'application/vnd.stardivision.impress', 'application/vnd.oasis.opendocument.presentation', 'application/vnd.oasis.opendocument.presentation-template' => 'fa-file-powerpoint-o',
|
||||
'application/vnd.sun.xml.draw', 'application/vnd.sun.xml.draw.template', 'application/vnd.stardivision.draw', 'application/vnd.oasis.opendocument.chart' => 'fa-paint-brush',
|
||||
'application/vnd.oasis.opendocument.graphics', 'application/vnd.oasis.opendocument.graphics-template', 'application/vnd.sun.xml.math', 'application/vnd.stardivision.math', 'application/vnd.oasis.opendocument.formula', 'application/vnd.oasis.opendocument.database' => 'fa-calculator',
|
||||
default => 'fa-file-o',
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
return $carbon->isoFormat($format);
|
||||
/**
|
||||
* Show URL host name
|
||||
*/
|
||||
protected function phpHostName(): TwigFilter
|
||||
{
|
||||
return new TwigFilter(
|
||||
'phphost',
|
||||
static function (string $string): string {
|
||||
$proto = parse_url($string, PHP_URL_SCHEME);
|
||||
$host = parse_url($string, PHP_URL_HOST);
|
||||
if (is_array($host)) {
|
||||
$host = implode(' ', $host);
|
||||
}
|
||||
if (is_array($proto)) {
|
||||
$proto = implode(' ', $proto);
|
||||
}
|
||||
|
||||
return e(sprintf('%s://%s', $proto, $host));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Remove me when v2 hits.
|
||||
* Basic example thing for some views.
|
||||
*/
|
||||
protected function getMetaField(): TwigFunction
|
||||
protected function phpdate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'accountGetMetaField',
|
||||
static function (Account $account, string $field): string {
|
||||
/** @var AccountRepositoryInterface $repository */
|
||||
$repository = app(AccountRepositoryInterface::class);
|
||||
$result = $repository->getMetaValue($account, $field);
|
||||
if (null === $result) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Will return true if the user is of role X.
|
||||
*/
|
||||
protected function hasRole(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'hasRole',
|
||||
static function (string $role): bool {
|
||||
$repository = app(UserRepositoryInterface::class);
|
||||
if ($repository->hasRole(auth()->user(), $role)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRootSearchOperator(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'getRootSearchOperator',
|
||||
static function (string $operator): string {
|
||||
$result = OperatorQuerySearch::getRootOperator($operator);
|
||||
|
||||
return str_replace('-', 'not_', $result);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function carbonize(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'carbonize',
|
||||
static fn (string $date): Carbon => new Carbon($date, config('app.timezone'))
|
||||
'phpdate',
|
||||
static fn(string $str): string => date($str)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -23,34 +23,43 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
use Config;
|
||||
use Override;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
/**
|
||||
* Class Rule.
|
||||
*/
|
||||
class Rule extends AbstractExtension
|
||||
{
|
||||
#[Override]
|
||||
public function getFunctions(): array
|
||||
public function allActionTriggers(): TwigFunction
|
||||
{
|
||||
return [
|
||||
$this->allJournalTriggers(),
|
||||
$this->allRuleTriggers(),
|
||||
$this->allActionTriggers(),
|
||||
];
|
||||
return new TwigFunction(
|
||||
'allRuleActions',
|
||||
static function () {
|
||||
// array of valid values for actions
|
||||
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
|
||||
$possibleActions = [];
|
||||
foreach ($ruleActions as $key) {
|
||||
$possibleActions[$key] = (string)trans('firefly.rule_action_' . $key . '_choice');
|
||||
}
|
||||
unset($ruleActions);
|
||||
asort($possibleActions);
|
||||
|
||||
return $possibleActions;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function allJournalTriggers(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'allJournalTriggers',
|
||||
static fn () => [
|
||||
'store-journal' => (string) trans('firefly.rule_trigger_store_journal'),
|
||||
'update-journal' => (string) trans('firefly.rule_trigger_update_journal'),
|
||||
'manual-activation' => (string) trans('firefly.rule_trigger_manual'),
|
||||
static fn() => [
|
||||
'store-journal' => (string)trans('firefly.rule_trigger_store_journal'),
|
||||
'update-journal' => (string)trans('firefly.rule_trigger_update_journal'),
|
||||
'manual-activation' => (string)trans('firefly.rule_trigger_manual'),
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -64,7 +73,7 @@ class Rule extends AbstractExtension
|
||||
$possibleTriggers = [];
|
||||
foreach ($ruleTriggers as $key) {
|
||||
if ('user_action' !== $key) {
|
||||
$possibleTriggers[$key] = (string) trans('firefly.rule_trigger_'.$key.'_choice');
|
||||
$possibleTriggers[$key] = (string)trans('firefly.rule_trigger_' . $key . '_choice');
|
||||
}
|
||||
}
|
||||
unset($ruleTriggers);
|
||||
@@ -75,22 +84,13 @@ class Rule extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
public function allActionTriggers(): TwigFunction
|
||||
#[Override]
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return new TwigFunction(
|
||||
'allRuleActions',
|
||||
static function () {
|
||||
// array of valid values for actions
|
||||
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
|
||||
$possibleActions = [];
|
||||
foreach ($ruleActions as $key) {
|
||||
$possibleActions[$key] = (string) trans('firefly.rule_action_'.$key.'_choice');
|
||||
}
|
||||
unset($ruleActions);
|
||||
asort($possibleActions);
|
||||
|
||||
return $possibleActions;
|
||||
}
|
||||
);
|
||||
return [
|
||||
$this->allJournalTriggers(),
|
||||
$this->allRuleTriggers(),
|
||||
$this->allActionTriggers(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
@@ -31,10 +31,9 @@ use FireflyIII\Models\Transaction;
|
||||
use FireflyIII\Models\TransactionJournal;
|
||||
use FireflyIII\Models\TransactionJournalMeta;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Override;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
use Override;
|
||||
|
||||
use function Safe\json_decode;
|
||||
|
||||
/**
|
||||
@@ -76,6 +75,141 @@ class TransactionGroupTwig extends AbstractExtension
|
||||
);
|
||||
}
|
||||
|
||||
public function journalGetMetaDate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalGetMetaDate',
|
||||
static function (int $journalId, string $metaField) {
|
||||
/** @var null|TransactionJournalMeta $entry */
|
||||
$entry = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->first();
|
||||
if (null === $entry) {
|
||||
return today(config('app.timezone'));
|
||||
}
|
||||
|
||||
return new Carbon(json_decode((string)$entry->data, false));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function journalGetMetaField(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalGetMetaField',
|
||||
static function (int $journalId, string $metaField) {
|
||||
/** @var null|TransactionJournalMeta $entry */
|
||||
$entry = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->first();
|
||||
if (null === $entry) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return json_decode((string)$entry->data, true);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function journalHasMeta(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalHasMeta',
|
||||
static function (int $journalId, string $metaField) {
|
||||
$count = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->count();
|
||||
|
||||
return 1 === $count;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the amount for a single journal object.
|
||||
*/
|
||||
public function journalObjectAmount(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalObjectAmount',
|
||||
function (TransactionJournal $journal): string {
|
||||
$result = $this->normalJournalObjectAmount($journal);
|
||||
// now append foreign amount, if any.
|
||||
if ($this->journalObjectHasForeign($journal)) {
|
||||
$foreign = $this->foreignJournalObjectAmount($journal);
|
||||
$result = sprintf('%s (%s)', $result, $foreign);
|
||||
}
|
||||
|
||||
return $result;
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate foreign amount for transaction from a transaction group.
|
||||
*/
|
||||
private function foreignJournalArrayAmount(array $array): string
|
||||
{
|
||||
$type = $array['transaction_type_type'] ?? TransactionTypeEnum::WITHDRAWAL->value;
|
||||
$amount = $array['foreign_amount'] ?? '0';
|
||||
$colored = true;
|
||||
|
||||
$sourceType = $array['source_account_type'] ?? 'invalid';
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int)$array['foreign_currency_decimal_places'], $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate foreign amount for journal from a transaction group.
|
||||
*/
|
||||
private function foreignJournalObjectAmount(TransactionJournal $journal): string
|
||||
{
|
||||
$type = $journal->transactionType->type;
|
||||
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$currency = $first->foreignCurrency;
|
||||
$amount = '' === $first->foreign_amount ? '0' : $first->foreign_amount;
|
||||
$colored = true;
|
||||
$sourceType = $first->account->accountType()->first()->type;
|
||||
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function journalObjectHasForeign(TransactionJournal $journal): bool
|
||||
{
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
return '' !== $first->foreign_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate normal amount for transaction from a transaction group.
|
||||
*/
|
||||
@@ -91,7 +225,34 @@ class TransactionGroupTwig extends AbstractExtension
|
||||
$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 (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate normal amount for transaction from a transaction group.
|
||||
*/
|
||||
private function normalJournalObjectAmount(TransactionJournal $journal): string
|
||||
{
|
||||
$type = $journal->transactionType->type;
|
||||
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$currency = $journal->transactionCurrency;
|
||||
$amount = $first->amount ?? '0';
|
||||
$colored = true;
|
||||
$sourceType = $first->account->accountType()->first()->type;
|
||||
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
@@ -118,169 +279,4 @@ class TransactionGroupTwig extends AbstractExtension
|
||||
|
||||
return $amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate foreign amount for transaction from a transaction group.
|
||||
*/
|
||||
private function foreignJournalArrayAmount(array $array): string
|
||||
{
|
||||
$type = $array['transaction_type_type'] ?? TransactionTypeEnum::WITHDRAWAL->value;
|
||||
$amount = $array['foreign_amount'] ?? '0';
|
||||
$colored = true;
|
||||
|
||||
$sourceType = $array['source_account_type'] ?? 'invalid';
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($array['foreign_currency_symbol'], (int) $array['foreign_currency_decimal_places'], $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shows the amount for a single journal object.
|
||||
*/
|
||||
public function journalObjectAmount(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalObjectAmount',
|
||||
function (TransactionJournal $journal): string {
|
||||
$result = $this->normalJournalObjectAmount($journal);
|
||||
// now append foreign amount, if any.
|
||||
if ($this->journalObjectHasForeign($journal)) {
|
||||
$foreign = $this->foreignJournalObjectAmount($journal);
|
||||
$result = sprintf('%s (%s)', $result, $foreign);
|
||||
}
|
||||
|
||||
return $result;
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate normal amount for transaction from a transaction group.
|
||||
*/
|
||||
private function normalJournalObjectAmount(TransactionJournal $journal): string
|
||||
{
|
||||
$type = $journal->transactionType->type;
|
||||
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$currency = $journal->transactionCurrency;
|
||||
$amount = $first->amount ?? '0';
|
||||
$colored = true;
|
||||
$sourceType = $first->account->accountType()->first()->type;
|
||||
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function journalObjectHasForeign(TransactionJournal $journal): bool
|
||||
{
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
|
||||
return '' !== $first->foreign_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate foreign amount for journal from a transaction group.
|
||||
*/
|
||||
private function foreignJournalObjectAmount(TransactionJournal $journal): string
|
||||
{
|
||||
$type = $journal->transactionType->type;
|
||||
|
||||
/** @var Transaction $first */
|
||||
$first = $journal->transactions()->where('amount', '<', 0)->first();
|
||||
$currency = $first->foreignCurrency;
|
||||
$amount = '' === $first->foreign_amount ? '0' : $first->foreign_amount;
|
||||
$colored = true;
|
||||
$sourceType = $first->account->accountType()->first()->type;
|
||||
|
||||
$amount = $this->signAmount($amount, $type, $sourceType);
|
||||
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($currency->symbol, $currency->decimal_places, $amount, $colored);
|
||||
if (TransactionTypeEnum::TRANSFER->value === $type) {
|
||||
return sprintf('<span class="text-info money-transfer">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function journalHasMeta(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalHasMeta',
|
||||
static function (int $journalId, string $metaField) {
|
||||
$count = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->count()
|
||||
;
|
||||
|
||||
return 1 === $count;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function journalGetMetaDate(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalGetMetaDate',
|
||||
static function (int $journalId, string $metaField) {
|
||||
/** @var null|TransactionJournalMeta $entry */
|
||||
$entry = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->first()
|
||||
;
|
||||
if (null === $entry) {
|
||||
return today(config('app.timezone'));
|
||||
}
|
||||
|
||||
return new Carbon(json_decode((string) $entry->data, false));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function journalGetMetaField(): TwigFunction
|
||||
{
|
||||
return new TwigFunction(
|
||||
'journalGetMetaField',
|
||||
static function (int $journalId, string $metaField) {
|
||||
/** @var null|TransactionJournalMeta $entry */
|
||||
$entry = DB::table('journal_meta')
|
||||
->where('name', $metaField)
|
||||
->where('transaction_journal_id', $journalId)
|
||||
->whereNull('deleted_at')
|
||||
->first()
|
||||
;
|
||||
if (null === $entry) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return json_decode((string) $entry->data, true);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -23,10 +23,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Twig;
|
||||
|
||||
use Override;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use Twig\TwigFunction;
|
||||
use Override;
|
||||
|
||||
/**
|
||||
* Class Budget.
|
||||
@@ -39,7 +39,7 @@ class Translation extends AbstractExtension
|
||||
return [
|
||||
new TwigFilter(
|
||||
'_',
|
||||
static fn ($name) => (string) trans(sprintf('firefly.%s', $name)),
|
||||
static fn($name) => (string)trans(sprintf('firefly.%s', $name)),
|
||||
['is_safe' => ['html']]
|
||||
),
|
||||
];
|
||||
|
Reference in New Issue
Block a user