[ 'class' => VerbFilter::class, 'actions' => [ ], ], 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->hasAccessBackend(); } ] ], ], ]; } /** * Liste les modèles Produit. * * @return mixed */ public function actionIndex() { $searchModel = new ProductSearch(); $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Crée un Product. */ public function actionCreate() { $productManager = $this->getProductManager(); $distributionManager = $this->getDistributionManager(); $model = $productManager->instanciateProduct(); $model->active = 1; $model->id_producer = GlobalParam::getCurrentProducerId(); $model->monday = 1; $model->tuesday = 1; $model->wednesday = 1; $model->thursday = 1; $model->friday = 1; $model->saturday = 1; $model->sunday = 1; $model->available_on_points_sale = 1; if ($model->load(\Yii::$app->request->post()) && $productManager->saveCreate($model)) { $lastProductOrder = Product::find()->where('id_producer = :id_producer')->params([':id_producer' => GlobalParam::getCurrentProducerId()])->orderBy('order DESC')->one(); if ($lastProductOrder) { $model->order = ++$lastProductOrder->order; } Upload::uploadFile($model, 'photo'); $productManager->saveUpdate($model); $this->processAvailabilityPointsSale($model); $distributionManager->addProductIncomingDistributions($model); $this->setFlash('success', 'Produit ' . Html::encode($model->name) . ' ajouté'); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Modifie un Product. */ public function actionUpdate($id) { $productManager = $this->getProductManager(); $distributionManager = $this->getDistributionManager(); $request = Yii::$app->request; $model = $this->findModel($id); foreach ($model->productPointSale as $productPointSale) { $model->pointsSale[] = $productPointSale->id_point_sale; } $photoFilenameOld = $model->photo; if ($model->load(\Yii::$app->request->post()) && $productManager->saveUpdate($model)) { Upload::uploadFile($model, 'photo', $photoFilenameOld); $deletePhoto = $request->post('delete_photo', 0); if ($deletePhoto) { $model->photo = ''; $model->save(); } $this->processAvailabilityPointsSale($model); if ($model->apply_distributions) { $distributionManager->addProductIncomingDistributions($model); } $this->setFlash('success', 'Produit ' . Html::encode($model->name) . ' modifié'); return $this->redirect(['index']); } return $this->render('update/update', [ 'model' => $model, 'action' => 'update', ]); } /** * Traite les accès restreints d'un point de vente. */ public function processAvailabilityPointsSale($product) { $pointSaleManager = $this->getPointSaleManager(); $productPointSaleManager = $this->getProductPointSaleManager(); ProductPointSale::deleteAll(['id_product' => $product->id]); if (is_array($product->pointsSale) && count($product->pointsSale)) { foreach ($product->pointsSale as $key => $idPointSale) { $pointSale = $pointSaleManager->findOnePointSaleById($idPointSale); if ($pointSale) { $productPointSaleManager->createProductPointSale( $product, $pointSale, ($product->available_on_points_sale) ? false : true ); } } } } public function actionPricesList(int $id) { $model = $this->findModel($id); $searchModel = new ProductPriceSearch(); $searchModel->id_product = $id; $dataProvider = $searchModel->search(array_merge(\Yii::$app->request->queryParams, [ 'id_product' => $id ])); $userProducerWithProductPercent = UserProducer::searchAll([], [ 'join_with' => ['user'], 'conditions' => 'user_producer.product_price_percent != 0', ]); $pointSaleWithProductPercent = PointSale::searchAll([], [ 'conditions' => 'point_sale.product_price_percent != 0' ]); return $this->render('update/prices/list', [ 'model' => $model, 'action' => 'prices-list', 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'userProducerWithProductPercent' => $userProducerWithProductPercent, 'pointSaleWithProductPercent' => $pointSaleWithProductPercent, ]); } public function actionPricesCreate($idProduct) { $model = new ProductPrice(); $model->id_product = $idProduct; $modelProduct = $this->findModel($idProduct); if ($model->load(\Yii::$app->request->post())) { $conditionsProductPriceExist = [ 'id_product' => $idProduct, 'id_user' => $model->id_user ?? null, 'id_user_group' => $model->id_user_group ?? null, 'id_point_sale' => $model->id_point_sale ?? null, 'from_quantity' => $model->from_quantity ?? null, ]; $productPriceExist = ProductPrice::findOne($conditionsProductPriceExist); if ($productPriceExist) { $productPriceExist->delete(); $this->setFlash('warning', 'Un prix existait déjà pour cet utilisateur / point de vente, il a été supprimé.'); } if ($model->save()) { $this->setFlash('success', 'Le prix a bien été ajouté.'); return $this->redirect(['product/prices-list', 'id' => $idProduct]); } } return $this->render('update/prices/create', [ 'model' => $model, 'modelProduct' => $modelProduct, ]); } public function actionPricesUpdate($id) { $model = $this->findModelProductPrice($id); $modelProduct = $this->findModel($model->id_product); if ($model->load(\Yii::$app->request->post()) && $model->save()) { $this->setFlash('success', 'Prix modifié'); return $this->redirect(['product/prices-list', 'id' => $model->id_product]); } return $this->render('update/prices/update', [ 'model' => $model, 'modelProduct' => $modelProduct, 'action' => 'prices-update', ]); } public function actionPricesDelete($id) { $productPrice = $this->findModelProductPrice($id); $productPrice->delete(); $this->setFlash('success', 'Prix supprimé'); return $this->redirect(['product/prices-list', 'id' => $productPrice->id_product]); } /** * Supprime un Product. */ public function actionDelete(int $id, bool $confirm = false) { $product = $this->findModel($id); if ($confirm) { $product->delete(); ProductDistribution::deleteAll(['id_product' => $id]); $this->setFlash('success', 'Produit ' . Html::encode($product->name) . ' supprimé'); } else { $this->setFlash('info', 'Souhaitez-vous vraiment supprimer le produit ' . Html::encode($product->name) . ' ? ' . Html::a('Oui', ['product/delete', 'id' => $id, 'confirm' => 1], ['class' => 'btn btn-default']) . ' ' . Html::a('Non', ['product/index'], ['class' => 'btn btn-default'])); } return $this->redirect(['index']); } /** * Modifie l'ordre des produits. */ public function actionOrder() { $array = Yii::$app->request->post('array'); $orderArray = json_decode(stripslashes($array)); foreach ($orderArray as $id => $order) { $product = $this->findModel($id); $product->order = $order; $product->save(); } } public function actionAjaxToggleActive($id, $active) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $distributionManager = $this->getDistributionManager(); $product = $this->findModel($id); $product->active = (int) $active; $product->save(); $distributionManager->addProductIncomingDistributions($product); return ['success', 'id' => $id, 'active' => $active]; } /** * Recherche un produit en fonction de son ID. */ protected function findModel(int $id) { $productManager = $this->getProductManager(); if (($product = $productManager->findOneProductById($id)) !== null) { return $product; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } protected function findModelProductPrice($id) { $productPriceManager = $this->getProductPriceManager(); if (($productPrice = $productPriceManager->findOneProductPriceById($id)) !== null) { return $productPrice; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }