Commit 76da9dd0 authored by Dmitrii Poddubnyi's avatar Dmitrii Poddubnyi
Browse files

Fixed exchange rates service

parent a0bed32f
Pipeline #5778 passed with stages
in 2 minutes and 6 seconds
......@@ -4,17 +4,17 @@
"require": {
"php": "^7.4",
"ext-ctype": "*",
"ext-gd": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-sqlite3": "*",
"ext-gd": "*",
"consolibyte/quickbooks": "dev-qbxmlops130",
"craue/formflow-bundle": "^3.3",
"doctrine/doctrine-bundle": "^1.11",
"doctrine/doctrine-migrations-bundle": "^2.0",
"doctrine/orm": "^2.6",
"easycorp/easyadmin-bundle": "^2.2",
"florianv/exchanger": "^2.2",
"florianv/exchanger": "^2.7",
"league/flysystem-webdav": "^1.0.7",
"nyholm/psr7": "^1.1",
"oneup/flysystem-bundle": "^3.1",
......
......@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "3e2d2c73c4374f2d0b9b2f5f02def08f",
"content-hash": "724007e0c6e01775913b66c306ff0899",
"packages": [
{
"name": "brick/math",
......@@ -57,12 +57,12 @@
"version": "v1.4.1",
"source": {
"type": "git",
"url": "https://github.com/clue/php-stream-filter.git",
"url": "https://github.com/clue/stream-filter.git",
"reference": "5a58cc30a8bd6a4eb8f856adf61dd3e013f53f71"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/clue/php-stream-filter/zipball/5a58cc30a8bd6a4eb8f856adf61dd3e013f53f71",
"url": "https://api.github.com/repos/clue/stream-filter/zipball/5a58cc30a8bd6a4eb8f856adf61dd3e013f53f71",
"reference": "5a58cc30a8bd6a4eb8f856adf61dd3e013f53f71",
"shasum": ""
},
......@@ -802,6 +802,7 @@
"cache",
"caching"
],
"abandoned": true,
"time": "2019-11-29T11:22:01+00:00"
},
{
......@@ -1469,6 +1470,7 @@
"reflection",
"static"
],
"abandoned": "roave/better-reflection",
"time": "2020-03-27T11:06:43+00:00"
},
{
......@@ -1616,20 +1618,21 @@
},
{
"name": "florianv/exchanger",
"version": "2.4.0",
"version": "2.7.0",
"source": {
"type": "git",
"url": "https://github.com/florianv/exchanger.git",
"reference": "eaece7c0f16d5b923b55d4b81a0c90c4e93708b8"
"reference": "082894c630e9dc03154ef75b1bbb5586b7c2841a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/florianv/exchanger/zipball/eaece7c0f16d5b923b55d4b81a0c90c4e93708b8",
"reference": "eaece7c0f16d5b923b55d4b81a0c90c4e93708b8",
"url": "https://api.github.com/repos/florianv/exchanger/zipball/082894c630e9dc03154ef75b1bbb5586b7c2841a",
"reference": "082894c630e9dc03154ef75b1bbb5586b7c2841a",
"shasum": ""
},
"require": {
"php": "^7.1.3",
"ext-simplexml": "*",
"php": "^7.1.3 || ^8.0",
"php-http/client-implementation": "^1.0",
"php-http/discovery": "^1.6",
"php-http/httplug": "^1.0 || ^2.0",
......@@ -1640,7 +1643,7 @@
"nyholm/psr7": "^1.0",
"php-http/message": "^1.7",
"php-http/mock-client": "^1.0",
"phpunit/phpunit": "^7.5"
"phpunit/phpunit": "^7 || ^8 || ^9.4"
},
"suggest": {
"php-http/guzzle6-adapter": "Required to use Guzzle for sending HTTP requests",
......@@ -1677,7 +1680,7 @@
"exchange rates",
"money"
],
"time": "2020-03-03T10:00:48+00:00"
"time": "2021-04-26T07:00:48+00:00"
},
{
"name": "guzzlehttp/guzzle",
......@@ -2757,12 +2760,12 @@
"version": "v2.1.3",
"source": {
"type": "git",
"url": "https://github.com/whiteoctober/Pagerfanta.git",
"url": "https://github.com/BabDev/Pagerfanta.git",
"reference": "a53ff01d521648d9dbca19b93ac6bc75a59b0972"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/whiteoctober/Pagerfanta/zipball/a53ff01d521648d9dbca19b93ac6bc75a59b0972",
"url": "https://api.github.com/repos/BabDev/Pagerfanta/zipball/a53ff01d521648d9dbca19b93ac6bc75a59b0972",
"reference": "a53ff01d521648d9dbca19b93ac6bc75a59b0972",
"shasum": ""
},
......@@ -8046,12 +8049,12 @@
"version": "1.8.0",
"source": {
"type": "git",
"url": "https://github.com/webmozart/assert.git",
"url": "https://github.com/webmozarts/assert.git",
"reference": "ab2cb0b3b559010b75981b1bdce728da3ee90ad6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webmozart/assert/zipball/ab2cb0b3b559010b75981b1bdce728da3ee90ad6",
"url": "https://api.github.com/repos/webmozarts/assert/zipball/ab2cb0b3b559010b75981b1bdce728da3ee90ad6",
"reference": "ab2cb0b3b559010b75981b1bdce728da3ee90ad6",
"shasum": ""
},
......@@ -9007,10 +9010,11 @@
"platform": {
"php": "^7.4",
"ext-ctype": "*",
"ext-gd": "*",
"ext-iconv": "*",
"ext-intl": "*",
"ext-sqlite3": "*",
"ext-gd": "*"
"ext-sqlite3": "*"
},
"platform-dev": []
"platform-dev": [],
"plugin-api-version": "1.1.0"
}
......@@ -4,9 +4,9 @@ namespace App\Currency;
use App\Exception\RuntimeException;
use DateTime;
use Exception;
use Exchanger\Contract\ExchangeRateService;
use Exchanger\Service\EuropeanCentralBank;
use Exchanger\Service\ExchangeRatesApi;
use Exchanger\Service\WebserviceX;
use Exchanger\ExchangeRateQueryBuilder;
use Exchanger\Service\Chain;
use Http\Adapter\Guzzle6\Client;
......@@ -23,22 +23,27 @@ class CurrencyExchanger implements CurrencyExchangerInterface
public function getExchangeRate(string $base, string $target, string $date): float
{
if ($base === $target) {
return 1.0;
}
$client = new Client();
$exchanger = new Chain([
new ExchangeRatesApi($client, $this->requestFactory),
new WebserviceX($client, $this->requestFactory),
$exchangerEuroBased = new Chain([
new EuropeanCentralBank($client, $this->requestFactory),
new ExchangeRatesApi($client, $this->requestFactory, ['access_key' => '403571724895eb3f98edce29864cceb4']),
]);
$currencyPair = "{$base}/{$target}";
try {
$query = new ExchangeRateQueryBuilder($currencyPair);
$query->setDate(new DateTime($date));
$rate = $exchanger->getExchangeRate($query->build());
$rate = 1 / $rate->getValue();
return $rate;
} catch (Exception $e) {
throw new RuntimeException("Unable to get currency rate. Currency pair: {$currencyPair}, date: {$date}");
$rateBase = $this->getInternalExchangeRate($exchangerEuroBased, "EUR/{$base}", $date);
$rateTarget = $this->getInternalExchangeRate($exchangerEuroBased, "EUR/{$target}", $date);
return $rateBase / $rateTarget;
} catch (\Throwable $e) {
throw new RuntimeException("Unable to get currency rate. Currency pair: {$base}/{$target}, date: {$date}. Error: {$e->getMessage()}");
}
}
public function getInternalExchangeRate(ExchangeRateService $exchanger, string $currencyPair, string $date): float
{
$query = new ExchangeRateQueryBuilder($currencyPair);
$query->setDate(new DateTime($date));
return $exchanger->getExchangeRate($query->build())->getValue();
}
}
......@@ -30,6 +30,7 @@ class TransactionsConverter
const DEFAULT_EXPENSE_ACCOUNT = 'Ask My Accountant';
private const ACCOUNTS_MAPPING = [
//aknos hardcoded id
'aknos' => [
'Активы:Текущие активы:MKB HUF' => 'MKB Bank HUF',
'Активы:Текущие активы:MKB EUR' => 'MKB Bank EUR',
......@@ -46,7 +47,8 @@ class TransactionsConverter
// 'Активы:Текущие активы:HSBC USD Savings'
// 'Расходы:HK AKNOS (USD)'
],
'optdeal' => [
//optdeal hardcoded id
'a407629c-57fc-4237-afb1-d77871db2bfb' => [
'Активы:Текущие активы:HSBC Current' => 'HSBC HKD Current',
'Активы:Текущие активы:HSBC Savings' => 'HSBC HKD Savings',
'Активы:Текущие активы:HSBC USD Savings' => 'HSBC USD Savings',
......
......@@ -14,7 +14,7 @@ use Symfony\Component\Serializer\Serializer;
class TransactionsConverterTest extends TestCase
{
const COMPANY = 'optdeal';
const COMPANY = 'a407629c-57fc-4237-afb1-d77871db2bfb';
const COMPANY2 = 'aknos';
/** @var CsvEncoder */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment