Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

156 Zeilen
4.4KB

  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. 'orderby' => 'date DESC',
  86. 'limit' => 15,
  87. 'condition' => 'production.date > \''.date('Y-m-d 00:00:00').'\' AND (type = \''.Commande::TYPE_USER.'\' OR type = \''.Commande::TYPE_ADMIN.'\')'
  88. ]) ;
  89. foreach($commandes as $c)
  90. $c->init() ;
  91. // clients
  92. $nb_clients = User::findBy()->count();
  93. $clients = User::findBy()
  94. ->orderBy('created_at DESC')
  95. ->limit(5)
  96. ->all();
  97. $clients_credit_pain_negatif = User::findBy(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  98. ->andWhere('user_etablissement.credit < 0')
  99. ->all();
  100. // paramètres
  101. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  102. return $this->render('index',[
  103. 'productions' => $productions,
  104. 'commandes' => $commandes,
  105. 'clients' => $clients,
  106. 'nb_clients' => $nb_clients,
  107. 'clients_credit_pain_negatif' => $clients_credit_pain_negatif,
  108. 'etablissement' => $etablissement,
  109. ]);
  110. }
  111. public function actionLogin()
  112. {
  113. if (!\Yii::$app->user->isGuest) {
  114. return $this->goHome();
  115. }
  116. $model = new LoginForm();
  117. if ($model->load(Yii::$app->request->post()) && $model->login())
  118. {
  119. return $this->goBack();
  120. }
  121. else {
  122. return $this->render('login', [
  123. 'model' => $model,
  124. ]);
  125. }
  126. }
  127. public function actionLogout()
  128. {
  129. Yii::$app->user->logout();
  130. return $this->goHome();
  131. }
  132. public function actionChangeEtablissement($id)
  133. {
  134. Yii::$app->user->identity->id_etablissement = $id ;
  135. Yii::$app->user->identity->save() ;
  136. $this->redirect(['site/index']) ;
  137. }
  138. }