Refactor currency repository.

This commit is contained in:
James Cole 2023-10-28 06:58:33 +02:00
parent 9e94b9e57e
commit 1d138eed8d
No known key found for this signature in database
GPG Key ID: B49A324B7EAD6D80
42 changed files with 676 additions and 818 deletions

View File

@ -26,7 +26,7 @@ namespace FireflyIII\Api\V1\Controllers\Autocomplete;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Autocomplete\AutocompleteRequest;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;

View File

@ -27,7 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Models\TransactionCurrency;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;

View File

@ -38,7 +38,6 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Recurring\RecurringRepositoryInterface;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Support\Http\Api\AccountFilter;
@ -65,26 +64,6 @@ class ListController extends Controller
use AccountFilter;
use TransactionFilter;
private CurrencyRepositoryInterface $repository;
/**
* CurrencyRepository constructor.
*
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
$this->repository = app(CurrencyRepositoryInterface::class);
$this->repository->setUser(auth()->user());
return $next($request);
}
);
}
/**
* This endpoint is documented at:
* https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/currencies/listAccountByCurrency

View File

@ -27,7 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Models\TransactionCurrency;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\CurrencyTransformer;
@ -116,7 +116,7 @@ class ShowController extends Controller
/** @var User $user */
$user = auth()->user();
$manager = $this->getManager();
$defaultCurrency = app('amount')->getDefaultCurrencyByUser(auth()->user());
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
$this->parameters->set('defaultCurrency', $defaultCurrency);
// update fields with user info.
@ -146,7 +146,7 @@ class ShowController extends Controller
/** @var User $user */
$user = auth()->user();
$manager = $this->getManager();
$currency = app('amount')->getDefaultCurrencyByUser($user);
$currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
// update fields with user info.
$currency->refreshForUser($user);

View File

@ -27,7 +27,7 @@ namespace FireflyIII\Api\V1\Controllers\Models\TransactionCurrency;
use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Models\TransactionCurrency\StoreRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\CurrencyTransformer;

View File

@ -28,7 +28,7 @@ use FireflyIII\Api\V1\Controllers\Controller;
use FireflyIII\Api\V1\Requests\Models\TransactionCurrency\UpdateRequest;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\AccountFilter;
use FireflyIII\Support\Http\Api\TransactionFilter;
use FireflyIII\Transformers\CurrencyTransformer;
@ -83,6 +83,10 @@ class UpdateController extends Controller
if ($this->repository->currencyInUse($currency)) {
return response()->json([], 409);
}
// must not be the only one in use:
if (1 === $this->repository->get()->count()) {
return response()->json([], 409);
}
/** @var User $user */
$user = auth()->user();
$this->repository->disable($currency);
@ -180,6 +184,13 @@ class UpdateController extends Controller
/** @var User $user */
$user = auth()->user();
// safety catch on currency disablement.
$set = $this->repository->get();
if(array_key_exists('enabled', $data) && false === $data['enabled'] && 1 === count($set) && $set->first()->id === $currency->id){
return response()->json([], 409);
}
$currency = $this->repository->update($currency, $data);
app('preferences')->mark();

View File

@ -39,7 +39,7 @@ use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Http\JsonResponse;

View File

@ -33,7 +33,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\CleansChartData;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;

View File

@ -41,7 +41,7 @@ use FireflyIII\Repositories\UserGroups\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;
use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait;
use FireflyIII\User;

View File

@ -162,6 +162,6 @@ class CorrectOpeningBalanceCurrencies extends Command
$repos = app(AccountRepositoryInterface::class);
$repos->setUser($account->user);
return $repos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUser($account->user);
return $repos->getAccountCurrency($account) ?? app('amount')->getDefaultCurrencyByUserGroup($account->userGroup);
}
}

View File

@ -129,7 +129,7 @@ class AccountCurrencies extends Command
$accounts = $this->accountRepos->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]);
// get user's currency preference:
$defaultCurrency = app('amount')->getDefaultCurrencyByUser($user);
$defaultCurrency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
/** @var Account $account */
foreach ($accounts as $account) {

View File

@ -77,7 +77,7 @@ class BudgetLimitCurrency extends Command
if (null !== $budget) {
$user = $budget->user;
if (null !== $user) {
$currency = app('amount')->getDefaultCurrencyByUser($user);
$currency = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
$budgetLimit->transaction_currency_id = $currency->id;
$budgetLimit->save();
$this->friendlyInfo(

View File

@ -39,7 +39,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface;
use FireflyIII\Repositories\TransactionType\TransactionTypeRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\JournalDestroyService;

View File

@ -29,7 +29,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\UserGroup;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Api\ExchangeRateConverter;

View File

@ -29,7 +29,7 @@ use FireflyIII\Models\Budget;
use FireflyIII\Models\Category;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use Illuminate\Support\Collection;

View File

@ -32,7 +32,7 @@ use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;

View File

@ -34,7 +34,7 @@ use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetLimitRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\DateCalculation;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;

View File

@ -33,7 +33,7 @@ use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use FireflyIII\Support\Http\Controllers\AugumentData;
use FireflyIII\Support\Http\Controllers\ChartGeneration;

View File

@ -1,48 +0,0 @@
<?php
/**
* CurrencyController.php
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class CurrencyController.
*/
class CurrencyController extends Controller
{
}

View File

@ -29,7 +29,7 @@ use FireflyIII\Models\Account;
use FireflyIII\Models\AccountType;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

View File

@ -35,7 +35,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Budget\AvailableBudgetRepositoryInterface;
use FireflyIII\Repositories\Budget\OperationsRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Log;

View File

@ -27,7 +27,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Requests\NewUserFormRequest;
use FireflyIII\Models\AccountType;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\Http\Controllers\CreateStuff;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
@ -41,8 +41,7 @@ class NewUserController extends Controller
{
use CreateStuff;
/** @var AccountRepositoryInterface The account repository */
private $repository;
private AccountRepositoryInterface $repository;
/**
* NewUserController constructor.

View File

@ -7,7 +7,7 @@ namespace FireflyIII\Http\Controllers\TransactionCurrency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;

View File

@ -6,7 +6,7 @@ namespace FireflyIII\Http\Controllers\TransactionCurrency;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;

View File

@ -7,7 +7,7 @@ namespace FireflyIII\Http\Controllers\TransactionCurrency;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\CurrencyFormRequest;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;

View File

@ -4,18 +4,14 @@ declare(strict_types=1);
namespace FireflyIII\Http\Controllers\TransactionCurrency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

View File

@ -103,6 +103,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property string $current_debt
* @property int|null $user_group_id
* @method static EloquentBuilder|Account whereUserGroupId($value)
* @property-read \FireflyIII\Models\UserGroup|null $userGroup
* @mixin Eloquent
*/
class Account extends Model
@ -283,6 +284,14 @@ class Account extends Model
return $this->hasMany(Transaction::class);
}
/**
* @return BelongsTo
*/
public function userGroup(): BelongsTo
{
return $this->belongsTo(UserGroup::class);
}
/**
* Get the virtual balance
*

View File

@ -69,6 +69,10 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @method static \Illuminate\Database\Eloquent\Builder|TransactionCurrency whereUpdatedAt($value)
* @method static Builder|TransactionCurrency withTrashed()
* @method static Builder|TransactionCurrency withoutTrashed()
* @property-read Collection<int, \FireflyIII\Models\UserGroup> $userGroups
* @property-read int|null $user_groups_count
* @property-read Collection<int, User> $users
* @property-read int|null $users_count
* @mixin Eloquent
*/
class TransactionCurrency extends Model
@ -118,8 +122,8 @@ class TransactionCurrency extends Model
*/
public function refreshForUser(User $user)
{
$current = $user->currencies()->where('transaction_currencies.id', $this->id)->first();
$default = app('amount')->getDefaultCurrencyByUser($user);
$current = $user->userGroup->currencies()->where('transaction_currencies.id', $this->id)->first();
$default = app('amount')->getDefaultCurrencyByUserGroup($user->userGroup);
$this->userDefault = (int)$default->id === (int)$this->id;
$this->userEnabled = null !== $current;
}

View File

@ -86,6 +86,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read int|null $transaction_groups_count
* @property-read Collection<int, \FireflyIII\Models\Webhook> $webhooks
* @property-read int|null $webhooks_count
* @property-read Collection<int, \FireflyIII\Models\TransactionCurrency> $currencies
* @property-read int|null $currencies_count
* @mixin Eloquent
*/
class UserGroup extends Model

View File

@ -25,6 +25,8 @@ namespace FireflyIII\Providers;
use FireflyIII\Repositories\Currency\CurrencyRepository;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepository as GroupCurrencyRepository;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface as GroupCurrencyRepositoryInterface;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
@ -55,6 +57,19 @@ class CurrencyServiceProvider extends ServiceProvider
$repository->setUser(auth()->user());
}
return $repository;
}
);
$this->app->bind(
GroupCurrencyRepositoryInterface::class,
function (Application $app) {
/** @var GroupCurrencyRepository $repository */
$repository = app(GroupCurrencyRepository::class);
// phpstan does not get the reference to auth
if ($app->auth->check()) { // @phpstan-ignore-line
$repository->setUser(auth()->user());
}
return $repository;
}
);

View File

@ -28,7 +28,7 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Collector\GroupCollectorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Models\TransactionType;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;

View File

@ -24,25 +24,12 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\Currency;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Bill;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\Preference;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use JsonException;
/**
* Class CurrencyRepository.
@ -51,358 +38,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
{
private User $user;
/**
* @param TransactionCurrency $currency
*
* @return bool
* @throws FireflyException
*/
public function currencyInUse(TransactionCurrency $currency): bool
{
$result = $this->currencyInUseAt($currency);
return null !== $result;
}
/**
* @param TransactionCurrency $currency
*
* @return string|null
* @throws FireflyException
*/
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 'journals';
}
// is the only currency left
if (1 === $this->getAll()->count()) {
Log::info('Is the last currency in the system, return true. ');
return 'last_left';
}
// is being used in accounts:
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count();
if ($meta > 0) {
Log::info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return 'account_meta';
}
// is being used in bills:
$bills = Bill::where('transaction_currency_id', $currency->id)->count();
if ($bills > 0) {
Log::info(sprintf('Used in %d bills as currency, return true. ', $bills));
return 'bills';
}
// is being used in recurring transactions
$recurringAmount = RecurrenceTransaction::where('transaction_currency_id', $currency->id)->count();
$recurringForeign = RecurrenceTransaction::where('foreign_currency_id', $currency->id)->count();
if ($recurringAmount > 0 || $recurringForeign > 0) {
Log::info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign));
return 'recurring';
}
// is being used in accounts (as integer)
$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 'account_meta';
}
// is being used in available budgets
$availableBudgets = AvailableBudget::where('transaction_currency_id', $currency->id)->count();
if ($availableBudgets > 0) {
Log::info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets));
return 'available_budgets';
}
// is being used in budget limits
$budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count();
if ($budgetLimit > 0) {
Log::info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit));
return 'budget_limits';
}
// is the default currency for the user or the system
$count = $this->user->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('user_default', 1)->count();
if ($count > 0) {
Log::info('Is the default currency of the user, return true.');
return 'current_default';
}
Log::debug('Currency is not used, return false.');
return null;
}
/**
* @param TransactionCurrency $currency
*
* @return int
*/
public function countJournals(TransactionCurrency $currency): int
{
$count = $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
// also count foreign:
return $count + Transaction::where('foreign_currency_id', $currency->id)->count();
}
/**
* Returns ALL currencies, regardless of whether they are enabled or not.
*
* @return Collection
*/
public function getAll(): Collection
{
$all = TransactionCurrency::orderBy('code', 'ASC')->get();
$local = $this->get();
return $all->map(function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(function (TransactionCurrency $entry) use ($current) {
return (int)$entry->id === (int)$current->id;
});
$isDefault = $local->contains(function (TransactionCurrency $entry) use ($current) {
return 1 === (int)$entry->pivot->user_default && (int)$entry->id === (int)$current->id;
});
$current->userEnabled = $hasId;
$current->userDefault = $isDefault;
return $current;
});
}
/**
* @return Collection
*/
public function get(): Collection
{
$all = $this->user->currencies()->orderBy('code', 'ASC')->withPivot(['user_default'])->get();
$all->map(function (TransactionCurrency $current) {
$current->userEnabled = true;
$current->userDefault = 1 === (int)$current->pivot->user_default;
return $current;
});
return $all;
}
/**
* Returns the complete set of transactions but needs
* no user object.
*
* @return Collection
*/
public function getCompleteSet(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function destroy(TransactionCurrency $currency): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole($this->user, 'owner')) {
/** @var CurrencyDestroyService $service */
$service = app(CurrencyDestroyService::class);
$service->destroy($currency);
}
return true;
}
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void
{
$this->user->currencies()->detach($currency->id);
$currency->enabled = false;
$currency->save();
}
/**
* @inheritDoc
*/
public function ensureMinimalEnabledCurrencies(): void
{
// if no currencies are enabled, enable the first one in the DB (usually the EUR)
if (0 === $this->user->currencies()->count()) {
$euro = app('amount')->getSystemCurrency();
if (null === $euro) {
throw new FireflyException('No currencies found. You broke Firefly III');
}
Log::channel('audit')->info(sprintf('Auto-enabled currency %s.', $euro->code));
$this->enable($euro);
app('preferences')->mark();
}
}
/**
* @param TransactionCurrency $currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
$this->user->currencies()->syncWithoutDetaching([$currency->id]);
$currency->enabled = false;
$currency->save();
}
/**
* Find by currency code, return NULL if unfound.
* Used in Import Currency!
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
* @deprecated
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency
{
return TransactionCurrency::where('code', $currencyCode)->first();
}
/**
* Find by currency name.
*
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
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
*
* @return TransactionCurrency|null
* @deprecated
*/
public function findByNameNull(string $currencyName): ?TransactionCurrency
{
return TransactionCurrency::whereName($currencyName)->first();
}
/**
* Find by currency symbol.
*
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
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
*
* @return TransactionCurrency|null
* @deprecated
*/
public function findBySymbolNull(string $currencySymbol): ?TransactionCurrency
{
return TransactionCurrency::whereSymbol($currencySymbol)->first();
}
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
* @throws FireflyException
* @throws JsonException
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency
{
$result = $this->findCurrencyNull($currencyId, $currencyCode);
if (null === $result) {
Log::debug('Grabbing default currency for this user...');
$result = app('amount')->getDefaultCurrencyByUser($this->user);
}
if (null === $result) {
Log::debug('Grabbing EUR as fallback.');
$result = $this->findByCode('EUR');
}
Log::debug(sprintf('Final result: %s', $result->code));
if (false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
$this->enable($result);
}
return $result;
}
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
Log::debug('Now in findCurrencyNull()');
$result = $this->find((int)$currencyId);
if (null === $result) {
Log::debug(sprintf('Searching for currency with code %s...', $currencyCode));
$result = $this->findByCode((string)$currencyCode);
}
if (null !== $result && false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
$this->enable($result);
}
return $result;
}
/**
* 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.
@ -416,30 +51,18 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return TransactionCurrency::where('code', $currencyCode)->first();
}
/**
* @param array $ids
* Returns the complete set of transactions but needs
* no user object.
*
* @return Collection
*/
public function getByIds(array $ids): Collection
public function getCompleteSet(): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->whereIn('id', $ids)->get();
return TransactionCurrency::orderBy('code', 'ASC')->get();
}
/**
* @param Preference $preference
*
* @return TransactionCurrency
*/
public function getCurrencyByPreference(Preference $preference): TransactionCurrency
{
$preferred = TransactionCurrency::where('code', $preference->data)->first();
if (null === $preferred) {
$preferred = TransactionCurrency::first();
}
return $preferred;
}
/**
* Get currency exchange rate.
@ -473,38 +96,6 @@ class CurrencyRepository implements CurrencyRepositoryInterface
return null;
}
/**
* @inheritDoc
*/
public function getUserCurrencies(User $user): Collection
{
return $user->currencies()->get();
}
/**
* @inheritDoc
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
if ('' !== $search) {
$query->where('name', 'LIKE', sprintf('%%%s%%', $search));
}
return $query->take($limit)->get();
}
/**
* TODO must be a factory
*
@ -538,88 +129,4 @@ class CurrencyRepository implements CurrencyRepositoryInterface
}
}
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency
{
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
$result = $factory->create($data);
if (true === $data['enabled']) {
$this->user->currencies()->attach($result->id);
}
return $result;
}
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency
{
app('log')->debug('Now in update()');
// can be true, false, null
$enabled = array_key_exists('enabled', $data) ? $data['enabled'] : null;
// can be true, false, but method only responds to "true".
$default = array_key_exists('default', $data) ? $data['default'] : false;
// remove illegal combo's:
if (false === $enabled && true === $default) {
$enabled = true;
}
if (false === $default) {
app('log')->warning(sprintf('Set default=false will NOT do anything for currency %s', $currency->code));
}
// update currency with current user specific settings
$currency->refreshForUser($this->user);
// currency is enabled, must be disabled.
if (false === $enabled) {
app('log')->debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->user->id));
$this->user->currencies()->detach($currency->id);
}
// currency must be enabled
if (true === $enabled) {
app('log')->debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->user->id));
$this->user->currencies()->detach($currency->id);
$this->user->currencies()->syncWithoutDetaching([$currency->id => ['user_default' => false]]);
}
// currency must be made default.
if (true === $default) {
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->user->id));
$this->user->currencies()->detach($currency->id);
foreach ($this->user->currencies()->get() as $item) {
$this->user->currencies()->updateExistingPivot($item->id, ['user_default' => false]);
}
$this->user->currencies()->syncWithoutDetaching([$currency->id => ['user_default' => true]]);
}
/** @var CurrencyUpdateService $service */
$service = app(CurrencyUpdateService::class);
return $service->update($currency, $data);
}
/**
* @inheritDoc
*/
public function makeDefault(TransactionCurrency $currency): void
{
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->user->id));
$this->user->currencies()->detach($currency->id);
foreach ($this->user->currencies()->get() as $item) {
$this->user->currencies()->updateExistingPivot($item->id, ['user_default' => false]);
}
$this->user->currencies()->syncWithoutDetaching([$currency->id => ['user_default' => true]]);
}
}

View File

@ -26,7 +26,6 @@ namespace FireflyIII\Repositories\Currency;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\CurrencyExchangeRate;
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\User;
use Illuminate\Contracts\Auth\Authenticatable;
@ -37,75 +36,11 @@ use Illuminate\Support\Collection;
*/
interface CurrencyRepositoryInterface
{
/**
* Returns the complete set of transactions but needs
* no user object.
*
* @return Collection
*/
public function getCompleteSet(): Collection;
/**
* @param TransactionCurrency $currency
*
* @return int
*/
public function countJournals(TransactionCurrency $currency): int;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function currencyInUse(TransactionCurrency $currency): bool;
/**
* Currency is in use where exactly.
*
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
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;
/**
* @return void
*/
public function ensureMinimalEnabledCurrencies(): 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.
*
* Used in the download exchange rates cron job. Does not require user object.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
@ -113,97 +48,20 @@ interface CurrencyRepositoryInterface
public function findByCode(string $currencyCode): ?TransactionCurrency;
/**
* Find by currency code, return NULL if unfound.
* Returns the complete set of transactions but needs
* no user object.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
*/
public function findByCodeNull(string $currencyCode): ?TransactionCurrency;
/**
* Find by currency name.
*
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
public function findByName(string $currencyName): ?TransactionCurrency;
/**
* Find by currency name.
*
* @param string $currencyName
*
* @return TransactionCurrency|null
*/
public function findByNameNull(string $currencyName): ?TransactionCurrency;
/**
* Find by currency symbol.
*
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
public function findBySymbol(string $currencySymbol): ?TransactionCurrency;
/**
* Find by currency symbol.
*
* @param string $currencySymbol
*
* @return TransactionCurrency|null
*/
public function findBySymbolNull(string $currencySymbol): ?TransactionCurrency;
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency;
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency;
/**
* @return Collection
*/
public function get(): Collection;
/**
* @return Collection
*/
public function getAll(): Collection;
/**
* @param array $ids
* Used by the download exchange rate cron job.
*
* @return Collection
*/
public function getByIds(array $ids): Collection;
/**
* @param Preference $preference
*
* @return TransactionCurrency
*/
public function getCurrencyByPreference(Preference $preference): TransactionCurrency;
public function getCompleteSet(): Collection;
/**
* Get currency exchange rate.
*
* Used in the download exchange rate cron job. Needs the user object!
*
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
@ -213,36 +71,9 @@ interface CurrencyRepositoryInterface
public function getExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): ?CurrencyExchangeRate;
/**
* @param TransactionCurrency $currency
* Set currency exchange rate.
*
* @return void
*/
public function makeDefault(TransactionCurrency $currency): void;
/**
* @param User $user
*
* @return Collection
*/
public function getUserCurrencies(User $user): Collection;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool;
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection;
/**
* TODO must be a factory
* Used in download exchange rate cron job. Needs the user object!
*
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
@ -258,19 +89,4 @@ interface CurrencyRepositoryInterface
*/
public function setUser(User | Authenticatable | null $user): void;
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency;
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency;
}

View File

@ -0,0 +1,418 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Currency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Factory\TransactionCurrencyFactory;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AvailableBudget;
use FireflyIII\Models\Bill;
use FireflyIII\Models\BudgetLimit;
use FireflyIII\Models\RecurrenceTransaction;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\Services\Internal\Destroy\CurrencyDestroyService;
use FireflyIII\Services\Internal\Update\CurrencyUpdateService;
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use JsonException;
/**
* Class CurrencyRepository
*/
class CurrencyRepository implements CurrencyRepositoryInterface
{
use UserGroupTrait;
/**
* @param TransactionCurrency $currency
*
* @return bool
* @throws FireflyException
*/
public function currencyInUse(TransactionCurrency $currency): bool
{
$result = $this->currencyInUseAt($currency);
return null !== $result;
}
/**
* @param TransactionCurrency $currency
*
* @return string|null
* @throws FireflyException
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string
{
app('log')->debug(sprintf('Now in currencyInUse() for #%d ("%s")', $currency->id, $currency->code));
$countJournals = $this->countJournals($currency);
if ($countJournals > 0) {
app('log')->info(sprintf('Count journals is %d, return true.', $countJournals));
return 'journals';
}
// is the only currency left
if (1 === $this->getAll()->count()) {
app('log')->info('Is the last currency in the system, return true. ');
return 'last_left';
}
// is being used in accounts:
$meta = AccountMeta::where('name', 'currency_id')->where('data', json_encode((string)$currency->id))->count();
if ($meta > 0) {
app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return 'account_meta';
}
// is being used in bills:
$bills = Bill::where('transaction_currency_id', $currency->id)->count();
if ($bills > 0) {
app('log')->info(sprintf('Used in %d bills as currency, return true. ', $bills));
return 'bills';
}
// is being used in recurring transactions
$recurringAmount = RecurrenceTransaction::where('transaction_currency_id', $currency->id)->count();
$recurringForeign = RecurrenceTransaction::where('foreign_currency_id', $currency->id)->count();
if ($recurringAmount > 0 || $recurringForeign > 0) {
app('log')->info(sprintf('Used in %d recurring transactions as (foreign) currency id, return true. ', $recurringAmount + $recurringForeign));
return 'recurring';
}
// is being used in accounts (as integer)
$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) {
app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return 'account_meta';
}
// is being used in available budgets
$availableBudgets = AvailableBudget::where('transaction_currency_id', $currency->id)->count();
if ($availableBudgets > 0) {
app('log')->info(sprintf('Used in %d available budgets as currency, return true. ', $availableBudgets));
return 'available_budgets';
}
// is being used in budget limits
$budgetLimit = BudgetLimit::where('transaction_currency_id', $currency->id)->count();
if ($budgetLimit > 0) {
app('log')->info(sprintf('Used in %d budget limits as currency, return true. ', $budgetLimit));
return 'budget_limits';
}
// is the default currency for the user or the system
$count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count();
if ($count > 0) {
app('log')->info('Is the default currency of the user, return true.');
return 'current_default';
}
// is the default currency for the user or the system
$count = $this->userGroup->currencies()->where('transaction_currencies.id', $currency->id)->wherePivot('group_default', 1)->count();
if ($count > 0) {
app('log')->info('Is the default currency of the user group, return true.');
return 'current_default';
}
app('log')->debug('Currency is not used, return false.');
return null;
}
/**
* @param TransactionCurrency $currency
*
* @return int
*/
private function countJournals(TransactionCurrency $currency): int
{
$count = $currency->transactions()->whereNull('deleted_at')->count() + $currency->transactionJournals()->whereNull('deleted_at')->count();
// also count foreign:
return $count + Transaction::where('foreign_currency_id', $currency->id)->count();
}
/**
* Returns ALL currencies, regardless of whether they are enabled or not.
*
* @return Collection
*/
public function getAll(): Collection
{
$all = TransactionCurrency::orderBy('code', 'ASC')->get();
$local = $this->get();
return $all->map(function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(function (TransactionCurrency $entry) use ($current) {
return (int)$entry->id === (int)$current->id;
});
$isDefault = $local->contains(function (TransactionCurrency $entry) use ($current) {
return 1 === (int)$entry->pivot->group_default && (int)$entry->id === (int)$current->id;
});
$current->userEnabled = $hasId;
$current->userDefault = $isDefault;
return $current;
});
}
/**
* Get the user group's currencies.
* @return Collection
*/
public function get(): Collection
{
$all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get();
$all->map(function (TransactionCurrency $current) {
$current->userEnabled = true;
$current->userDefault = 1 === (int)$current->pivot->group_default;
return $current;
});
return $all;
}
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function destroy(TransactionCurrency $currency): bool
{
/** @var UserRepositoryInterface $repository */
$repository = app(UserRepositoryInterface::class);
if ($repository->hasRole($this->user, 'owner')) {
/** @var CurrencyDestroyService $service */
$service = app(CurrencyDestroyService::class);
$service->destroy($currency);
}
return true;
}
/**
* Disables a currency
*
* @param TransactionCurrency $currency
*/
public function disable(TransactionCurrency $currency): void
{
$this->userGroup->currencies()->detach($currency->id);
$currency->enabled = false;
$currency->save();
}
/**
* Find by object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
* @throws FireflyException
* @throws JsonException
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency
{
$result = $this->findCurrencyNull($currencyId, $currencyCode);
if (null === $result) {
Log::debug('Grabbing default currency for this user...');
$result = app('amount')->getDefaultCurrencyByUser($this->user);
}
if (null === $result) {
Log::debug('Grabbing EUR as fallback.');
$result = $this->findByCode('EUR');
}
Log::debug(sprintf('Final result: %s', $result->code));
if (false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
$this->enable($result);
}
return $result;
}
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
Log::debug('Now in findCurrencyNull()');
$result = $this->find((int)$currencyId);
if (null === $result) {
Log::debug(sprintf('Searching for currency with code %s...', $currencyCode));
$result = $this->findByCode((string)$currencyCode);
}
if (null !== $result && false === $result->enabled) {
Log::debug(sprintf('Also enabled currency %s', $result->code));
$this->enable($result);
}
return $result;
}
/**
* 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
*/
private function findByCode(string $currencyCode): ?TransactionCurrency
{
return TransactionCurrency::where('code', $currencyCode)->first();
}
/**
* @param TransactionCurrency $currency
* Enables a currency
*/
public function enable(TransactionCurrency $currency): void
{
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id]);
$currency->enabled = false;
$currency->save();
}
/**
* @inheritDoc
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
/**
* @inheritDoc
*/
public function makeDefault(TransactionCurrency $currency): void
{
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
foreach ($this->userGroup->currencies()->get() as $item) {
$this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]);
}
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => true]]);
}
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
if ('' !== $search) {
$query->where('name', 'LIKE', sprintf('%%%s%%', $search));
}
return $query->take($limit)->get();
}
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency
{
/** @var TransactionCurrencyFactory $factory */
$factory = app(TransactionCurrencyFactory::class);
$result = $factory->create($data);
if (true === $data['enabled']) {
$this->userGroup->currencies()->attach($result->id);
}
return $result;
}
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency
{
app('log')->debug('Now in update()');
// can be true, false, null
$enabled = array_key_exists('enabled', $data) ? $data['enabled'] : null;
// can be true, false, but method only responds to "true".
$default = array_key_exists('default', $data) ? $data['default'] : false;
// remove illegal combo's:
if (false === $enabled && true === $default) {
$enabled = true;
}
if (false === $default) {
app('log')->warning(sprintf('Set default=false will NOT do anything for currency %s', $currency->code));
}
// update currency with current user specific settings
$currency->refreshForUser($this->user);
// currency is enabled, must be disabled.
if (false === $enabled) {
app('log')->debug(sprintf('Disabled currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
}
// currency must be enabled
if (true === $enabled) {
app('log')->debug(sprintf('Enabled currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => false]]);
}
// currency must be made default.
if (true === $default) {
app('log')->debug(sprintf('Enabled + made default currency %s for user #%d', $currency->code, $this->userGroup->id));
$this->userGroup->currencies()->detach($currency->id);
foreach ($this->userGroup->currencies()->get() as $item) {
$this->userGroup->currencies()->updateExistingPivot($item->id, ['group_default' => false]);
}
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id => ['group_default' => true]]);
}
/** @var CurrencyUpdateService $service */
$service = app(CurrencyUpdateService::class);
return $service->update($currency, $data);
}
}

View File

@ -0,0 +1,129 @@
<?php
declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Currency;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use Illuminate\Support\Collection;
interface CurrencyRepositoryInterface
{
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function currencyInUse(TransactionCurrency $currency): bool;
/**
* Currency is in use where exactly.
*
* @param TransactionCurrency $currency
*
* @return string|null
*/
public function currencyInUseAt(TransactionCurrency $currency): ?string;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
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 object, ID or code. Returns user default or system default.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency
*/
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency;
/**
* Find by object, ID or code. Returns NULL if nothing found.
*
* @param int|null $currencyId
* @param string|null $currencyCode
*
* @return TransactionCurrency|null
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency;
/**
* Get the user group's currencies.
*
* @return Collection
*/
public function get(): Collection;
/**
* Get ALL currencies.
*
* @return Collection
*/
public function getAll(): Collection;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function isFallbackCurrency(TransactionCurrency $currency): bool;
/**
* @param TransactionCurrency $currency
*
* @return void
*/
public function makeDefault(TransactionCurrency $currency): void;
/**
* @param string $search
* @param int $limit
*
* @return Collection
*/
public function searchCurrency(string $search, int $limit): Collection;
/**
* @param array $data
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function store(array $data): TransactionCurrency;
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
public function update(TransactionCurrency $currency, array $data): TransactionCurrency;
}

View File

@ -39,7 +39,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Services\Internal\Support\JournalServiceTrait;
use FireflyIII\Support\NullArrayObject;
use FireflyIII\Validation\AccountValidator;

View File

@ -26,6 +26,7 @@ namespace FireflyIII\Support;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Models\UserGroup;
use FireflyIII\User;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Support\Collection;
@ -129,7 +130,6 @@ class Amount
* @param User $user
*
* @return TransactionCurrency
* @throws FireflyException
*/
public function getDefaultCurrencyByUser(User $user): TransactionCurrency
{
@ -148,6 +148,28 @@ class Amount
return $default;
}
/**
* @param User $user
*
* @return TransactionCurrency
*/
public function getDefaultCurrencyByUserGroup(UserGroup $userGroup): TransactionCurrency
{
$cache = new CacheProperties();
$cache->addProperty('getDefaultCurrencyByGroup');
$cache->addProperty($userGroup->id);
if ($cache->has()) {
return $cache->get();
}
$default = $userGroup->currencies()->where('group_default', true)->first();
if(null === $default) {
$default = $this->getSystemCurrency();
$userGroup->currencies()->sync([$default->id => ['group_default' => true]]);
}
$cache->store($default);
return $default;
}
/**
* @param string $value

View File

@ -26,7 +26,7 @@ namespace FireflyIII\Support\Form;
use Amount as Amt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\TransactionCurrency;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Throwable;

View File

@ -28,7 +28,6 @@ use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Generator\Chart\Basic\GeneratorInterface;
use FireflyIII\Models\Account;
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Support\CacheProperties;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
@ -66,8 +65,6 @@ trait ChartGeneration
/** @var GeneratorInterface $generator */
$generator = app(GeneratorInterface::class);
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
/** @var AccountRepositoryInterface $accountRepos */
$accountRepos = app(AccountRepositoryInterface::class);
@ -76,7 +73,7 @@ trait ChartGeneration
/** @var Account $account */
foreach ($accounts as $account) {
// TODO we can use getAccountCurrency instead.
$currency = $repository->find((int)$accountRepos->getMetaValue($account, 'currency_id'));
$currency = $accountRepos->getAccountCurrency($account);
if (null === $currency) {
$currency = $default;
}

View File

@ -34,7 +34,7 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use FireflyIII\Repositories\Budget\BudgetRepositoryInterface;
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\UserGroups\Currency\CurrencyRepositoryInterface;
use FireflyIII\Repositories\Tag\TagRepositoryInterface;
use FireflyIII\Support\ParseDateString;
use FireflyIII\User;

View File

@ -167,6 +167,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read int|null $group_memberships_count
* @property-read UserGroup|null $userGroup
* @method static Builder|User whereUserGroupId($value)
* @property-read \Illuminate\Database\Eloquent\Collection<int, TransactionCurrency> $currencies
* @property-read int|null $currencies_count
* @mixin Eloquent
*/
class User extends Authenticatable

14
composer.lock generated
View File

@ -3590,16 +3590,16 @@
},
{
"name": "monolog/monolog",
"version": "3.4.0",
"version": "3.5.0",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/monolog.git",
"reference": "e2392369686d420ca32df3803de28b5d6f76867d"
"reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/e2392369686d420ca32df3803de28b5d6f76867d",
"reference": "e2392369686d420ca32df3803de28b5d6f76867d",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/c915e2634718dbc8a4a15c61b0e62e7a44e14448",
"reference": "c915e2634718dbc8a4a15c61b0e62e7a44e14448",
"shasum": ""
},
"require": {
@ -3675,7 +3675,7 @@
],
"support": {
"issues": "https://github.com/Seldaek/monolog/issues",
"source": "https://github.com/Seldaek/monolog/tree/3.4.0"
"source": "https://github.com/Seldaek/monolog/tree/3.5.0"
},
"funding": [
{
@ -3687,7 +3687,7 @@
"type": "tidelift"
}
],
"time": "2023-06-21T08:46:11+00:00"
"time": "2023-10-27T15:32:31+00:00"
},
{
"name": "nesbot/carbon",
@ -11693,5 +11693,5 @@
"platform-overrides": {
"php": "8.2"
},
"plugin-api-version": "2.6.0"
"plugin-api-version": "2.3.0"
}