[ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsAdministrator($this->getUserCurrent()); } ] ], ], ]; } /** * Liste les producteurs. * * @return mixed */ public function actionIndex() { $dataProviderProducer = new ActiveDataProvider([ 'query' => Producer::find() ->with('userProducer', 'user') ->orderBy('active DESC, free_price DESC'), 'pagination' => [ 'pageSize' => 1000, ], ]); return $this->render('index', [ 'dataProviderProducer' => $dataProviderProducer ]); } /** * Crée un producteur. */ public function actionCreate() { $producerModule = $this->getProducerModule(); $producer = $producerModule->instanciateProducer(); if ($producer->load(\Yii::$app->request->post()) && $producerModule->saveCreate($producer)) { $this->setFlash('success', 'Producteur créé.'); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $producer, ]); } } /** * Modification d'un producteur. */ public function actionUpdate(int $id) { $producerModule = $this->getProducerModule(); $producer = $this->findProducer($id); if ($producer->load(\Yii::$app->request->post()) && $producerModule->saveCreate($producer)) { $this->setFlash('success', 'Producteur modifié.'); return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $producer, ]); } } /** * Facturation producteur. */ public function actionBilling(int $id) { $producer = $this->findProducer($id); return $this->render('billing', [ 'producer' => $producer, ]); } public function actionAlwaysdata(int $id) { $producer = $this->findProducer($id); if($producer->contact_email) { Yii::$app->alwaysdataClient->initRedirections($producer); return Ajax::responseSuccess('Redirections initialisées chez Alwaysdata'); } else { return Ajax::responseError("L'adresse email de contact du producteur n'est pas définie"); } } public function actionDolibarr(int $id) { $producerModule = $this->getProducerModule(); $producer = $this->findProducer($id); if($producer->dolibarr_socid) { $producerModule->generateDolibarrProducerInvoice($producer); return Ajax::responseSuccess("Facture générée sur Dolibarr pour le producteur \"".Html::encode($producer->name)."\""); } else { return Ajax::responseError("Dolibarr : l'id user du producteur doit être défini"); } } public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1) { $producerModule = $this->getProducerModule(); $userModule = $this->getUserModule(); $fromProducerId = (int)$fromProducerId; $toProducerId = (int)$toProducerId; $count = 0; $usersArray = $userModule->queryUsersBy(['id_producer' => $fromProducerId])->all(); foreach ($usersArray as $user) { $idUser = $user['user_id']; $countOrders = 0; if ($withOrders) { $countOrders = Order::searchCount([ 'id_user' => $idUser, 'distribution.id_producer' => $fromProducerId, 'order_status_alias' => OrderStatus::ALIAS_ORDERED ]); } if (($withOrders && $countOrders) || !$withOrders) { $producerModule->addUser( $userModule->findOneUserById($idUser), $producerModule->findOneProducerById($toProducerId) ); $count++; } } if ($count) { $this->setFlash('success', $count . ' clients importés du producteur #' . $fromProducerId . ' vers le producteur #' . $toProducerId . '.'); } else { $this->setFlash('error', 'Aucun client à importer.'); } return $this->redirect(['producer-admin/index']); } public function actionProducerInstallTaxUpdatePrices($idProducer) { $productsArray = Product::searchAll([ 'id_producer' => $idProducer ]); $connection = Yii::$app->getDb(); foreach ($productsArray as $product) { $product->price = round($product->price / (1 + $product->taxRate->value), 2); $product->save(); $command = $connection->createCommand(" UPDATE `product_order` SET price = ROUND(price / (1 + :tax_value), 2), id_tax_rate = :id_tax_rate WHERE id_product = :id_product", [ ':id_product' => $product->id, ':tax_value' => $product->taxRate->value, ':id_tax_rate' => $product->taxRate->id, ]); $result = $command->query(); } echo 'ok'; } /** * Recherche un producteur. */ protected function findProducer(int $id) { $producerModule = $this->getProducerModule(); if (($model = $producerModule->findOneProducerById($id)) !== null) { return $model; } else { throw new NotFoundHttpException('Producteur introuvable.'); } } }