public function actionUpdate() { $mainCurrency = Currency::getMainCurrency(); if ($mainCurrency === null) { throw new \Exception("Main currency is not set"); } $currencies = Currency::find()->andWhere('currency_rate_provider_id != 0')->andWhere('id != :main_id', [':main_id' => $mainCurrency->id])->all(); $httpAdapter = new \Ivory\HttpAdapter\CurlHttpAdapter(); foreach ($currencies as $currency) { /** @var app\modules\shop\models\CurrencyRateProvider $providerModel */ $providerModel = $currency->rateProvider; if ($providerModel !== null) { try { $provider = $providerModel->getImplementationInstance($httpAdapter); if ($provider !== null) { $swap = new \Swap\Swap($provider); $rate = $swap->quote($currency->iso_code . '/' . $mainCurrency->iso_code)->getValue(); $currency->convert_rate = floatval($rate); if ($currency->additional_rate > 0) { // additional rate is in % $currency->convert_rate *= 1 + $currency->additional_rate / 100; } if ($currency->additional_nominal !== 0) { $currency->convert_rate += $currency->additional_nominal; } $currency->save(); echo $currency->iso_code . '/' . $mainCurrency->iso_code . ': ' . $rate . " == " . $currency->convert_rate . "\n"; } } catch (\Exception $e) { echo "Error updating " . $currency->name . ': ' . $e->getMessage() . "\n\n"; } } } }
/** * @param string $code * @param bool|true $useMainCurrency * @return Currency */ public static function findCurrencyByIso($code) { $currency = Currency::find()->where(['iso_code' => $code])->one(); $currency = null === $currency ? static::getMainCurrency() : $currency; return $currency; }
<?php /** * @var $this \yii\web\View * @var $model \app\modules\shop\models\Yml **/ use yii\helpers\Html; use kartik\icons\Icon; use app\backend\widgets\BackendWidget; use yii\helpers\ArrayHelper; $this->title = Yii::t('app', 'Settings'); $this->params['breadcrumbs'][] = 'Google Feed'; \app\backend\assets\YmlAsset::register($this); $formName = 'YmlSettings'; $currencies = ArrayHelper::map(\app\modules\shop\models\Currency::find()->select('iso_code')->orderBy(['is_main' => SORT_DESC, 'sort_order' => SORT_ASC, 'iso_code' => SORT_ASC])->asArray()->all(), 'iso_code', 'iso_code'); ?> <?php echo app\widgets\Alert::widget(['id' => 'alert']); ?> <?php $feed_relations = ['getImage' => \app\modules\image\models\Image::className(), 'getMainCategory' => \app\modules\shop\models\Category::className()]; $feed_settings = []; $feed_settings['product_fields'] = (new \app\modules\shop\models\Product())->attributeLabels(); $feed_settings['relations_keys'] = array_combine(array_keys($feed_relations), array_keys($feed_relations)); $feed_settings['relations_map'] = []; foreach ($feed_relations as $key => $value) { $_fields = (new $value())->attributeLabels(); $feed_settings['relations_map'][$key] = ['fields' => $_fields, 'html' => array_reduce($_fields, function ($result, $i) { $result .= '<option value="' . addslashes(htmlspecialchars($i)) . '">' . addslashes(htmlspecialchars($i)) . '</option>';
<?php /** * @var \app\modules\config\models\Configurable $configurable * @var \app\backend\components\ActiveForm $form * @var \app\modules\seo\models\ConfigConfigurationModel $model * @var \yii\web\View $this */ use app\backend\widgets\BackendWidget; use app\modules\seo\SeoModule; use kartik\widgets\SwitchInput; use app\modules\shop\models\Currency; if (is_array($model->include)) { $model->include = implode(',', $model->include); } $currencies = [\app\modules\seo\handlers\AnalyticsHandler::CURRENCY_MAIN => Yii::t('app', 'Main currency'), \app\modules\seo\handlers\AnalyticsHandler::CURRENCY_USER => Yii::t('app', 'User currency')] + \yii\helpers\ArrayHelper::map(Currency::find()->select(['iso_code', 'name'])->asArray()->all(), 'iso_code', 'name'); ?> <div> <div class="col-md-6 col-sm-12"> <?php BackendWidget::begin(['title' => Yii::t('app', 'Main settings'), 'options' => ['class' => 'visible-header']]); ?> <?php echo $form->field($model, 'mainPage'); ?> <?php echo $form->field($model, 'include'); ?> <?php echo $form->field($model, 'redirectWWW')->dropDownList(SeoModule::getRedirectTypes());
/** * Returns Currency instance by name * @param $name * @return array|mixed|null|\yii\db\ActiveRecord */ public static function getByName($name) { // first search in identity map foreach (static::$identity_map as $id => $currency) { if ($currency->name === $name) { return $currency; } } // if not - find in db $currency = Yii::$app->cache->get('Currency:name:' . $name); if ($currency === false) { $currency = Currency::find()->where(['name' => $name])->one(); Yii::$app->cache->set('Currency:name:' . $name, $currency, 86400, new TagDependency(['tags' => [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(static::className())]])); } if ($currency !== null) { // put to identity map static::$identity_map[$currency->id] = $currency; } return $currency; }
/** * @inheritdoc */ public function rules() { return [[['productListingSortId'], 'integer'], ['productListingSortId', 'default', 'value' => 1], ['productListingSortId', 'in', 'range' => array_keys(ProductListingSort::enabledSorts())], ['listViewType', 'default', 'value' => Yii::$app->getModule('shop')->listViewType], ['listViewType', 'in', 'range' => ['listView', 'blockView'], 'strict' => true], ['productsPerPage', 'default', 'value' => Yii::$app->getModule('shop')->productsPerPage], ['productsPerPage', 'integer', 'max' => 50], ['userCurrency', 'default', 'value' => CurrencyHelper::getMainCurrency()->iso_code], ['userCurrency', 'in', 'range' => array_column(Currency::find()->select(['iso_code'])->asArray()->all(), 'iso_code'), 'strict' => true]]; }