|
- <?php
-
-
-
- namespace backend\controllers;
-
- use domain\PointSale\SharedPointSale\SharedPointSale;
- use domain\Feature\Feature\Feature;
- use yii\filters\AccessControl;
- use yii\web\NotFoundHttpException;
-
-
- class SharedPointSaleController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- $authorizationChecker = $this->getUserModule()->getAuthorizationChecker();
- $featureChecker = $this->getFeatureModule()->getChecker();
-
- return $authorizationChecker->isGrantedAsProducer($this->getUserCurrent())
- && $featureChecker->isEnabled(Feature::ALIAS_SHARED_POINT_SALE);
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionIndex()
- {
- $sharedPointSaleRepository = $this->getSharedPointSaleModule()->getRepository();
-
- return $this->render('index', [
- 'sharedPointsSaleRequestOfMe' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOfMe(),
- 'sharedPointsSaleRequestOthers' => $sharedPointSaleRepository->findSharedPointsSaleRequestsOthers(),
- 'pointsSaleArray' => $this->getPointSaleModule()->getRepository()->findPointSales()
- ]);
- }
-
- public function actionCreate()
- {
- $sharedPointSaleModule = $this->getSharedPointSaleModule();
- $sharedPointSaleModel = $sharedPointSaleModule->getBuilder()->instanciateSharedPointSale(null, null, $this->getUserCurrent());
- $sharedPointSaleModel->scenario = SharedPointSale::SCENARIO_CREATE;
- if($sharedPointSaleModel->load(\Yii::$app->request->post()) && $sharedPointSaleModel->validate()) {
- $sharedPointSaleModule->getManager()->createSharedPointSale(
- $sharedPointSaleModel->getPointSale(),
- $sharedPointSaleModel->getProducerWithSharing(),
- $this->getUserCurrent()
- );
- $this->setFlash('success', "La demande de partage a bien été créée");
- return $this->redirect(['index']);
- }
-
- return $this->render('create', [
- 'sharedPointSaleModel' => $sharedPointSaleModel
- ]);
- }
-
- public function actionConfirm(int $id)
- {
- $sharedPointSale = $this->findSharedPointSale($id);
- $sharedPointSale->scenario = SharedPointSale::SCENARIO_CONFIRM;
- $sharedPointSale->producerCurrent = $this->getProducerCurrent();
-
- if($sharedPointSale->load(\Yii::$app->request->post()) && $sharedPointSale->validate()) {
- $pointSaleWithSharing = $sharedPointSale->getPointSaleWithSharing();
- if($this->getSharedPointSaleModule()->getManager()->confirmSharedPointSale($sharedPointSale, $pointSaleWithSharing, $this->getUserCurrent())) {
- $this->addFlash('success', "Le partage de point de vente a bien été confirmé");
- }
- else {
- $this->addFlash('error', "Une erreur est survenue lors de la confirmation du partage");
- }
- }
- else {
- foreach($sharedPointSale->getErrors() as $errorArray) {
- foreach($errorArray as $errorMessage) {
- $this->addFlash('error', $errorMessage);
- }
- }
- }
-
- return $this->redirect(['index']);
- }
-
- public function actionDecline(int $id)
- {
- $sharedPointSale = $this->findSharedPointSale($id);
-
- if(!in_array($this->getProducerCurrent()->id, [$sharedPointSale->getProducerWithSharing()->id, $sharedPointSale->getPointSale()->id_producer])) {
- $this->addFlash('error', "Vous ne pouvez pas refuser cette demande de partage.");
- }
- else {
- if($this->getSharedPointSaleModule()->getManager()->declineSharedPointSale($sharedPointSale, $this->getUserCurrent())) {
- $this->addFlash('success', "La demande de partage de point de vente a bien été annulée.");
- }
- else {
- $this->addFlash('error', "Une erreur est survenue lors de l'annulation de la demande de partage.");
- }
- }
-
- return $this->redirect(['index']);
- }
-
- public function findSharedPointSale(int $id)
- {
- $sharedPointSale = $this->getSharedPointSaleModule()->getRepository()->findOneSharedPointSaleById($id);
-
- if(!$sharedPointSale) {
- throw new NotFoundHttpException("La demande de point de vente partagé n'a pas été trouvée.");
- }
-
- return $sharedPointSale;
- }
- }
|