|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\logic\Order\Order\Model\Order;
- use common\logic\Producer\Producer\Model\Producer;
- use common\logic\Product\Product\Model\Product;
- use Yii;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use yii\data\ActiveDataProvider;
-
-
- class ProducerAdminController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::class,
- 'actions' => [
- 'delete' => ['post'],
- ],
- ],
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserManager()->isCurrentAdmin();
- }
- ]
- ],
- ],
- ];
- }
-
-
-
- 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
- ]);
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- 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';
- }
-
-
-
- protected function findModel(int $id)
- {
- if (($model = Producer::findOne($id)) !== null) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|