Files
firefly-iii/app/Repositories/UserGroups/Currency/CurrencyRepository.php

393 lines
14 KiB
PHP
Raw Normal View History

2023-10-28 06:58:33 +02:00
<?php
2023-10-28 15:03:33 +02:00
2023-10-28 17:17:09 +02:00
/*
* CurrencyRepository.php
* Copyright (c) 2023 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/>.
*/
2023-10-28 06:58:33 +02:00
declare(strict_types=1);
namespace FireflyIII\Repositories\UserGroups\Currency;
2024-12-21 07:12:11 +01:00
use FireflyIII\Events\Preferences\UserGroupChangedDefaultCurrency;
2023-10-28 06:58:33 +02:00
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;
2024-12-21 07:12:11 +01:00
use Illuminate\Support\Facades\Log;
2023-10-28 06:58:33 +02:00
/**
* Class CurrencyRepository
*/
class CurrencyRepository implements CurrencyRepositoryInterface
{
use UserGroupTrait;
/**
* @throws FireflyException
*/
public function currencyInUse(TransactionCurrency $currency): bool
{
$result = $this->currencyInUseAt($currency);
return null !== $result;
}
/**
* @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);
2023-10-28 06:58:33 +02:00
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();
2023-10-28 06:58:33 +02:00
if ($meta > 0) {
app('log')->info(sprintf('Used in %d accounts as currency_id, return true. ', $meta));
return 'account_meta';
}
// second search using integer check.
$meta = AccountMeta::where('name', 'currency_id')->where('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';
}
2023-10-28 06:58:33 +02:00
// is being used in bills:
$bills = Bill::where('transaction_currency_id', $currency->id)->count();
2023-10-28 06:58:33 +02:00
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($currency->id))->count()
;
2023-10-28 06:58:33 +02:00
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();
2023-10-28 06:58:33 +02:00
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();
2023-10-28 06:58:33 +02:00
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();
2023-10-28 06:58:33 +02:00
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;
}
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();
}
2023-10-28 06:58:33 +02:00
/**
* Returns ALL currencies, regardless of whether they are enabled or not.
*/
public function getAll(): Collection
{
$all = TransactionCurrency::orderBy('code', 'ASC')->get();
$local = $this->get();
2023-12-20 19:35:52 +01:00
2023-11-04 14:18:49 +01:00
return $all->map(static function (TransactionCurrency $current) use ($local) {
$hasId = $local->contains(static function (TransactionCurrency $entry) use ($current) {
2023-11-05 19:41:37 +01:00
return $entry->id === $current->id;
2023-10-28 06:58:33 +02:00
});
2025-01-19 11:40:07 +01:00
$isNative = $local->contains(static function (TransactionCurrency $entry) use ($current) {
2024-12-21 07:12:11 +01:00
return 1 === (int) $entry->pivot->group_default && $entry->id === $current->id;
2023-10-28 06:58:33 +02:00
});
2023-12-16 16:06:23 +01:00
$current->userGroupEnabled = $hasId;
2025-01-19 11:40:07 +01:00
$current->userGroupNative = $isNative;
2023-12-20 19:35:52 +01:00
2023-10-28 06:58:33 +02:00
return $current;
});
}
public function get(): Collection
{
$all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get();
$all->map(static function (TransactionCurrency $current) { // @phpstan-ignore-line
2023-12-16 16:06:23 +01:00
$current->userGroupEnabled = true;
2025-01-19 11:40:07 +01:00
$current->userGroupNative = 1 === (int) $current->pivot->group_default;
2023-12-20 19:35:52 +01:00
2023-10-28 06:58:33 +02:00
return $current;
});
2025-01-04 07:10:37 +01:00
/** @var Collection */
2023-10-28 06:58:33 +02:00
return $all;
}
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
*/
public function disable(TransactionCurrency $currency): void
{
$this->userGroup->currencies()->detach($currency->id);
$currency->enabled = false;
$currency->save();
}
2023-10-28 18:08:43 +02:00
public function findByName(string $name): ?TransactionCurrency
{
return TransactionCurrency::where('name', $name)->first();
}
2023-10-28 06:58:33 +02:00
/**
* Find by object, ID or code. Returns user default or system default.
*
* @throws FireflyException
2023-12-22 20:12:38 +01:00
*/
2023-10-28 06:58:33 +02:00
public function findCurrency(?int $currencyId, ?string $currencyCode): TransactionCurrency
{
$result = $this->findCurrencyNull($currencyId, $currencyCode);
if (null === $result) {
2023-10-29 06:33:43 +01:00
app('log')->debug('Grabbing default currency for this user...');
2023-12-20 19:35:52 +01:00
/** @var null|TransactionCurrency $result */
2023-10-29 17:41:14 +01:00
$result = app('amount')->getDefaultCurrencyByUserGroup($this->user->userGroup);
2023-10-28 06:58:33 +02:00
}
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Final result: %s', $result->code));
2023-10-28 06:58:33 +02:00
if (false === $result->enabled) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Also enabled currency %s', $result->code));
2023-10-28 06:58:33 +02:00
$this->enable($result);
}
return $result;
}
/**
* Find by object, ID or code. Returns NULL if nothing found.
*/
public function findCurrencyNull(?int $currencyId, ?string $currencyCode): ?TransactionCurrency
{
2023-10-29 06:33:43 +01:00
app('log')->debug('Now in findCurrencyNull()');
2024-12-21 07:12:11 +01:00
$result = $this->find((int) $currencyId);
2023-10-28 06:58:33 +02:00
if (null === $result) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Searching for currency with code %s...', $currencyCode));
2024-12-21 07:12:11 +01:00
$result = $this->findByCode((string) $currencyCode);
2023-10-28 06:58:33 +02:00
}
if (null !== $result && false === $result->enabled) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Also enabled currency %s', $result->code));
2023-10-28 06:58:33 +02:00
$this->enable($result);
}
return $result;
}
/**
* Find by ID, return NULL if not found.
*/
public function find(int $currencyId): ?TransactionCurrency
{
return TransactionCurrency::find($currencyId);
}
/**
* Find by currency code, return NULL if unfound.
*/
2023-10-28 14:59:16 +02:00
public function findByCode(string $currencyCode): ?TransactionCurrency
2023-10-28 06:58:33 +02:00
{
return TransactionCurrency::where('code', $currencyCode)->first();
}
public function enable(TransactionCurrency $currency): void
{
$this->userGroup->currencies()->syncWithoutDetaching([$currency->id]);
$currency->enabled = false;
$currency->save();
}
2023-10-28 14:59:16 +02:00
public function getByIds(array $ids): Collection
{
return TransactionCurrency::orderBy('code', 'ASC')->whereIn('id', $ids)->get();
}
2023-10-28 06:58:33 +02:00
public function isFallbackCurrency(TransactionCurrency $currency): bool
{
return $currency->code === config('firefly.default_currency', 'EUR');
}
public function searchCurrency(string $search, int $limit): Collection
{
$query = TransactionCurrency::where('enabled', true);
if ('' !== $search) {
$query->whereLike('name', sprintf('%%%s%%', $search));
2023-10-28 06:58:33 +02:00
}
return $query->take($limit)->get();
}
/**
* @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;
}
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;
}
// 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) {
2024-12-21 07:12:11 +01:00
$this->makeDefault($currency);
2023-10-28 06:58:33 +02:00
}
/** @var CurrencyUpdateService $service */
$service = app(CurrencyUpdateService::class);
return $service->update($currency, $data);
}
2024-12-22 08:43:12 +01:00
public function makeDefault(TransactionCurrency $currency): void
{
$current = app('amount')->getDefaultCurrencyByUserGroup($this->userGroup);
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]]);
if ($current->id !== $currency->id) {
Log::debug('Trigger on a different default currency.');
// clear all native amounts through an event.
event(new UserGroupChangedDefaultCurrency($this->userGroup));
}
}
2023-10-28 06:58:33 +02:00
}