diff --git a/app/Api/V2/Controllers/Autocomplete/AccountController.php b/app/Api/V2/Controllers/Autocomplete/AccountController.php index 4f0bdc4931..011895326e 100644 --- a/app/Api/V2/Controllers/Autocomplete/AccountController.php +++ b/app/Api/V2/Controllers/Autocomplete/AccountController.php @@ -33,7 +33,6 @@ use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface; use FireflyIII\Support\Http\Api\AccountFilter; use Illuminate\Http\JsonResponse; -use JsonException; /** * Class AccountController diff --git a/app/Api/V2/Controllers/Chart/AccountController.php b/app/Api/V2/Controllers/Chart/AccountController.php index 3b7f025331..330171cb23 100644 --- a/app/Api/V2/Controllers/Chart/AccountController.php +++ b/app/Api/V2/Controllers/Chart/AccountController.php @@ -33,7 +33,6 @@ use FireflyIII\Models\AccountType; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Administration\Account\AccountRepositoryInterface; use FireflyIII\Support\Http\Api\CleansChartData; -use FireflyIII\User; use Illuminate\Http\JsonResponse; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; diff --git a/app/Api/V2/Controllers/Model/Bill/SumController.php b/app/Api/V2/Controllers/Model/Bill/SumController.php index f8288b833d..1f37b0b5bf 100644 --- a/app/Api/V2/Controllers/Model/Bill/SumController.php +++ b/app/Api/V2/Controllers/Model/Bill/SumController.php @@ -27,7 +27,6 @@ namespace FireflyIII\Api\V2\Controllers\Model\Bill; use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Repositories\Administration\Bill\BillRepositoryInterface; -use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use Illuminate\Http\JsonResponse; /** diff --git a/app/Api/V2/Controllers/Model/Budget/ShowController.php b/app/Api/V2/Controllers/Model/Budget/ShowController.php index b9b5ea1229..74f34709b6 100644 --- a/app/Api/V2/Controllers/Model/Budget/ShowController.php +++ b/app/Api/V2/Controllers/Model/Budget/ShowController.php @@ -29,7 +29,6 @@ use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Models\Budget; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; -use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use Illuminate\Http\JsonResponse; /** @@ -37,8 +36,6 @@ use Illuminate\Http\JsonResponse; */ class ShowController extends Controller { - use ConvertsExchangeRates; - private BudgetRepositoryInterface $repository; /** @@ -63,6 +60,7 @@ class ShowController extends Controller */ public function budgeted(DateRequest $request, Budget $budget): JsonResponse { + die('deprecated use of thing.'); $data = $request->getAll(); $result = $this->repository->budgetedInPeriodForBudget($budget, $data['start'], $data['end']); $converted = $this->cerSum(array_values($result)); @@ -77,6 +75,7 @@ class ShowController extends Controller */ public function spent(DateRequest $request, Budget $budget): JsonResponse { + die('deprecated use of thing.'); $data = $request->getAll(); $result = $this->repository->spentInPeriodForBudget($budget, $data['start'], $data['end']); $converted = $this->cerSum(array_values($result)); diff --git a/app/Api/V2/Controllers/Model/Budget/SumController.php b/app/Api/V2/Controllers/Model/Budget/SumController.php index 15de6ca665..0f27fed42b 100644 --- a/app/Api/V2/Controllers/Model/Budget/SumController.php +++ b/app/Api/V2/Controllers/Model/Budget/SumController.php @@ -27,7 +27,6 @@ namespace FireflyIII\Api\V2\Controllers\Model\Budget; use FireflyIII\Api\V2\Controllers\Controller; use FireflyIII\Api\V2\Request\Generic\DateRequest; use FireflyIII\Repositories\Budget\BudgetRepositoryInterface; -use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use Illuminate\Http\JsonResponse; /** @@ -35,8 +34,6 @@ use Illuminate\Http\JsonResponse; */ class SumController extends Controller { - use ConvertsExchangeRates; - private BudgetRepositoryInterface $repository; /** @@ -64,6 +61,7 @@ class SumController extends Controller */ public function budgeted(DateRequest $request): JsonResponse { + die('deprecated use of thing.'); $data = $request->getAll(); $result = $this->repository->budgetedInPeriod($data['start'], $data['end']); $converted = $this->cerSum(array_values($result)); @@ -81,6 +79,7 @@ class SumController extends Controller */ public function spent(DateRequest $request): JsonResponse { + die('deprecated use of thing.'); $data = $request->getAll(); $result = $this->repository->spentInPeriod($data['start'], $data['end']); $converted = $this->cerSum(array_values($result)); diff --git a/app/Api/V2/Controllers/NetWorthController.php b/app/Api/V2/Controllers/NetWorthController.php index 1c7d196ef1..0679eb561d 100644 --- a/app/Api/V2/Controllers/NetWorthController.php +++ b/app/Api/V2/Controllers/NetWorthController.php @@ -26,7 +26,6 @@ namespace FireflyIII\Api\V2\Controllers; use FireflyIII\Api\V2\Request\Generic\SingleDateRequest; use FireflyIII\Helpers\Report\NetWorthInterface; -use FireflyIII\Support\Http\Api\ConvertsExchangeRates; use Illuminate\Http\JsonResponse; /** @@ -34,8 +33,6 @@ use Illuminate\Http\JsonResponse; */ class NetWorthController extends Controller { - use ConvertsExchangeRates; - private NetWorthInterface $netWorth; /** @@ -64,6 +61,7 @@ class NetWorthController extends Controller */ public function get(SingleDateRequest $request): JsonResponse { + die('deprecated use of thing.'); $date = $request->getDate(); $result = $this->netWorth->sumNetWorthByCurrency($date); $converted = $this->cerSum($result); diff --git a/app/Support/Http/Api/ConvertsExchangeRates.php b/app/Support/Http/Api/ConvertsExchangeRates.php index b34eecd144..e4bcef608b 100644 --- a/app/Support/Http/Api/ConvertsExchangeRates.php +++ b/app/Support/Http/Api/ConvertsExchangeRates.php @@ -103,141 +103,6 @@ trait ConvertsExchangeRates return $result; } - /** - * @param TransactionCurrency $from - * @param TransactionCurrency $to - * @param Carbon $date - * - * @return string - * @throws FireflyException - * - * @deprecated - */ - private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string - { - // first attempt: - $rate = $this->getFromDB((int)$from->id, (int)$to->id, $date->format('Y-m-d')); - if (null !== $rate) { - return $rate; - } - // no result. perhaps the other way around? - $rate = $this->getFromDB((int)$to->id, (int)$from->id, $date->format('Y-m-d')); - if (null !== $rate) { - return bcdiv('1', $rate); - } - - // if nothing in place, fall back on the rate for $from to EUR - $first = $this->getEuroRate($from, $date); - $second = $this->getEuroRate($to, $date); - - // combined (if present), they can be used to calculate the necessary conversion rate. - if ('0' === $first || '0' === $second) { - return '0'; - } - - $second = bcdiv('1', $second); - return bcmul($first, $second); - } - - /** - * @param int $from - * @param int $to - * @param string $date - * - * @return string|null - * - * @deprecated - */ - private function getFromDB(int $from, int $to, string $date): ?string - { - $key = sprintf('cer-%d-%d-%s', $from, $to, $date); - - $cache = new CacheProperties(); - $cache->addProperty($key); - if ($cache->has()) { - $rate = $cache->get(); - if ('' === $rate) { - return null; - } - return $rate; - } - app('log')->debug(sprintf('Going to get rate #%d->#%d (%s) from DB.', $from, $to, $date)); - - /** @var CurrencyExchangeRate $result */ - $result = auth()->user() - ->currencyExchangeRates() - ->where('from_currency_id', $from) - ->where('to_currency_id', $to) - ->where('date', '<=', $date) - ->orderBy('date', 'DESC') - ->first(); - $rate = (string)$result?->rate; - $cache->store($rate); - if ('' === $rate) { - return null; - } - return $rate; - } - - /** - * @param TransactionCurrency $currency - * @param Carbon $date - * - * @return string - * @throws FireflyException - * - * @deprecated - */ - private function getEuroRate(TransactionCurrency $currency, Carbon $date): string - { - $euroId = $this->getEuroId(); - if ($euroId === (int)$currency->id) { - return '1'; - } - $rate = $this->getFromDB((int)$currency->id, $euroId, $date->format('Y-m-d')); - - if (null !== $rate) { - // app('log')->debug(sprintf('Rate for %s to EUR is %s.', $currency->code, $rate)); - return $rate; - } - $rate = $this->getFromDB($euroId, (int)$currency->id, $date->format('Y-m-d')); - if (null !== $rate) { - $rate = bcdiv('1', $rate); - // app('log')->debug(sprintf('Inverted rate for %s to EUR is %s.', $currency->code, $rate)); - return $rate; - } - // grab backup values from config file: - $backup = config(sprintf('cer.rates.%s', $currency->code)); - if (null !== $backup) { - $backup = bcdiv('1', (string)$backup); - // app('log')->debug(sprintf('Backup rate for %s to EUR is %s.', $currency->code, $backup)); - return $backup; - } - - // app('log')->debug(sprintf('No rate for %s to EUR.', $currency->code)); - return '0'; - } - - /** - * @return int - * @throws FireflyException - * - * @deprecated - */ - private function getEuroId(): int - { - $cache = new CacheProperties(); - $cache->addProperty('cer-euro-id'); - if ($cache->has()) { - return $cache->get(); - } - $euro = TransactionCurrency::whereCode('EUR')->first(); - if (null === $euro) { - throw new FireflyException('Cannot find EUR in system, cannot do currency conversion.'); - } - $cache->store((int)$euro->id); - return (int)$euro->id; - } /** * For a sum of entries, get the exchange rate to the native currency of diff --git a/app/Support/Http/Api/ExchangeRateConverter.php b/app/Support/Http/Api/ExchangeRateConverter.php index 3996e5c394..a3fb105ee3 100644 --- a/app/Support/Http/Api/ExchangeRateConverter.php +++ b/app/Support/Http/Api/ExchangeRateConverter.php @@ -25,14 +25,16 @@ namespace FireflyIII\Support\Http\Api; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Models\CurrencyExchangeRate; use FireflyIII\Models\TransactionCurrency; +use FireflyIII\Support\CacheProperties; /** * Class ExchangeRateConverter */ class ExchangeRateConverter { - use ConvertsExchangeRates; + //use ConvertsExchangeRates; /** * @param TransactionCurrency $from @@ -59,17 +61,138 @@ class ExchangeRateConverter */ public function getCurrencyRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string { - if (null === $this->enabled) { - $this->getPreference(); - } - - // if not enabled, return "1" - if (false === $this->enabled) { - return '1'; - } - $rate = $this->getRate($from, $to, $date); return '0' === $rate ? '1' : $rate; } + /** + * @param TransactionCurrency $from + * @param TransactionCurrency $to + * @param Carbon $date + * + * @return string + * @throws FireflyException + */ + private function getRate(TransactionCurrency $from, TransactionCurrency $to, Carbon $date): string + { + // first attempt: + $rate = $this->getFromDB((int)$from->id, (int)$to->id, $date->format('Y-m-d')); + if (null !== $rate) { + return $rate; + } + // no result. perhaps the other way around? + $rate = $this->getFromDB((int)$to->id, (int)$from->id, $date->format('Y-m-d')); + if (null !== $rate) { + return bcdiv('1', $rate); + } + + // if nothing in place, fall back on the rate for $from to EUR + $first = $this->getEuroRate($from, $date); + $second = $this->getEuroRate($to, $date); + + // combined (if present), they can be used to calculate the necessary conversion rate. + if ('0' === $first || '0' === $second) { + return '0'; + } + + $second = bcdiv('1', $second); + return bcmul($first, $second); + } + + /** + * @param int $from + * @param int $to + * @param string $date + * + * @return string|null + */ + private function getFromDB(int $from, int $to, string $date): ?string + { + $key = sprintf('cer-%d-%d-%s', $from, $to, $date); + + $cache = new CacheProperties(); + $cache->addProperty($key); + if ($cache->has()) { + $rate = $cache->get(); + if ('' === $rate) { + return null; + } + return $rate; + } + app('log')->debug(sprintf('Going to get rate #%d->#%d (%s) from DB.', $from, $to, $date)); + + /** @var CurrencyExchangeRate $result */ + $result = auth()->user() + ->currencyExchangeRates() + ->where('from_currency_id', $from) + ->where('to_currency_id', $to) + ->where('date', '<=', $date) + ->orderBy('date', 'DESC') + ->first(); + $rate = (string)$result?->rate; + $cache->store($rate); + if ('' === $rate) { + return null; + } + return $rate; + } + + + /** + * @param TransactionCurrency $currency + * @param Carbon $date + * + * @return string + * @throws FireflyException + * + */ + private function getEuroRate(TransactionCurrency $currency, Carbon $date): string + { + $euroId = $this->getEuroId(); + if ($euroId === (int)$currency->id) { + return '1'; + } + $rate = $this->getFromDB((int)$currency->id, $euroId, $date->format('Y-m-d')); + + if (null !== $rate) { + // app('log')->debug(sprintf('Rate for %s to EUR is %s.', $currency->code, $rate)); + return $rate; + } + $rate = $this->getFromDB($euroId, (int)$currency->id, $date->format('Y-m-d')); + if (null !== $rate) { + return bcdiv('1', $rate); + // app('log')->debug(sprintf('Inverted rate for %s to EUR is %s.', $currency->code, $rate)); + //return $rate; + } + // grab backup values from config file: + $backup = config(sprintf('cer.rates.%s', $currency->code)); + if (null !== $backup) { + return bcdiv('1', (string)$backup); + // app('log')->debug(sprintf('Backup rate for %s to EUR is %s.', $currency->code, $backup)); + //return $backup; + } + + // app('log')->debug(sprintf('No rate for %s to EUR.', $currency->code)); + return '0'; + } + + + /** + * @return int + * @throws FireflyException + */ + private function getEuroId(): int + { + $cache = new CacheProperties(); + $cache->addProperty('cer-euro-id'); + if ($cache->has()) { + return (int)$cache->get(); + } + $euro = TransactionCurrency::whereCode('EUR')->first(); + if (null === $euro) { + throw new FireflyException('Cannot find EUR in system, cannot do currency conversion.'); + } + $cache->store((int)$euro->id); + return (int)$euro->id; + } } diff --git a/app/Transformers/V2/TransactionGroupTransformer.php b/app/Transformers/V2/TransactionGroupTransformer.php index 154917179e..9b16fd3e14 100644 --- a/app/Transformers/V2/TransactionGroupTransformer.php +++ b/app/Transformers/V2/TransactionGroupTransformer.php @@ -306,6 +306,13 @@ class TransactionGroupTransformer extends AbstractTransformer if (10 === strlen($string)) { return Carbon::createFromFormat('Y-m-d', $string, config('app.timezone')); } + if (25 === strlen($string)) { + return Carbon::parse($string, config('app.timezone')); + } + if (19 === strlen($string) && str_contains($string, 'T')) { + return Carbon::createFromFormat('Y-m-d\TH:i:s', substr($string, 0, 19), config('app.timezone')); + } + // 2022-01-01 01:01:01 return Carbon::createFromFormat('Y-m-d H:i:s', substr($string, 0, 19), config('app.timezone')); }