[ 'class' => VerbFilter::class, 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->isCurrentAdmin(); } ] ], ], ]; } /** * Liste les producteurs. * * @return mixed */ public function actionIndex() { $producerManager = $this->getProducerManager(); $dataProviderProducer = new ActiveDataProvider([ 'query' => Producer::find() ->with('userProducer', 'user') ->orderBy('active DESC, free_price DESC'), 'pagination' => [ 'pageSize' => 1000, ], ]); $producersArray = $producerManager->findProducersActive(); $sumPrices = 0; foreach ($producersArray as $producer) { $sumPrices += $producerManager->getAmountBilledLastMonth($producer); } return $this->render('index', [ 'dataProviderProducer' => $dataProviderProducer, 'sumPrices' => $sumPrices ]); } /** * Crée un producteur. */ public function actionCreate() { $producerManager = $this->getProducerManager(); $producer = $producerManager->instanciateProducer(); if ($producer->load(\Yii::$app->request->post()) && $producerManager->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) { $producerManager = $this->getProducerManager(); $producer = $this->findModel($id); if ($producer->load(\Yii::$app->request->post()) && $producerManager->saveCreate($producer)) { $this->setFlash('success', 'Producteur modifié.'); return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $producer, ]); } } public function actionUserTransfer($fromProducerId, $toProducerId, $withOrders = 1) { $producerManager = $this->getProducerManager(); $userManager = $this->getUserManager(); $fromProducerId = (int)$fromProducerId; $toProducerId = (int)$toProducerId; $count = 0; $usersArray = $userManager->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 ], ['conditions' => 'date_delete IS NULL']); } if (($withOrders && $countOrders) || !$withOrders) { $producerManager->addUser( $userManager->findOneUserById($idUser), $producerManager->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 findModel(int $id) { if (($model = Producer::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }