- <?php
-
- namespace backend\controllers;
-
- use Yii;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use common\models\LoginForm;
- use common\models\User;
- use yii\filters\VerbFilter;
- use common\models\Produit;
- use common\models\PointVente;
- use common\models\Etablissement;
- use common\models\Production;
- use common\models\Commande;
-
- /**
- * Site controller
- */
- class SiteController extends BackendController {
-
- /**
- * @inheritdoc
- */
- public function behaviors() {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['login', 'error'],
- 'allow' => true,
- ],
- [
- 'actions' => ['logout', 'index'],
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return Yii::$app->user->identity->status == USER::STATUS_ADMIN
- || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
- }
- ],
- [
- 'actions' => ['change-etablissement'],
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return Yii::$app->user->identity->status == USER::STATUS_ADMIN;
- }
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function actions() {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- ];
- }
-
- public function actionIndex() {
- // commandes
- $productions = Production::find()
- ->with('commande')
- ->where(['>=', 'production.date', date('Y-m-d')])
- ->andWhere([
- 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement,
- 'production.actif' => 1
- ])
- ->orderBy('date ASC')
- ->limit(5)
- ->all();
-
- // dernières commandes
- $commandes = Commande::findBy([
- 'orderby' => 'date DESC',
- 'limit' => 15,
- 'condition' => 'production.date > \'' . date('Y-m-d 00:00:00') . '\' AND (type = \'' . Commande::TYPE_USER . '\' OR type = \'' . Commande::TYPE_ADMIN . '\')'
- ]);
-
- foreach ($commandes as $c)
- $c->init();
-
- // clients
- $nb_clients = User::findBy()->count();
-
- $clients = User::findBy()
- ->orderBy('created_at DESC')
- ->limit(5)
- ->all();
-
- $clients_credit_pain_negatif = User::findBy(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
- ->andWhere('user_etablissement.credit < 0')
- ->all();
-
- // paramètres
- $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement);
-
- return $this->render('index', [
- 'productions' => $productions,
- 'commandes' => $commandes,
- 'clients' => $clients,
- 'nb_clients' => $nb_clients,
- 'clients_credit_pain_negatif' => $clients_credit_pain_negatif,
- 'etablissement' => $etablissement,
- ]);
- }
-
- public function actionLogin() {
- if (!\Yii::$app->user->isGuest) {
- return $this->goHome();
- }
-
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- } else {
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- }
-
- public function actionLogout() {
- Yii::$app->user->logout();
-
- return $this->goHome();
- }
-
- public function actionChangeEtablissement($id) {
- Yii::$app->user->identity->id_etablissement = $id;
- Yii::$app->user->identity->save();
- $this->redirect(['site/index']);
- }
-
- }
|