public function init() { $this->name = \Yii::t('skeeks/shop/app', 'Discount goods'); $this->modelShowAttribute = "id"; $this->modelClassName = ShopDiscount::className(); parent::init(); }
public function init() { $this->name = \skeeks\cms\shop\Module::t('app', 'Cumulative discounts'); $this->modelShowAttribute = "id"; $this->modelClassName = ShopDiscount::className(); parent::init(); }
/** * @return \yii\db\ActiveQuery */ public function getDiscount() { return $this->hasOne(ShopDiscount::className(), ['id' => 'discount_id']); }
/** * * Пересчет состояния позиции согласно текущемим данным * * * @return $this */ public function recalculate() { if (!$this->product) { return $this; } $product = $this->product; $productPrice = $product->minProductPrice; $productPriceMoney = $productPrice->money->convertToCurrency(\Yii::$app->money->getCurrencyObject()); $this->measure_name = $product->measure->symbol_rus; $this->measure_code = $product->measure->code; $this->product_price_id = $productPrice->id; $this->notes = $productPrice->typePrice->name; $this->detail_page_url = $product->cmsContentElement->url; $this->name = $product->cmsContentElement->name; $this->weight = $product->weight; $this->site_id = \Yii::$app->cms->site->id; $this->dimensions = Json::encode(['height' => $product->height, 'width' => $product->width, 'length' => $product->length]); //Рассчет налогов if ($product->vat) { $this->vat_rate = $product->vat->rate; if ($product->vat_included == Cms::BOOL_Y) { $this->price = $productPriceMoney->getValue(); } else { $this->price = $productPriceMoney->getValue() * $this->vat_rate; } } else { $this->price = $productPriceMoney->getValue(); } $this->currency_code = $productPriceMoney->getCurrency()->getCurrencyCode(); //Проверка скидок /** * @var ShopDiscount $shopDiscount */ $shopDiscounts = ShopDiscount::find()->active()->orderBy(['shop_discount.priority' => SORT_ASC])->leftJoin('shop_discount2type_price', '`shop_discount2type_price`.`discount_id` = `shop_discount`.`id`')->andWhere(['or', ['shop_discount.site_id' => ""], ['shop_discount.site_id' => null], ['shop_discount.site_id' => \Yii::$app->cms->site->id]])->andWhere(['shop_discount2type_price.type_price_id' => $this->productPrice->typePrice->id])->all(); //->createCommand()->rawSql; $price = $this->price; $this->discount_price = 0; $this->discount_value = ""; $this->discount_name = ""; if ($shopDiscounts) { foreach ($shopDiscounts as $shopDiscount) { if (\Yii::$app->user->can($shopDiscount->permissionName)) { $this->discount_name = $shopDiscount->name; if ($shopDiscount->value_type == ShopDiscount::VALUE_TYPE_P) { $percent = $shopDiscount->value / 100; $this->discount_value = \Yii::$app->formatter->asPercent($percent); $discountPrice = $price * $percent; $this->price = $this->price - $discountPrice; $this->discount_price = $this->discount_price + $discountPrice; //Нужно остановится и не применять другие скидки if ($shopDiscount->last_discount === Cms::BOOL_Y) { break; } } } } } return $this; }
/** * * Пересчет состояния позиции согласно текущемим данным * * * @return $this */ public function recalculate() { if (!$this->product) { return $this; } $product = $this->product; $parentElement = $product->cmsContentElement->parentContentElement; $productPrice = $product->minProductPrice; $productPriceMoney = $productPrice->money->convertToCurrency(\Yii::$app->money->getCurrencyObject()); $this->measure_name = $product->measure->symbol_rus; $this->measure_code = $product->measure->code; $this->product_price_id = $productPrice->id; $this->notes = $productPrice->typePrice->name; $this->name = $parentElement ? $parentElement->name : $product->cmsContentElement->name; $this->weight = $product->weight; $this->site_id = \Yii::$app->cms->site->id; //TODO: неправильно $this->dimensions = Json::encode(['height' => $product->height, 'width' => $product->width, 'length' => $product->length]); //Рассчет налогов if ($product->vat) { $this->vat_rate = $product->vat->rate; if ($product->vat_included == Cms::BOOL_Y) { $this->price = $productPriceMoney->getValue(); } else { $this->price = $productPriceMoney->getValue() * $this->vat_rate; } } else { $this->price = $productPriceMoney->getValue(); } $this->currency_code = $productPriceMoney->getCurrency()->getCurrencyCode(); //Проверка скидок /** * @var ShopDiscount $shopDiscount */ $shopDiscounts = ShopDiscount::find()->active()->orderBy(['shop_discount.priority' => SORT_ASC])->leftJoin('shop_discount2type_price', '`shop_discount2type_price`.`discount_id` = `shop_discount`.`id`')->andWhere(['or', ['shop_discount.site_id' => ""], ['shop_discount.site_id' => null], ['shop_discount.site_id' => \Yii::$app->cms->site->id]])->andWhere(['shop_discount2type_price.type_price_id' => $this->productPrice->typePrice->id])->all(); $price = $this->price; $this->discount_price = 0; $this->discount_value = ""; $this->discount_name = ""; if ($shopDiscounts) { $discountNames = []; $discountPercent = 0; foreach ($shopDiscounts as $shopDiscount) { if (\Yii::$app->user->can($shopDiscount->permissionName)) { $discountNames[] = $shopDiscount->name; if ($shopDiscount->value_type == ShopDiscount::VALUE_TYPE_P) { $percent = $shopDiscount->value / 100; $discountPercent = $discountPercent + $percent; $discountPrice = $price * $percent; $this->price = $this->price - $discountPrice; $this->discount_price = $this->discount_price + $discountPrice; //Нужно остановится и не применять другие скидки if ($shopDiscount->last_discount === Cms::BOOL_Y) { break; } } } } $this->discount_name = implode(" + ", $discountNames); $this->discount_value = \Yii::$app->formatter->asPercent($discountPercent); } //Если это предложение, нужно добавить свойства if ($parentElement && !$this->isNewRecord) { if ($properties = $product->cmsContentElement->relatedPropertiesModel->toArray()) { foreach ($properties as $code => $value) { if (!$this->getShopBasketProps()->andWhere(['code' => $code])->count() && $value) { $property = $product->cmsContentElement->relatedPropertiesModel->getRelatedProperty($code); $basketProperty = new ShopBasketProps(); $basketProperty->shop_basket_id = $this->id; $basketProperty->code = $code; $basketProperty->value = $product->cmsContentElement->relatedPropertiesModel->getSmartAttribute($code); $basketProperty->name = $property->name; $basketProperty->save(); } } } } return $this; }
echo $form->fieldSet(\skeeks\cms\shop\Module::t('app', 'Main')); ?> <?php echo $form->fieldCheckboxBoolean($model, 'active'); ?> <?php echo $form->field($model, 'name')->textInput(); ?> <?php echo $form->fieldSelect($model, 'site_id', \yii\helpers\ArrayHelper::map(\skeeks\cms\models\CmsSite::find()->all(), 'id', 'name')); ?> <?php echo $form->fieldSelect($model, 'value_type', \skeeks\cms\shop\models\ShopDiscount::getValueTypes()); ?> <?php echo $form->field($model, 'value')->textInput(); ?> <?php echo $form->field($model, 'max_discount')->textInput(); ?> <?php echo $form->fieldSelect($model, 'currency_code', \yii\helpers\ArrayHelper::map(\skeeks\modules\cms\money\models\Currency::find()->active()->all(), 'code', 'code')); ?> <?php echo $form->fieldInputInt($model, 'priority'); ?>