[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ] ], ], ]; } public function actionOrder($id = 0) { $params = [] ; if($id) { $order = Order::searchOne([ 'id' => $id ]) ; if($order) { if($order->getState() == Order::STATE_OPEN) { $params['order'] = $order ; } } } return $this->render('order', $params) ; } /** * Affiche l'historique des commandes de l'utilisateur * * @return ProducerView */ public function actionHistory($type = 'incoming') { $query = Order::find() ->with('productOrder', 'pointSale', 'creditHistory') ->joinWith('distribution', 'distribution.producer') ->where([ 'id_user' => Yii::$app->user->id, 'distribution.id_producer' => Producer::getId() ]) ->params([':date_today' => date('Y-m-d')]); $queryIncoming = clone $query ; $queryIncoming->andWhere('distribution.date >= :date_today')->orderBy('distribution.date ASC'); $queryPassed = clone $query ; $queryPassed->andWhere('distribution.date < :date_today')->orderBy('distribution.date DESC'); $dataProviderOrders = new ActiveDataProvider([ 'query' => ($type == 'incoming') ? $queryIncoming : $queryPassed, 'pagination' => [ 'pageSize' => 10, ], ]); return $this->render('history', [ 'dataProviderOrders' => $dataProviderOrders, 'orderOk' => Yii::$app->getRequest()->get('orderOk', false), 'cancelOk' => Yii::$app->getRequest()->get('cancelOk', false), 'type' => $type, 'countIncoming' => $queryIncoming->count(), 'countPassed' => $queryPassed->count(), ]); } /** * 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 actionAjaxProcess() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $order = new Order ; $idProducer = $this->getProducer()->id ; $posts = Yii::$app->request->post(); if ($idProducer) { $this->_verifyProducerActive($idProducer); } if ($order->load($posts)) { $order = Order::find() ->where('id_distribution = :id_distribution') ->andWhere('id_user = :id_user') ->params([ ':id_distribution' => $posts['Order']['id_distribution'], ':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->origin = Order::ORIGIN_USER; } $errors = $this->processForm($order); if(count($errors)) { return ['status' => 'error', 'errors' => $errors] ; } } return ['status' => 'success', 'idOrder' => $order->id] ; } /** * 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['products'] as $key => $quantity) { $product = Product::find()->where(['id' => (int) $key])->one(); $totalQuantity += $quantity; if ($product && $quantity) { $productsArray[] = $product; } } $producer = $this->getProducer() ; // date $errorDate = false; if (isset($order->id_distribution)) { // date de commande $distribution = Distribution::find()->where(['id' => $order->id_distribution])->one(); $date = $this->getProducer()->getEarliestDateOrder() ; if($order->getState() != Order::STATE_OPEN) { $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['Order']['id_point_sale']); if ($pointSale) { if (strlen($pointSale->code) && !$pointSale->validateCode($posts['code_point_sale'])) { $errorPointSale = true; } } else { $errorPointSale = true; } } $errors = [] ; if ($order->validate() && count($productsArray) && !$errorDate && !$errorPointSale) { // gestion point de vente $pointSale = PointSale::searchOne([ 'id' => $order->id_point_sale ]) ; $order->comment_point_sale = ($pointSale && strlen($pointSale->getComment())) ? $pointSale->getComment() : '' ; // la commande est automatiquement réactivée lors d'une modification $order->date_delete = null ; // 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['products'][$product->id] ; if ($availableProducts[$product->id]['quantity_max'] && $quantity > $availableProducts[$product->id]['quantity_remaining']) { $quantity = $availableProducts[$product->id]['quantity_remaining']; } $productOrder->quantity = $quantity; $productOrder->sale_mode = Product::SALE_MODE_UNIT ; $productOrder->save(); } } // credit $credit = Producer::getConfig('credit'); $order = Order::searchOne([ 'id' => $order->id ]) ; if ($credit && ($pointSale->credit || $order->getAmount(Order::AMOUNT_PAID))) { $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->saveCreditHistory( 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->saveCreditHistory( CreditHistory::TYPE_REFUND, $amountSurplus, $distribution->id_producer, User::getCurrentId(), User::getCurrentId() ); } } } else { if (!count($productsArray)) { $errors[] = "Vous n'avez choisi aucun produit" ; } if ($errorDate) { $errors[] = "Vous ne pouvez pas commander pour cette date." ; } if ($errorPointSale) { $errors[] = "Point de vente invalide." ; } } return $errors ; } /** * 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->saveCreditHistory( CreditHistory::TYPE_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 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; } public function actionAjaxInfos($date = '') { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $json = [] ; $format = 'Y-m-d' ; $dateObject = DateTime::createFromFormat($format, $date); // Producteur $producer = Producer::searchOne([ 'id' => $this->getProducer()->id ]) ; $json['producer'] = [ 'order_infos' => $producer->order_infos, 'credit' => $producer->credit ] ; // Distributions $dateMini = $producer->getEarliestDateOrder() ; $distributionsArray = Distribution::searchAll([ 'active' => 1 ], [ 'conditions' => ['date > :date'], 'params' => [':date' => $dateMini], ]) ; $json['distributions'] = $distributionsArray ; // Commandes de l'utilisateur $ordersUserArray = Order::searchAll([ 'id_user' => User::getCurrentId() ], [ 'conditions' => [ 'distribution.date > :date' ], 'params' => [ ':date' => $dateMini ] ]); if(is_array($ordersUserArray) && count($ordersUserArray)) { foreach($ordersUserArray as &$order) { $order = array_merge($order->getAttributes(), [ 'amount_total' => $order->getAmount(Order::AMOUNT_TOTAL), 'date_distribution' => $order->distribution->date, 'pointSale' => $order->pointSale->getAttributes() ]) ; } $json['orders'] = $ordersUserArray; } // User $userProducer = UserProducer::searchOne([ 'id_producer' => $producer->id, 'id_user' => User::getCurrentId() ]) ; $json['credit'] = $userProducer->credit ; if($dateObject && $dateObject->format($format) === $date) { // Commande de l'utilisateur $orderUser = Order::searchOne([ 'distribution.date' => $date, 'id_user' => User::getCurrentId(), ]); if($orderUser) { $json['order'] = array_merge($orderUser->getAttributes(), [ 'amount_total' => $orderUser->getAmount(Order::AMOUNT_TOTAL), 'amount_paid' => $orderUser->getAmount(Order::AMOUNT_PAID), ]) ; } // distribution $distribution = Distribution::initDistribution($date) ; $json['distribution'] = $distribution ; $pointsSaleArray = PointSale::find() ->joinWith(['pointSaleDistribution' => function($query) use ($distribution) { $query->where(['id_distribution' => $distribution->id]); } ]) ->with(['userPointSale' => function($query) { $query->onCondition(['id_user' => User::getCurrentId()]) ; }]) ->where([ 'id_producer' => $distribution->id_producer, ]) ->all(); foreach($pointsSaleArray as &$pointSale) { $pointSale = array_merge($pointSale->getAttributes(),[ 'pointSaleDistribution' => [ 'id_distribution' => $pointSale->pointSaleDistribution[0]->id_distribution, 'id_point_sale' => $pointSale->pointSaleDistribution[0]->id_point_sale, 'delivery' => $pointSale->pointSaleDistribution[0]->delivery ], 'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '') ]) ; if($pointSale['code'] && strlen($pointSale['code'])) { $pointSale['code'] = '***' ; } } $json['points_sale'] = $pointsSaleArray; // Commandes totales $ordersArray = Order::searchAll([ 'distribution.date' => $date, ]); // Produits $productsArray = Product::find() ->where([ 'id_producer' => $this->getProducer()->id, ]) ->joinWith(['productDistribution' => function($query) use($distribution) { $query->andOnCondition('product_distribution.id_distribution = '.$distribution->id) ; }]) ->orderBy('product_distribution.active DESC, order ASC') ->asArray() ->all(); $indexProduct = 0 ; foreach($productsArray as &$product) { $quantityOrder = Order::getProductQuantity($product['id'], $ordersArray) ; $product['quantity_ordered'] = $quantityOrder ; $product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder ; if($orderUser) { $quantityOrderUser = Order::getProductQuantity($product['id'], [$orderUser], true) ; $product['quantity_ordered'] = $quantityOrder ; $product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder + $quantityOrderUser ; $product['quantity_form'] = $quantityOrderUser ; } else { $product['quantity_form'] = 0 ; } if($product['quantity_remaining'] < 0) $product['quantity_remaining'] = 0 ; $product['index'] = $indexProduct ++ ; } $json['products'] = $productsArray; } return $json ; } public function actionConfirm($idOrder) { $order = Order::searchOne(['id' => $idOrder]) ; if(!$order || $order->id_user != User::getCurrentId()) { throw new \yii\base\UserException('Commande introuvable.') ; } return $this->render('confirm',[ 'order' => $order ]) ; } }