public function actionGetdiscount() { $code = rand(); $value = 0.95; $discount = new Discounts(); $discount->code = $code; $discount->value = $value; $discount->save(); return $this->actionAccount($code); }
public function compositeUnique($attribute, $params) { $ar = [':providers_id' => $this->providers_id, ':values' => $this->values, ':goods_type_type' => $this->goods_type_type]; $valid = Discounts::find()->where('providers_id=:providers_id and goods_type_type=:goods_type_type and `values`=:values', $ar)->one(); if ($valid instanceof Discounts && $valid->id != $this->id) { $this->addError($attribute, 'Такая скидка уже задана'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Discounts::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'providers_id' => $this->providers_id, 'coef' => $this->coef, 'price_type_id' => $this->price_type_id]); $query->andFilterWhere(['like', 'goods_type_type', $this->goods_type_type])->andFilterWhere(['like', 'params', $this->params])->andFilterWhere(['like', 'values', $this->values]); return $dataProvider; }
public static function calc($goods_id, $providers_id, $priceType) { //Получить список наценок if (!isset(self::$arDisconts)) { $arODisconts = \app\models\Discounts::find()->where('price_type_id = :price_type_id', [':price_type_id' => $priceType])->asArray()->all(); self::$arDisconts = []; foreach ($arODisconts as $arDiscont) { $arP = explode(';', $arDiscont['params']); $arV = explode(';', $arDiscont['values']); $arW = []; foreach ($arP as $key => $value) { if ($value) { $arW[$value] = $arV[$key]; } } $arDiscont['arPV'] = $arW; self::$arDisconts[$arDiscont['providers_id']][$arDiscont['goods_type_type']][] = $arDiscont; } } //Получить свойства товара $good = \app\models\Goods::findOne($goods_id); $goodT = \yii\helpers\ArrayHelper::toArray($good->getGoodsT()); //Для каждой наценки проверить - подходит ли товар под нее $dds = self::$arDisconts[$providers_id][$good->goods_type_type]; $k = 1; $arK = []; if (!is_array($dds)) { return 1; } foreach ($dds as $arDiscount) { if (count(array_intersect_assoc($arDiscount['arPV'], $goodT)) == count($arDiscount['arPV'])) { $k = $k * $arDiscount['coef']; $arK[] = $arDiscount['coef']; } } return $k > 1 ? $k : 1; }
public function actionCart() { $ids = Yii::$app->session->get('ids'); $id_count = Yii::$app->session->get('id_count'); $products = array(); for ($i = 0; $i < count($ids); $i++) { $products[] = Products::find()->where(['id' => $ids[$i]])->one(); } $model = new DiscountForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (!Yii::$app->session->get('value_of_code')) { $discount = Discounts::find(['value'])->where(['code' => $model->code])->one(); if ($discount) { Yii::$app->session->set('price', Yii::$app->session->get('price') * $discount->value); Yii::$app->session->set('value_of_code', $discount->value); } else { $error = 'Введен не существующий номер дисконта'; } } else { $error = 'Вы уже вводили номер дисконта'; } } return $this->render('cart', ['products' => $products, 'id_count' => $id_count, 'model' => $model, 'discount' => $discount, 'error' => $error]); }
/** * @return \yii\db\ActiveQuery */ public function getDiscounts() { return $this->hasMany(Discounts::className(), ['price_type_id' => 'id']); }
/** * Finds the Discounts model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Discounts the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Discounts::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }