2015-02-06 23:49:24 -06:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* Amount.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
2016-02-05 05:08:25 -06:00
|
|
|
declare(strict_types = 1);
|
2015-02-06 23:49:24 -06:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
use FireflyIII\Models\Transaction;
|
2015-02-07 01:23:44 -06:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2015-03-15 03:34:57 -05:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2015-04-03 00:33:18 -05:00
|
|
|
use Illuminate\Support\Collection;
|
2016-01-09 11:02:36 -06:00
|
|
|
use NumberFormatter;
|
2015-02-07 01:23:44 -06:00
|
|
|
use Preferences as Prefs;
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2015-02-06 23:49:24 -06:00
|
|
|
/**
|
|
|
|
* Class Amount
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class Amount
|
|
|
|
{
|
2016-01-09 11:02:36 -06:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
2016-02-05 02:25:15 -06:00
|
|
|
* @param string $amount
|
|
|
|
* @param bool $coloured
|
2016-01-20 08:23:36 -06:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 03:17:41 -06:00
|
|
|
public function format(string $amount, bool $coloured = true): string
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
|
|
|
return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:19:28 -06:00
|
|
|
/**
|
2016-01-09 11:02:36 -06: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.
|
|
|
|
*
|
2016-12-29 02:02:23 -06:00
|
|
|
* @param \FireflyIII\Models\TransactionCurrency $format
|
|
|
|
* @param string $amount
|
|
|
|
* @param bool $coloured
|
2015-02-07 16:19:28 -06:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 03:17:41 -06:00
|
|
|
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string
|
2015-02-07 16:19:28 -06:00
|
|
|
{
|
2016-01-15 11:21:59 -06:00
|
|
|
$locale = setlocale(LC_MONETARY, 0);
|
2016-12-29 02:02:23 -06:00
|
|
|
$float = round($amount, 12);
|
2016-01-15 11:21:59 -06:00
|
|
|
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
|
2016-02-05 02:25:15 -06:00
|
|
|
$result = $formatter->formatCurrency($float, $format->code);
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2016-01-09 11:02:36 -06:00
|
|
|
if ($coloured === true) {
|
2016-03-21 13:28:22 -05:00
|
|
|
|
2016-01-09 11:02:36 -06:00
|
|
|
if ($amount > 0) {
|
2016-11-02 01:04:14 -05:00
|
|
|
return sprintf('<span class="text-success">%s</span>', $result);
|
2016-03-21 13:28:22 -05:00
|
|
|
} else {
|
|
|
|
if ($amount < 0) {
|
2016-11-02 01:04:14 -05:00
|
|
|
return sprintf('<span class="text-danger">%s</span>', $result);
|
2016-03-21 13:28:22 -05:00
|
|
|
}
|
2016-01-09 11:02:36 -06:00
|
|
|
}
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2016-11-02 01:04:14 -05:00
|
|
|
return sprintf('<span style="color:#999">%s</span>', $result);
|
2016-03-21 13:28:22 -05:00
|
|
|
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2016-01-09 11:02:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-02-07 16:19:28 -06:00
|
|
|
/**
|
2016-12-29 02:02:23 -06:00
|
|
|
* Used in many places (unfortunately).
|
2015-03-15 03:34:57 -05:00
|
|
|
*
|
2016-12-29 02:02:23 -06:00
|
|
|
* @param string $currencyCode
|
|
|
|
* @param string $amount
|
|
|
|
* @param bool $coloured
|
2015-05-05 03:23:01 -05:00
|
|
|
*
|
|
|
|
* @return string
|
2015-02-07 16:19:28 -06:00
|
|
|
*/
|
2016-12-29 02:02:23 -06:00
|
|
|
public function formatByCode(string $currencyCode, string $amount, bool $coloured = true): string
|
2015-02-07 16:19:28 -06:00
|
|
|
{
|
2016-12-29 02:02:23 -06:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyCode)->first();
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2016-12-29 02:02:23 -06:00
|
|
|
return $this->formatAnything($currency, $amount, $coloured);
|
2015-03-15 03:34:57 -05:00
|
|
|
}
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2015-03-15 03:34:57 -05:00
|
|
|
/**
|
2016-12-29 02:02:23 -06:00
|
|
|
*
|
|
|
|
* @param \FireflyIII\Models\TransactionJournal $journal
|
|
|
|
* @param bool $coloured
|
2015-03-15 03:34:57 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-29 02:02:23 -06:00
|
|
|
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
|
2015-03-15 03:34:57 -05:00
|
|
|
{
|
2016-12-29 02:02:23 -06:00
|
|
|
$currency = $journal->transactionCurrency;
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2016-12-29 02:02:23 -06:00
|
|
|
return $this->formatAnything($currency, TransactionJournal::amount($journal), $coloured);
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|
|
|
|
|
2016-10-09 14:36:03 -05:00
|
|
|
/**
|
2016-12-29 02:02:23 -06:00
|
|
|
* @param Transaction $transaction
|
|
|
|
* @param bool $coloured
|
2016-10-09 14:36:03 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-12-29 02:02:23 -06:00
|
|
|
public function formatTransaction(Transaction $transaction, bool $coloured = true)
|
2016-10-09 14:36:03 -05:00
|
|
|
{
|
2016-12-29 02:02:23 -06:00
|
|
|
$currency = $transaction->transactionJournal->transactionCurrency;
|
2016-10-09 14:36:03 -05:00
|
|
|
|
2016-12-29 02:02:23 -06:00
|
|
|
return $this->formatAnything($currency, strval($transaction->amount), $coloured);
|
2016-10-09 14:36:03 -05:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:33:18 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getAllCurrencies(): Collection
|
2015-04-03 00:33:18 -05:00
|
|
|
{
|
|
|
|
return TransactionCurrency::orderBy('code', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:23:44 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getCurrencyCode(): string
|
2015-02-07 01:23:44 -06:00
|
|
|
{
|
|
|
|
|
2015-07-10 00:39:59 -05:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getCurrencyCode');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
} else {
|
2016-09-18 12:57:21 -05:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
2015-05-03 09:16:43 -05:00
|
|
|
|
2015-07-10 00:39:59 -05:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
if ($currency) {
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-07-10 00:39:59 -05:00
|
|
|
$cache->store($currency->code);
|
|
|
|
|
|
|
|
return $currency->code;
|
|
|
|
}
|
2016-09-18 12:57:21 -05:00
|
|
|
$cache->store(config('firefly.default_currency', 'EUR'));
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2016-09-18 12:57:21 -05:00
|
|
|
return config('firefly.default_currency', 'EUR');
|
2015-07-10 00:39:59 -05:00
|
|
|
}
|
2015-02-07 01:23:44 -06:00
|
|
|
}
|
2015-02-07 18:15:15 -06:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getCurrencySymbol(): string
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getCurrencySymbol');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
} else {
|
2016-09-18 12:57:21 -05:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
2016-01-20 08:23:36 -06:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
|
|
|
|
$cache->store($currency->symbol);
|
|
|
|
|
|
|
|
return $currency->symbol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 03:23:01 -05:00
|
|
|
/**
|
2016-09-13 12:19:58 -05:00
|
|
|
* @return \FireflyIII\Models\TransactionCurrency
|
2015-05-05 03:23:01 -05:00
|
|
|
*/
|
2016-04-05 15:00:03 -05:00
|
|
|
public function getDefaultCurrency(): TransactionCurrency
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
2015-07-10 00:39:59 -05:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getDefaultCurrency');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
}
|
2016-09-18 12:57:21 -05:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', config('firefly.default_currency', 'EUR'));
|
2015-02-07 18:15:15 -06:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
2015-07-10 00:39:59 -05:00
|
|
|
$cache->store($currency);
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
return $currency;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|