[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ] ], ], ]; } /** * Retourne au format JSON toutes les informations relatives à une * production donnée. * * @param integer $idDistribution * @return mixed */ public function actionInfosDistribution($idDistribution) { $distribution = Distribution::searchOne($idDistribution); if ($distribution) { $arr = []; $productsArray = ProductDistribution::searchByDistribution($distribution->id) ; $data['products'] = $productsArray; $pointSaleArray = PointSale::find() ->joinWith(['pointSaleDistribution' => function($q) use ($distribution) { $q->where(['id_distribution' => $distribution->id]); }]) ->where([ 'id_producer' => $distribution->id_producer, ]) ->all(); $data['pointsSale'] = []; foreach ($pointSaleArray as $pointSale) { if (isset($pointSale->pointSaleDistribution) && isset($pointSale->pointSaleDistribution[0])) { $data['points_vente'][$pointSale->id] = $pointSale->pointSaleDistribution[0]->delivery; } else { $data['points_vente'][$pointSale->id] = false; } } return json_encode($data); } return json_encode([]); } /** * Initialise le formulaire de création/modification de commande. * * @param Order $order * @return array */ public function initForm($order = null) { // Producteurs $producersArray = Yii::$app->user->identity->getBookmarkedProducers(); $idProducer = $this->getProducer()->id; // Producteur $producer = Producer::findOne($idProducer); // Points de vente $pointsSaleArray = PointSale::find() ->with('userPointSale') ->where(['id_producer' => $idProducer]) ->andWhere('restricted_access = 0 OR (restricted_access = 1 AND (SELECT COUNT(*) FROM user_point_sale WHERE point_sale.id = user_point_sale.id_point_sale AND point_sale_user.id_user = :id_user) > 0)') ->params([':id_user' => User::getCurrentId()]) ->all(); // jours de production $deadline = 20; $date = date('Y-m-d'); if (isset($producer)) { $deadline = $producer->order_deadline; if (date('H') >= $deadline) { $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($producer->order_delay) * (24 * 60 * 60)); } else { $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($producer->order_delay - 1) * (24 * 60 * 60)); } } $distributionDays = Distribution::find() ->where(['active' => 1]) ->andWhere('date > :date') ->andWhere(['id_producer' => $idProducer]) ->addParams([':date' => $date]) ->all(); $distributionDaysArray = array('' => '--'); foreach ($distributionDays as $distribution) { $distributionDaysArray[$distribution->id] = date('d/m/Y', strtotime($distribution->date)); } // produits $products = Product::find() ->leftJoin('product_distribution', 'product.id = product_distribution.id_product') ->where(['product.active' => 1, 'id_producer' => $idProducer]) ->orderBy('product.order ASC')->all(); $productsArray = [] ; foreach ($products as $p) $productsArray[] = $p; // produits selec $posts = Yii::$app->request->post(); $selectedProducts = []; if (isset($posts['Product'])) { foreach ($posts['Product'] as $key => $quantity) { $key = (int) str_replace('product_', '', $key); $product = Product::find()->where(['id' => $key])->one(); if ($product && $quantity) $selectedProducts[$product->id] = (int) $quantity; } } elseif (!is_null($order)) { $productOrderArray = ProductOrder::searchAll([ 'id_order' => $order->id ]) ; foreach ($productOrderArray as $productOrder) { $selectedProducts[$productOrder->id_product] = (int) $productOrder->quantity; } } $availableProducts = [] ; $distribution = null; if (!is_null($order) && $order->id_distribution) { $availableProducts = ProductDistribution::searchByDistribution($order->id_distribution); $distribution = Distribution::find()->where(['id' => $order->id_distribution])->one(); } $orders = Order::searchAll([ 'id_user' => User::getCurrentId() ]) ; if ($idProducer) { $userProducer = UserProducer::searchOne([ 'id_producer' => $idProducer, 'id_user' => User::getCurrentId() ]) ; $credit = $userProducer->credit; } else { $credit = 0; } return [ 'pointsSaleArray' => $pointsSaleArray, 'distributionDaysArray' => $distributionDaysArray, 'productsArray' => $productsArray, 'selectedProducts' => $selectedProducts, 'availableProducts' => $availableProducts, 'distribution' => $distribution, 'ordersArray' => $orders, 'producersArray' => $producersArray, 'idProducer' => $idProducer, 'producer' => $producer, 'credit' => $credit ]; } /** * Affiche l'historique des commandes de l'utilisateur * * @return ProducerView */ public function actionHistory() { $dataProviderOrders = new ActiveDataProvider([ 'query' => Order::find() ->with('productOrder', 'pointSale', 'creditHistory') ->joinWith('distribution', 'distribution.producer') ->where([ 'id_user' => Yii::$app->user->id, 'distribution.id_producer' => $this->getProducer()->id ]) ->andWhere('date_delete IS NULL') ->orderBy('distribution.date DESC'), 'pagination' => [ 'pageSize' => 10, ], ]); return $this->render('history', [ 'dataProviderOrders' => $dataProviderOrders, 'orderOk' => Yii::$app->getRequest()->get('orderOk', false), 'cancelOk' => Yii::$app->getRequest()->get('cancelOk', false), ]); } /** * Supprime un producteur. * * @param integer $id */ public function actionRemoveProducer($id = 0) { $userProducer = UserProducer::find() ->where(['id_producer' => $id, 'id_user' => User::getCurrentId()]) ->one(); $userProducer->active = 0; $userProducer->save(); $this->redirect(['order/index']); } /** * Crée une commande. * * @return mixed */ public function actionCreate() { $idProducer = $this->getProducer()->id ; $order = new Order; $posts = Yii::$app->request->post(); if ($idProducer) { $this->_verifyProducerActive($idProducer); } if ($order->load($posts)) { $order = Order::find()->where('id_distribution = ' . $posts['Order']['id_distribution'])->andWhere('id_user = ' . User::getCurrentId())->one(); if (!$order) { $order = new Order; $order->load(Yii::$app->request->post()); $order->id_user = User::getCurrentId(); $order->date = date('Y-m-d H:i:s'); $order->type = Order::ORIGIN_USER; } $this->processForm($Order); } return $this->render('create', array_merge($this->initForm($order), [ 'model' => $order ])); } /** * Modifie une commande. * * @param integer $id * @return mixed * @throws UserException */ public function actionUpdate($id) { $order = sOrder::searchOne([ 'id' => $id ]) ; if ($order->getState() != Order::STATE_OPEN) { throw new UserException('Cette commande n\'est pas modifiable.'); } $this->_verifyProducerActive($order->distribution->id_producer); if ($order && $order->load(Yii::$app->request->post())) { $order->date_update = date('Y-m-d H:i:s'); $this->processForm($order); } return $this->render('update', array_merge($this->processForm($order), [ 'model' => $order, 'orderNotfound' => !$order, ])); } /** * Vérifie si un producteur est actif. * * @param integer $idProducer * @throws NotFoundHttpException */ public function _verifyProducerActive($idProducer) { $producer = Producer::findOne($idProducer); if ($producer && !$producer->active) { throw new NotFoundHttpException('Ce producteur est actuellement hors ligne.'); } } /** * Traite le formulaire de création/modification de commande. * * @param Commande $order */ public function processForm($order) { $posts = Yii::$app->request->post(); $productsArray = []; $totalQuantity = 0; foreach ($posts['Product'] as $key => $quantity) { $key = (int) str_replace('product_', '', $key); $product = Produit::find()->where(['id' => $key])->one(); $totalQuantity += $quantity; if ($product && $quantity) { $productsArray[] = $product; } } // date $errorDate = false; if (isset($order->id_distribution)) { // date de commande $distribution = Distribution::find()->where(['id' => $order->id_distribution])->one(); if (date('H') >= 20) { $date = date('Y-m-d', strtotime(date('Y-m-d')) + 60 * 60 * 24); } else { $date = date('Y-m-d'); } if ($distribution->date < $date) { $errorDate = true; } } // point de vente $errorPointSale = false; if (isset($distribution) && $distribution) { $pointSaleDistribution = PointSaleDistribution::searchOne([ 'id_distribution' => $distribution->id, 'id_point_sale' => $posts['Order']['id_point_sale'] ]) ; if (!$pointSaleDistribution || !$pointSaleDistribution->delivery) { $errorPointSale = true; } $pointSale = PointSale::findOne($posts['Commande']['id_point_vente']); if ($pointSale) { if (strlen($pointSale->code) && !$pointSale->validateCode($posts['code_point_sale_' . $pointSale->id])) { $errorPointSale = true; } } else { $errorPointSale = true; } } if ($order->validate() && count($productsArray) && !$errorDate && !$errorPointSale) { // gestion point de vente $pointSale = PointSale::searchOne([ 'id' => $order->id_point_sale ]) ; if ($pointSale && strlen($pointSale->getComment())) $order->comment_point_sale = $pv->getComment(); else $order->comment_point_sale = ''; // sauvegarde de la commande $order->save(); // ajout de l'utilisateur à l'établissement Producer::addUser(User::getCurrentId(), $distribution->id_producer) ; // suppression de tous les enregistrements ProductOrder if (!is_null($order)) { ProductOrder::deleteAll(['id_order' => $order->id]); } // produits dispos $availableProducts = ProductDistribution::searchByDistribution($distribution->id) ; // sauvegarde des produits foreach ($productsArray as $product) { if (isset($availableProducts[$product->id])) { $productOrder = new ProductOrder(); $productOrder->id_order = $order->id; $productOrder->id_product = $product->id; $productOrder->price = $product->price; $quantity = (int) $posts['Product']['product_' . $product->id]; if ($produits_dispos[$product->id]['quantity_max'] && $quantity > $availableProducts[$product->id]['quantity_remaining']) { $quantity = $availableProducts[$p->id]['quantity_remaining']; } $productOrder->quantity = $quantity; $productOrder->save(); } } // credit $credit = isset($posts['credit']) && $posts['credit']; if ($credit && ($pointSale->credit || $order->getAmount(Order::AMOUNT_PAID))) { $order = Order::searchOne([ 'id' => $order->id ]) ; $amountPaid = $order->getAmount(Order::AMOUNT_PAID); // à payer if ($order->getPaymentStatus() == Order::PAYMENT_UNPAID) { $amountRemaining = $order->getAmount(Order::AMOUNT_REMAINING) ; $credit = Yii::$app->user->identity->getCredit($distribution->id_producer); if ($amountRemaining > $credit) { $amountRemaining = $credit; } if ($amountRemaining > 0) { $order->creditHistory( CreditHistory::TYPE_PAYMENT, $amountRemaining, $distribution->id_producer, User::getCurrentId(), User::getCurrentId() ); } } // surplus à rembourser elseif ($order->getPaymentStatus() == Order::PAYMENT_SURPLUS) { $amountSurplus = $order->getAmount(Order::AMOUNT_SURPLUS) ; $order->creditHistory( CreditHistory::TYPE_REFUND, $amountSurplus, $distribution->id_producer, User::getCurrentId(), User::getCurrentId() ); } } // redirection $this->redirect(Yii::$app->urlManager->createUrl(['order/history', 'orderOk' => true])); } else { if (!count($productsArray)) { Yii::$app->session->setFlash('error', "Vous n'avez choisi aucun produit"); } if ($errorDate) { Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander pour cette date."); } if ($errorPointSale) { Yii::$app->session->setFlash('error', "Point de vente invalide."); } } } /** * Annule une commande. * * @param integer $id * @throws \yii\web\NotFoundHttpException * @throws UserException */ public function actionCancel($id) { $order = Order::searchOne([ 'id' => $id ]) ; if(!$order) { throw new \yii\web\NotFoundHttpException('Commande introuvable'); } if ($order->getState() != Order::STATE_OPEN) { throw new UserException('Vous ne pouvez plus annuler cette commande.'); } if ($order && User::getCurrentId() == $order->id_user) { // remboursement if ($order->getAmount(Order::AMOUNT_PAID)) { $order->creditHistory( CreditHistory::PAYMENT_REFUND, $order->getAmount(Order::AMOUNT_PAID), $order->distribution->id_producer, User::getCurrentId(), User::getCurrentId() ); } // delete $order->date_delete = date('Y-m-d H:i:s'); $order->save() ; Yii::$app->session->setFlash('success','Votre commande a bien été annulée.') ; } $this->redirect(Yii::$app->urlManager->createUrl(['order/history'])); } /** * Vérifie le code saisi pour un point de vente. * * @param integer $idPointSale * @param string $code * @return boolean */ public function actionvalidateCodePointSale($idPointSale, $code) { $pointSale = PointSale::findOne($idPointSale); if ($pointSale) { if ($pointSale->validateCode($code)) { return true; } } return false; } }