[ '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->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; } $productManager->create($model); if($model->photoFile) { Upload::uploadFile($model, 'photoFile', 'photo'); } $this->processAvailabilityPointsSale($model); $distributionManager->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) { $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 (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) { $distributionManager->addProductIncomingDistributions($model); } $productManager->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) { $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) { $productContainer = $this->getProductContainer(); $productDistributionContainer = $this->getProductDistributionContainer(); $product = $this->findModel($id); if ($confirm) { $productContainer->getBuilder()->updateStatusDeleted($product); $productDistributionContainer->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; $distributionManager = $this->getDistributionManager(); $product = $this->findModel($id); $product->status = (int) $status; $product->save(); $distributionManager->addProductIncomingDistributions($product); return ['success', 'id' => $id, 'status' => $status]; } /** * Import des prix produits via un fichier CSV. * * @return mixed */ public function actionPriceImport() { $productManager = $this->getProductManager(); $productPriceManager = $this->getProductPriceManager(); $userGroupManager = $this->getUserGroupManager(); $pointSaleManager = $this->getPointSaleManager(); $userManager = $this->getUserManager(); $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 ? $productManager->findOneProductByName($productName) : null; $user = (count($userArray) > 1) ? $userManager->findOneUserById((int)$userArray[1]) : null; $userGroup = $userGroupName ? $userGroupManager->findOneUserGroupByName($userGroupName) : null; $pointSale = $pointSaleName ? $pointSaleManager->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; $productManager->saveUpdate($product); $countUpdate++; } // prix spécifique else { $productPrice = $productPriceManager->findOneProductPriceBy($product, $user, $userGroup, $pointSale, $quantityFrom); if ($productPrice) { $productPrice->price = $price; $productPriceManager->saveUpdate($productPrice); $countUpdate++; } // Création automatique du prix spécifique else { $productPrice = $productPriceManager->instanciateProductPrice($product, $price, $user, $userGroup, $pointSale, $quantityFrom); $productPriceManager->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() { $productManager = $this->getProductManager(); $productPriceManager = $this->getProductPriceManager(); $userManager = $this->getUserManager(); $data = []; $data[] = [ "Produit", "Utilisateur", "Groupe d'utilisateur", "Point de vente", "À partir de la quantité", "Prix HT" ]; $productArray = $productManager->findProducts(); foreach($productArray as $product) { // prix produit $data[] = [ $product->name, '', '', '', '', CSV::formatPrice($product->price) ]; // prix spécifiques foreach($product->productPrice as $productPrice) { $productPrice = $productPriceManager->findOneProductPriceById($productPrice->id); if($productPrice->user || $productPrice->userGroup || $productPrice->pointSale || $productPrice->from_quantity) { $data[] = [ $product->name, $productPrice->user ? str_replace('#', '', $userManager->getUsername($productPrice->user)).' #'.$productPrice->user->id : '', $productPrice->userGroup ? $productPrice->userGroup->name : '', $productPrice->pointSale ? $productPrice->pointSale->name : '', $productPrice->from_quantity ?? '', CSV::formatPrice($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) { $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.'); } } }