Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

149 lines
4.5KB

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