mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2026-01-07 14:41:20 +00:00
Clean up code and comments.
This commit is contained in:
@@ -74,60 +74,6 @@ trait ConvertsExchangeRates
|
||||
return $set;
|
||||
}
|
||||
|
||||
/**
|
||||
* For a sum of entries, get the exchange rate to the native currency of
|
||||
* the user.
|
||||
* @param array $entries
|
||||
* @return array
|
||||
*/
|
||||
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']);
|
||||
if ($currency->id !== $native->id) {
|
||||
$amount = $this->convertAmount($entry['sum'], $currency, $native);
|
||||
$entry['converted'] = true;
|
||||
$entry['native_sum'] = $amount;
|
||||
$entry['native_id'] = (string)$native->id;
|
||||
$entry['native_name'] = $native->name;
|
||||
$entry['native_symbol'] = $native->symbol;
|
||||
$entry['native_code'] = $native->code;
|
||||
$entry['native_decimal_places'] = $native->decimal_places;
|
||||
}
|
||||
if ($currency->id === $native->id) {
|
||||
$entry['converted'] = false;
|
||||
$entry['native_sum'] = $entry['sum'];
|
||||
$entry['native_id'] = (string)$native->id;
|
||||
$entry['native_name'] = $native->name;
|
||||
$entry['native_symbol'] = $native->symbol;
|
||||
$entry['native_code'] = $native->code;
|
||||
$entry['native_decimal_places'] = $native->decimal_places;
|
||||
}
|
||||
$return[] = $entry;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
@@ -149,15 +95,6 @@ trait ConvertsExchangeRates
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string
|
||||
{
|
||||
Log::debug(sprintf('Converting %s from %s to %s', $amount, $from->code, $to->code));
|
||||
$date = $date ?? today(config('app.timezone'));
|
||||
$rate = $this->getRate($from, $to, $date);
|
||||
|
||||
return bcmul($amount, $rate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TransactionCurrency $from
|
||||
* @param TransactionCurrency $to
|
||||
@@ -260,4 +197,67 @@ trait ConvertsExchangeRates
|
||||
Log::debug(sprintf('No rate for %s to EUR.', $currency->code));
|
||||
return '0';
|
||||
}
|
||||
|
||||
/**
|
||||
* For a sum of entries, get the exchange rate to the native currency of
|
||||
* the user.
|
||||
* @param array $entries
|
||||
* @return array
|
||||
*/
|
||||
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']);
|
||||
if ($currency->id !== $native->id) {
|
||||
$amount = $this->convertAmount($entry['sum'], $currency, $native);
|
||||
$entry['converted'] = true;
|
||||
$entry['native_sum'] = $amount;
|
||||
$entry['native_id'] = (string)$native->id;
|
||||
$entry['native_name'] = $native->name;
|
||||
$entry['native_symbol'] = $native->symbol;
|
||||
$entry['native_code'] = $native->code;
|
||||
$entry['native_decimal_places'] = $native->decimal_places;
|
||||
}
|
||||
if ($currency->id === $native->id) {
|
||||
$entry['converted'] = false;
|
||||
$entry['native_sum'] = $entry['sum'];
|
||||
$entry['native_id'] = (string)$native->id;
|
||||
$entry['native_name'] = $native->name;
|
||||
$entry['native_symbol'] = $native->symbol;
|
||||
$entry['native_code'] = $native->code;
|
||||
$entry['native_decimal_places'] = $native->decimal_places;
|
||||
}
|
||||
$return[] = $entry;
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
private function convertAmount(string $amount, TransactionCurrency $from, TransactionCurrency $to, ?Carbon $date = null): string
|
||||
{
|
||||
Log::debug(sprintf('Converting %s from %s to %s', $amount, $from->code, $to->code));
|
||||
$date = $date ?? today(config('app.timezone'));
|
||||
$rate = $this->getRate($from, $to, $date);
|
||||
|
||||
return bcmul($amount, $rate);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user