2015-04-05 20:47:19 +02:00
|
|
|
<?php
|
2022-12-29 19:42:26 +01:00
|
|
|
|
2016-05-20 12:41:23 +02:00
|
|
|
/**
|
|
|
|
* CurrencyRepository.php
|
2020-02-16 14:00:57 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:41:23 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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/>.
|
2016-05-20 12:41:23 +02:00
|
|
|
*/
|
2017-04-09 07:44:22 +02:00
|
|
|
declare(strict_types=1);
|
2015-04-05 20:47:19 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Repositories\Currency;
|
|
|
|
|
2017-04-13 20:47:59 +02:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Models\CurrencyExchangeRate;
|
2015-04-05 20:47:19 +02:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2025-02-16 19:38:07 +01:00
|
|
|
use FireflyIII\Models\UserGroup;
|
2016-12-11 16:25:46 +01:00
|
|
|
use FireflyIII\User;
|
2023-02-19 08:43:28 +01:00
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
2015-04-05 20:47:19 +02:00
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class CurrencyRepository.
|
2015-04-05 20:47:19 +02:00
|
|
|
*/
|
|
|
|
class CurrencyRepository implements CurrencyRepositoryInterface
|
|
|
|
{
|
2020-08-22 12:24:01 +02:00
|
|
|
private User $user;
|
2025-02-16 19:38:07 +01:00
|
|
|
private UserGroup $userGroup;
|
2018-02-28 21:32:59 +01:00
|
|
|
|
2024-12-22 08:43:12 +01:00
|
|
|
#[\Override]
|
|
|
|
public function find(int $currencyId): ?TransactionCurrency
|
|
|
|
{
|
|
|
|
return TransactionCurrency::find($currencyId);
|
|
|
|
}
|
|
|
|
|
2022-03-29 14:59:58 +02:00
|
|
|
/**
|
2023-06-21 12:34:58 +02:00
|
|
|
* Find by currency code, return NULL if unfound.
|
2022-03-29 14:59:58 +02:00
|
|
|
*/
|
2023-06-21 12:34:58 +02:00
|
|
|
public function findByCode(string $currencyCode): ?TransactionCurrency
|
2022-03-29 14:59:58 +02:00
|
|
|
{
|
2023-06-21 12:34:58 +02:00
|
|
|
return TransactionCurrency::where('code', $currencyCode)->first();
|
2022-03-29 14:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-28 06:58:33 +02:00
|
|
|
* Returns the complete set of transactions but needs
|
|
|
|
* no user object.
|
2017-08-18 15:14:44 +02:00
|
|
|
*/
|
2023-10-28 06:58:33 +02:00
|
|
|
public function getCompleteSet(): Collection
|
2017-08-18 15:14:44 +02:00
|
|
|
{
|
2025-02-02 16:13:56 +01:00
|
|
|
return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get();
|
2017-08-18 15:14:44 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 20:47:59 +02:00
|
|
|
/**
|
2018-06-28 07:32:58 +02:00
|
|
|
* Get currency exchange rate.
|
2017-04-13 20:47:59 +02:00
|
|
|
*/
|
2018-06-28 07:32:58 +02:00
|
|
|
public function getExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): ?CurrencyExchangeRate
|
2017-04-13 20:47:59 +02:00
|
|
|
{
|
2017-04-14 10:16:52 +02:00
|
|
|
if ($fromCurrency->id === $toCurrency->id) {
|
2022-10-30 14:24:28 +01:00
|
|
|
$rate = new CurrencyExchangeRate();
|
2023-12-20 19:35:52 +01:00
|
|
|
$rate->rate = '1';
|
2017-04-14 10:16:52 +02:00
|
|
|
$rate->id = 0;
|
|
|
|
|
|
|
|
return $rate;
|
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
|
|
|
/** @var null|CurrencyExchangeRate $rate */
|
2017-04-13 20:47:59 +02:00
|
|
|
$rate = $this->user->currencyExchangeRates()
|
2023-12-20 19:35:52 +01:00
|
|
|
->where('from_currency_id', $fromCurrency->id)
|
|
|
|
->where('to_currency_id', $toCurrency->id)
|
|
|
|
->where('date', $date->format('Y-m-d'))->first()
|
|
|
|
;
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $rate) {
|
2023-10-29 06:33:43 +01:00
|
|
|
app('log')->debug(sprintf('Found cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d')));
|
2017-04-14 10:16:52 +02:00
|
|
|
|
2017-04-13 20:47:59 +02:00
|
|
|
return $rate;
|
|
|
|
}
|
|
|
|
|
2018-06-28 07:32:58 +02:00
|
|
|
return null;
|
2017-04-13 20:47:59 +02:00
|
|
|
}
|
|
|
|
|
2017-02-17 06:42:36 +01:00
|
|
|
/**
|
2022-12-29 19:42:26 +01:00
|
|
|
* TODO must be a factory
|
|
|
|
*/
|
|
|
|
public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate
|
|
|
|
{
|
|
|
|
return CurrencyExchangeRate::create(
|
|
|
|
[
|
2024-12-22 08:43:12 +01:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'user_group_id' => $this->user->user_group_id,
|
|
|
|
'from_currency_id' => $fromCurrency->id,
|
|
|
|
'to_currency_id' => $toCurrency->id,
|
|
|
|
'date' => $date,
|
|
|
|
'date_tz' => $date->format('e'),
|
|
|
|
'rate' => $rate,
|
2022-12-29 19:42:26 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-20 19:35:52 +01:00
|
|
|
public function setUser(null|Authenticatable|User $user): void
|
2017-02-17 06:42:36 +01:00
|
|
|
{
|
2023-10-30 19:49:40 +01:00
|
|
|
if ($user instanceof User) {
|
2025-02-17 04:11:50 +01:00
|
|
|
$this->user = $user;
|
2025-02-16 19:38:07 +01:00
|
|
|
$this->userGroup = $user->userGroup;
|
2023-02-19 08:43:28 +01:00
|
|
|
}
|
2017-02-17 06:42:36 +01:00
|
|
|
}
|
2025-02-16 19:38:07 +01:00
|
|
|
|
2025-02-17 04:11:50 +01:00
|
|
|
#[\Override]
|
|
|
|
public function setUserGroup(UserGroup $userGroup): void
|
2025-02-16 19:38:07 +01:00
|
|
|
{
|
|
|
|
$this->userGroup = $userGroup;
|
|
|
|
}
|
2015-04-07 18:25:21 +02:00
|
|
|
}
|