mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 18:54:58 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			616 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			616 lines
		
	
	
		
			20 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| /**
 | |
|  * CurrencyRepository.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\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.
 | |
|  */
 | |
| 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;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @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.
 | |
|      *
 | |
|      * @param string $currencyCode
 | |
|      *
 | |
|      * @return TransactionCurrency|null
 | |
|      */
 | |
|     public function findByCode(string $currencyCode): ?TransactionCurrency
 | |
|     {
 | |
|         return TransactionCurrency::where('code', $currencyCode)->first();
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param array $ids
 | |
|      *
 | |
|      * @return Collection
 | |
|      */
 | |
|     public function getByIds(array $ids): Collection
 | |
|     {
 | |
|         return TransactionCurrency::orderBy('code', 'ASC')->whereIn('id', $ids)->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.
 | |
|      *
 | |
|      * @param TransactionCurrency $fromCurrency
 | |
|      * @param TransactionCurrency $toCurrency
 | |
|      * @param Carbon              $date
 | |
|      *
 | |
|      * @return CurrencyExchangeRate|null
 | |
|      */
 | |
|     public function getExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): ?CurrencyExchangeRate
 | |
|     {
 | |
|         if ($fromCurrency->id === $toCurrency->id) {
 | |
|             $rate       = new CurrencyExchangeRate();
 | |
|             $rate->rate = 1;
 | |
|             $rate->id   = 0;
 | |
| 
 | |
|             return $rate;
 | |
|         }
 | |
|         /** @var CurrencyExchangeRate $rate */
 | |
|         $rate = $this->user->currencyExchangeRates()
 | |
|                            ->where('from_currency_id', $fromCurrency->id)
 | |
|                            ->where('to_currency_id', $toCurrency->id)
 | |
|                            ->where('date', $date->format('Y-m-d'))->first();
 | |
|         if (null !== $rate) {
 | |
|             Log::debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d')));
 | |
| 
 | |
|             return $rate;
 | |
|         }
 | |
| 
 | |
|         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
 | |
|      *
 | |
|      * @param TransactionCurrency $fromCurrency
 | |
|      * @param TransactionCurrency $toCurrency
 | |
|      * @param Carbon              $date
 | |
|      * @param float               $rate
 | |
|      *
 | |
|      * @return CurrencyExchangeRate
 | |
|      */
 | |
|     public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate
 | |
|     {
 | |
|         return CurrencyExchangeRate::create(
 | |
|             [
 | |
|                 'user_id'          => $this->user->id,
 | |
|                 'from_currency_id' => $fromCurrency->id,
 | |
|                 'to_currency_id'   => $toCurrency->id,
 | |
|                 'date'             => $date,
 | |
|                 'rate'             => $rate,
 | |
|             ]
 | |
|         );
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param User|Authenticatable|null $user
 | |
|      */
 | |
|     public function setUser(User | Authenticatable | null $user): void
 | |
|     {
 | |
|         if (null !== $user) {
 | |
|             $this->user = $user;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * @param array $data
 | |
|      *
 | |
|      * @return TransactionCurrency
 | |
|      * @throws FireflyException
 | |
|      */
 | |
|     public function store(array $data): TransactionCurrency
 | |
|     {
 | |
|         throw new FireflyException(sprintf('Method "%s" needs a refactor.', __METHOD__));
 | |
|         /** @var TransactionCurrencyFactory $factory */
 | |
|         $factory = app(TransactionCurrencyFactory::class);
 | |
|         $result  = $factory->create($data);
 | |
| 
 | |
|         if (null === $result) {
 | |
|             throw new FireflyException('400004: Could not store new currency.');
 | |
|         }
 | |
| 
 | |
|         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]]);
 | |
|     }
 | |
| }
 |