Preference can be turned on and off (is always off now)

This commit is contained in:
James Cole
2022-06-07 19:24:08 +02:00
parent 46e97f95f9
commit c3bd46d7c0
8 changed files with 473 additions and 4 deletions

View File

@@ -22,6 +22,7 @@
namespace FireflyIII\Support\Http\Api;
use Carbon\Carbon;
use DateTimeInterface;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\TransactionCurrency;
use Log;
@@ -31,6 +32,8 @@ use Log;
*/
trait ConvertsExchangeRates
{
private ?bool $enabled = null;
/**
* For a sum of entries, get the exchange rate to the native currency of
* the user.
@@ -39,12 +42,29 @@ trait ConvertsExchangeRates
*/
public function cerSum(array $entries): array
{
if (null === $this->enabled) {
$this->getPreference();
}
// if false, return the same array without conversion info
if (false === $this->enabled) {
$return = [];
/** @var array $entry */
foreach ($entries as $entry) {
$entry['converted'] = false;
$return[] = $entry;
}
return $return;
}
/** @var TransactionCurrency $native */
$native = app('amount')->getDefaultCurrency();
$return = [];
/** @var array $entry */
foreach ($entries as $entry) {
$currency = $this->getCurrency((int) $entry['id']);
$currency = $this->getCurrency((int) $entry['id']);
$entry['converted'] = true;
if ($currency->id !== $native->id) {
$amount = $this->convertAmount($entry['sum'], $currency, $native);
$entry['native_sum'] = $amount;
@@ -68,6 +88,42 @@ trait ConvertsExchangeRates
return $return;
}
/**
* @param array $set
* @return array
*/
public function cerChartSet(array $set): array
{
if (null === $this->enabled) {
$this->getPreference();
}
// if not enabled, return the same array but without conversion:
if (false === $this->enabled) {
$set['converted'] = false;
return $set;
}
$set['converted'] = true;
/** @var TransactionCurrency $native */
$native = app('amount')->getDefaultCurrency();
$currency = $this->getCurrency((int) $set['currency_id']);
if ($native->id === $currency->id) {
$set['native_id'] = (string) $currency->id;
$set['native_code'] = $currency->code;
$set['native_symbol'] = $currency->symbol;
$set['native_decimal_places'] = $currency->decimal_places;
return $set;
}
foreach ($set['entries'] as $date => $entry) {
$carbon = Carbon::createFromFormat(DateTimeInterface::ATOM, $date);
$rate = $this->getRate($currency, $native, $carbon);
$rate = '0' === $rate ? '1' : $rate;
$set['entries'][$date] = (float) bcmul((string) $entry, $rate);
}
return $set;
}
/**
* @param int $currencyId
* @return TransactionCurrency
@@ -199,4 +255,12 @@ trait ConvertsExchangeRates
return '0';
}
/**
* @return void
*/
private function getPreference(): void
{
$this->enabled = false;
}
}