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

132 lines
3.9 KiB
PHP
Raw Normal View History

2015-04-05 20:47:19 +02:00
<?php
2022-12-29 19:42:26 +01:00
/**
* CurrencyRepository.php
2020-02-16 14:00:57 +01:00
* 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.
2017-10-21 08:40:00 +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
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +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/>.
*/
declare(strict_types=1);
2015-04-05 20:47:19 +02:00
namespace FireflyIII\Repositories\Currency;
use Carbon\Carbon;
use FireflyIII\Models\CurrencyExchangeRate;
2015-04-05 20:47:19 +02:00
use FireflyIII\Models\TransactionCurrency;
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;
2018-02-28 21:32:59 +01:00
2022-03-29 14:59:58 +02:00
/**
2023-06-21 12:34:58 +02:00
* Find by currency code, return NULL if unfound.
*
* @param string $currencyCode
*
* @return TransactionCurrency|null
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
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
*
* @return Collection
*/
2023-10-28 06:58:33 +02:00
public function getCompleteSet(): Collection
2017-08-18 15:14:44 +02:00
{
2023-10-28 06:58:33 +02:00
return TransactionCurrency::orderBy('code', 'ASC')->get();
2017-08-18 15:14:44 +02:00
}
2015-04-05 20:47:19 +02:00
/**
2018-06-28 07:32:58 +02:00
* Get currency exchange rate.
*
2023-06-21 12:34:58 +02:00
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
2018-06-28 07:32:58 +02:00
* @return CurrencyExchangeRate|null
*/
2018-06-28 07:32:58 +02:00
public function getExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): ?CurrencyExchangeRate
{
if ($fromCurrency->id === $toCurrency->id) {
2022-10-30 14:24:28 +01:00
$rate = new CurrencyExchangeRate();
2023-10-30 19:49:40 +01:00
$rate->rate = "1";
$rate->id = 0;
return $rate;
}
2023-11-04 06:52:40 +01:00
/** @var CurrencyExchangeRate|null $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();
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')));
return $rate;
}
2018-06-28 07:32:58 +02:00
return null;
}
2017-02-17 06:42:36 +01:00
/**
2022-12-29 19:42:26 +01:00
* TODO must be a factory
2023-06-21 12:34:58 +02:00
*
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
* @param float $rate
2023-07-15 16:02:42 +02:00
*
2022-12-29 19:42:26 +01:00
* @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,
]
);
}
/**
2023-06-21 12:34:58 +02:00
* @param User|Authenticatable|null $user
2017-02-17 06:42:36 +01:00
*/
2023-06-21 12:34:58 +02:00
public function setUser(User | Authenticatable | null $user): void
2017-02-17 06:42:36 +01:00
{
2023-10-30 19:49:40 +01:00
if ($user instanceof User) {
2023-02-19 08:43:28 +01:00
$this->user = $user;
}
2017-02-17 06:42:36 +01:00
}
2015-04-07 18:25:21 +02:00
}