firefly-iii/app/Support/Amount.php

193 lines
5.2 KiB
PHP
Raw Normal View History

2015-02-06 23:49:24 -06:00
<?php
namespace FireflyIII\Support;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
2015-03-15 03:34:57 -05:00
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
2016-01-09 11:02:36 -06:00
use NumberFormatter;
use Preferences as Prefs;
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-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.
*
* @param TransactionCurrency $format
* @param $amount
* @param bool $coloured
*
* @return string
*/
2016-01-09 11:02:36 -06:00
public function formatAnything(TransactionCurrency $format, $amount, $coloured = true)
{
2016-01-09 11:02:36 -06:00
$locale = setlocale(LC_MONETARY, 0);
$a = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$result = $a->formatCurrency($amount, $format->code);
2016-01-09 11:02:36 -06:00
if ($coloured === true) {
if ($amount == 0) {
return '<span style="color:#999">' . $result . '</span>';
}
if ($amount > 0) {
return '<span class="text-success">' . $result . '</span>';
}
2016-01-09 11:02:36 -06:00
return '<span class="text-danger">' . $result . '</span>';
2016-01-09 11:02:36 -06:00
}
return $result;
}
/**
* @param $amount
* @param bool $coloured
*
* @return string
*/
public function format($amount, $coloured = true)
{
return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
}
2015-02-06 23:49:24 -06:00
/**
* @return string
*/
2015-03-15 03:34:57 -05:00
public function getCurrencySymbol()
2015-02-06 23:49:24 -06:00
{
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty('getCurrencySymbol');
if ($cache->has()) {
return $cache->get();
} else {
2015-12-18 09:38:50 -06:00
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));
2015-07-10 00:39:59 -05:00
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
2015-06-13 03:02:36 -05:00
2015-07-10 00:39:59 -05:00
$cache->store($currency->symbol);
return $currency->symbol;
}
2015-02-06 23:49:24 -06:00
}
/**
* @param string $symbol
* @param float $amount
* @param bool $coloured
*
* @return string
*/
public function formatWithSymbol($symbol, $amount, $coloured = true)
{
2016-01-09 11:02:36 -06:00
return $this->formatAnything($this->getDefaultCurrency(), $amount, $coloured);
2015-02-06 23:49:24 -06:00
}
/**
2015-03-15 03:34:57 -05:00
*
* @param TransactionJournal $journal
2015-05-05 03:23:01 -05:00
* @param bool $coloured
*
* @return string
*/
2015-03-15 03:34:57 -05:00
public function formatJournal(TransactionJournal $journal, $coloured = true)
{
2015-06-03 14:58:06 -05:00
$cache = new CacheProperties;
$cache->addProperty($journal->id);
$cache->addProperty('formatJournal');
if ($cache->has()) {
2015-06-04 14:35:36 -05:00
return $cache->get(); // @codeCoverageIgnore
2015-06-03 14:58:06 -05:00
}
if ($journal->isTransfer() && $coloured) {
2016-01-09 11:02:36 -06:00
$txt = '<span class="text-info">' . $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false) . '</span>';;
2015-06-03 14:58:06 -05:00
$cache->store($txt);
return $txt;
2015-03-20 16:39:07 -05:00
}
if ($journal->isTransfer() && !$coloured) {
2016-01-09 11:02:36 -06:00
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount_positive, false);
2015-06-03 14:58:06 -05:00
$cache->store($txt);
return $txt;
}
2016-01-09 11:02:36 -06:00
$txt = $this->formatAnything($journal->transactionCurrency, $journal->amount, $coloured);
2015-06-03 14:58:06 -05:00
$cache->store($txt);
return $txt;
2015-03-15 03:34:57 -05:00
}
2015-03-15 03:34:57 -05:00
/**
* @param Transaction $transaction
* @param bool $coloured
*
* @return string
*/
public function formatTransaction(Transaction $transaction, $coloured = true)
{
2016-01-09 11:02:36 -06:00
$currency = $transaction->transactionJournal->transactionCurrency;
2016-01-09 11:02:36 -06:00
return $this->formatAnything($currency, $transaction->amount);
}
/**
* @return Collection
*/
public function getAllCurrencies()
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
public function getCurrencyCode()
{
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty('getCurrencyCode');
if ($cache->has()) {
return $cache->get();
} else {
2015-12-18 09:38:50 -06:00
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));
2015-07-10 00:39:59 -05:00
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
if ($currency) {
2015-07-10 00:39:59 -05:00
$cache->store($currency->code);
return $currency->code;
}
2015-12-18 09:38:50 -06:00
$cache->store(env('DEFAULT_CURRENCY', 'EUR'));
2015-12-18 09:38:50 -06:00
return env('DEFAULT_CURRENCY', 'EUR'); // @codeCoverageIgnore
2015-07-10 00:39:59 -05:00
}
}
2015-05-05 03:23:01 -05:00
/**
2016-01-09 11:02:36 -06:00
* @return TransactionCurrency
2015-05-05 03:23:01 -05:00
*/
public function getDefaultCurrency()
{
2015-07-10 00:39:59 -05:00
$cache = new CacheProperties;
$cache->addProperty('getDefaultCurrency');
if ($cache->has()) {
return $cache->get();
}
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
2015-07-10 00:39:59 -05:00
$cache->store($currency);
return $currency;
}
2015-03-29 01:14:32 -05:00
}