mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Add and remove exchange rates
This commit is contained in:
@@ -24,6 +24,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\UserGroups\ExchangeRate;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -39,19 +41,57 @@ class ExchangeRateRepository implements ExchangeRateRepositoryInterface
|
||||
// orderBy('date', 'DESC')->toRawSql();
|
||||
return
|
||||
$this->userGroup->currencyExchangeRates()
|
||||
->where(function (Builder $q1) use ($from, $to): void {
|
||||
$q1->where(function (Builder $q) use ($from, $to): void {
|
||||
$q->where('from_currency_id', $from->id)
|
||||
->where('to_currency_id', $to->id)
|
||||
;
|
||||
})->orWhere(function (Builder $q) use ($from, $to): void {
|
||||
$q->where('from_currency_id', $to->id)
|
||||
->where('to_currency_id', $from->id)
|
||||
;
|
||||
});
|
||||
})
|
||||
->orderBy('date', 'DESC')->get(['currency_exchange_rates.*'])
|
||||
;
|
||||
->where(function (Builder $q1) use ($from, $to): void {
|
||||
$q1->where(function (Builder $q) use ($from, $to): void {
|
||||
$q->where('from_currency_id', $from->id)
|
||||
->where('to_currency_id', $to->id);
|
||||
})->orWhere(function (Builder $q) use ($from, $to): void {
|
||||
$q->where('from_currency_id', $to->id)
|
||||
->where('to_currency_id', $from->id);
|
||||
});
|
||||
})
|
||||
->orderBy('date', 'DESC')
|
||||
->get(['currency_exchange_rates.*']);
|
||||
|
||||
}
|
||||
|
||||
#[\Override] public function getSpecificRateOnDate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): ?CurrencyExchangeRate
|
||||
{
|
||||
return
|
||||
$this->userGroup->currencyExchangeRates()
|
||||
->where('from_currency_id', $from->id)
|
||||
->where('to_currency_id', $to->id)
|
||||
->where('date', $date->format('Y-m-d'))
|
||||
->first();
|
||||
}
|
||||
|
||||
#[\Override] public function deleteRate(CurrencyExchangeRate $rate): void
|
||||
{
|
||||
$this->userGroup->currencyExchangeRates()->where('id', $rate->id)->delete();
|
||||
}
|
||||
|
||||
#[\Override] public function updateExchangeRate(CurrencyExchangeRate $object, string $rate, ?Carbon $date = null): CurrencyExchangeRate
|
||||
{
|
||||
$object->rate = $rate;
|
||||
if (null !== $date) {
|
||||
$object->date = $date;
|
||||
}
|
||||
$object->save();
|
||||
return $object;
|
||||
}
|
||||
|
||||
#[\Override] public function storeExchangeRate(TransactionCurrency $from, TransactionCurrency $to, string $rate, Carbon $date): CurrencyExchangeRate
|
||||
{
|
||||
$object = new CurrencyExchangeRate();
|
||||
$object->user_id = auth()->user()->id;
|
||||
$object->user_group_id = $this->userGroup->id;
|
||||
$object->from_currency_id = $from->id;
|
||||
$object->to_currency_id = $to->id;
|
||||
$object->rate = $rate;
|
||||
$object->date = $date;
|
||||
$object->date_tz = $date->format('e');
|
||||
$object->save();
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
|
@@ -24,10 +24,21 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\UserGroups\ExchangeRate;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use FireflyIII\Models\CurrencyExchangeRate;
|
||||
use FireflyIII\Models\TransactionCurrency;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface ExchangeRateRepositoryInterface
|
||||
{
|
||||
public function getRates(TransactionCurrency $from, TransactionCurrency $to): Collection;
|
||||
|
||||
public function getSpecificRateOnDate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): ?CurrencyExchangeRate;
|
||||
|
||||
public function deleteRate(CurrencyExchangeRate $rate): void;
|
||||
|
||||
public function updateExchangeRate(CurrencyExchangeRate $object, string $rate, ?Carbon $date = null): CurrencyExchangeRate;
|
||||
|
||||
public function storeExchangeRate(TransactionCurrency $from, TransactionCurrency $to, string $rate, Carbon $date): CurrencyExchangeRate;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user