2015-02-07 06:49:24 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
{
|
2015-02-07 23:19:28 +01:00
|
|
|
/**
|
|
|
|
* @param $amount
|
|
|
|
* @param bool $coloured
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function format($amount, $coloured = true)
|
|
|
|
{
|
|
|
|
$currencySymbol = $this->getCurrencySymbol();
|
|
|
|
|
|
|
|
return $this->formatWithSymbol($currencySymbol, $amount, $coloured);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-07 06:49:24 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-03-15 09:34:57 +01:00
|
|
|
public function getCurrencySymbol()
|
2015-02-07 06:49:24 +01:00
|
|
|
{
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty('getCurrencySymbol');
|
|
|
|
if ($cache->has()) {
|
|
|
|
return $cache->get();
|
|
|
|
} else {
|
2015-11-22 11:30:06 +01:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY','EUR'));
|
2015-07-10 07:39:59 +02:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
2015-06-13 10:02:36 +02:00
|
|
|
|
2015-07-10 07:39:59 +02:00
|
|
|
$cache->store($currency->symbol);
|
|
|
|
|
|
|
|
return $currency->symbol;
|
|
|
|
}
|
2015-02-07 06:49:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $symbol
|
|
|
|
* @param float $amount
|
|
|
|
* @param bool $coloured
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function formatWithSymbol($symbol, $amount, $coloured = true)
|
|
|
|
{
|
|
|
|
$amount = floatval($amount);
|
|
|
|
$amount = round($amount, 2);
|
2015-11-22 11:25:15 +01:00
|
|
|
$string = money_format('%!.2n', $amount);
|
2015-02-07 06:49:24 +01:00
|
|
|
|
|
|
|
if ($coloured === true) {
|
|
|
|
if ($amount === 0.0) {
|
2015-07-19 12:20:35 +02:00
|
|
|
return '<span style="color:#999">' . $symbol . ' ' . $string . '</span>';
|
2015-02-07 06:49:24 +01:00
|
|
|
}
|
|
|
|
if ($amount > 0) {
|
2015-07-19 12:20:35 +02:00
|
|
|
return '<span class="text-success">' . $symbol . ' ' . $string . '</span>';
|
2015-02-07 06:49:24 +01:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:20:35 +02:00
|
|
|
return '<span class="text-danger">' . $symbol . ' ' . $string . '</span>';
|
2015-02-07 06:49:24 +01:00
|
|
|
}
|
|
|
|
|
2015-07-19 12:20:35 +02:00
|
|
|
return $symbol . ' ' . $string;
|
2015-02-07 06:49:24 +01:00
|
|
|
}
|
2015-02-07 08:23:44 +01:00
|
|
|
|
2015-02-07 23:19:28 +01:00
|
|
|
/**
|
2015-03-15 09:34:57 +01:00
|
|
|
*
|
|
|
|
* @param TransactionJournal $journal
|
2015-05-05 10:23:01 +02:00
|
|
|
* @param bool $coloured
|
|
|
|
*
|
|
|
|
* @return string
|
2015-02-07 23:19:28 +01:00
|
|
|
*/
|
2015-03-15 09:34:57 +01:00
|
|
|
public function formatJournal(TransactionJournal $journal, $coloured = true)
|
2015-02-07 23:19:28 +01:00
|
|
|
{
|
2015-06-03 21:58:06 +02:00
|
|
|
$cache = new CacheProperties;
|
|
|
|
$cache->addProperty($journal->id);
|
|
|
|
$cache->addProperty('formatJournal');
|
|
|
|
|
|
|
|
if ($cache->has()) {
|
2015-06-04 21:35:36 +02:00
|
|
|
return $cache->get(); // @codeCoverageIgnore
|
2015-06-03 21:58:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-20 22:39:07 +01:00
|
|
|
if (is_null($journal->symbol)) {
|
|
|
|
$symbol = $journal->transactionCurrency->symbol;
|
|
|
|
} else {
|
|
|
|
$symbol = $journal->symbol;
|
2015-02-07 23:19:28 +01:00
|
|
|
}
|
2015-09-30 19:31:21 +02:00
|
|
|
|
2015-12-09 22:39:50 -02:00
|
|
|
if ($journal->isTransfer() && $coloured) {
|
2015-09-30 19:31:21 +02:00
|
|
|
$txt = '<span class="text-info">' . $this->formatWithSymbol($symbol, $journal->amount_positive, false) . '</span>';
|
2015-06-03 21:58:06 +02:00
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
2015-03-20 22:39:07 +01:00
|
|
|
}
|
2015-12-09 22:39:50 -02:00
|
|
|
if ($journal->isTransfer() && !$coloured) {
|
2015-09-30 19:31:21 +02:00
|
|
|
$txt = $this->formatWithSymbol($symbol, $journal->amount_positive, false);
|
2015-06-03 21:58:06 +02:00
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
2015-02-07 23:19:28 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 19:31:21 +02:00
|
|
|
$txt = $this->formatWithSymbol($symbol, $journal->amount, $coloured);
|
2015-06-03 21:58:06 +02:00
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
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
|
|
|
|
*/
|
|
|
|
public function formatTransaction(Transaction $transaction, $coloured = true)
|
|
|
|
{
|
|
|
|
$symbol = $transaction->transactionJournal->transactionCurrency->symbol;
|
|
|
|
$amount = floatval($transaction->amount);
|
2015-02-07 23:19:28 +01:00
|
|
|
|
2015-03-15 09:34:57 +01:00
|
|
|
return $this->formatWithSymbol($symbol, $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-11-22 11:30:06 +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-11-22 11:30:06 +01:00
|
|
|
$cache->store(env('DEFAULT_CURRENCY','EUR'));
|
2015-02-07 08:23:44 +01:00
|
|
|
|
2015-11-22 11:30:06 +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
|
|
|
|
2015-05-05 10:23:01 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|static
|
|
|
|
*/
|
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
|
|
|
}
|