Browse Source

Refactoring services #885

refactoring
Guillaume 1 year ago
parent
commit
38fdd24ce3
4 changed files with 45 additions and 36 deletions
  1. +3
    -2
      common/controllers/CommonController.php
  2. +13
    -0
      common/logic/Order/Order/OrderSolver.php
  3. +10
    -0
      common/logic/User/UserProducer/UserProducerBuilder.php
  4. +19
    -34
      producer/controllers/OrderController.php

+ 3
- 2
common/controllers/CommonController.php View File

@@ -40,6 +40,7 @@ namespace common\controllers;

use common\components\BusinessLogic;
use common\components\BusinessLogicContainerTrait;
use common\logic\User\User\User;
use yii;

class CommonController extends \yii\web\Controller
@@ -75,9 +76,9 @@ class CommonController extends \yii\web\Controller
return Yii::$app->urlManagerBackend;
}

public function getUserCurrent()
public function getUserCurrent(): User
{
return Yii::$app->getUser();
return Yii::$app->user->identity;
}

public function isUserCurrentAdmin()

+ 13
- 0
common/logic/Order/Order/OrderSolver.php View File

@@ -365,5 +365,18 @@ class OrderSolver extends BaseService implements SolverInterface
}
}

public function isStateOpen(Order $order): bool
{
return Order::STATE_OPEN == $order->getState();
}

public function isStatePreparation(Order $order): bool
{
return Order::STATE_PREPARATION == $order->getState();
}

public function isStateDelivered(Order $order): bool
{
return Order::STATE_DELIVERED == $order->getState();
}
}

+ 10
- 0
common/logic/User/UserProducer/UserProducerBuilder.php View File

@@ -131,4 +131,14 @@ class UserProducerBuilder extends BaseService implements BuilderInterface
$oldCredit > $creditLimitReminder
&& $newCredit <= $creditLimitReminder;
}

public function updateActive(User $user, Producer $producer, bool $active): void
{
$userProducer = $this->getUserProducerContainer()->getRepository()->getOne(
$user,
$producer
);
$userProducer->active = $active;
$userProducer->save();
}
}

+ 19
- 34
producer/controllers/OrderController.php View File

@@ -40,6 +40,7 @@ namespace producer\controllers;

use common\helpers\GlobalParam;
use common\helpers\Mailjet;
use common\logic\Order\Order\Order;
use common\logic\Producer\Producer\Producer;
use common\logic\User\CreditHistory\CreditHistory;
use common\logic\User\User\User;
@@ -57,7 +58,7 @@ class OrderController extends ProducerBaseController
return [];
}

public function actionOrder($id = 0, $date = '')
public function actionOrder(int $id = 0, $date = '')
{
$params = [];
$producer = $this->getProducer();
@@ -69,24 +70,16 @@ class OrderController extends ProducerBaseController
}

if ($id) {
$order = Order::searchOne([
'id' => $id
]);
if ($order) {
if ($order->getState() == Order::STATE_OPEN) {
$params['order'] = $order;
}
$order = $this->getOrderContainer()->getRepository()->getOneById($id);
if ($order && $this->getOrderContainer()->getSolver()->isStateOpen($order)) {
$params['order'] = $order;
}
}

if (strlen($date)) {
$distribution = DistributionModel::searchOne([
'date' => $date,
'id_producer' => $producer->id
]);

$distribution = $this->getDistributionContainer()->getRepository()->getOne($producer, $date);
if ($distribution) {
$distributionsArray = DistributionModel::filterDistributionsByDateDelay([$distribution]);
$distributionsArray = $this->getDistributionContainer()->getSolver()->filterDistributionsByDateDelay([$distribution]);
if (count($distributionsArray) == 1) {
$params['date'] = $date;
}
@@ -107,7 +100,7 @@ class OrderController extends ProducerBaseController
->with('productOrder', 'pointSale', 'creditHistory')
->joinWith('distribution', 'distribution.producer')
->where([
'id_user' => Yii::$app->user->id,
'id_user' => $this->getUserCurrent()->id,
'distribution.id_producer' => $producer->id
])
->params([':date_today' => date('Y-m-d')]);
@@ -137,25 +130,22 @@ class OrderController extends ProducerBaseController

/**
* Supprime un producteur.
*
* @param integer $id
*/
public function actionRemoveProducer($id = 0)
public function actionRemoveProducer(int $id = 0)
{
$userProducer = UserProducer::find()
->where(['id_producer' => $id, 'id_user' => GlobalParam::getCurrentUserId()])
->one();
$producer = $this->getProducerContainer()->getRepository()->getOneById($id);

$userProducer->active = 0;
$userProducer->save();
$this->getUserProducerContainer()->getBuilder()->updateActive(
$this->getUserCurrent(),
$producer,
false
);

$this->redirect(['order/index']);
return $this->redirect(['order/index']);
}

/**
* Crée une commande.
*
* @return mixed
*/
public function actionAjaxProcess()
{
@@ -165,14 +155,14 @@ class OrderController extends ProducerBaseController
$producer = $this->getProducer();
$idProducer = $producer->id;

$posts = Yii::$app->request->post();
$posts = \Yii::$app->request->post();

if ($idProducer) {
$this->_verifyProducerActive($idProducer);
}

if ($order->load($posts)) {
$user = User::getCurrent();
$user = $this->getUserCurrent();

$order = Order::find()
->where('id_distribution = :id_distribution')
@@ -312,9 +302,6 @@ class OrderController extends ProducerBaseController

/**
* Vérifie si un producteur est actif.
*
* @param integer $idProducer
* @throws NotFoundHttpException
*/
public function _verifyProducerActive($idProducer)
{
@@ -326,12 +313,10 @@ class OrderController extends ProducerBaseController

/**
* Traite le formulaire de création/modification de commande.
*
* @param Commande $order
*/
public function processForm($order, $user)
{
$posts = Yii::$app->request->post();
$posts = \Yii::$app->request->post();
$productsArray = [];
$totalQuantity = 0;
$producer = $this->getProducer();

Loading…
Cancel
Save