|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?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']);
- }
-
- }
|