mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-21 09:00:07 +00:00
Build a new collector and first view online.
This commit is contained in:
@@ -154,6 +154,49 @@ class Amount
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will properly format the given number, in color or "black and white",
|
||||
* as a currency, given two things: the currency required and the current locale.
|
||||
*
|
||||
* @param string $symbol
|
||||
* @param int $decimalPlaces
|
||||
* @param string $amount
|
||||
* @param bool $coloured
|
||||
*
|
||||
* @return string
|
||||
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
|
||||
* @noinspection MoreThanThreeArgumentsInspection
|
||||
*/
|
||||
public function formatFlat(string $symbol, int $decimalPlaces, string $amount, bool $coloured = null): string
|
||||
{
|
||||
$coloured = $coloured ?? true;
|
||||
$locale = explode(',', (string)trans('config.locale'));
|
||||
$locale = array_map('trim', $locale);
|
||||
setlocale(LC_MONETARY, $locale);
|
||||
$float = round($amount, 12);
|
||||
$info = localeconv();
|
||||
$formatted = number_format($float, $decimalPlaces, $info['mon_decimal_point'], $info['mon_thousands_sep']);
|
||||
|
||||
// some complicated switches to format the amount correctly:
|
||||
$precedes = $amount < 0 ? $info['n_cs_precedes'] : $info['p_cs_precedes'];
|
||||
$separated = $amount < 0 ? $info['n_sep_by_space'] : $info['p_sep_by_space'];
|
||||
$space = true === $separated ? ' ' : '';
|
||||
$result = false === $precedes ? $formatted . $space . $symbol : $symbol . $space . $formatted;
|
||||
|
||||
if (true === $coloured) {
|
||||
if ($amount > 0) {
|
||||
return sprintf('<span class="text-success">%s</span>', $result);
|
||||
}
|
||||
if ($amount < 0) {
|
||||
return sprintf('<span class="text-danger">%s</span>', $result);
|
||||
}
|
||||
|
||||
return sprintf('<span style="color:#999">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
|
||||
192
app/Support/Twig/Extension/TransactionGroupTwig.php
Normal file
192
app/Support/Twig/Extension/TransactionGroupTwig.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?php
|
||||
/**
|
||||
* TransactionGroupTwig.php
|
||||
* Copyright (c) 2019 thegrumpydictator@gmail.com
|
||||
*
|
||||
* This file is part of Firefly III.
|
||||
*
|
||||
* Firefly III is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Firefly III is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Support\Twig\Extension;
|
||||
|
||||
use FireflyIII\Models\TransactionType;
|
||||
use Twig_Extension;
|
||||
use Twig_SimpleFunction;
|
||||
|
||||
/**
|
||||
* Class TransactionGroupTwig
|
||||
*/
|
||||
class TransactionGroupTwig extends Twig_Extension
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
$this->transactionAmount(),
|
||||
$this->groupAmount(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Twig_SimpleFunction
|
||||
*/
|
||||
public function groupAmount(): Twig_SimpleFunction
|
||||
{
|
||||
return new Twig_SimpleFunction(
|
||||
'groupAmount',
|
||||
function (array $array): string {
|
||||
$result = $this->normalGroupAmount($array);
|
||||
// now append foreign amount, if any.
|
||||
if (0 !== bccomp('0', $array['foreign_sum'])) {
|
||||
$foreign = $this->foreignGroupAmount($array);
|
||||
$result = sprintf('%s (%s)', $result, $foreign);
|
||||
}
|
||||
|
||||
return $result;
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Twig_SimpleFunction
|
||||
*/
|
||||
public function transactionAmount(): Twig_SimpleFunction
|
||||
{
|
||||
return new Twig_SimpleFunction(
|
||||
'transactionAmount',
|
||||
function (array $array): string {
|
||||
// if is not a withdrawal, amount positive.
|
||||
$result = $this->normalAmount($array);
|
||||
// now append foreign amount, if any.
|
||||
if (null !== $array['foreign_amount']) {
|
||||
$foreign = $this->foreignAmount($array);
|
||||
$result = sprintf('%s (%s)', $result, $foreign);
|
||||
}
|
||||
|
||||
return $result;
|
||||
},
|
||||
['is_safe' => ['html']]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate foreign amount for transaction from a transaction group.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function foreignAmount(array $array): string
|
||||
{
|
||||
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
||||
$amount = $array['foreign_amount'] ?? '0';
|
||||
$colored = true;
|
||||
if ($type !== TransactionType::WITHDRAWAL) {
|
||||
$amount = bcmul($amount, '-1');
|
||||
}
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$colored = false;
|
||||
}
|
||||
$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);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function foreignGroupAmount(array $array): string
|
||||
{
|
||||
// take cue from the first entry in the array:
|
||||
$first = $array['transactions'][0];
|
||||
$type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
||||
$amount = $array['foreign_sum'] ?? '0';
|
||||
$colored = true;
|
||||
if ($type !== TransactionType::WITHDRAWAL) {
|
||||
$amount = bcmul($amount, '-1');
|
||||
}
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($first['foreign_currency_symbol'], (int)$first['foreign_currency_decimal_places'], $amount, $colored);
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$result = sprintf('<span class="text-info">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate normal amount for transaction from a transaction group.
|
||||
*
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function normalAmount(array $array): string
|
||||
{
|
||||
$type = $array['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
||||
$amount = $array['amount'] ?? '0';
|
||||
$colored = true;
|
||||
if ($type !== TransactionType::WITHDRAWAL) {
|
||||
$amount = bcmul($amount, '-1');
|
||||
}
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$colored = false;
|
||||
}
|
||||
$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);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function normalGroupAmount(array $array): string
|
||||
{
|
||||
// take cue from the first entry in the array:
|
||||
$first = $array['transactions'][0];
|
||||
$type = $first['transaction_type_type'] ?? TransactionType::WITHDRAWAL;
|
||||
$amount = $array['sum'] ?? '0';
|
||||
$colored = true;
|
||||
if ($type !== TransactionType::WITHDRAWAL) {
|
||||
$amount = bcmul($amount, '-1');
|
||||
}
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$colored = false;
|
||||
}
|
||||
$result = app('amount')->formatFlat($first['currency_symbol'], (int)$first['currency_decimal_places'], $amount, $colored);
|
||||
if ($type === TransactionType::TRANSFER) {
|
||||
$result = sprintf('<span class="text-info">%s</span>', $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user