Inform user on currency disabling and where a currency may still be used. #2432

This commit is contained in:
James Cole
2019-08-18 08:46:36 +02:00
parent 4d2c5c1b58
commit 525f69cf63
4 changed files with 76 additions and 35 deletions

View File

@@ -65,7 +65,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
*/
public function countJournals(TransactionCurrency $currency): int
{
return $currency->transactions()->count() + $currency->transactionJournals()->count();
return $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
}
@@ -75,20 +75,32 @@ class CurrencyRepository implements CurrencyRepositoryInterface
* @return bool
*/
public function currencyInUse(TransactionCurrency $currency): bool
{
$result = $this->currencyInUseAt($currency);
return null !== $result;
}
/**
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string
{
Log::debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code));
$countJournals = $this->countJournals($currency);
if ($countJournals > 0) {
Log::info(sprintf('Count journals is %d, return true.', $countJournals));
return true;
return 'journals';
}
// is the only currency left
if (1 === $this->getAll()->count()) {
Log::info('Is the last currency in the system, return true. ');
return true;
return 'last_left';
}
// is being used in accounts:
@@ -96,7 +108,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return true;
return 'account_meta';
}
// is being used in bills:
@@ -104,7 +116,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($bills > 0) {
Log::info(sprintf('Used in %d bills as currency, return true. ', $bills));
return true;
return 'bills';
}
// is being used in recurring transactions
@@ -114,15 +126,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($recurringAmount > 0 || $recurringForeign > 0) {
Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign));
return true;
return 'recurring';
}
// is being used in accounts (as integer)
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((int)$currency->id))->count();
$meta = AccountMeta
::leftJoin('accounts', 'accounts.id', '=', 'account_meta.account_id')
->whereNull('accounts.deleted_at')
->where('account_meta.name', 'currency_id')->where('account_meta.data', json_encode((int)$currency->id))->count();
if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return true;
return 'account_meta';
}
// is being used in available budgets
@@ -130,7 +145,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($availableBudgets > 0) {
Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets));
return true;
return 'available_budgets';
}
// is being used in budget limits
@@ -138,7 +153,7 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($budgetLimit > 0) {
Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit));
return true;
return 'budget_limits';
}
// is the default currency for the user or the system
@@ -146,20 +161,20 @@ class CurrencyRepository implements CurrencyRepositoryInterface
if ($currency->code === $defaultCode) {
Log::info('Is the default currency of the user, return true.');
return true;
return 'current_default';
}
// is the default currency for the system
// $defaultSystemCode = config('firefly.default_currency', 'EUR');
// $result = $currency->code === $defaultSystemCode;
// if (true === $result) {
// Log::info('Is the default currency of the SYSTEM, 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::info('Is the default currency of the SYSTEM, return true.');
//
// return 'system_fallback';
// }
Log::debug('Currency is not used, return false.');
return false;
return null;
}
/**