firefly-iii/app/Support/Amount.php

167 lines
4.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;
use Preferences as Prefs;
2015-02-06 23:49:24 -06:00
/**
* Class Amount
*
* @package FireflyIII\Support
*/
class Amount
{
/**
* @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>';
}
// &#8364;
return $symbol . ' ' . $string;
}
/**
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
}
2015-03-20 16:39:07 -05:00
if (is_null($journal->symbol)) {
$symbol = $journal->transactionCurrency->symbol;
} else {
$symbol = $journal->symbol;
}
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-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-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-03-15 03:34:57 -05:00
return $this->formatWithSymbol($symbol, $amount, $coloured);
}
/**
* @return Collection
*/
public function getAllCurrencies()
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
public function getCurrencyCode()
{
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
2015-05-05 00:51:02 -05:00
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
if ($currency) {
return $currency->code;
}
2015-06-07 03:28:26 -05:00
return 'EUR'; // @codeCoverageIgnore
}
2015-05-05 03:23:01 -05:00
/**
* @return mixed|static
*/
public function getDefaultCurrency()
{
$currencyPreference = Prefs::get('currencyPreference', 'EUR');
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
return $currency;
}
2015-03-29 01:14:32 -05:00
}