firefly-iii/app/Support/Amount.php

190 lines
5.5 KiB
PHP
Raw Normal View History

2015-02-06 23:49:24 -06:00
<?php
/**
* Amount.php
* Copyright (C) 2016 thegrumpydictator@gmail.com
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
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;
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-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);
}
/**
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
2016-02-05 02:25:15 -06:00
* @param string $amount
2016-01-09 11:02:36 -06:00
* @param bool $coloured
*
* @return string
*/
2016-02-06 03:17:41 -06:00
public function formatAnything(TransactionCurrency $format, string $amount, bool $coloured = true): string
{
$locale = setlocale(LC_MONETARY, 0);
2016-03-16 11:48:07 -05:00
$float = round($amount, 2);
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
2016-02-05 02:25:15 -06:00
$result = $formatter->formatCurrency($float, $format->code);
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-03-21 13:24:15 -05:00
return '<span class="text-success" title="' . e($float) . '">' . $result . '</span>';
2016-03-21 13:28:22 -05:00
} else {
if ($amount < 0) {
2016-03-21 13:29:24 -05:00
return '<span class="text-danger" title="' . e($float) . '">' . $result . '</span>';
2016-03-21 13:28:22 -05:00
}
2016-01-09 11:02:36 -06:00
}
2016-03-21 13:28:22 -05:00
return '<span style="color:#999" title="' . e($float) . '">' . $result . '</span>';
2016-01-09 11:02:36 -06:00
}
return $result;
}
/**
2015-03-15 03:34:57 -05:00
*
2016-02-23 00:05:18 -06:00
* @param \FireflyIII\Models\TransactionJournal $journal
* @param bool $coloured
2015-05-05 03:23:01 -05:00
*
* @return string
*/
2016-02-06 08:01:26 -06:00
public function formatJournal(TransactionJournal $journal, bool $coloured = true): string
{
$locale = setlocale(LC_MONETARY, 0);
$float = round(TransactionJournal::amount($journal), 2);
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);
$currencyCode = $journal->transaction_currency_code ?? $journal->transactionCurrency->code;
$result = $formatter->formatCurrency($float, $currencyCode);
2015-06-03 14:58:06 -05:00
2016-03-21 13:22:37 -05:00
if ($coloured === true && $float === 0.00) {
return '<span style="color:#999">' . $result . '</span>'; // always grey.
2015-03-20 16:39:07 -05:00
}
if (!$coloured) {
return $result;
}
if (!$journal->isTransfer()) {
if ($float > 0) {
return '<span class="text-success">' . $result . '</span>';
}
return '<span class="text-danger">' . $result . '</span>';
} else {
return '<span class="text-info">' . $result . '</span>';
}
2015-03-15 03:34:57 -05:00
}
2015-03-15 03:34:57 -05:00
/**
* @param Transaction $transaction
* @param bool $coloured
*
* @return string
*/
2016-02-05 02:25:15 -06:00
public function formatTransaction(Transaction $transaction, bool $coloured = true)
2015-03-15 03:34:57 -05:00
{
2016-01-09 11:02:36 -06:00
$currency = $transaction->transactionJournal->transactionCurrency;
return $this->formatAnything($currency, $transaction->amount, $coloured);
}
/**
* @return Collection
*/
2016-04-05 15:00:03 -05:00
public function getAllCurrencies(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @return string
*/
2016-04-05 15:00:03 -05:00
public function getCurrencyCode(): string
{
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'));
2016-04-06 02:27:45 -05:00
return env('DEFAULT_CURRENCY', 'EUR');
2015-07-10 00:39:59 -05: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 {
$currencyPreference = Prefs::get('currencyPreference', env('DEFAULT_CURRENCY', 'EUR'));
$currency = TransactionCurrency::whereCode($currencyPreference->data)->first();
$cache->store($currency->symbol);
return $currency->symbol;
}
}
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
*/
2016-04-05 15:00:03 -05:00
public function getDefaultCurrency(): TransactionCurrency
{
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
}