[ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()); } ] ], ], ]; } /** * Liste les commandes récurrente du producteur. * * @return string */ public function actionIndex() { $this->checkProductsPointsSale(); $searchModel = new SubscriptionSearch(); $dataProvider = $searchModel->search(\Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider ]); } /** * Crée un abonnement. */ public function actionCreate($idOrder = 0) { $orderModule = $this->getOrderModule(); $producerModule = $this->getProducerModule(); $productModule = $this->getProductModule(); $subscriptionManger = $this->getSubscriptionModule(); $distributionModule = $this-> getDistributionModule(); $model = new SubscriptionForm(); $model->isAdmin = true; $model->id_producer = GlobalParam::getCurrentProducerId(); if ($idOrder) { $order = $orderModule->findOneOrderById($idOrder); if ($order) { $model->id_user = $order->id_user; $model->username = $order->username; $model->id_point_sale = $order->id_point_sale; $model->date_begin = date('d/m/Y'); $dateDay = strtolower(date('l', strtotime($order->distribution->date))); $model->$dateDay = 1; $model->week_frequency = 1; if ($model->id_user && $producerModule->getConfig('credit')) { $model->auto_payment = 1; } foreach ($order->productOrder as $productOrder) { $model->products['product_' . $productOrder->id_product] = $productOrder->quantity; } } else { throw new NotFoundHttpException('La commande est introuvable.', 404); } } // produits $productsArray = $productModule->findProducts(); if ($model->load(\Yii::$app->request->post()) && $model->validate() && $model->save()) { $this->setFlash('success', 'Abonnement ajouté'); $subscription = $subscriptionManger->findOneSubscriptionById($model->id); $matchedDistributionsArray = $distributionModule->findDistributionsIncomingMatchWithSubscrtiption($subscription); if (count($matchedDistributionsArray)) { return $this->redirect(['subscription/update-distributions', 'idSubscription' => $subscription->id]); } else { return $this->redirect(['subscription/index']); } } return $this->render('create', [ 'model' => $model, 'productsArray' => $productsArray ]); } /** * Modifie un abonnement. */ public function actionUpdate($id) { $subscriptionModule = $this->getSubscriptionModule(); $productSubscriptionModule = $this->getProductSubscriptionModule(); $productModule = $this->getProductModule(); $distributionModule = $this-> getDistributionModule(); $model = new SubscriptionForm; $model->isAdmin = true; $subscription = $subscriptionModule->findOneSubscriptionById($id); if ($subscription) { $model->id = $id; $model->id_producer = $subscription->id_producer; $model->id_user = $subscription->id_user; $model->username = $subscription->username; $model->id_point_sale = $subscription->id_point_sale; $model->monday = $subscription->monday; $model->tuesday = $subscription->tuesday; $model->wednesday = $subscription->wednesday; $model->thursday = $subscription->thursday; $model->friday = $subscription->friday; $model->saturday = $subscription->saturday; $model->sunday = $subscription->sunday; $model->auto_payment = $subscription->auto_payment; $model->week_frequency = $subscription->week_frequency; $model->date_begin = date('d/m/Y', strtotime($subscription->date_begin)); if ($subscription->date_end) { $model->date_end = date('d/m/Y', strtotime($subscription->date_end)); } if (strlen($subscription->comment)) { $model->comment = $subscription->comment; } $arrayProductsSubscription = $productSubscriptionModule->findProductSubscriptionsBySubscription($subscription); foreach ($arrayProductsSubscription as $productSubscription) { $model->products['product_' . $productSubscription->id_product] = $productSubscription->quantity; } } else { throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); } // produits $productsArray = $productModule->findProducts(); if ($model->load(\Yii::$app->request->post()) && $model->validate()) { if (!$model->date_end) { $model->date_end = null; } if ($model->save()) { $subscription = $subscriptionModule->findOneSubscriptionById($model->id); $messageOrdersDeleted = ''; if ($model->date_end) { $countOrdersDeleted = $subscriptionModule->getOrderManager() ->deleteOrdersIncomingDistributionsFromSubscription($subscription, true); if ($countOrdersDeleted) { $messageOrdersDeleted = '
' . $countOrdersDeleted . ' commandes supprimées'; } } $this->setFlash('success', 'Abonnement modifié' . $messageOrdersDeleted); $matchedDistributionsArray = $distributionModule->findDistributionsIncomingMatchWithSubscrtiption($subscription); if (count($matchedDistributionsArray)) { return $this->redirect(['subscription/update-distributions', 'idSubscription' => $subscription->id, 'update' => true]); } else { return $this->redirect(['subscription/index']); } } } return $this->render('update', [ 'model' => $model, 'productsArray' => $productsArray ]); } /** * Supprime une commande récurrente. */ public function actionDelete(int $id) { $subscriptionModule = $this->getSubscriptionModule(); $orderModule = $this->getOrderModule(); $subscription = $subscriptionModule->getRepository()->findOneSubscriptionById($id); if($subscription) { $subscriptionModule->getOrderManager()->deleteOrdersIncomingDistributionsFromSubscription($subscription); $subscriptionModule->deleteSubscription($subscription); $this->setFlash('success', 'Abonnement supprimé'); } else { $this->setFlash('danger', 'Abonnement introuvable'); } return $this->redirect(['subscription/index']); } public function actionUpdateDistributions(int $idSubscription, bool $generate = false, bool $update = false) { $subscriptionModule = $this->getSubscriptionModule(); $distributionModule = $this-> getDistributionModule(); $subscription = $subscriptionModule->findOneSubscriptionById($idSubscription); if($subscription) { $matchedDistributionsArray = $distributionModule->findDistributionsIncomingMatchWithSubscrtiption($subscription, true); if ($generate) { if ($update) { $subscriptionModule->getOrderManager() ->deleteOrdersIncomingDistributionsFromSubscription($subscription); } foreach ($matchedDistributionsArray as $distribution) { $subscriptionModule->createOrderFromSubscription($subscription, $distribution->date); } $this->setFlash('success', 'Commandes ' . ($update ? 're-' : '') . 'générées dans les distributions futures.'); return $this->redirect(['subscription/index']); } return $this->render('update_distributions', [ 'matchedDistributionsArray' => $matchedDistributionsArray, 'idSubscription' => $idSubscription, 'update' => $update ]); } else { $this->setFlash('error', 'Abonnement introuvable'); return $this->redirect(['subscription/index']); } } public function actionAjaxInfos(int $idSubscription = 0) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $unitModule = $this->getUnitModule(); $productModule = $this->getProductModule(); $productsQuery = Product::find() ->where(['id_producer' => GlobalParam::getCurrentProducerId()]) ->andWhere('status >= :status') ->addParams(['status' => Product::STATUS_OFFLINE]); if ($idSubscription) { $productsQuery->joinWith(['productSubscription' => function ($query) use ($idSubscription) { $query->andOnCondition('product_subscription.id_subscription = ' . ((int)$idSubscription)); }]); } $productsArray = $productsQuery->asArray()->orderBy('order ASC')->all(); foreach ($productsArray as &$theProduct) { $theProduct['wording_unit'] = $unitModule->getSolver()->strUnit($theProduct['unit']); if (isset($theProduct['productSubscription'][0])) { $theProduct['quantity'] = $theProduct['productSubscription'][0]['quantity'] * Product::$unitsArray[$theProduct['unit']]['coefficient']; } else { $theProduct['quantity'] = ''; } } return [ 'products' => $productsArray ]; } }