Commit 6cf62b9b authored by Dmitrii Poddubnyi's avatar Dmitrii Poddubnyi
Browse files

Fixed exchange rates service

parent a0bed32f
Pipeline #5766 passed with stages
in 2 minutes and 12 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": "dev-exchange-rates-adapting-new-api",
"league/flysystem-webdav": "^1.0.7",
"nyholm/psr7": "^1.1",
"oneup/flysystem-bundle": "^3.1",
......@@ -81,6 +81,10 @@
{
"type": "git",
"url": "https://github.com/karser/quickbooks-php.git"
},
{
"type": "git",
"url": "https://github.com/Seagull-4auKa/exchanger.git"
}
],
"conflict": {
......
......@@ -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": "3b12ab735dfbe35fab2e168f0a72c3e2",
"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,15 @@
},
{
"name": "florianv/exchanger",
"version": "2.4.0",
"version": "dev-exchange-rates-adapting-new-api",
"source": {
"type": "git",
"url": "https://github.com/florianv/exchanger.git",
"reference": "eaece7c0f16d5b923b55d4b81a0c90c4e93708b8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/florianv/exchanger/zipball/eaece7c0f16d5b923b55d4b81a0c90c4e93708b8",
"reference": "eaece7c0f16d5b923b55d4b81a0c90c4e93708b8",
"shasum": ""
"url": "https://github.com/Seagull-4auKa/exchanger.git",
"reference": "0e93e602455feac0566a76afef2c58469128255e"
},
"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 +1637,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",
......@@ -1657,7 +1654,16 @@
"Exchanger\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"autoload-dev": {
"psr-4": {
"Exchanger\\": "tests/"
}
},
"scripts": {
"test": [
"vendor/bin/phpunit"
]
},
"license": [
"MIT"
],
......@@ -1671,13 +1677,13 @@
"description": "Currency exchange rates framework for PHP",
"homepage": "https://github.com/florianv/exchanger",
"keywords": [
"Rate",
"conversion",
"currency",
"exchange rates",
"money"
"money",
"rate"
],
"time": "2020-03-03T10:00:48+00:00"
"time": "2021-04-23T16:06:48+00:00"
},
{
"name": "guzzlehttp/guzzle",
......@@ -2757,12 +2763,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 +8052,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": ""
},
......@@ -9000,17 +9006,19 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"consolibyte/quickbooks": 20
"consolibyte/quickbooks": 20,
"florianv/exchanger": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"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;
......@@ -24,21 +24,23 @@ class CurrencyExchanger implements CurrencyExchangerInterface
public function getExchangeRate(string $base, string $target, string $date): float
{
$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