2015-02-07 06:49:24 +01:00
|
|
|
<?php
|
2016-02-05 12:08:25 +01:00
|
|
|
declare(strict_types = 1);
|
2015-02-07 06:49:24 +01:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Transaction;
|
2015-02-07 08:23:44 +01:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2015-03-15 09:34:57 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-04-03 07:33:18 +02:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-09 18:02:36 +01:00
|
|
|
use NumberFormatter;
|
2015-02-07 08:23:44 +01:00
|
|
|
use Preferences as Prefs;
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2015-02-07 06:49:24 +01:00
|
|
|
/**
|
|
|
|
* Class Amount
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class Amount
|
|
|
|
{
|
2016-01-09 18:02:36 +01:00
|
|
|
|
2016-01-20 15:23:36 +01:00
|
|
|
/**
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $amount
|
|
|
|
* @param bool $coloured
|
2016-01-20 15:23:36 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 10:17:41 +01:00
|
|
|
public function format(string $amount, bool $coloured = true): string
|
2016-01-20 15:23:36 +01:00
|
|
|
{
|
|
|
|
return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:19:28 +01:00
|
|
|
/**
|
2016-01-09 18:02:36 +01:00
|
|
|
* 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 TransactionCurrency $format
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $amount
|
2016-01-09 18:02:36 +01:00
|
|
|
* @param bool $coloured
|
2015-02-07 23:19:28 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 10:17:41 +01:00
|
|
|
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string
|
2015-02-07 23:19:28 +01:00
|
|
|
{
|
2016-01-15 18:21:59 +01:00
|
|
|
$locale = setlocale(LC_MONETARY, 0);
|
2016-03-16 17:48:07 +01:00
|
|
|
$float = round($amount, 2);
|
2016-01-15 18:21:59 +01:00
|
|
|
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
2016-02-05 09:25:15 +01:00
|
|
|
$result = $formatter->formatCurrency($float, $format->code);
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2016-01-09 18:02:36 +01:00
|
|
|
if ($coloured === true) {
|
2016-03-21 19:28:22 +01:00
|
|
|
|
2016-01-09 18:02:36 +01:00
|
|
|
if ($amount > 0) {
|
2016-03-21 19:24:15 +01:00
|
|
|
return '<span class="text-success" title="' . e($float) . '">' . $result . '</span>';
|
2016-03-21 19:28:22 +01:00
|
|
|
} else {
|
|
|
|
if ($amount < 0) {
|
2016-03-21 19:29:24 +01:00
|
|
|
return '<span class="text-danger" title="' . e($float) . '">' . $result . '</span>';
|
2016-03-21 19:28:22 +01:00
|
|
|
}
|
2016-01-09 18:02:36 +01:00
|
|
|
}
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2016-03-21 19:28:22 +01:00
|
|
|
return '<span style="color:#999" title="' . e($float) . '">' . $result . '</span>';
|
|
|
|
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2016-01-09 18:02:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-02-07 23:19:28 +01:00
|
|
|
/**
|
2015-03-15 09:34:57 +01:00
|
|
|
*
|
2016-02-23 07:05:18 +01:00
|
|
|
* @param \FireflyIII\Models\TransactionJournal $journal
|
|
|
|
* @param bool $coloured
|
2015-05-05 10:23:01 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2015-02-07 23:19:28 +01:00
|
|
|
*/
|
2016-02-06 15:01:26 +01:00
|
|
|
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
|
2015-02-07 23:19:28 +01:00
|
|
|
{
|
2016-03-20 16:41:48 +01:00
|
|
|
$locale = setlocale(LC_MONETARY, 0);
|
|
|
|
$float = round(TransactionJournal::amount($journal), 2);
|
2016-03-07 19:52:59 +01:00
|
|
|
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
|
|
|
$currencyCode = $journal->transaction_currency_code ?? $journal->transactionCurrency->code;
|
|
|
|
$result = $formatter->formatCurrency($float, $currencyCode);
|
2015-06-03 21:58:06 +02:00
|
|
|
|
2016-03-21 19:22:37 +01:00
|
|
|
if ($coloured === true && $float === 0.00) {
|
2016-03-02 12:25:00 +01:00
|
|
|
return '<span style="color:#999">' . $result . '</span>'; // always grey.
|
2015-03-20 22:39:07 +01:00
|
|
|
}
|
2016-03-02 12:25:00 +01:00
|
|
|
if (!$coloured) {
|
|
|
|
return $result;
|
2015-02-07 23:19:28 +01:00
|
|
|
}
|
2016-03-02 12:25:00 +01:00
|
|
|
if (!$journal->isTransfer()) {
|
|
|
|
if ($float > 0) {
|
|
|
|
return '<span class="text-success">' . $result . '</span>';
|
|
|
|
}
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2016-03-02 12:25:00 +01:00
|
|
|
return '<span class="text-danger">' . $result . '</span>';
|
|
|
|
} else {
|
|
|
|
return '<span class="text-info">' . $result . '</span>';
|
|
|
|
}
|
2015-03-15 09:34:57 +01:00
|
|
|
}
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2015-03-15 09:34:57 +01:00
|
|
|
/**
|
|
|
|
* @param Transaction $transaction
|
|
|
|
* @param bool $coloured
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-05 09:25:15 +01:00
|
|
|
public function formatTransaction(Transaction $transaction, bool $coloured = true)
|
2015-03-15 09:34:57 +01:00
|
|
|
{
|
2016-01-09 18:02:36 +01:00
|
|
|
$currency = $transaction->transactionJournal->transactionCurrency;
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2016-01-15 17:38:09 +01:00
|
|
|
return $this->formatAnything($currency, $transaction->amount, $coloured);
|
2015-02-07 23:19:28 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 07:33:18 +02:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAllCurrencies()
|
|
|
|
{
|
|
|
|
return TransactionCurrency::orderBy('code', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2015-02-07 08:23:44 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCurrencyCode()
|
|
|
|
{
|
|
|
|
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getCurrencyCode');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
} else {
|
2015-12-18 16:38:50 +01:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));
|
2015-05-03 16:16:43 +02:00
|
|
|
|
2015-07-10 07:39:59 +02:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
if ($currency) {
|
2015-02-07 08:23:44 +01:00
|
|
|
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache->store($currency->code);
|
|
|
|
|
|
|
|
return $currency->code;
|
|
|
|
}
|
2015-12-18 16:38:50 +01:00
|
|
|
$cache->store(env('DEFAULT_CURRENCY', 'EUR'));
|
2015-02-07 08:23:44 +01:00
|
|
|
|
2015-12-18 16:38:50 +01:00
|
|
|
return env('DEFAULT_CURRENCY', 'EUR'); // @codeCoverageIgnore
|
2015-07-10 07:39:59 +02:00
|
|
|
}
|
2015-02-07 08:23:44 +01:00
|
|
|
}
|
2015-02-08 01:15:15 +01:00
|
|
|
|
2016-01-20 15:23:36 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCurrencySymbol()
|
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getCurrencySymbol');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
} else {
|
|
|
|
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));
|
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
|
|
|
|
$cache->store($currency->symbol);
|
|
|
|
|
|
|
|
return $currency->symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:23:01 +02:00
|
|
|
/**
|
2016-01-09 18:02:36 +01:00
|
|
|
* @return TransactionCurrency
|
2015-05-05 10:23:01 +02:00
|
|
|
*/
|
2015-02-08 01:15:15 +01:00
|
|
|
public function getDefaultCurrency()
|
|
|
|
{
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getDefaultCurrency');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
}
|
2015-02-08 01:15:15 +01:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
|
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache->store($currency);
|
2015-02-08 01:15:15 +01:00
|
|
|
|
|
|
|
return $currency;
|
|
|
|
}
|
2015-03-29 08:14:32 +02:00
|
|
|
}
|