mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-08-31 07:09:45 +02:00
Added a button to update exchange rates via web gui.
This commit is contained in:
parent
ff7c1cddc7
commit
af42c3cca0
11 changed files with 1137 additions and 909 deletions
|
@ -47,14 +47,29 @@ use App\Entity\Base\AbstractNamedDBElement;
|
|||
use App\Entity\Parameters\CurrencyParameter;
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use App\Form\AdminPages\CurrencyAdminForm;
|
||||
use App\Services\Attachments\AttachmentSubmitHandler;
|
||||
use App\Services\EntityExporter;
|
||||
use App\Services\EntityImporter;
|
||||
use App\Services\ExchangeRateUpdater;
|
||||
use App\Services\LabelSystem\Barcodes\BarcodeExampleElementsGenerator;
|
||||
use App\Services\LabelSystem\LabelGenerator;
|
||||
use App\Services\LogSystem\EventCommentHelper;
|
||||
use App\Services\LogSystem\HistoryHelper;
|
||||
use App\Services\LogSystem\TimeTravel;
|
||||
use App\Services\StructuralElementRecursionHelper;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Exchanger\Exception\ChainException;
|
||||
use Exchanger\Exception\Exception;
|
||||
use Exchanger\Exception\UnsupportedCurrencyPairException;
|
||||
use Omines\DataTablesBundle\DataTableFactory;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
/**
|
||||
* @Route("/currency")
|
||||
|
@ -70,6 +85,40 @@ class CurrencyController extends BaseAdminController
|
|||
protected $attachment_class = CurrencyAttachment::class;
|
||||
protected $parameter_class = CurrencyParameter::class;
|
||||
|
||||
protected $exchangeRateUpdater;
|
||||
|
||||
public function __construct(
|
||||
TranslatorInterface $translator,
|
||||
UserPasswordEncoderInterface $passwordEncoder,
|
||||
AttachmentSubmitHandler $attachmentSubmitHandler,
|
||||
EventCommentHelper $commentHelper,
|
||||
HistoryHelper $historyHelper,
|
||||
TimeTravel $timeTravel,
|
||||
DataTableFactory $dataTableFactory,
|
||||
EventDispatcherInterface $eventDispatcher,
|
||||
BarcodeExampleElementsGenerator $barcodeExampleGenerator,
|
||||
LabelGenerator $labelGenerator,
|
||||
EntityManagerInterface $entityManager,
|
||||
ExchangeRateUpdater $exchangeRateUpdater
|
||||
|
||||
) {
|
||||
$this->exchangeRateUpdater = $exchangeRateUpdater;
|
||||
|
||||
parent::__construct(
|
||||
$translator,
|
||||
$passwordEncoder,
|
||||
$attachmentSubmitHandler,
|
||||
$commentHelper,
|
||||
$historyHelper,
|
||||
$timeTravel,
|
||||
$dataTableFactory,
|
||||
$eventDispatcher,
|
||||
$barcodeExampleGenerator,
|
||||
$labelGenerator,
|
||||
$entityManager
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{id}", name="currency_delete", methods={"DELETE"})
|
||||
*
|
||||
|
@ -80,6 +129,30 @@ class CurrencyController extends BaseAdminController
|
|||
return $this->_delete($request, $entity, $recursionHelper);
|
||||
}
|
||||
|
||||
public function additionalActionEdit(FormInterface $form, AbstractNamedDBElement $entity): bool
|
||||
{
|
||||
if (!$entity instanceof Currency) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($form->get('update_exchange_rate')->isClicked()) {
|
||||
$this->denyAccessUnlessGranted('edit', $entity);
|
||||
try {
|
||||
$this->exchangeRateUpdater->update($entity);
|
||||
$this->addFlash('info', 'currency.edit.exchange_rate_updated.success');
|
||||
} catch (ChainException $exception) {
|
||||
$exception = $exception->getExceptions()[0];
|
||||
if ($exception instanceof UnsupportedCurrencyPairException || stripos($exception->getMessage(), "supported") !== false) {
|
||||
$this->addFlash('error', 'currency.edit.exchange_rate_update.unsupported_currency');
|
||||
} else {
|
||||
$this->addFlash('error', 'currency.edit.exchange_rate_update.generic_error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Route("/{id}/edit/{timestamp}", requirements={"id"="\d+"}, name="currency_edit")
|
||||
* @Route("/{id}", requirements={"id"="\d+"})
|
||||
|
|
|
@ -46,6 +46,7 @@ use App\Entity\Base\AbstractNamedDBElement;
|
|||
use App\Form\Type\BigDecimalMoneyType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
|
@ -82,5 +83,16 @@ class CurrencyAdminForm extends BaseEntityAdminForm
|
|||
'scale' => 6,
|
||||
'disabled' => ! $this->security->isGranted($is_new ? 'create' : 'edit', $entity),
|
||||
]);
|
||||
|
||||
if(!$is_new) {
|
||||
$builder->add(
|
||||
'update_exchange_rate',
|
||||
SubmitType::class,
|
||||
[
|
||||
'label' => 'currency.edit.update_rate',
|
||||
'disabled' => ! $this->security->isGranted('edit', $entity)
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
34
src/Services/ExchangeRateUpdater.php
Normal file
34
src/Services/ExchangeRateUpdater.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
use App\Entity\PriceInformations\Currency;
|
||||
use Brick\Math\BigDecimal;
|
||||
use Swap\Swap;
|
||||
|
||||
class ExchangeRateUpdater
|
||||
{
|
||||
private $base_currency;
|
||||
private $swap;
|
||||
|
||||
public function __construct(string $base_currency, Swap $swap)
|
||||
{
|
||||
$this->base_currency = $base_currency;
|
||||
$this->swap = $swap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the exchange rate of the given currency using the globally configured providers.
|
||||
* @param Currency $currency
|
||||
* @return Currency
|
||||
*/
|
||||
public function update(Currency $currency): Currency
|
||||
{
|
||||
$rate = $this->swap->latest($currency->getIsoCode().'/'.$this->base_currency);
|
||||
$currency->setExchangeRate(BigDecimal::of($rate->getValue()));
|
||||
return $currency;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue