. */ declare(strict_types=1); namespace FireflyIII\Repositories\Currency; use Carbon\Carbon; use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Support\Repositories\UserGroup\UserGroupInterface; use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; use FireflyIII\User; use Illuminate\Support\Collection; /** * Class CurrencyRepository. */ class CurrencyRepository implements CurrencyRepositoryInterface, UserGroupInterface { use UserGroupTrait; public function searchCurrency(string $search, int $limit): Collection { $query = TransactionCurrency::where('enabled', true); if ('' !== $search) { $query->whereLike('name', sprintf('%%%s%%', $search)); } return $query->take($limit)->get(); } #[\Override] public function find(int $currencyId): ?TransactionCurrency { return TransactionCurrency::find($currencyId); } /** * Find by currency code, return NULL if unfound. */ public function findByCode(string $currencyCode): ?TransactionCurrency { return TransactionCurrency::where('code', $currencyCode)->first(); } /** * Returns the complete set of transactions but needs * no user object. */ public function getCompleteSet(): Collection { return TransactionCurrency::where('enabled', true)->orderBy('code', 'ASC')->get(); } public function get(): Collection { $all = $this->userGroup->currencies()->orderBy('code', 'ASC')->withPivot(['group_default'])->get(); $all->map(static function (TransactionCurrency $current) { // @phpstan-ignore-line $current->userGroupEnabled = true; $current->userGroupNative = 1 === (int) $current->pivot->group_default; return $current; }); /** @var Collection */ return $all; } /** * Get currency exchange rate. */ 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 null|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) { 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; } return null; } /** * TODO must be a factory */ public function setExchangeRate(TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date, float $rate): CurrencyExchangeRate { return CurrencyExchangeRate::create( [ '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, ] ); } }