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

277 lines
7.2 KiB
PHP
Raw Normal View History

2015-04-05 13:47:19 -05:00
<?php
/**
* CurrencyRepository.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2015-04-05 13:47:19 -05:00
namespace FireflyIII\Repositories\Currency;
use Carbon\Carbon;
use FireflyIII\Models\CurrencyExchangeRate;
2015-04-05 13:47:19 -05:00
use FireflyIII\Models\Preference;
use FireflyIII\Models\TransactionCurrency;
2016-12-11 09:25:46 -06:00
use FireflyIII\User;
2015-04-05 13:47:19 -05:00
use Illuminate\Support\Collection;
use Log;
2016-12-11 09:25:46 -06:00
use Preferences;
2015-04-05 13:47:19 -05:00
/**
* Class CurrencyRepository
*
* @package FireflyIII\Repositories\Currency
*/
class CurrencyRepository implements CurrencyRepositoryInterface
{
2016-12-11 09:25:46 -06:00
/** @var User */
private $user;
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function canDeleteCurrency(TransactionCurrency $currency): bool
{
if ($this->countJournals($currency) > 0) {
return false;
}
// is the only currency left
if ($this->get()->count() === 1) {
return false;
}
// is the default currency for the user or the system
$defaultCode = Preferences::getForUser($this->user, 'currencyPreference', config('firefly.default_currency', 'EUR'))->data;
if ($currency->code === $defaultCode) {
return false;
}
// is the default currency for the system
$defaultSystemCode = config('firefly.default_currency', 'EUR');
if ($currency->code === $defaultSystemCode) {
return false;
}
// can be deleted
return true;
}
2015-04-05 13:47:19 -05:00
/**
* @param TransactionCurrency $currency
*
* @return int
*/
2016-04-05 15:00:03 -05:00
public function countJournals(TransactionCurrency $currency): int
2015-04-05 13:47:19 -05:00
{
return $currency->transactionJournals()->count();
}
2016-12-11 09:25:46 -06:00
/**
* @param TransactionCurrency $currency
*
* @return bool
*/
public function destroy(TransactionCurrency $currency): bool
{
if ($this->user->hasRole('owner')) {
$currency->forceDelete();
}
return true;
}
2016-04-01 07:10:08 -05:00
/**
* Find by ID
*
* @param int $currencyId
*
* @return TransactionCurrency
*/
public function find(int $currencyId): TransactionCurrency
2016-04-01 07:10:08 -05:00
{
$currency = TransactionCurrency::find($currencyId);
if (is_null($currency)) {
$currency = new TransactionCurrency;
}
return $currency;
}
/**
* Find by currency code
*
* @param string $currencyCode
*
* @return TransactionCurrency
*/
public function findByCode(string $currencyCode): TransactionCurrency
2016-04-01 07:10:08 -05:00
{
2017-03-16 14:46:18 -05:00
$currency = TransactionCurrency::where('code', $currencyCode)->first();
2016-04-01 07:10:08 -05:00
if (is_null($currency)) {
$currency = new TransactionCurrency;
}
return $currency;
}
/**
* Find by currency name
*
* @param string $currencyName
*
* @return TransactionCurrency
*/
public function findByName(string $currencyName): TransactionCurrency
2016-04-01 07:10:08 -05:00
{
$preferred = TransactionCurrency::whereName($currencyName)->first();
if (is_null($preferred)) {
$preferred = new TransactionCurrency;
}
return $preferred;
}
/**
* Find by currency symbol
*
* @param string $currencySymbol
*
* @return TransactionCurrency
*/
public function findBySymbol(string $currencySymbol): TransactionCurrency
2016-04-01 07:10:08 -05:00
{
$currency = TransactionCurrency::whereSymbol($currencySymbol)->first();
if (is_null($currency)) {
$currency = new TransactionCurrency;
}
return $currency;
}
2015-04-05 13:47:19 -05:00
/**
* @return Collection
*/
2016-04-05 15:00:03 -05:00
public function get(): Collection
2015-04-05 13:47:19 -05:00
{
return TransactionCurrency::get();
}
2017-08-18 08:14:44 -05:00
/**
* @param array $ids
*
* @return Collection
*/
public function getByIds(array $ids): Collection
{
return TransactionCurrency::whereIn('id', $ids)->get();
}
2015-04-05 13:47:19 -05:00
/**
* @param Preference $preference
*
* @return TransactionCurrency
*/
2016-04-05 15:00:03 -05:00
public function getCurrencyByPreference(Preference $preference): TransactionCurrency
2015-04-05 13:47:19 -05:00
{
2017-03-16 14:46:18 -05:00
$preferred = TransactionCurrency::where('code', $preference->data)->first();
2015-04-05 13:47:19 -05:00
if (is_null($preferred)) {
$preferred = TransactionCurrency::first();
}
return $preferred;
}
/**
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
* @return CurrencyExchangeRate
*/
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;
}
$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 (!is_null($rate)) {
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 new CurrencyExchangeRate;
}
2017-02-16 23:42:36 -06:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
2015-04-05 13:47:19 -05:00
/**
* @param array $data
*
* @return TransactionCurrency
*/
2016-04-05 15:00:03 -05:00
public function store(array $data): TransactionCurrency
2015-04-05 13:47:19 -05:00
{
2017-02-24 22:57:01 -06:00
/** @var TransactionCurrency $currency */
2015-04-05 13:47:19 -05:00
$currency = TransactionCurrency::create(
[
'name' => $data['name'],
'code' => $data['code'],
'symbol' => $data['symbol'],
'decimal_places' => $data['decimal_places'],
2015-04-05 13:47:19 -05:00
]
);
return $currency;
}
/**
* @param TransactionCurrency $currency
* @param array $data
*
* @return TransactionCurrency
*/
2016-04-05 15:00:03 -05:00
public function update(TransactionCurrency $currency, array $data): TransactionCurrency
2015-04-05 13:47:19 -05:00
{
$currency->code = $data['code'];
$currency->symbol = $data['symbol'];
$currency->name = $data['name'];
$currency->decimal_places = $data['decimal_places'];
2015-04-05 13:47:19 -05:00
$currency->save();
return $currency;
}
2015-04-07 11:25:21 -05:00
}