mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +02:00
Added an console command for updating currency exchange rates.
This commit is contained in:
parent
2468409212
commit
568367b59e
9 changed files with 1371 additions and 91 deletions
2
.env
2
.env
|
@ -37,6 +37,6 @@ MAILER_URL=null://localhost
|
|||
|
||||
### Custom vars
|
||||
|
||||
|
||||
FIXER_API_KEY=CHANGEME
|
||||
|
||||
### End custom vars
|
|
@ -8,10 +8,15 @@
|
|||
"ext-intl": "*",
|
||||
"ext-mbstring": "*",
|
||||
"doctrine/annotations": "^1.6",
|
||||
"florianv/swap": "^4.0",
|
||||
"friendsofsymfony/ckeditor-bundle": "^2.0",
|
||||
"gerardojbaez/money": "^0.3.1",
|
||||
"nyholm/psr7": "^1.1",
|
||||
"ocramius/proxy-manager": "2.1.*",
|
||||
"omines/datatables-bundle": "^0.2.2",
|
||||
"php-http/curl-client": "^2.0",
|
||||
"php-http/guzzle6-adapter": "^2.0",
|
||||
"php-http/message": "^1.8",
|
||||
"php-translation/symfony-bundle": "^0.8.1",
|
||||
"s9e/text-formatter": "^2.0",
|
||||
"sensio/framework-extra-bundle": "^5.1",
|
||||
|
|
1258
composer.lock
generated
1258
composer.lock
generated
File diff suppressed because it is too large
Load diff
21
config/packages/nyholm_psr7.yaml
Normal file
21
config/packages/nyholm_psr7.yaml
Normal file
|
@ -0,0 +1,21 @@
|
|||
services:
|
||||
# Register nyholm/psr7 services for autowiring with PSR-17 (HTTP factories)
|
||||
Psr\Http\Message\RequestFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
Psr\Http\Message\ResponseFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
Psr\Http\Message\ServerRequestFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
Psr\Http\Message\StreamFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
Psr\Http\Message\UploadedFileFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
Psr\Http\Message\UriFactoryInterface: '@nyholm.psr7.psr17_factory'
|
||||
|
||||
# Register nyholm/psr7 services for autowiring with HTTPlug factories
|
||||
Http\Message\MessageFactory: '@nyholm.psr7.httplug_factory'
|
||||
Http\Message\RequestFactory: '@nyholm.psr7.httplug_factory'
|
||||
Http\Message\ResponseFactory: '@nyholm.psr7.httplug_factory'
|
||||
Http\Message\StreamFactory: '@nyholm.psr7.httplug_factory'
|
||||
Http\Message\UriFactory: '@nyholm.psr7.httplug_factory'
|
||||
|
||||
nyholm.psr7.psr17_factory:
|
||||
class: Nyholm\Psr7\Factory\Psr17Factory
|
||||
|
||||
nyholm.psr7.httplug_factory:
|
||||
class: Nyholm\Psr7\Factory\HttplugFactory
|
|
@ -37,5 +37,10 @@ services:
|
|||
tags:
|
||||
- { name: "doctrine.orm.entity_listener" }
|
||||
|
||||
App\Command\UpdateExchangeRatesCommand:
|
||||
arguments:
|
||||
$base_current: '%default_currency%'
|
||||
|
||||
|
||||
# add more service definitions when explicit configuration is needed
|
||||
# please note that last definitions always *replace* previous ones
|
||||
|
|
102
src/Command/UpdateExchangeRatesCommand.php
Normal file
102
src/Command/UpdateExchangeRatesCommand.php
Normal file
|
@ -0,0 +1,102 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Form\AdminPages\CurrencyAdminForm;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use PHPUnit\Runner\Exception;
|
||||
use Swap\Builder;
|
||||
use Swap\Swap;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
class UpdateExchangeRatesCommand extends Command
|
||||
{
|
||||
protected static $defaultName = 'app:update-exchange-rates';
|
||||
|
||||
protected $base_current;
|
||||
protected $em;
|
||||
|
||||
public function __construct(string $base_current, EntityManagerInterface $entityManager)
|
||||
{
|
||||
//$this->swap = $swap;
|
||||
$this->base_current = $base_current;
|
||||
|
||||
$this->em = $entityManager;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this
|
||||
->setDescription('Updates the currency exchange rates.')
|
||||
->addArgument('iso_code', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The ISO Codes of the currencies that should be updated.')
|
||||
->addOption('service', null, InputOption::VALUE_REQUIRED,
|
||||
'Which service should be used for requesting the exchange rates (e.g. fixer). See florianv/swap for full list.',
|
||||
'exchange_rates_api')
|
||||
->addOption('api_key', null, InputOption::VALUE_REQUIRED,
|
||||
'The API key to use for the service.',
|
||||
null);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
|
||||
//Check for valid base current
|
||||
if (strlen($this->base_current) !== 3) {
|
||||
$io->error("Choosen Base current is not valid. Check your settings!");
|
||||
return;
|
||||
}
|
||||
|
||||
$io->note('Update currency exchange rates with base currency: ' . $this->base_current);
|
||||
|
||||
$service = $input->getOption('service');
|
||||
$api_key = $input->getOption('api_key');
|
||||
|
||||
//Construct Swap with the given options
|
||||
$swap = (new Builder())
|
||||
->add($service, ['access_key' => $api_key])
|
||||
->build();
|
||||
|
||||
//Check what currencies we need to update:
|
||||
$iso_code = $input->getArgument('iso_code');
|
||||
$repo = $this->em->getRepository(Currency::class);
|
||||
$candidates = array();
|
||||
|
||||
if (!empty($iso_code)) {
|
||||
$candidates = $repo->findBy(['iso_code' => $iso_code]);
|
||||
} else {
|
||||
$candidates = $repo->findAll();
|
||||
}
|
||||
|
||||
$success_counter = 0;
|
||||
|
||||
//Iterate over each candidate and update exchange rate
|
||||
foreach ($candidates as $currency) {
|
||||
try {
|
||||
$rate = $swap->latest($currency->getIsoCode() . '/' . $this->base_current);
|
||||
$currency->setExchangeRate($rate->getValue());
|
||||
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate()));
|
||||
$this->em->persist($currency);
|
||||
|
||||
$success_counter++;
|
||||
} catch (\Exchanger\Exception\Exception $ex) {
|
||||
$io->warning(sprintf('Error updating %s:', $currency->getIsoCode()));
|
||||
$io->warning($ex->getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Save to database
|
||||
$this->em->flush();
|
||||
|
||||
$io->success(sprintf('%d (of %d) currency exchange rates were updated.', $success_counter, count($candidates)));
|
||||
}
|
||||
}
|
|
@ -52,6 +52,7 @@ class CurrencyAdminForm extends BaseEntityAdminForm
|
|||
|
||||
$builder->add('exchange_rate', MoneyType::class, ['required' => false,
|
||||
'label' => 'currency.exchange_rate.label', 'currency' => $this->params->get('default_currency'),
|
||||
'scale' => 6,
|
||||
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
|
||||
}
|
||||
}
|
66
symfony.lock
66
symfony.lock
|
@ -1,4 +1,7 @@
|
|||
{
|
||||
"clue/stream-filter": {
|
||||
"version": "v1.4.1"
|
||||
},
|
||||
"doctrine/annotations": {
|
||||
"version": "1.0",
|
||||
"recipe": {
|
||||
|
@ -96,6 +99,12 @@
|
|||
"fig/link-util": {
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"florianv/exchanger": {
|
||||
"version": "1.4.1"
|
||||
},
|
||||
"florianv/swap": {
|
||||
"version": "3.5.0"
|
||||
},
|
||||
"friendsofsymfony/ckeditor-bundle": {
|
||||
"version": "2.0",
|
||||
"recipe": {
|
||||
|
@ -111,6 +120,15 @@
|
|||
"gerardojbaez/money": {
|
||||
"version": "v0.3.1"
|
||||
},
|
||||
"guzzlehttp/guzzle": {
|
||||
"version": "6.3.3"
|
||||
},
|
||||
"guzzlehttp/promises": {
|
||||
"version": "v1.3.1"
|
||||
},
|
||||
"guzzlehttp/psr7": {
|
||||
"version": "1.6.1"
|
||||
},
|
||||
"jdorn/sql-formatter": {
|
||||
"version": "v1.2.17"
|
||||
},
|
||||
|
@ -126,6 +144,18 @@
|
|||
"nyholm/nsa": {
|
||||
"version": "1.1.0"
|
||||
},
|
||||
"nyholm/psr7": {
|
||||
"version": "1.0",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "master",
|
||||
"version": "1.0",
|
||||
"ref": "0cd4d2d0e7f646fda75f9944f747a56e6ed13d4c"
|
||||
},
|
||||
"files": [
|
||||
"./config/packages/nyholm_psr7.yaml"
|
||||
]
|
||||
},
|
||||
"ocramius/proxy-manager": {
|
||||
"version": "2.1.1"
|
||||
},
|
||||
|
@ -141,6 +171,27 @@
|
|||
"./config/packages/datatables.yaml"
|
||||
]
|
||||
},
|
||||
"php-http/curl-client": {
|
||||
"version": "2.0.0"
|
||||
},
|
||||
"php-http/discovery": {
|
||||
"version": "1.7.0"
|
||||
},
|
||||
"php-http/guzzle6-adapter": {
|
||||
"version": "v2.0.1"
|
||||
},
|
||||
"php-http/httplug": {
|
||||
"version": "v2.0.0"
|
||||
},
|
||||
"php-http/message": {
|
||||
"version": "1.8.0"
|
||||
},
|
||||
"php-http/message-factory": {
|
||||
"version": "v1.0.2"
|
||||
},
|
||||
"php-http/promise": {
|
||||
"version": "v1.0.0"
|
||||
},
|
||||
"php-translation/common": {
|
||||
"version": "1.0.0"
|
||||
},
|
||||
|
@ -180,12 +231,27 @@
|
|||
"psr/container": {
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"psr/http-client": {
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"psr/http-factory": {
|
||||
"version": "1.0.1"
|
||||
},
|
||||
"psr/http-message": {
|
||||
"version": "1.0.1"
|
||||
},
|
||||
"psr/link": {
|
||||
"version": "1.0.0"
|
||||
},
|
||||
"psr/log": {
|
||||
"version": "1.1.0"
|
||||
},
|
||||
"psr/simple-cache": {
|
||||
"version": "1.0.1"
|
||||
},
|
||||
"ralouphie/getallheaders": {
|
||||
"version": "3.0.3"
|
||||
},
|
||||
"roave/security-advisories": {
|
||||
"version": "dev-master"
|
||||
},
|
||||
|
|
|
@ -20,7 +20,7 @@
|
|||
{{ form_row(form.exchange_rate) }}
|
||||
{% if entity.inverseExchangeRate %}
|
||||
<span class="form-text text-muted offset-3 col-9">
|
||||
{{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate | format_currency(entity.isoCode) }}
|
||||
{{ '1'|format_currency(default_currency) }} = {{ entity.inverseExchangeRate | format_currency(entity.isoCode, {fraction_digit: 5}) }}
|
||||
</span>
|
||||
{% endif %}
|
||||
{% endblock %}
|
Loading…
Add table
Add a link
Reference in a new issue