[ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], ] ], ], ]; } public function actionInfosProduction($id_production) { $production = Production::findOne($id_production); if ($production) { $arr = []; $produits_dispos = ProductionProduit::findProduits($production->id); $arr['produits_dispos'] = $produits_dispos; $points_vente = PointVente::find() ->joinWith(['productionPointVente' => function($q) use ($production) { $q->where(['id_production' => $production->id]); }]) ->where([ 'id_etablissement' => $production->id_etablissement, ]) ->all(); $arr['points_vente'] = []; foreach ($points_vente as $pv) { if (isset($pv->productionPointVente) && isset($pv->productionPointVente[0])) { $arr['points_vente'][$pv->id] = $pv->productionPointVente[0]->livraison; } else { $arr['points_vente'][$pv->id] = false; } } return json_encode($arr); } return json_encode([]); } public function initForm($commande = null) { // etablissements $etablissements = Yii::$app->user->identity->getEtablissementsFavoris(); $id_etablissement = $this->getProducer()->id; $etablissement_paiement_ok = false; if ($id_etablissement) { $etablissement = Etablissement::findOne($id_etablissement); if ($etablissement->etatPaiement() == Etablissement::PAIEMENT_OK || $etablissement->etatPaiement() == Etablissement::PAIEMENT_ESSAI) { $etablissement_paiement_ok = true; } } // etablissement $etablissement = Etablissement::findOne($id_etablissement); // points de vente $points_vente = PointVente::find() ->with('pointVenteUser') ->where(['id_etablissement' => $id_etablissement]) ->andWhere('acces_restreint = 0 OR (acces_restreint = 1 AND (SELECT COUNT(*) FROM point_vente_user WHERE point_vente.id = point_vente_user.id_point_vente AND point_vente_user.id_user = :id_user) > 0)') ->params([':id_user' => Yii::$app->user->identity->id]) ->all(); $arr_points_vente = $points_vente; // jours de production $heure_limite = 20; $date = date('Y-m-d'); if (isset($etablissement)) { $heure_limite = $etablissement->heure_limite_commande; if (date('H') >= $heure_limite) { $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($etablissement->delai_commande) * (24 * 60 * 60)); } else { $date = date('Y-m-d', strtotime(date('Y-m-d')) + ($etablissement->delai_commande - 1) * (24 * 60 * 60)); } } $jours_production = Production::find() ->where(['actif' => 1]) ->andWhere('date > :date') ->andWhere(['id_etablissement' => $id_etablissement]) ->addParams([':date' => $date]) ->all(); $arr_jours_production = array('' => '--'); foreach ($jours_production as $j) $arr_jours_production[$j->id] = date('d/m/Y', strtotime($j->date)); // produits $produits = Produit::find() ->leftJoin('production_produit', 'produit.id = production_produit.id_produit') ->where(['produit.actif' => 1, 'id_etablissement' => $id_etablissement]) ->andWhere('produit.vrac IS NULL OR produit.vrac = 0') ->orderBy('produit.order ASC')->all(); $arr_produits = array(); foreach ($produits as $p) $arr_produits[] = $p; // produits vrac $produits_vrac = Produit::find()->where(['actif' => 1, 'vrac' => 1])->orderBy('order ASC')->all(); // produits selec $posts = Yii::$app->request->post(); $produits_selec = []; if (isset($posts['Produit'])) { foreach ($posts['Produit'] as $key => $quantity) { $key = (int) str_replace('produit_', '', $key); $p = Produit::find()->where(['id' => $key])->one(); if ($p && $quantity) $produits_selec[$p->id] = (int) $quantity; } } elseif (!is_null($commande)) { $produits_commande = CommandeProduit::find()->where(['id_commande' => $commande->id])->all(); foreach ($produits_commande as $pc) { $produits_selec[$pc->id_produit] = (int) $pc->quantite; } } $produits_dispos = []; $production = null; if (!is_null($commande) && $commande->id_production) { $produits_dispos = ProductionProduit::findProduits($commande->id_production); $production = Production::find()->where(['id' => $commande->id_production])->one(); } $commandes = Commande::find() ->where(['id_user' => Yii::$app->user->identity->id]) ->all(); if ($id_etablissement) { $user_etablissement = UserEtablissement::find() ->where([ 'id_etablissement' => $id_etablissement, 'id_user' => Yii::$app->user->identity->id ]) ->one(); $credit = $user_etablissement->credit; } else { $credit = 0; } return [ 'points_vente' => $arr_points_vente, 'jours_production' => $arr_jours_production, 'produits' => $produits, 'produits_selec' => $produits_selec, 'produits_dispos' => $produits_dispos, 'production' => $production, 'commandes_en_cours' => $commandes, 'produits_vrac' => $produits_vrac, 'etablissements' => $etablissements, 'id_etablissement' => $id_etablissement, 'etablissement' => $etablissement, 'etablissement_paiement_ok' => $etablissement_paiement_ok, 'credit' => $credit ]; } /** * Affiche l'historique des commandes de l'utilisateur * * @return ProducerView */ public function actionHistorique() { $data_provider_commandes = new ActiveDataProvider([ 'query' => Commande::find() ->with('commandeProduits', 'pointVente', 'creditHistorique') ->joinWith('production', 'production.etablissement') ->where([ 'id_user' => Yii::$app->user->id, 'production.id_etablissement' => $this->getProducer()->id ]) ->andWhere('date_delete IS NULL') ->orderBy('production.date DESC'), 'pagination' => [ 'pageSize' => 10, ], ]); return $this->render('historique', [ 'data_provider_commandes' => $data_provider_commandes, 'commande_ok' => Yii::$app->getRequest()->get('commande_ok', false), 'annule_ok' => Yii::$app->getRequest()->get('annule_ok', false), ]); } public function actionRemoveEtablissement($id = 0) { $user_etablissement = UserEtablissement::find() ->where(['id_etablissement' => $id, 'id_user' => Yii::$app->user->identity->id]) ->one(); $user_etablissement->actif = 0; $user_etablissement->save(); $this->redirect(['commande/index']); } public function actionCreate() { $id_etablissement = $this->getProducer()->id ; $commande = new Commande; $posts = Yii::$app->request->post(); if ($id_etablissement) $this->_verifEtablissementActif($id_etablissement); if ($commande->load($posts)) { $commande = Commande::find()->where('id_production = ' . $posts['Commande']['id_production'])->andWhere('id_user = ' . Yii::$app->user->id)->one(); if (!$commande) { $commande = new Commande; $commande->load(Yii::$app->request->post()); $commande->id_user = Yii::$app->user->id; $commande->date = date('Y-m-d H:i:s'); $commande->type = Commande::TYPE_USER; } $this->gestionForm($commande); } return $this->render('create', array_merge($this->initForm($commande), [ 'model' => $commande ])); } public function actionUpdate($id) { $commande = Commande::find() ->with('production') ->where(['id' => $id]) ->one(); if ($commande->getEtat() != Commande::ETAT_MODIFIABLE) { throw new UserException('Cette commande n\'est pas modifiable.'); } $this->_verifEtablissementActif($commande->production->id_etablissement); if ($commande && $commande->load(Yii::$app->request->post())) { $commande->date_update = date('Y-m-d H:i:s'); $this->gestionForm($commande); } return $this->render('update', array_merge($this->initForm($commande), [ 'model' => $commande, 'commande_introuvable' => !$commande, ])); } public function _verifEtablissementActif($id_etablissement) { $etablissement = Etablissement::findOne($id_etablissement); if ($etablissement && !$etablissement->actif) { throw new NotFoundHttpException('Cet établissement est actuellement hors ligne.'); } } public function gestionForm($commande) { $posts = Yii::$app->request->post(); $produits = array(); $quantite_totale = 0; foreach ($posts['Produit'] as $key => $quantity) { $key = (int) str_replace('produit_', '', $key); $p = Produit::find()->where(['id' => $key])->one(); $quantite_totale += $quantity; if ($p && $quantity) $produits[] = $p; } // nombre de produits $err_nb_produits = false; if (!Yii::$app->user->identity->confiance && $quantite_totale > 3) { $err_nb_produits = true; } // date $err_date = false; $pate_deja_petrie = false; if (isset($commande->id_production)) { // date de commande $production = Production::find()->where(['id' => $commande->id_production])->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 ($production->date < $date) { $err_date = true; } } // point de vente $err_point_vente = false; if (isset($production) && $production) { $ppv = ProductionPointVente::find() ->where([ 'id_production' => $production->id, 'id_point_vente' => $posts['Commande']['id_point_vente'] ]) ->one(); if (!$ppv || !$ppv->livraison) { $err_point_vente = true; } $point_vente = PointVente::findOne($posts['Commande']['id_point_vente']); if ($point_vente) { if (strlen($point_vente->code) && !$point_vente->verifCode($posts['code_point_vente_' . $point_vente->id])) { $err_point_vente = true; } } else { $err_point_vente = true; } } if ($commande->validate() && count($produits) && !$err_nb_produits && !$err_date && !$err_point_vente) { // gestion point de vente $pv = PointVente::find() ->with('pointVenteUser') ->where(['id' => $commande->id_point_vente]) ->one(); if ($pv && strlen($pv->getCommentaire())) $commande->commentaire_point_vente = $pv->getCommentaire(); else $commande->commentaire_point_vente = ''; // sauvegarde de la commande $commande->save(); // ajout de l'utilisateur à l'établissement Etablissement::addUser(Yii::$app->user->identity->id, $production->id_etablissement) ; // suppression de tous les enregistrements CommandeProduit if (!is_null($commande)) { CommandeProduit::deleteAll(['id_commande' => $commande->id]); } // produits dispos $produits_dispos = ProductionProduit::findProduits($production->id); // sauvegarde des produits foreach ($produits as $p) { // le produit doit etre dispo à la vente if (isset($produits_dispos[$p->id]) && $produits_dispos[$p->id]['actif']) { $commande_produit = new CommandeProduit(); $commande_produit->id_commande = $commande->id; $commande_produit->id_produit = $p->id; $commande_produit->prix = $p->prix; $quantite_voulue = (int) $posts['Produit']['produit_' . $p->id]; if ($produits_dispos[$p->id]['quantite_max'] && $quantite_voulue > $produits_dispos[$p->id]['quantite_restante']) $quantite_voulue = $produits_dispos[$p->id]['quantite_restante']; $commande_produit->quantite = $quantite_voulue; $commande_produit->save(); } } // credit pain $credit_pain = isset($posts['credit_pain']) && $posts['credit_pain']; if ($credit_pain && ($pv->credit_pain || $commande->getMontantPaye())) { $commande = Commande::find() ->with('commandeProduits') ->where(['id' => $commande->id]) ->one(); $commande->init(); $montant_paye = $commande->getMontantPaye(); // à payer if ($commande->getStatutPaiement() == Commande::STATUT_IMPAYEE) { $montant_payer = $commande->montant - $montant_paye; $credit = Yii::$app->user->identity->getCredit($production->id_etablissement); if ($montant_payer > $credit) { $montant_payer = $credit; } if ($montant_payer > 0) { $commande->creditHistorique( CreditHistorique::TYPE_PAIEMENT, $montant_payer, $production->id_etablissement, Yii::$app->user->identity->id, Yii::$app->user->identity->id ); } } // surplus à rembourser elseif ($commande->getStatutPaiement() == Commande::STATUT_SURPLUS) { $montant_rembourser = $montant_paye - $commande->montant; $commande->creditHistorique( CreditHistorique::TYPE_REMBOURSEMENT, $montant_rembourser, $production->id_etablissement, Yii::$app->user->identity->id, Yii::$app->user->identity->id ); } } // redirection $this->redirect(Yii::$app->urlManager->createUrl(['commande/historique', 'commande_ok' => true, 'pate_deja_petrie' => $pate_deja_petrie])); } else { if (!count($produits)) Yii::$app->session->setFlash('error', "Vous n'avez choisi aucun produit"); if ($err_nb_produits) Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander plus de 3 produits"); if ($err_date) Yii::$app->session->setFlash('error', "Vous ne pouvez pas commander pour cette date."); if ($err_point_vente) Yii::$app->session->setFlash('error', "Point de vente invalide."); } } public function actionAnnuler($id) { $commande = Commande::find() ->with('production', 'creditHistorique', 'commandeProduits') ->where(['id' => $id]) ->one(); if(!$commande) throw new \yii\web\NotFoundHttpException('Commande introuvable'); if ($commande->getEtat() != Commande::ETAT_MODIFIABLE) { throw new UserException('Vous ne pouvez plus annuler cette commande.'); } $commande->init(); if ($commande && Yii::$app->user->id == $commande->id_user) { // remboursement if ($commande->getMontantPaye()) { $commande->creditHistorique( CreditHistorique::TYPE_REMBOURSEMENT, $commande->getMontantPaye(), $commande->production->id_etablissement, Yii::$app->user->identity->id, Yii::$app->user->identity->id ); } // delete $commande->date_delete = date('Y-m-d H:i:s'); $commande->save() ; Yii::$app->session->setFlash('success','Votre commande a bien été annulée.') ; } $this->redirect(Yii::$app->urlManager->createUrl(['commande/historique'])); } public function actionVerifCodePointVente($id_point_vente, $code) { $point_vente = PointVente::findOne($id_point_vente); if ($point_vente) { if ($point_vente->verifCode($code)) { return true; } } return false; } }