Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

155 lines
4.2KB

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