2017-04-13 20:47:59 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ExchangeController.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2017-04-13 20:47:59 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* 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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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/>.
|
2017-04-13 20:47:59 +02:00
|
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace FireflyIII\Http\Controllers\Json;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
|
|
|
use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface;
|
|
|
|
use FireflyIII\Services\Currency\ExchangeRateInterface;
|
2018-07-09 19:24:08 +02:00
|
|
|
use FireflyIII\User;
|
2018-07-08 12:28:42 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-04-13 20:47:59 +02:00
|
|
|
use Illuminate\Http\Request;
|
2017-04-14 10:16:52 +02:00
|
|
|
use Log;
|
2017-04-13 20:47:59 +02:00
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class ExchangeController.
|
2017-04-13 20:47:59 +02:00
|
|
|
*/
|
|
|
|
class ExchangeController extends Controller
|
|
|
|
{
|
2019-08-16 21:38:35 +02:00
|
|
|
|
2017-04-13 20:47:59 +02:00
|
|
|
/**
|
2018-07-21 08:06:24 +02:00
|
|
|
* Returns an exchange rate.
|
|
|
|
*
|
2017-04-13 20:47:59 +02:00
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionCurrency $fromCurrency
|
|
|
|
* @param TransactionCurrency $toCurrency
|
|
|
|
* @param Carbon $date
|
2017-04-13 21:19:00 +02:00
|
|
|
*
|
2018-07-08 12:28:42 +02:00
|
|
|
* @return JsonResponse
|
2017-04-13 20:47:59 +02:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function getRate(Request $request, TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): JsonResponse
|
2017-04-13 20:47:59 +02:00
|
|
|
{
|
|
|
|
/** @var CurrencyRepositoryInterface $repository */
|
|
|
|
$repository = app(CurrencyRepositoryInterface::class);
|
|
|
|
$rate = $repository->getExchangeRate($fromCurrency, $toCurrency, $date);
|
2018-03-25 09:00:45 +02:00
|
|
|
|
|
|
|
|
2018-06-28 07:32:58 +02:00
|
|
|
if (null === $rate) {
|
2017-04-14 10:16:52 +02:00
|
|
|
Log::debug(sprintf('No cached exchange rate in database for %s to %s on %s', $fromCurrency->code, $toCurrency->code, $date->format('Y-m-d')));
|
2018-03-25 09:00:45 +02:00
|
|
|
|
|
|
|
// create service:
|
2018-07-09 19:24:08 +02:00
|
|
|
/** @var User $user */
|
|
|
|
$user = auth()->user();
|
2018-03-25 09:00:45 +02:00
|
|
|
/** @var ExchangeRateInterface $service */
|
|
|
|
$service = app(ExchangeRateInterface::class);
|
2018-07-09 19:24:08 +02:00
|
|
|
$service->setUser($user);
|
2018-03-25 09:00:45 +02:00
|
|
|
|
|
|
|
// get rate:
|
|
|
|
$rate = $service->getRate($fromCurrency, $toCurrency, $date);
|
2017-04-13 20:47:59 +02:00
|
|
|
}
|
2018-03-25 09:00:45 +02:00
|
|
|
|
2017-04-13 20:47:59 +02:00
|
|
|
$return = $rate->toArray();
|
|
|
|
$return['amount'] = null;
|
2017-11-15 12:25:49 +01:00
|
|
|
if (null !== $request->get('amount')) {
|
2017-04-13 20:47:59 +02:00
|
|
|
// assume amount is in "from" currency:
|
2020-03-17 15:01:00 +01:00
|
|
|
$return['amount'] = bcmul($request->get('amount'), (string) $rate->rate, 12);
|
2017-04-14 10:16:52 +02:00
|
|
|
// round to toCurrency decimal places:
|
|
|
|
$return['amount'] = round($return['amount'], $toCurrency->decimal_places);
|
2017-04-13 20:47:59 +02:00
|
|
|
}
|
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json($return);
|
2017-04-13 20:47:59 +02:00
|
|
|
}
|
2017-07-07 08:09:42 +02:00
|
|
|
}
|