|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use domain\Distribution\PointSaleDistribution\PointSaleDistribution;
- use domain\Order\Order\Order;
- use domain\Order\Order\OrderRepositoryQuery;
- use domain\Order\OrderStatus\OrderStatus;
- use domain\PointSale\PointSale\PointSale;
- use domain\PointSale\PointSale\PointSaleSearch;
- use domain\PointSale\UserPointSale\UserPointSale;
- use yii\filters\AccessControl;
- use yii\helpers\Html;
- use yii\web\NotFoundHttpException;
-
- class PointSaleController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserModule()
- ->getAuthorizationChecker()
- ->isGrantedAsProducer($this->getUserCurrent());
- }
- ],
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
- $searchModel = new PointSaleSearch();
- $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $pointSaleModule = $this->getPointSaleModule();
- $distributionModule = $this-> getDistributionModule();
-
- $pointSale = $pointSaleModule->instanciatePointSale();
-
- if ($pointSale->load(\Yii::$app->request->post()) && $pointSale->save()) {
- $pointSaleModule->updatePointSalePointProduction($pointSale);
- $pointSaleModule->processRestrictedAccess($pointSale);
- $this->initPaymentMethodsByCreditFunctioning($pointSale);
- $distributionModule->addPointSaleIncomingDistributions($pointSale);
-
- $this->setFlash('success', 'Point de vente créé.');
- return $this->redirectAfterSave('point-sale', $pointSale->id);
- } else {
- return $this->render('create', array_merge($this->initForm(), [
- 'model' => $pointSale,
- ]));
- }
- }
-
-
-
- public function actionUpdate(int $id)
- {
- $distributionModule = $this-> getDistributionModule();
- $pointSaleModule = $this->getPointSaleModule();
- $model = $this->findModel($id);
-
- 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()) {
-
- $pointSaleModule->updatePointSalePointProduction($model);
- $pointSaleModule->processRestrictedAccess($model);
- $this->initPaymentMethodsByCreditFunctioning($model);
- $distributionModule->addPointSaleIncomingDistributions($model);
-
- $this->setFlash('success', 'Point de vente modifié.');
- return $this->redirectAfterSave('point-sale', $model->id);
- } else {
- return $this->render('update', array_merge($this->initForm($id), [
- 'model' => $model,
- ]));
- }
- }
-
- public function initPaymentMethodsByCreditFunctioning(PointSale $pointSale)
- {
- if($this->getPointSaleModule()->getBuilder()->initPaymentMethodsByCreditFunctioning($pointSale)) {
- $this->addFlash('info', 'Le paiement sur place a été ajusté par rapport au fonctionnement de la cagnotte.');
- }
- }
-
-
-
- public function initForm(int $id = 0)
- {
- $userModule = $this->getUserModule();
-
- $users = $userModule->queryUsersBy()
- ->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(int $id, $confirm = false)
- {
- $orderModule = $this->getOrderModule();
- $distributionModule = $this-> getDistributionModule();
-
- $pointSale = $this->findModel($id);
-
- if ($confirm) {
- $pointSale->status = -1;
- $pointSale->save();
-
-
- UserPointSale::deleteAll(['id_point_sale' => $id]);
-
-
- $incomingDistributions = $distributionModule->findDistributionsIncoming(true);
- foreach ($incomingDistributions as $distribution) {
- PointSaleDistribution::deleteAll(['id_point_sale' => $id, 'id_distribution' => $distribution->id]);
- }
-
-
- $ordersArray = Order::searchAll(
- [
- 'id_point_sale' => $id,
- ],
- [
- 'conditions' => [
- 'distribution.date > :today',
- OrderRepositoryQuery::getSqlFilterIsValid()
- ],
- 'params' => [':today' => date('Y-m-d')]
- ]
- );
-
- if ($ordersArray) {
- foreach ($ordersArray as $order) {
- $orderModule->getManager()->deleteOrder($order, $this->getUserCurrent(), true);
- }
- }
-
- $this->setFlash('success', 'Point de vente <strong>' . Html::encode($pointSale->name) . '</strong> supprimé.');
- } else {
- $this->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(int $id)
- {
- $pointSaleModule = $this->getPointSaleModule();
-
- $pointSale = $this->findModel($id);
- if ($pointSale) {
- PointSale::updateAll(['default' => 0], 'id_producer = :id_producer', [':id_producer' => GlobalParam::getCurrentProducerId()]);
-
- if (!$pointSale->default) {
- $pointSale->default = 1;
- $pointSaleModule->saveUpdate($pointSale);
-
- $this->setFlash('success', 'Point de vente <strong>' . Html::encode($pointSale->name) . '</strong> défini par défaut.');
- } else {
- $this->setFlash('success', 'Aucun point de vente défini par défaut');
- }
- }
-
- return $this->redirect(['index']);
- }
-
-
-
- protected function findModel(int $id)
- {
- $model = PointSale::find()
- ->with('userPointSale')
- ->where(['id' => $id])
- ->one();
-
- if(!$model || $model->id_producer != $this->getProducerCurrent()->id) {
- throw new NotFoundHttpException('Le point de vente demandé est introuvable.');
- }
-
- return $model;
- }
- }
|