[ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()); } ] ], ], ]; } /** * 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() { $productModule = $this->getProductModule(); $distributionModule = $this-> getDistributionModule(); $model = $productModule->instanciateProduct(); $model->status = Product::STATUS_ONLINE; $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())) { $model->photoFile = UploadedFile::getInstance($model, 'photoFile'); if($model->validate()) { $lastProductOrder = Product::find()->where('id_producer = :id_producer')->params([':id_producer' => GlobalParam::getCurrentProducerId()])->orderBy('order DESC')->one(); if ($lastProductOrder) { $model->order = ++$lastProductOrder->order; } $productModule->create($model); if($model->photoFile) { Upload::uploadFile($model, 'photoFile', 'photo'); } $this->processAvailabilityPointsSale($model); $distributionModule->addProductIncomingDistributions($model); $this->setFlash('success', 'Produit ' . Html::encode($model->name) . ' ajouté'); return $this->redirect(['index']); } } return $this->render('create', [ 'model' => $model, ]); } /** * Modifie un Product. */ public function actionUpdate($id) { $productModule = $this->getProductModule(); $distributionModule = $this-> getDistributionModule(); $request = Yii::$app->request; $model = $this->findModel($id); foreach ($model->productPointSale as $productPointSale) { $model->pointsSale[] = $productPointSale->id_point_sale; } $photoFilenameOld = $model->photo; if (Yii::$app->request->isPost && $model->load(\Yii::$app->request->post())) { $model->photoFile = UploadedFile::getInstance($model, 'photoFile'); if($model->validate()) { if($model->photoFile) { Upload::uploadFile($model, 'photoFile', 'photo', $photoFilenameOld); } $deletePhoto = $request->post('delete_photo', 0); if ($deletePhoto) { $model->photo = ''; $model->save(); } $this->processAvailabilityPointsSale($model); if ($model->apply_distributions) { $distributionModule->addProductIncomingDistributions($model); } $productModule->update($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) { $pointSaleModule = $this->getPointSaleModule(); $productPointSaleModule = $this->getProductPointSaleModule(); ProductPointSale::deleteAll(['id_product' => $product->id]); if (is_array($product->pointsSale) && count($product->pointsSale)) { foreach ($product->pointsSale as $key => $idPointSale) { $pointSale = $pointSaleModule->findOnePointSaleById($idPointSale); if ($pointSale) { $productPointSaleModule->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) { $productModule = $this->getProductModule(); $productDistributionModule = $this->getProductDistributionModule(); $product = $this->findModel($id); if ($confirm) { $productModule->getBuilder()->updateStatusDeleted($product); $productDistributionModule->getBuilder()->disableProductDistributionsIncomingByProduct($product); $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 actionAjaxToggleStatus($id, $status) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $distributionModule = $this-> getDistributionModule(); $product = $this->findModel($id); $product->status = (int) $status; $product->save(); $distributionModule->addProductIncomingDistributions($product); return ['success', 'id' => $id, 'status' => $status]; } /** * Import des prix produits via un fichier CSV. * * @return mixed */ public function actionPriceImport() { if($this->getFeatureModule()->getChecker()->isDisabled(Feature::ALIAS_PRODUCT_PRICE_IMPORT)) { return $this->redirectDashboard(); } $productModule = $this->getProductModule(); $productPriceModule = $this->getProductPriceModule(); $userGroupModule = $this->getUserGroupModule(); $pointSaleModule = $this->getPointSaleModule(); $userModule = $this->getUserModule(); $model = new ProductPriceUploadForm(); if (Yii::$app->request->isPost) { $model->file = UploadedFile::getInstance($model, 'file'); if ($model->file && $model->validate()) { $productPriceCsvArray = array_map(function($data) { return str_getcsv($data,";");}, file($model->file->tempName)); if(!$productPriceCsvArray || count($productPriceCsvArray[0]) != 6) { $this->setFlash('error', "Format de fichier invalide. Veuillez vérifier que le séparateur de champs de votre fichier est bien \";\"."); } else { unset($productPriceCsvArray[0]); $countUpdate = 0; $countCreate = 0; $cptLine = 1; $dataNotFound = false; $dataNotFoundArray = []; foreach ($productPriceCsvArray as $productPriceCsv) { $cptLine ++; if (count($productPriceCsv) != 6) { $dataNotFound = true; continue; } $productName = $productPriceCsv[0]; $userArray = explode('#', $productPriceCsv[1]); $userGroupName = $productPriceCsv[2]; $pointSaleName = $productPriceCsv[3]; $quantityFrom = (float)$productPriceCsv[4]; $price = (float) str_replace(',', '.', $productPriceCsv[5]); $product = $productName ? $productModule->findOneProductByName($productName) : null; $user = (count($userArray) > 1) ? $userModule->findOneUserById((int)$userArray[1]) : null; $userGroup = $userGroupName ? $userGroupModule->findOneUserGroupByName($userGroupName) : null; $pointSale = $pointSaleName ? $pointSaleModule->findOnePointSaleByName($pointSaleName) : null; if (($productName && !$product) || (count($userArray) > 1 && !$user) || ($userGroupName && !$userGroup) || ($pointSaleName && !$pointSale)) { $dataNotFound = true; $dataNotFoundArray[] = $cptLine; continue; } if ($product) { // prix de base if (!$user && !$userGroup && !$pointSale && !$quantityFrom) { $product->price = $price; $productModule->saveUpdate($product); $countUpdate++; } // prix spécifique else { $productPrice = $productPriceModule->findOneProductPriceBy($product, $user, $userGroup, $pointSale, $quantityFrom); if ($productPrice) { $productPrice->price = $price; $productPriceModule->saveUpdate($productPrice); $countUpdate++; } // Création automatique du prix spécifique else { $productPrice = $productPriceModule->instanciateProductPrice($product, $price, $user, $userGroup, $pointSale, $quantityFrom); $productPriceModule->saveCreate($productPrice); $countCreate ++; } } } } if ($dataNotFound) { $strLinesDataNotFound = '('.implode(', ', $dataNotFoundArray).')'; $this->addFlash('error', "Attention, certaines lignes ".$strLinesDataNotFound." du fichier n'ont pas été prises en compte. Veuillez réessayer en repartant du fichier d'export.
Contacter l'administrateur du site si le problème persiste."); } if ($countUpdate) { $this->addFlash('success', $countUpdate . ' prix produits mis à jour.'); } if($countCreate) { $this->addFlash('success', $countCreate . ' prix produits créés.'); } } } } return $this->render('price_import', [ 'model' => $model ]); } /** * Export des prix produits au format CSV. * * @return mixed */ public function actionPriceExport() { if($this->getFeatureModule()->getChecker()->isDisabled(Feature::ALIAS_PRODUCT_PRICE_IMPORT)) { return $this->redirectDashboard(); } $productModule = $this->getProductModule(); $productPriceModule = $this->getProductPriceModule(); $userModule = $this->getUserModule(); $data = []; $data[] = [ "Produit", "Utilisateur", "Groupe d'utilisateur", "Point de vente", "À partir de la quantité", "Prix HT" ]; $productArray = $productModule->findProducts(); foreach($productArray as $product) { // prix produit $data[] = [ $product->name, '', '', '', '', CSV::formatNumber($product->price) ]; // prix spécifiques foreach($product->productPrice as $productPrice) { $productPrice = $productPriceModule->findOneProductPriceById($productPrice->id); if($productPrice->user || $productPrice->userGroup || $productPrice->pointSale || $productPrice->from_quantity) { $data[] = [ $product->name, $productPrice->user ? str_replace('#', '', $userModule->getUsername($productPrice->user)).' #'.$productPrice->user->id : '', $productPrice->userGroup ? $productPrice->userGroup->name : '', $productPrice->pointSale ? $productPrice->pointSale->name : '', $productPrice->from_quantity ?? '', CSV::formatNumber($productPrice->price) ]; } } } CSV::downloadSendHeaders('prix_produits.csv'); echo CSV::array2csv($data); die(); } /** * Recherche un produit en fonction de son ID. */ protected function findModel(int $id) { $productModule = $this->getProductModule(); if (($product = $productModule->findOneProductById($id)) !== null) { return $product; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } protected function findModelProductPrice($id) { $productPriceModule = $this->getProductPriceModule(); if (($productPrice = $productPriceModule->findOneProductPriceById($id)) !== null) { return $productPrice; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }