firefly-iii/app/Http/Controllers/Json/ExchangeController.php

79 lines
2.8 KiB
PHP
Raw Normal View History

<?php
/**
* ExchangeController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
* 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
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
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-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Log;
/**
2017-11-15 05:25:49 -06:00
* Class ExchangeController.
*/
class ExchangeController extends Controller
{
2018-07-08 05:08:53 -05:00
/** @noinspection MoreThanThreeArgumentsInspection */
/**
* @param Request $request
* @param TransactionCurrency $fromCurrency
* @param TransactionCurrency $toCurrency
* @param Carbon $date
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
*/
2018-07-08 05:28:42 -05:00
public function getRate(Request $request, TransactionCurrency $fromCurrency, TransactionCurrency $toCurrency, Carbon $date): JsonResponse
{
/** @var CurrencyRepositoryInterface $repository */
$repository = app(CurrencyRepositoryInterface::class);
$rate = $repository->getExchangeRate($fromCurrency, $toCurrency, $date);
2018-03-25 02:00:45 -05:00
2018-06-28 00:32:58 -05:00
if (null === $rate) {
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 02:00:45 -05:00
// create service:
/** @var ExchangeRateInterface $service */
$service = app(ExchangeRateInterface::class);
$service->setUser(auth()->user());
// get rate:
$rate = $service->getRate($fromCurrency, $toCurrency, $date);
}
2018-03-25 02:00:45 -05:00
$return = $rate->toArray();
$return['amount'] = null;
2017-11-15 05:25:49 -06:00
if (null !== $request->get('amount')) {
// assume amount is in "from" currency:
2018-04-02 08:10:40 -05:00
$return['amount'] = bcmul($request->get('amount'), (string)$rate->rate, 12);
// round to toCurrency decimal places:
$return['amount'] = round($return['amount'], $toCurrency->decimal_places);
}
2018-03-10 13:30:09 -06:00
return response()->json($return);
}
2017-07-07 01:09:42 -05:00
}