|
- <?php
-
-
-
- namespace backend\controllers;
-
- use Yii;
- use yii\filters\AccessControl;
- use common\models\PointSale;
- use yii\data\ActiveDataProvider;
- use yii\web\Controller;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use common\models\User;
- use common\models\UserPointSale;
- use common\models\Order ;
- use common\models\Producer ;
- use common\models\Distribution ;
- use yii\helpers\Html;
-
-
- class PointSaleController extends BackendController
- {
-
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- ],
- ],
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend();
- }
- ],
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
- $searchModel = new PointSaleSearch() ;
- $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $model = new PointSale();
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- $model->processPointProduction();
- $model->processRestrictedAccess();
- Distribution::linkPointSaleIncomingDistributions($model) ;
- return $this->redirect(['index']);
- } else {
- return $this->render('create', array_merge($this->initForm(), [
- 'model' => $model,
- ]));
- }
- }
-
-
-
- public function actionUpdate($id)
- {
- $model = PointSale::find()
- ->with('userPointSale')
- ->where(['id' => $id])
- ->one();
-
- foreach ($model->userPointSale as $userPointSale) {
- $model->users[] = $userPointSale->id_user;
- $model->users_comment[$userPointSale->id_user] = $userPointSale->comment;
- }
-
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- $model->processPointProduction();
- $model->processRestrictedAccess();
- Distribution::linkPointSaleIncomingDistributions($model) ;
- Yii::$app->getSession()->setFlash('success', 'Point de vente modifié.');
- return $this->redirect(['index']);
- } else {
- return $this->render('update', array_merge($this->initForm($id), [
- 'model' => $model,
- ]));
- }
- }
-
-
-
- public function initForm($id = 0)
- {
- $users = User::findBy()
- ->leftJoin('user_point_sale', 'user_point_sale.id_user = user.id AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $id])
- ->orderBy('user_point_sale.id_point_sale DESC, lastname ASC, name ASC')
- ->all();
-
- return [
- 'users' => $users
- ];
- }
-
-
-
- public function actionDelete($id, $confirm = false)
- {
- $pointSale = $this->findModel($id) ;
-
- if($confirm) {
- $pointSale->delete();
- UserPointSale::deleteAll(['id_point_sale' => $id]);
- PointSaleDistribution::deleteAll(['id_point_sale' => $id]) ;
- Order::updateAll(['id_point_sale' => 0], 'id_point_sale = :id_point_sale', [':id_point_sale' => $id]) ;
- Yii::$app->getSession()->setFlash('success', 'Point de vente <strong>'.Html::encode($pointSale->name).'</strong> supprimé.');
- }
- else {
- Yii::$app->getSession()->setFlash('info', 'Souhaitez-vous vraiment supprimer le point de vente <strong>'.Html::encode($pointSale->name).'</strong> ? '
- . Html::a('Oui',['point-sale/delete','id' => $id, 'confirm' => 1], ['class' => 'btn btn-default']).' '.Html::a('Non', ['point-sale/index'], ['class' => 'btn btn-default']));
- }
-
- return $this->redirect(['index']);
- }
-
-
-
- public function actionDefault($id)
- {
- $pointSale = $this->findModel($id) ;
- if($pointSale) {
- PointSale::updateAll(['default' => 0], 'id_producer = :id_producer', [':id_producer' => GlobalParam::getCurrentProducerId()]) ;
- if(!$pointSale->default) {
- $pointSale->default = 1 ;
- $pointSale->save() ;
- Yii::$app->getSession()->setFlash('success', 'Point de vente <strong>'.Html::encode($pointSale->name).'</strong> défini par défaut.') ;
- }
- else {
- Yii::$app->getSession()->setFlash('success', 'Aucun point de vente défini par défaut') ;
- }
- }
-
- return $this->redirect(['index']);
- }
-
-
-
- protected function findModel($id)
- {
- if (($model = PointSale::findOne($id)) !== null) {
- return $model;
- }
- else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|