Currencies can now be enabled and disabled.

This commit is contained in:
James Cole
2018-11-10 10:04:46 +01:00
parent daa8aa5c9d
commit e491dda229
25 changed files with 457 additions and 51 deletions

View File

@@ -24,6 +24,7 @@ namespace FireflyIII\Repositories\Currency;
use Carbon\Carbon;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
@@ -55,39 +56,62 @@ class CurrencyRepository implements CurrencyRepositoryInterface
/**
* @param TransactionCurrency $currency
*
* @return bool
* @return int
*/
public function canDeleteCurrency(TransactionCurrency $currency): bool
public function countJournals(TransactionCurrency $currency): int
{
if ($this->countJournals($currency) > 0) {
return false;
}
// is the only currency left
if (1 === $this->get()->count()) {
return false;
}
// is the default currency for the user or the system
$defaultCode = app('preferences')->getForUser($this->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data;
if ($currency->code === $defaultCode) {
return false;
}
// is the default currency for the system
$defaultSystemCode = config('firefly.default_currency', 'EUR');
return !($currency->code === $defaultSystemCode);
return $currency->transactions()->count();
}
/**
* @param TransactionCurrency $currency
*
* @return int
* @return bool
*/
public function countJournals(TransactionCurrency $currency): int
public function currencyInUse(TransactionCurrency $currency): bool
{
return $currency->transactionJournals()->count();
Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code));
$countJournals = $this->countJournals($currency);
if ($countJournals > 0) {
Log::debug(sprintf('Count journals is %d, return true.', $countJournals));
return true;
}
// is the only currency left
if (1 === $this->getAll()->count()) {
Log::debug('Is the last currency in the system, return true. ', $countJournals);
return true;
}
// is being used in accounts:
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count();
if ($meta > 0) {
Log::debug(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return true;
}
// is the default currency for the user or the system
$defaultCode = app('preferences')->getForUser($this->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data;
if ($currency->code === $defaultCode) {
Log::debug('Is the default currency of the user, return true.');
return true;
}
// is the default currency for the system
$defaultSystemCode = config('firefly.default_currency', 'EUR');
$result = $currency->code === $defaultSystemCode;
if (true === $result) {
Log::debug('Is the default currency of the SYSTEM, return true.');
return true;
}
Log::debug('Currency is not used, return false.');
return false;
}
/**
@@ -110,12 +134,58 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return true;
}
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void
{
$currency->enabled = false;
$currency->save();
}
/**
* @param TransactionCurrency $currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
$currency->enabled = true;
$currency->save();
}
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function find(int $currencyId): ?TransactionCurrency
{
return TransactionCurrency::find($currencyId);
}
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCode(string $currencyCode): ?TransactionCurrency
{
return TransactionCurrency::where('code', $currencyCode)->first();
}
/**
* Find by currency code, return NULL if unfound.
* Used in Import Currency!
*
* @param string $currencyCode
*
* @deprecated
* @return TransactionCurrency|null
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency
@@ -123,12 +193,25 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return TransactionCurrency::where('code', $currencyCode)->first();
}
/**
* Find by currency name.
*
* @param string $currencyName
*
* @return TransactionCurrency
*/
public function findByName(string $currencyName): ?TransactionCurrency
{
return TransactionCurrency::whereName($currencyName)->first();
}
/**
* Find by currency name or return null.
* Used in Import Currency!
*
* @param string $currencyName
*
* @deprecated
* @return TransactionCurrency
*/
public function findByNameNull(string $currencyName): ?TransactionCurrency
@@ -136,12 +219,25 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return TransactionCurrency::whereName($currencyName)->first();
}
/**
* Find by currency symbol.
*
* @param string $currencySymbol
*
* @return TransactionCurrency
*/
public function findBySymbol(string $currencySymbol): ?TransactionCurrency
{
return TransactionCurrency::whereSymbol($currencySymbol)->first();
}
/**
* Find by currency symbol or return NULL
* Used in Import Currency!
*
* @param string $currencySymbol
*
* @deprecated
* @return TransactionCurrency
*/
public function findBySymbolNull(string $currencySymbol): ?TransactionCurrency
@@ -155,6 +251,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
*
* @param int $currencyId
*
* @deprecated
* @return TransactionCurrency|null
*/
public function findNull(int $currencyId): ?TransactionCurrency
@@ -166,6 +263,14 @@ class CurrencyRepository implements CurrencyRepositoryInterface
* @return Collection
*/
public function get(): Collection
{
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
}
/**
* @return Collection
*/
public function getAll(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}

View File

@@ -39,7 +39,7 @@ interface CurrencyRepositoryInterface
*
* @return bool
*/
public function canDeleteCurrency(TransactionCurrency $currency): bool;
public function currencyInUse(TransactionCurrency $currency): bool;
/**
* @param TransactionCurrency $currency
@@ -55,6 +55,38 @@ interface CurrencyRepositoryInterface
*/
public function destroy(TransactionCurrency $currency): bool;
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void;
/**
* Enables a currency
*
* @param TransactionCurrency $currency
*/
public function enable(TransactionCurrency $currency): void;
/**
* Find by ID, return NULL if not found.
*
* @param int $currencyId
*
* @return TransactionCurrency|null
*/
public function find(int $currencyId): ?TransactionCurrency;
/**
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCode(string $currencyCode): ?TransactionCurrency;
/**
* Find by currency code, return NULL if unfound.
*
@@ -64,6 +96,15 @@ interface CurrencyRepositoryInterface
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency;
/**
* Find by currency name.
*
* @param string $currencyName
*
* @return TransactionCurrency
*/
public function findByName(string $currencyName): ?TransactionCurrency;
/**
* Find by currency name.
*
@@ -73,6 +114,15 @@ interface CurrencyRepositoryInterface
*/
public function findByNameNull(string $currencyName): ?TransactionCurrency;
/**
* Find by currency symbol.
*
* @param string $currencySymbol
*
* @return TransactionCurrency
*/
public function findBySymbol(string $currencySymbol): ?TransactionCurrency;
/**
* Find by currency symbol.
*
@@ -96,6 +146,11 @@ interface CurrencyRepositoryInterface
*/
public function get(): Collection;
/**
* @return Collection
*/
public function getAll(): Collection;
/**
* @param array $ids
*