function get_tovar_url($id) { $tovar = Products::findOne(['id' => $id]); $cat = ModArendaTree::findOne(['id' => $tovar->cat_id]); $url = []; $url[] = $cat->url; while ($cat->parent_id != 0) { $cat = ModArendaTree::findOne(['id' => $cat->parent_id]); $url[] = strtolower($cat->url); } $total_url = '/catalog'; for ($i = count($url) - 1; $i >= 0; $i--) { $total_url .= '/' . $url[$i]; } $total_url .= '/' . strtolower($this->translit($tovar->name)) . '-' . $tovar->id; return $total_url; }
public function actionIndex($id) { assert($id > 0); $product = Products::findOne($id); assert(isset($product)); $model = new OrderForm(); if (Yii::$app->request->isPost) { //POST $model->load(Yii::$app->request->post()); if ($model->validate()) { //TODO: store order in DB $order = new Order(); //etc............. return $this->render('success'); } else { $errors = $model->errors; } } else { //GET } $model->Product = $product; return $this->render('index', ['model' => $model]); }
public function actionEdit($id) { $model = Products::findOne(['id' => $id]); $all_cats = ModArendaTree::find()->asArray()->all(); if ($model->load(Yii::$app->request->post())) { $model->imageFile = UploadedFile::getInstance($model, 'imageFile'); if ($model->validate()) { if (!empty($model->imageFile)) { $model->upload(); $model->image = md5($model->imageFile->baseName . date("Y-m-d-H-i-s")) . '.' . $model->imageFile->extension; } if ($model->save()) { $last_id = $_GET['id']; foreach (Yii::$app->request->post('character') as $key => $value) { $model1 = CharacteristicsProducts::findOne(['product_id' => $last_id, 'character_id' => $key]); if (!$model1) { $model1 = new CharacteristicsProducts(); $model1->product_id = $last_id; } $model1->character_id = $key; $model1->value = empty($value) ? 'none' : $value; $model1->save(); } } // form inputs are valid, do something here } } $result = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS characteristic_name'])->where(['cat_id' => $model->cat_id])->leftJoin('characteristics', 'characteristics_for_cats.character_id=characteristics.id')->leftJoin('characteristics_products', 'characteristics_for_cats.character_id=characteristics_products.character_id')->asArray()->all(); if (empty($result)) { $cat = ModArendaTree::findOne(['id' => $model->cat_id]); while ($cat->parent_id != 0) { $result = CharacteristicsForCats::find()->select(['characteristics_for_cats.*', 'characteristics.name AS characteristic_name', 'characteristics_products.value AS VALUE'])->where(['cat_id' => $cat->parent_id])->leftJoin('characteristics', 'characteristics_for_cats.character_id=characteristics.id')->leftJoin('characteristics_products', 'characteristics_for_cats.character_id=characteristics_products.character_id AND characteristics_products.product_id="' . $_GET['id'] . '"')->asArray()->all(); if (!empty($result)) { break; } $cat = ModArendaTree::findOne(['id' => $cat->parent_id]); } } else { } $html = ''; if (!empty($result)) { foreach ($result as $key => $value) { $VAL = !empty($value['VALUE']) ? $value['VALUE'] : ''; $html .= ' <div class="form-group"> <label><p>' . $value['characteristic_name'] . '</p> <input value="' . $VAL . '" type="text" class="form-control" name="character[' . $value['character_id'] . ']" > </label> </div> '; } } return $this->render('edit', ['model' => $model, 'all_cats' => $all_cats, 'html' => $html]); }
/** * Finds the Products model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Products the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Products::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Finds the Products model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $product_id * @param integer $category_id * @param integer $manufacturer_id * @param integer $size_id * @return Products the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($product_id, $category_id, $manufacturer_id, $size_id) { if (($model = Products::findOne(['product_id' => $product_id, 'category_id' => $category_id, 'manufacturer_id' => $manufacturer_id, 'size_id' => $size_id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionBuyItems() { /* @var $product Products */ Yii::$app->response->format = 'json'; $return_data = ['status' => 'error', 'data' => []]; $basket = Yii::$app->request->post('data'); $items = []; if (isset($basket['items'])) { $items = $basket['items']; } $amount_paid = 0; if (isset($basket['amount_paid'])) { $amount_paid = $basket['amount_paid']; } $total_amount = 0; if (isset($basket['total_amount'])) { $total_amount = $basket['total_amount']; } if (!empty($items)) { $transaction = new Transactions(); $transaction->total_quantity = 0; $transaction->amount_paid = $amount_paid; $transaction->user_id = Yii::$app->user->id; $transaction->reason_id = Transactions::SELL; $transaction->total_amount = $total_amount; if ($transaction->save()) { $total_quantity = 0; foreach ($items as $item) { $transaction_item = new TransactionItems(); $transaction_item->product_id = $item['id']; $transaction_item->transaction_id = $transaction->id; $transaction_item->quantity = $item['quantity']; $total_quantity += intval($transaction_item->quantity); $transaction_item->buy_price = $item['buy_price']; $transaction_item->sell_price = $item['sell_price']; $transaction_item->name = $item['name']; if ($transaction_item->save()) { $product = Products::findOne($transaction_item->product_id); $product->quantity = $product->quantity - $transaction_item->quantity; $product->save(); } } $transaction->total_quantity = $total_quantity; $transaction->save(); Yii::$app->getSession()->setFlash('success', 'Transaction was successfully saved.'); $return_data = ['status' => 'success', 'data' => ['bill_num' => str_pad($transaction->id, 8, 0, STR_PAD_LEFT)]]; } else { $return_data = ['status' => 'error', 'data' => ['Error in Transaction']]; } } return $return_data; }
public function actionProcess() { $model = new Inventory(); if ($model->load(Yii::$app->request->post())) { $model->bill_no = $model->bill_no; $model->inventory = 'i'; $model->save(); $cart = new Cart(); foreach ($cart->contents() as $items) { $detail = new \app\models\Inventorydetail(); $detail->load(Yii::$app->request->post()); $detail->inventory_id = $model->id; $detail->product_id = $items['id']; $detail->qty = $items['qty']; $detail->price = $items['price']; $product = Products::findOne($items['id']); echo $product->qty = $items['qty']; $product->save(); $detail->save(); } $cart->destroy(); return $this->redirect(['inventory/index']); } }
public function actionAdd_price_zapros_message($name, $email, $tel, $product_id) { $mail = EmailTo::findOne(['id' => 1]); $functions = new Functions(); $tovar = Products::findOne(['id' => $product_id]); $model = new ZaprosPriceMessages(); $model->name = $name; $model->email = empty($email) ? '' : $email; $model->tel = empty($tel) ? '' : $tel; $model->product_id = $product_id; if ($model->save()) { $to = $mail->email_to; $subject = "Запрос на запрос коммерческого предложения"; $message = ' <html> <head> <title>Поступила новая заявка на запрос коммерческого предложения</title> </head> <body> <p>Только что поступила новая заявка на запрос коммерческого предложения!</p> <p>Имя:' . $name . '</p> <p>Телефон:' . (empty($tel) ? 'не указано' : $tel) . '</p> <p>Email:' . (empty($email) ? 'не указано' : $email) . '</p> <p>Товар:<a href="' . $functions->get_tovar_url($product_id) . '">' . $tovar->name . '</a></p> </body> </html>'; $headers = "Content-type: text/html; charset=windows-utf-8 \r\n"; $headers .= "From: TEST\r\n"; $headers .= "Bcc:TEST1\r\n"; mail($to, $subject, $message, $headers); return 'success'; } return false; }
/** * Загружает запись модели текущего контроллера по айдишнику * @param $id * @return null|static * @throws \yii\web\HttpException */ public function loadModel($id) { $model = Products::findOne($id); if ($model === null) { throw new \yii\web\HttpException(404, 'The requested page does not exist.'); } return $model; }
public function actionRemoveProducts() { if (isset($_POST['order_id'])) { $order = Orders::findOne(['id' => (int) $_POST['order_id']]); if (empty($order)) { //проверка на пустой заказ die('error on finding order'); } // $this->redirect('/products/shop'); //error } if (empty(Yii::$app->user->identity->id) || $order->user_id !== Yii::$app->user->identity->id) { // проверка на // пользователя и соответствия что пользователь хочет удалить именно свой заказ die('error with order user'); } // $this->redirect('/products/shop'); //error if (isset($_POST['product_id'])) { $product = Products::findOne(['id' => (int) $_POST['product_id']]); //находим продукт с таблицы if (empty($product)) { die('error on finding product'); } // $this->redirect('/products/shop'); //error } if ($order->removeProducts($product->id)) { // после проверок удаляем нужный продукт в заказе (функция в моделе заказа) $this->redirect('cart'); } else { die('error on removing product from order'); } //$this->redirect('/products/shop'); //error }