/**
  * Removes given item from storage
  */
 public function run()
 {
     if (true === $this->model->delete()) {
         Yii::$app->session->setFlash('success', mb_convert_case($this->itemName, MB_CASE_TITLE, "UTF-8") . Yii::t('dotplant.currencies', ' {name} successfully deleted!', ['name' => $this->model->name]));
         return $this->controller->redirect($this->returnUrl);
     } else {
         Yii::$app->session->setFlash('error', Yii::t('dotplant.currencies', 'An error occurred while deleting {name}!', ['name' => $this->model->name]));
     }
 }
 /**
  * @throws \Exception
  */
 public function actionUpdate()
 {
     $mainCurrency = Currency::getMainCurrency();
     if ($mainCurrency === null) {
         throw new \Exception("Main currency is not set");
     }
     $currencies = Currency::getUpdateable();
     $httpAdapter = new CurlHttpAdapter();
     /** @var Currency $currency */
     foreach ($currencies as $currency) {
         /** @var CurrencyRateProvider $providerModel */
         $providerModel = $currency->rateProvider;
         if (null !== $providerModel) {
             try {
                 $provider = $providerModel->getImplementationInstance($httpAdapter);
                 if (null !== $provider) {
                     $swap = new 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";
             }
         }
     }
 }
 public function testFindCurrencyByIso()
 {
     $data = [['name' => 'US Dollar', 'iso_code' => 'USD', 'convert_nominal' => 1, 'convert_rate' => 80, 'sort_order' => 1, 'format_string' => '$ #', 'thousands_sep' => '.', 'dec_point' => ',', 'is_main' => 0, 'intl_formatting' => 1], 'Euro' => ['name' => 'Euro', 'iso_code' => 'EUR', 'convert_rate' => 90, 'format_string' => '€ #', 'is_main' => 1]];
     $storage = (new Currency())->getStorage();
     FunctionalHelper::flushStorage($storage);
     $this->assertFileNotExists($storage);
     CurrencyStorageHelper::generateStorage($data, $storage, Currency::className());
     $c = CurrencyHelper::findCurrencyByIso('USD');
     $this->assertEquals('US Dollar', $c->name);
     return $c;
 }
 /**
  * Using PHPUnit with static variables are painfully. Because of it this single test executes in different Class then others.
  */
 public function testGetMainCurrencyNotExists()
 {
     $data = [['name' => 'US Dollar', 'iso_code' => 'USD', 'convert_nominal' => 1, 'convert_rate' => 62.83, 'sort_order' => 1, 'format_string' => '$ #', 'thousands_sep' => '.', 'dec_point' => ',', 'is_main' => 0], 'Euro' => ['name' => 'Euro', 'iso_code' => 'EUR', 'convert_rate' => 71.31999999999999, 'format_string' => '€ #', 'is_main' => 0]];
     $storage = (new Currency())->getStorage();
     FunctionalHelper::flushStorage($storage);
     $this->assertFileNotExists($storage);
     CurrencyStorageHelper::generateStorage($data, $storage, Currency::className());
     $this->assertFileExists($storage);
     $c = CurrencyHelper::getMainCurrency();
     $this->assertNull($c);
     FunctionalHelper::flushCurrencyStorage();
 }
 /**
  * @inheritdoc
  */
 public function afterDelete()
 {
     parent::afterDelete();
     $currencies = Currency::findAll();
     /** @var Currency $currency */
     foreach ($currencies as $currency) {
         if ($this->name == $currency->currency_rate_provider_name) {
             $currency->currency_rate_provider_name = null;
             $currency->save();
         }
     }
 }
 /**
  * @inheritdoc
  */
 public function getFooter()
 {
     $deleteButton = $this->model->isNewItem() ? '' : Html::a(Icon::show('trash-o') . ' ' . Yii::t('dotplant.currencies', 'Delete'), [$this->controller->id . '/delete', 'id' => $this->model->name, 'returnUrl' => $this->returnUrl], ['class' => 'btn btn-danger']);
     return Html::tag('div', $deleteButton . Html::a(Yii::t('dotplant.currencies', 'Back'), $this->returnUrl, ['class' => 'btn btn-primary']) . Html::submitButton(Icon::show('floppy-o') . ' ' . Yii::t('dotplant.currencies', 'Save'), ['class' => 'btn btn-primary']), ['class' => 'btn-group pull-right', 'role' => 'group', 'aria-label' => 'Edit buttons']);
 }
 /**
  * @inheritdoc
  */
 public function actions()
 {
     return ['edit' => ['class' => ItemEditAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency'), 'editView' => '@vendor/dotplant/currencies/src/views/currency-edit', 'storage' => CurrenciesModule::module()->currenciesStorage], 'delete' => ['class' => ItemDeleteAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currency'), 'storage' => CurrenciesModule::module()->currenciesStorage], 'reset' => ['class' => ResetAction::className(), 'className' => Currency::className(), 'itemName' => Yii::t('dotplant.currencies', 'currencies'), 'storage' => CurrenciesModule::module()->currenciesStorage]];
 }
 public function testGenerateWithReformat()
 {
     $data = [['name' => 'Ruble', 'iso_code' => 'RUB', 'is_main' => 1, 'format_string' => '# руб.', 'intl_formatting' => 0], ['name' => 'US Dollar', 'iso_code' => 'USD', 'convert_nominal' => 1, 'convert_rate' => 62.8353, 'sort_order' => 1, 'format_string' => '$ #', 'thousands_sep' => '.', 'dec_point' => ','], 'Euro' => ['name' => 'Euro', 'iso_code' => 'EUR', 'convert_rate' => 71.32429999999999, 'format_string' => '€ #']];
     $storage = (new Currency())->getStorage();
     FunctionalHelper::flushStorage($storage);
     CurrencyStorageHelper::generateStorage($data, $storage, Currency::className());
     $this->assertFileExists($storage);
     $a = [];
     if (true === file_exists($storage)) {
         $a = (include $storage);
     }
     $this->assertCount(3, $a);
     FunctionalHelper::flushStorage($storage);
 }
 /**
  * Formats price with current currency settings
  *
  * @param $price
  * @param Currency $currency
  * @return string
  */
 public static function format($price, Currency $currency)
 {
     if ($currency->intl_formatting == 1) {
         return $currency->getFormatter()->asCurrency($price);
     } else {
         $number_value = $currency->getFormatter()->asDecimal($price);
         return strtr($currency->format_string, ['#' => $number_value]);
     }
 }
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     self::$items = [Currency::className() => [], CurrencyRateProvider::className() => []];
     self::$defaults = [Currency::className() => self::$defaultCurrencies, CurrencyRateProvider::className() => self::$defaultProviders];
 }
<?php

use DotPlant\Currencies\CurrenciesModule;
use DotPlant\Currencies\models\Currency;
use DevGroup\AdminUtils\Helper;
use yii\grid\GridView;
use kartik\icons\Icon;
use yii\helpers\Html;
$currencies = CurrenciesModule::module()->getData(Currency::className());
$currenciesProvider = new \yii\data\ArrayDataProvider(['allModels' => Currency::findAll(), 'pagination' => ['pageSize' => 10]]);
$currencyButtons = Html::tag('div', Html::a(Icon::show('plus') . '&nbsp;' . Yii::t('dotplant.currencies', 'Add currency'), ['/currencies/currencies-manage/edit', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-success']) . Html::a(Icon::show('eraser') . '&nbsp;' . Yii::t('dotplant.currencies', 'Reset currencies'), ['/currencies/currencies-manage/reset', 'returnUrl' => Helper::returnUrl()], ['role' => 'button', 'class' => 'btn btn-danger']), ['class' => 'btn-group pull-right', 'role' => 'group', 'aria-label' => 'Currencies buttons']);
$gridTpl = <<<TPL
<div class="box-body">
    {items}
</div>
<div class="box-footer">
    <div class="row ext-bottom">
        <div class="col-sm-5">
            {summary}
        </div>
        <div class="col-sm-7">
            {pager}
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">[button]</div>
    </div>
</div>
TPL;
echo GridView::widget(['id' => 'dotplant-currencies-list', 'dataProvider' => $currenciesProvider, 'layout' => str_replace('[button]', $currencyButtons, $gridTpl), 'tableOptions' => ['class' => 'table table-bordered table-hover table-responsive'], 'columns' => ['name', 'iso_code', ['attribute' => 'is_main', 'content' => function ($data) {
    return Yii::$app->formatter->asBoolean($data->is_main);