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"; } } } }
public static function getCurrencyPriceProduct(Product $product, Order $order = null, SpecialPriceList $specialPrice, $price) { $currency = Currency::getMainCurrency(); if ($product->currency_id !== $currency->id) { $foreignCurrency = Currency::findById($product->currency_id); $price = $price / $foreignCurrency->convert_nominal * $foreignCurrency->convert_rate; } return round($price, 2); }
/** * @return array */ protected function getData_LU() { $result = ['MERCHANT' => $this->merchantId, 'ORDER_REF' => $this->transaction->id, 'ORDER_DATE' => date('Y-m-d H:i:s'), 'ORDER_PNAME[]' => [], 'ORDER_PCODE[]' => [], 'ORDER_PINFO[]' => [], 'ORDER_PRICE[]' => [], 'ORDER_QTY[]' => [], 'ORDER_VAT[]' => [], 'ORDER_SHIPPING' => null !== $this->order->shippingOption ? $this->order->shippingOption->cost : 0, 'PRICES_CURRENCY' => null !== Currency::getMainCurrency() ? Currency::getMainCurrency()->iso_code : 'RUB', 'ORDER_PRICE_TYPE[]' => []]; /** @var OrderItem $item */ foreach ($this->order->items as $item) { $product = $item->product; $result['ORDER_PNAME[]'][] = $product->name; $result['ORDER_PINFO[]'][] = $product->name; $result['ORDER_PCODE[]'][] = $product->id; $result['ORDER_PRICE[]'][] = $product->convertedPrice(); $result['ORDER_QTY[]'][] = $item->quantity; $result['ORDER_VAT[]'][] = 0; $result['ORDER_PRICE_TYPE[]'][] = 'NET'; } $result['ORDER_HASH'] = $this->generateHash_LU($result); $result['LANGUAGE'] = 'RU'; $result['TESTORDER'] = true === $this->systemTestMode ? 'TRUE' : 'FALSE'; $result['DEBUG'] = true === $this->systemDebugMode ? 'TRUE' : 'FALSE'; $result['DEBUG'] = 'TRUE'; return $result; }
/** * Delete OrderItem action. * @param int $id * @throws NotFoundHttpException * @throws \Exception */ public function actionDelete($id) { Yii::$app->response->format = Response::FORMAT_JSON; $order = $this->loadOrder(); if (is_null($order->stage) || $order->stage->immutable_by_user == 1) { throw new BadRequestHttpException(); } if ($this->loadOrderItem($id)->delete() && $order->calculate(true)) { $mainCurrency = Currency::getMainCurrency(); return ['success' => true, 'itemsCount' => $order->items_count, 'totalPrice' => $mainCurrency->format($order->total_price)]; } else { return ['success' => false, 'message' => Yii::t('app', 'Cannot change additional params')]; } }
/** * @return Currency */ public static function getMainCurrency() { return null === static::$mainCurrency ? static::$mainCurrency = Currency::getMainCurrency() : static::$mainCurrency; }
<?php /** * @var \app\modules\shop\models\Order $order */ $mainCurrency = \app\modules\shop\models\Currency::getMainCurrency(); if (is_null($order)) { $itemsCount = 0; $totalPrice = $mainCurrency->format(0); } else { $itemsCount = $order->items_count; $totalPrice = $mainCurrency->format($order->total_price); } ?> <div class="span3" id="cart-info-widget"> <div class="pull-right"> <span class="btn btn-mini"> <span class="total-price"><?php echo $totalPrice; ?> </span> </span> <a href="<?php echo \yii\helpers\Url::toRoute(['/shop/cart']); ?> "> <span class="btn btn-mini btn-primary"> <i class="fa fa-shopping-cart"></i> <?php echo Yii::t('app', '[ {count} ] Itemes in your cart', ['count' => \kartik\helpers\Html::tag('span', $itemsCount, ['class' => 'items-count'])]); ?>
/** * Formats price to needed currency * @param null|Currency $currency Currency to use in conversion, null for main shop currency * @param boolean $oldPrice True if you want to return Old Price instead of price * @param boolean $schemaOrg Return schema.org http://schema.org/Offer markup with span's * @return string */ public function formattedPrice($currency = null, $oldPrice = false, $schemaOrg = true) { if ($currency === null) { $currency = Currency::getMainCurrency(); } $price = $this->convertedPrice($currency, $oldPrice); $formatted_string = $currency->format($price); if ($schemaOrg == true) { return strtr(' <span itemtype="http://schema.org/Offer" itemprop="offers" itemscope> <meta itemprop="priceCurrency" content="%iso_code%"> <span itemprop="price" content="%price%"> %formatted_string% </span> </span> ', ['%iso_code%' => $currency->iso_code, '%price%' => $price, '%formatted_string%' => $formatted_string]); } else { return $formatted_string; } }
if ($currentOrder) { ?> <div class="current-order"> <?php echo Yii::t('app', 'Current order:'); ?> <?php echo Html::a($currentOrder, ['/shop/cart']); ?> </div> <?php } if ($dataProvider !== null) { ?> <?php echo \yii\grid\GridView::widget(['columns' => [['attribute' => 'id', 'value' => function ($model, $key, $index, $column) { return Html::a('#' . $model->id, ['/shop/orders/show', 'hash' => $model->hash]); }, 'format' => 'raw'], 'start_date:date', ['attribute' => 'stage.name_frontend', 'header' => Yii::t('app', 'Status')], ['attribute' => 'total_price', 'header' => Yii::t('app', 'Total price'), 'value' => function ($model, $key, $index, $column) { return \app\modules\shop\models\Currency::getMainCurrency()->format($model->total_price); }, 'format' => 'raw']], 'dataProvider' => $dataProvider]); ?> <?php } else { ?> <?php echo Yii::t('app', 'You have no complete orders'); ?> <?php }
/** * @param null $id * @return string|\yii\web\Response * @throws NotFoundHttpException * @throws ServerErrorHttpException * @throws \Exception * @throws \yii\base\InvalidRouteException */ public function actionEdit($id = null) { /* * @todo Продумать механизм сохранения изображений для нового продукта. * Сейчас для нового продукта скрывается форма добавления изображений. */ if (null === ($object = Object::getForClass(Product::className()))) { throw new ServerErrorHttpException(); } /** @var null|Product|HasProperties|\devgroup\TagDependencyHelper\ActiveRecordHelper $model */ $model = null; $parent = null; if (null === $id) { $model = new Product(); $model->loadDefaultValues(); $model->currency_id = Currency::getMainCurrency()->id; $parent_id = Yii::$app->request->get('owner_id', 0); if (0 !== intval($parent_id) && null !== Product::findById($parent_id)) { $model->parent_id = $parent_id; } $model->measure_id = $this->module->defaultMeasureId; } else { $model = Product::findById($id, null); if (null !== $model && $model->parent_id > 0) { $parent = Product::findById($model->parent_id, null); } } if (null === $model) { throw new NotFoundHttpException(); } $model->loadRelatedProductsArray(); $event = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_EDIT, $event); $post = \Yii::$app->request->post(); if ($event->isValid && $model->load($post)) { $saveStateEvent = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_EDIT_SAVE, $saveStateEvent); if ($model->validate()) { if (isset($post['GeneratePropertyValue'])) { $generateValues = $post['GeneratePropertyValue']; } else { $generateValues = []; } if (isset($post['PropertyGroup'])) { $model->option_generate = Json::encode(['group' => $post['PropertyGroup']['id'], 'values' => $generateValues]); } else { $model->option_generate = ''; } $save_result = $model->save(); $model->saveProperties($post); $model->saveRelatedProducts(); if (null !== ($view_object = ViewObject::getByModel($model, true))) { if ($view_object->load($post, 'ViewObject')) { if ($view_object->view_id <= 0) { $view_object->delete(); } else { $view_object->save(); } } } if ($save_result) { $modelAfterSaveEvent = new BackendEntityEditEvent($model); $this->trigger(self::EVENT_BACKEND_PRODUCT_AFTER_SAVE, $modelAfterSaveEvent); $categories = isset($post['Product']['categories']) ? $post['Product']['categories'] : []; $model->saveCategoriesBindings($categories); $this->runAction('save-info', ['model_id' => $model->id]); $model->invalidateTags(); $action = Yii::$app->request->post('action', 'save'); if (Yii::$app->request->post(HasProperties::FIELD_ADD_PROPERTY_GROUP) || Yii::$app->request->post(HasProperties::FIELD_REMOVE_PROPERTY_GROUP)) { $action = 'save'; } $returnUrl = Yii::$app->request->get('returnUrl', ['index']); switch ($action) { case 'next': return $this->redirect(['edit', 'returnUrl' => $returnUrl, 'parent_id' => Yii::$app->request->get('parent_id', null)]); case 'back': return $this->redirect($returnUrl); default: return $this->redirect(Url::toRoute(['edit', 'id' => $model->id, 'returnUrl' => $returnUrl, 'parent_id' => $model->main_category_id])); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); } } else { Yii::$app->session->setFlash('error', Yii::t('app', 'Cannot save data')); } } $items = ArrayHelper::map(Category::find()->all(), 'id', 'name'); return $this->render('product-form', ['object' => $object, 'model' => $model, 'items' => $items, 'selected' => $model->getCategoryIds(), 'parent' => $parent]); }