You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.6KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use common\models\LoginForm;
  7. use common\models\User;
  8. use yii\filters\VerbFilter;
  9. use common\models\Produit;
  10. use common\models\PointVente;
  11. use common\models\Etablissement;
  12. use common\models\Production;
  13. /**
  14. * Site controller
  15. */
  16. class SiteController extends BackendController
  17. {
  18. /**
  19. * @inheritdoc
  20. */
  21. public function behaviors()
  22. {
  23. return [
  24. 'access' => [
  25. 'class' => AccessControl::className(),
  26. 'rules' => [
  27. [
  28. 'actions' => ['login', 'error'],
  29. 'allow' => true,
  30. ],
  31. [
  32. 'actions' => ['logout', 'index'],
  33. 'allow' => true,
  34. 'roles' => ['@'],
  35. 'matchCallback' => function ($rule, $action) {
  36. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  37. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  38. }
  39. ],
  40. [
  41. 'actions' => ['change-etablissement'],
  42. 'allow' => true,
  43. 'roles' => ['@'],
  44. 'matchCallback' => function ($rule, $action) {
  45. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  46. }
  47. ],
  48. ],
  49. ],
  50. 'verbs' => [
  51. 'class' => VerbFilter::className(),
  52. 'actions' => [
  53. 'logout' => ['post'],
  54. ],
  55. ],
  56. ];
  57. }
  58. /**
  59. * @inheritdoc
  60. */
  61. public function actions()
  62. {
  63. return [
  64. 'error' => [
  65. 'class' => 'yii\web\ErrorAction',
  66. ],
  67. ];
  68. }
  69. public function actionIndex()
  70. {
  71. // commandes
  72. $productions = Production::find()
  73. ->with('commande')
  74. ->where(['>=','production.date',date('Y-m-d')])
  75. ->andWhere([
  76. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement,
  77. 'production.actif' => 1
  78. ])
  79. ->orderBy('date ASC')
  80. ->limit(5)
  81. ->all() ;
  82. // produits
  83. $nb_produits = Produit::count() ;
  84. $produits = Produit::find()
  85. ->where([
  86. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  87. ])
  88. ->orderBy('id DESC')
  89. ->limit(5)
  90. ->all() ;
  91. // points de vente
  92. $nb_points_vente = PointVente::count() ;
  93. $points_vente = PointVente::find()
  94. ->where([
  95. 'id_etablissement' => Yii::$app->user->identity->id_etablissement
  96. ])
  97. ->orderBy('id DESC')
  98. ->limit(5)
  99. ->all() ;
  100. // clients
  101. $nb_clients = User::findBy()->count();
  102. $clients = User::findBy()
  103. ->orderBy('created_at DESC')
  104. ->limit(5)
  105. ->all();
  106. // paramètres
  107. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  108. return $this->render('index',[
  109. 'productions' => $productions,
  110. 'nb_produits' => $nb_produits,
  111. 'produits' => $produits,
  112. 'nb_points_vente' => $nb_points_vente,
  113. 'points_vente' => $points_vente,
  114. 'clients' => $clients,
  115. 'nb_clients' => $nb_clients,
  116. 'etablissement' => $etablissement,
  117. ]);
  118. }
  119. public function actionLogin()
  120. {
  121. if (!\Yii::$app->user->isGuest) {
  122. return $this->goHome();
  123. }
  124. $model = new LoginForm();
  125. if ($model->load(Yii::$app->request->post()) && $model->login())
  126. {
  127. return $this->goBack();
  128. }
  129. else {
  130. return $this->render('login', [
  131. 'model' => $model,
  132. ]);
  133. }
  134. }
  135. public function actionLogout()
  136. {
  137. Yii::$app->user->logout();
  138. return $this->goHome();
  139. }
  140. public function actionChangeEtablissement($id)
  141. {
  142. Yii::$app->user->identity->id_etablissement = $id ;
  143. Yii::$app->user->identity->save() ;
  144. $this->redirect(['site/index']) ;
  145. }
  146. }