[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ] ], ], ]; } /** * Liste les commandes récurrente du producteur. * * @return string */ public function actionIndex() { if(\Yii::$app->user->isGuest) { $producer = $this->getProducer(); return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } $searchModel = new SubscriptionSearch; $searchModel->id_user = User::getCurrentId(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider ]); } public function actionAjaxProcess() { // form $model = new SubscriptionForm; $model->id_producer = GlobalParam::getCurrentProducerId(); $model->id_user = User::getCurrentId(); $posts = Yii::$app->request->post(); $idSubscription = (int)$posts['idSubscription']; $isUpdate = false; if ($idSubscription) { $subscription = Subscription::findOne($idSubscription); if ($subscription) { $model->id = $idSubscription; $isUpdate = true; } } if ($model->load($posts) && $model->validate() && $model->save()) { $subscription = Subscription::searchOne([ 'id' => $model->id ]); $subscription->updateIncomingDistributions($isUpdate); if ($isUpdate) { Yii::$app->getSession()->setFlash('success', 'Abonnement modifié'); } else { Yii::$app->getSession()->setFlash('success', 'Abonnement ajouté'); } } } /** * Crée une commande récurrente. * * @return string */ public function actionForm($id = 0) { if(\Yii::$app->user->isGuest) { $producer = $this->getProducer(); return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } return $this->render('form', [ 'idSubscription' => (int)$id ]); } /** * Modifie une commande récurrente. * * @param integer $id * @return string * @throws NotFoundHttpException */ public function actionUpdate($id) { if(\Yii::$app->user->isGuest) { $producer = $this->getProducer(); return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } // form $model = new SubscriptionForm; $subscription = Subscription::findOne($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->date_begin = date('d/m/Y', strtotime($subscription->date_begin)); if (strlen($subscription->date_end)) { $model->date_end = date('d/m/Y', strtotime($subscription->date_end)); } $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; // produits $arrayProductsSubscription = ProductSubscription::searchAll([ 'id_subscription' => $model->id ]); foreach ($arrayProductsSubscription as $productSubscription) { $model->products['product_' . $productSubscription->id_product] = $productSubscription->quantity; } } else { throw new NotFoundHttpException('L\'abonnement est introuvable.', 404); } // produits $productsArray = Product::searchAll(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if (!strlen($model->date_end)) { $model->date_end = null; } if ($model->save()) { Yii::$app->getSession()->setFlash('success', 'Abonnement modifié'); $subscription = Subscription::findOne($model->id); $matchedDistributionsArray = $subscription->searchMatchedIncomingDistributions(); 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 un abonnement * * @param integer $id */ public function actionDelete($id) { if(\Yii::$app->user->isGuest) { $producer = $this->getProducer(); return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; } $subscription = Subscription::searchOne([ 'id' => $id ]); ProductSubscription::deleteAll(['id_subscription' => $id]); $subscription->deleteOrdersIncomingDistributions(); $subscription->delete(); Yii::$app->getSession()->setFlash('success', 'Abonnement supprimé'); return $this->redirect(['subscription/index']); } public function actionAjaxInfos($idSubscription = 0) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $params = []; $user = User::getCurrent(); $userProducer = UserProducer::searchOne([ 'id_user' => User::getCurrentId() ]); $pointSale = false; if ($idSubscription > 0) { $arrayProductsSubscription = ProductSubscription::searchAll([ 'id_subscription' => $idSubscription ]); $subscription = Subscription::findOne($idSubscription); if ($subscription) { if ($subscription->id_point_sale) { $pointSale = PointSale::findOne($subscription->id_point_sale); } } } // Produits $productsArray = Product::searchAll(); $indexProduct = 0; foreach ($productsArray as &$product) { $quantity = 0; $coefficientUnit = Product::$unitsArray[$product->unit]['coefficient']; if (isset($arrayProductsSubscription) && count($arrayProductsSubscription)) { foreach ($arrayProductsSubscription as $productSubscription) { if ($product->id == $productSubscription->id_product) { $coefficientUnit = Product::$unitsArray[$productSubscription->product->unit]['coefficient']; $quantity = $productSubscription->quantity * $coefficientUnit; } } } $product = array_merge( $product->getAttributes(), [ 'index' => $indexProduct++, 'quantity_form' => $quantity, 'coefficient_unit' => $coefficientUnit, 'wording_unit' => Product::strUnit($product->unit, 'wording_unit', true), 'wording_short' => Product::strUnit($product->unit, 'wording_short'), 'price_with_tax' => $product->getPriceWithTax([ 'user' => $user, 'user_producer' => $userProducer, 'point_sale' => $pointSale ]), ] ); } $params['products'] = $productsArray; $pointsSaleArray = PointSale::searchAll(); foreach ($pointsSaleArray as &$pointSale) { $pointSale = array_merge($pointSale->getAttributes(), [ 'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '') ]); if ($pointSale['code'] && strlen($pointSale['code'])) { $pointSale['code'] = '***'; } } $params['points_sale'] = $pointsSaleArray; if ($idSubscription > 0) { $subscription = Subscription::searchOne([ 'id' => $idSubscription ]); if (!$subscription || $subscription->id_user != User::getCurrentId()) { throw new UserException('Abonnement introuvable'); } else { $params = array_merge($params, $subscription->getAttributes()); } } return $params; } /** * Vérifie le code saisi pour un point de vente. * * @param integer $idPointSale * @param string $code * @return boolean */ public function actionAjaxValidateCodePointSale($idPointSale, $code) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $pointSale = PointSale::findOne($idPointSale); if ($pointSale) { if ($pointSale->validateCode($code)) { return 1; } } return 0; } }