Browse Source

[Backend] Refactoring UserController

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
f39e2b0d03
1 changed files with 140 additions and 151 deletions
  1. +140
    -151
      backend/controllers/UserController.php

+ 140
- 151
backend/controllers/UserController.php View File

@@ -67,11 +67,6 @@ class UserController extends BackendController
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::class,
'actions' => [
],
],
'access' => [
'class' => AccessControl::class,
'rules' => [
@@ -125,65 +120,14 @@ class UserController extends BackendController
]);
}

public function initForm($model)
{
$userPointSaleManager = $this->getUserPointSaleManager();
$userUserGroupManager = $this->getUserUserGroupManager();
$userProducerManager = $this->getUserProducerManager();
$userGroupManager = $this->getUserGroupManager();

if ($model->id) {
$userPointSaleArray = $userPointSaleManager->findUserPointSalesByUser($model);
if ($userPointSaleArray && count($userPointSaleArray) > 0) {
foreach ($userPointSaleArray as $userPointSale) {
$model->points_sale[] = $userPointSale->id_point_sale;
}
}

$userUserGroupsArray = $userUserGroupManager->findUserUserGroupsByUser($model);
if ($userUserGroupsArray && count($userUserGroupsArray) > 0) {
foreach ($userUserGroupsArray as $userUserGroup) {
$model->user_groups[] = $userUserGroup->id_user_group;
}
}
$userProducer = $userProducerManager->findOneUserProducer($model);
$model->product_price_percent = $userProducer->product_price_percent;
}

$pointsSaleArray = PointSale::find()
->where([
'id_producer' => GlobalParam::getCurrentProducerId(),
'status' => 1
])
->joinWith(['userPointSale' => function ($query) use ($model) {
if ($model->id) {
$query->andOnCondition('user_point_sale.id_user = ' . $model->id);
}
}])
->all();

$userGroupsArray = $userGroupManager->findUserGroups();

return [
'pointsSaleArray' => $pointsSaleArray,
'userGroupsArray' => $userGroupsArray,
];
}

/**
* Creates a new User model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate()
{
$userManager = $this->getUserManager();
$producerManager = $this->getProducerManager();

$producerCurrent = $this->getProducerCurrent();
$model = $userManager->instanciateUser();

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

if ($posts && isset($posts['User']['email']) && strlen($posts['User']['email']) > 0) {
$userExist = $userManager->findOneUserByEmail($posts['User']['email']);
@@ -300,6 +244,51 @@ class UserController extends BackendController
]));
}

public function initForm($model)
{
$userPointSaleManager = $this->getUserPointSaleManager();
$userUserGroupManager = $this->getUserUserGroupManager();
$userProducerManager = $this->getUserProducerManager();
$userGroupManager = $this->getUserGroupManager();

if ($model->id) {
$userPointSaleArray = $userPointSaleManager->findUserPointSalesByUser($model);
if ($userPointSaleArray && count($userPointSaleArray) > 0) {
foreach ($userPointSaleArray as $userPointSale) {
$model->points_sale[] = $userPointSale->id_point_sale;
}
}

$userUserGroupsArray = $userUserGroupManager->findUserUserGroupsByUser($model);
if ($userUserGroupsArray && count($userUserGroupsArray) > 0) {
foreach ($userUserGroupsArray as $userUserGroup) {
$model->user_groups[] = $userUserGroup->id_user_group;
}
}
$userProducer = $userProducerManager->findOneUserProducer($model);
$model->product_price_percent = $userProducer->product_price_percent;
}

$pointsSaleArray = PointSale::find()
->where([
'id_producer' => GlobalParam::getCurrentProducerId(),
'status' => 1
])
->joinWith(['userPointSale' => function ($query) use ($model) {
if ($model->id) {
$query->andOnCondition('user_point_sale.id_user = ' . $model->id);
}
}])
->all();

$userGroupsArray = $userGroupManager->findUserGroups();

return [
'pointsSaleArray' => $pointsSaleArray,
'userGroupsArray' => $userGroupsArray,
];
}

/**
* Lie un utilisateur aux points de vente sélectionnés.
*/
@@ -393,6 +382,100 @@ class UserController extends BackendController
$this->redirect(array_merge(['index'], $params));
}

/**
* Affiche les données liées au crédit d'un utilisateur (formulaire, historique).
*/
public function actionCredit(int $id)
{
$user = User::find()->with('userProducer')->where(['id' => $id])->one();
$userProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);

if (($userProducer) || $this->isUserCurrentAdmin()) {

$creditForm = new CreditForm();
if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) {
$creditForm->id_user = $id;
$creditForm->save();

$creditForm = new CreditForm;
}

$history = CreditHistory::find()
->with(['order', 'userAction'])
->where([
'id_user' => $user->id,
'id_producer' => GlobalParam::getCurrentProducerId(),
])
->orderBy('date DESC')
->all();

return $this->render('credit', [
'user' => $user,
'userProducer' => $userProducer,
'creditForm' => $creditForm,
'history' => $history
]);
} else {
throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre établissement.");
}
}

/**
* Modifie l'option "credit_active" d'un utilisateur pour le producteur courant.
* Redirige vers la page de crédit de l'utilisateur.
*/
public function actionStateCredit($idUser, $state)
{
$userManager = $this->getUserManager();
$userProducerManager = $this->getUserProducerManager();

$user = $userManager->findOneUserById($idUser);
$userProducer = $userProducerManager->findOneUserProducer($user);

if ($userProducer) {
$userProducer->credit_active = $state;
$userProducer->save();
}

return $this->redirect(['user/credit', 'id' => $idUser]);
}

/**
* Affiche les commandes d'un utilisateur.
*
* @param integer $id
* @return mixed
*/
public function actionOrders($id)
{
$userManager = $this->getUserManager();

$user = $userManager->findOneUserById($id);
$searchModel = new OrderSearch();
$dataProvider = $searchModel->search(array_merge(\Yii::$app->request->queryParams, ['id_user' => $id]));

return $this->render('orders', [
'user' => $user,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*/
protected function findModel($id)
{
$userManager = $this->getUserManager();

if (($user = $userManager->findOneUserById($id)) !== null) {
return $user;
} else {
throw new NotFoundHttpException("Utilisateur introuvable");
}
}

/**
* Affiche la liste des emails des utilisateurs liés à un point de vente donné.
*/
@@ -482,98 +565,4 @@ class UserController extends BackendController
'usersPointSaleHasOrder' => $usersPointSaleHasOrder,
]);
}

/**
* Affiche les données liées au crédit d'un utilisateur (formulaire, historique).
*/
public function actionCredit(int $id)
{
$user = User::find()->with('userProducer')->where(['id' => $id])->one();
$userProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);

if (($userProducer) || $this->isUserCurrentAdmin()) {

$creditForm = new CreditForm();
if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) {
$creditForm->id_user = $id;
$creditForm->save();

$creditForm = new CreditForm;
}

$history = CreditHistory::find()
->with(['order', 'userAction'])
->where([
'id_user' => $user->id,
'id_producer' => GlobalParam::getCurrentProducerId(),
])
->orderBy('date DESC')
->all();

return $this->render('credit', [
'user' => $user,
'userProducer' => $userProducer,
'creditForm' => $creditForm,
'history' => $history
]);
} else {
throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre établissement.");
}
}

/**
* Affiche les commandes d'un utilisateur.
*
* @param integer $id
* @return mixed
*/
public function actionOrders($id)
{
$userManager = $this->getUserManager();

$user = $userManager->findOneUserById($id);
$searchModel = new OrderSearch();
$dataProvider = $searchModel->search(array_merge(\Yii::$app->request->queryParams, ['id_user' => $id]));

return $this->render('orders', [
'user' => $user,
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}

/**
* Modifie l'option "credit_active" d'un utilisateur pour le producteur courant.
* Redirige vers la page de crédit de l'utilisateur.
*/
public function actionStateCredit($idUser, $state)
{
$userManager = $this->getUserManager();
$userProducerManager = $this->getUserProducerManager();

$user = $userManager->findOneUserById($idUser);
$userProducer = $userProducerManager->findOneUserProducer($user);

if ($userProducer) {
$userProducer->credit_active = $state;
$userProducer->save();
}

return $this->redirect(['user/credit', 'id' => $idUser]);
}

/**
* Finds the User model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
*/
protected function findModel($id)
{
$userManager = $this->getUserManager();

if (($user = $userManager->findOneUserById($id)) !== null) {
return $user;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}

Loading…
Cancel
Save