2015-02-06 23:49:24 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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;
|
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
|
|
|
|
{
|
2015-02-07 16:19:28 -06: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-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-03-15 03:34:57 -05:00
|
|
|
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
|
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$amount = floatval($amount);
|
|
|
|
$amount = round($amount, 2);
|
|
|
|
$string = number_format($amount, 2, ',', '.');
|
|
|
|
|
|
|
|
if ($coloured === true) {
|
|
|
|
if ($amount === 0.0) {
|
|
|
|
return '<span style="color:#999">' . $symbol . ' ' . $string . '</span>';
|
|
|
|
}
|
|
|
|
if ($amount > 0) {
|
|
|
|
return '<span class="text-success">' . $symbol . ' ' . $string . '</span>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return '<span class="text-danger">' . $symbol . ' ' . $string . '</span>';
|
|
|
|
}
|
|
|
|
|
|
|
|
// €
|
|
|
|
return $symbol . ' ' . $string;
|
|
|
|
}
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-02-07 16:19:28 -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-02-07 16:19:28 -06:00
|
|
|
*/
|
2015-03-15 03:34:57 -05:00
|
|
|
public function formatJournal(TransactionJournal $journal, $coloured = true)
|
2015-02-07 16:19:28 -06:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-20 16:39:07 -05:00
|
|
|
if (is_null($journal->symbol)) {
|
|
|
|
$symbol = $journal->transactionCurrency->symbol;
|
|
|
|
} else {
|
|
|
|
$symbol = $journal->symbol;
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|
2015-05-17 09:12:00 -05:00
|
|
|
$amount = $journal->amount;
|
|
|
|
if ($journal->transactionType->type == 'Withdrawal') {
|
|
|
|
$amount = $amount * -1;
|
2015-03-20 16:39:07 -05:00
|
|
|
}
|
2015-05-17 09:12:00 -05:00
|
|
|
if ($journal->transactionType->type == 'Transfer' && $coloured) {
|
2015-06-03 14:58:06 -05:00
|
|
|
$txt = '<span class="text-info">' . $this->formatWithSymbol($symbol, $amount, false) . '</span>';
|
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
2015-03-20 16:39:07 -05:00
|
|
|
}
|
2015-05-17 09:12:00 -05:00
|
|
|
if ($journal->transactionType->type == 'Transfer' && !$coloured) {
|
2015-06-03 14:58:06 -05:00
|
|
|
$txt = $this->formatWithSymbol($symbol, $amount, false);
|
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|
|
|
|
|
2015-06-03 14:58:06 -05:00
|
|
|
$txt = $this->formatWithSymbol($symbol, $amount, $coloured);
|
|
|
|
$cache->store($txt);
|
|
|
|
|
|
|
|
return $txt;
|
2015-03-15 03:34:57 -05:00
|
|
|
}
|
2015-02-07 16:19:28 -06:00
|
|
|
|
2015-03-15 03:34:57 -05: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 16:19:28 -06:00
|
|
|
|
2015-03-15 03:34:57 -05:00
|
|
|
return $this->formatWithSymbol($symbol, $amount, $coloured);
|
2015-02-07 16:19:28 -06:00
|
|
|
}
|
|
|
|
|
2015-04-03 00:33:18 -05:00
|
|
|
/**
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getAllCurrencies()
|
|
|
|
{
|
|
|
|
return TransactionCurrency::orderBy('code', 'ASC')->get();
|
|
|
|
}
|
|
|
|
|
2015-02-07 01:23:44 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCurrencyCode()
|
|
|
|
{
|
|
|
|
|
|
|
|
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
|
2015-05-03 09:16:43 -05:00
|
|
|
|
2015-05-05 00:51:02 -05:00
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
2015-05-02 12:44:12 -05:00
|
|
|
if ($currency) {
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-05-02 12:44:12 -05:00
|
|
|
return $currency->code;
|
|
|
|
}
|
2015-02-07 01:23:44 -06:00
|
|
|
|
2015-06-07 03:28:26 -05:00
|
|
|
return 'EUR'; // @codeCoverageIgnore
|
2015-02-07 01:23:44 -06:00
|
|
|
}
|
2015-02-07 18:15:15 -06:00
|
|
|
|
2015-05-05 03:23:01 -05:00
|
|
|
/**
|
|
|
|
* @return mixed|static
|
|
|
|
*/
|
2015-02-07 18:15:15 -06:00
|
|
|
public function getDefaultCurrency()
|
|
|
|
{
|
|
|
|
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
|
|
|
|
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
|
|
|
|
|
|
|
|
return $currency;
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|