<?php
namespace App\Cron\Job; class ImportCurrencyExchangeRates extends \App\Cron\Job\Utils\GeneralAbstract { protected $category = 'financial'; public function __construct() { parent::__construct(); $this->options['StopAndRestartJob']['maxRestarts'] = 10; $this->options['StopAndRestartJob']['maxDurationOfRestarts'] = 10*60; } public function getSettingsDialog(array $fields) { $form = parent::getSettingsDialog($fields); $_LANG = \App\Loader_Lang::getInstance()->getGlobal(); $lang = \App\Loader_Lang::getInstance()->getSection("Admin/CronJobs/Management/ImportCurrencyExchangeRates"); $formContainer = \Engine\Form\Container\Fieldset::create($_LANG['Admin']['CronJobs']['Management']['formFields']['legend']); $form->addContainer($formContainer); $fld=new \Engine\Form\Input\Checkbox($lang['formFields']['importPrevDate'], 'allowCopyingFromPreviousDate', $fields['settings']['allowCopyingFromPreviousDate']); $fld->setTooltip($lang['formFields']['importPrevDateTooltip']); $formContainer->addElement($fld); $formContainer->groupInputsInArray("settings"); return $form; } public function validateSettings(\App\Entity\Messages $msgs, array $fields) { if (!$fields['errorManagement'] or !count($fields['errorManagement']['email'])) { $msgs->addWarningText("The on-error email is mandatory."); } } protected function executeJob() { $updatingDate = TODAY_ISO; $lastDefinedDate = \App\CurrencyExchange\Date::getInstance($updatingDate)->getLastDefinedDate(); if ($lastDefinedDate==$updatingDate) { $this->logger->warning(sprintf('The currency exchange rates are already defined for this date `%s`', $updatingDate)); return true; } try { \DB::startTransaction(); $currenciesMngObj = new \App\CurrencyExchange\Admin($updatingDate); if ($this->settings['allowCopyingFromPreviousDate']) { $this->logger->info('The process is configured to get previously inputted rates for currency types that cannot be imported automatically.'); $currenciesMngObj->addRates_previousDate(); } else { $this->logger->info('The process is configured to NOT get previously inputted rates for currency types that cannot be imported automatically. If there are missing exchange rates the process will not finish successfully.'); } $currenciesMngObj->addRates_externalSystems($updatingDate); $currenciesMngObj->save(); \DB::commitTransaction(); $this->logger->success('The currency exchange rates were successfully imported and saved.'); } catch (\App\MessagesException $e) { \DB::rollbackTransaction(); $this->logger->error('The currency exchange rates could not be imported due to the following message(s):'); $this->importFromMessageCollector($e->getMessagesCollector()); throw new \Exception\CronJob\StopAndRestartJob('Could not import the currency exchange rates. Retrying in 1 minute.'); } } }