hasOne(User::className(), ['id' => 'id_user']); } public function getCommandeProduits() { return $this->hasMany(CommandeProduit::className(),['id_commande' => 'id']) ->with('produit'); } public function getProduction() { return $this->hasOne(Production::className(), ['id' => 'id_production']) ->with('etablissement'); } public function getPointVente() { return $this->hasOne(PointVente::className(), ['id' => 'id_point_vente']) ->with('pointVenteUser'); } public function getCreditHistorique() { return $this->hasMany(CreditHistorique::className(), ['id_commande' => 'id']); } /** * @inheritdoc */ public function rules() { return [ [['id_user', 'date', 'id_point_vente', 'id_production'], 'required', 'message' => ''], [['id_user', 'id_point_vente', 'id_production'], 'integer'], [['paiement_automatique'], 'boolean'], [['date', 'date_update', 'commentaire', 'commentaire_point_vente'], 'safe'] ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'id_user' => 'Id User', 'date' => 'Date', 'date_update' => 'Date Update', 'id_point_vente' => 'Point de vente', 'id_production' => 'Date de production', ]; } /** * Initialise le montant total, le montant déjà payé et le poids de la * commande */ public function init() { // Montant if (isset($this->commandeProduits)) { foreach ($this->commandeProduits as $p) { if ($p->mode_vente == 'unite') { $this->montant += $p->prix * $p->quantite; if(isset($p->produit)) { $this->poids += ($p->quantite * $p->produit->poids) / 1000 ; } } elseif ($p->mode_vente == 'poids') { $this->montant += $p->prix * $p->quantite / 1000; } } } // Montant payé if (isset($this->creditHistorique) && !$this->montant_paye) { foreach ($this->creditHistorique as $ch) { if ($ch->type == CreditHistorique::TYPE_PAIEMENT) { $this->montant_paye += $ch->montant; } elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT) { $this->montant_paye -= $ch->montant; } } } } /** * Retourne la quantité d'un produit donné de plusieurs commandes. * * @param integer $id_produit * @param array $commandes * * @return integer */ public static function getQuantiteProduit($id_produit, $commandes) { $quantite = 0; if (isset($commandes) && is_array($commandes) && count($commandes)) { foreach ($commandes as $c) { if(is_null($c->date_delete)) { foreach ($c->commandeProduits as $cp) { if ($cp->id_produit == $id_produit) { $quantite += $cp->quantite; } } } } } return $quantite; } /** * Retourne le montant payé de la commande. * * @return float */ public function getMontantPaye() { if ($this->montant_paye) { return $this->montant_paye; } else { $historique = CreditHistorique::find() ->where(['id_commande' => $this->id]) ->all(); $montant = 0; foreach ($historique as $ch) { if ($ch->type == CreditHistorique::TYPE_PAIEMENT) { $montant += $ch->montant; } elseif ($ch->type == CreditHistorique::TYPE_REMBOURSEMENT) { $montant -= $ch->montant; } } return $montant; } } /** * Retourne le montant de la commande. * * @param boolean $format * @return float */ public function getMontant($format = false) { if ($format) { return number_format($this->getMontant(), 2) . ' €'; } else { return $this->montant; } } /** * Retourne le montant restant à payer. * * @param boolean $format * @return float */ public function getMontantRestant($format = false) { $montant_restant = $this->getMontant() - $this->getMontantPaye(); if ($format) { return number_format($montant_restant, 2) . ' €'; } else { return $montant_restant; } } /** * Retourne le montant payé en surplus. * * @param boolean $format * @return float */ public function getMontantSurplus($format = false) { $montant_surplus = $this->getMontantPaye() - $this->getMontant(); if ($format) { return number_format($montant_surplus, 2) . ' €'; } else { return $montant_surplus; } } /** * Retourne les informations relatives à la commande au format JSON. * * @return string */ public function getDataJson() { $commande = Commande::find()->with('commandeProduits')->where(['id' => $this->id])->one(); $commande->init(); $json_commande = [ 'produits' => [], 'montant' => $commande->montant, 'str_montant' => $commande->getMontantFormat(), 'montant_paye' => $commande->getMontantPaye(), 'commentaire' => $commande->commentaire, ]; foreach ($commande->commandeProduits as $commande_produit) { $json_commande['produits'][$commande_produit->id_produit] = $commande_produit->quantite; } return json_encode($json_commande); } /** * Enregistre un modèle de type CreditHistorique. * * @param string $type * @param float $montant * @param integer $id_etablissement * @param integer $id_user * @param integer $id_user_action */ public function creditHistorique($type, $montant, $id_etablissement, $id_user, $id_user_action) { $credit_historique = new CreditHistorique; $credit_historique->id_user = $this->id_user; $credit_historique->id_commande = $this->id; $credit_historique->montant = $montant; $credit_historique->type = $type; $credit_historique->id_etablissement = $id_etablissement; $credit_historique->id_user_action = $id_user_action; $credit_historique->populateRelation('commande', $this) ; $credit_historique->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; $credit_historique->save(); } /** * Retourne le statut de paiement de la commande (payée, surplus, ou impayée). * * @return string */ public function getStatutPaiement() { // payé if ($this->getMontant() - $this->getMontantPaye() < 0.01 && $this->getMontant() - $this->getMontantPaye() >= 0) { return self::STATUT_PAYEE; } // à rembourser elseif ($this->getMontant() - $this->getMontantPaye() <= -0.01) { return self::STATUT_SURPLUS; } // reste à payer elseif ($this->getMontant() - $this->getMontantPaye() >= 0.01) { return self::STATUT_IMPAYEE; } } /** * Retourne le résumé du panier au format HTML. * * @return string */ public function getResumePanier() { if (!isset($this->commandeProduits)) { $this->commandeProduits = CommandeProduit::find()->where(['id_commande' => $this->id])->all(); } $html = ''; $count = count($this->commandeProduits); $i = 0; foreach ($this->commandeProduits as $p) { if (isset($p->produit)) { $html .= $p->quantite . ' x ' . Html::encode($p->produit->nom); if (++$i != $count) { $html .= '
'; } } } return $html; } /** * Retourne le résumé du point de vente lié à la commande au format HTML. * * @return string */ public function getResumePointVente() { $html = ''; if (isset($this->pointVente)) { $html .= '' . Html::encode($this->pointVente->nom) . '' . '
' . Html::encode($this->pointVente->localite) . ''; if (strlen($this->commentaire_point_vente)) { $html .= '
' . Html::encode($this->commentaire_point_vente) . '
'; } } else { $html .= 'Point de vente supprimé'; } return $html; } /** * Retourne le résumé du paiement (montant, statut). * * @return string */ public function getResumeMontant() { $html = ''; $html .= $this->getMontant(true) . '
'; if ($this->montant_paye) { if ($this->getStatutPaiement() == Commande::STATUT_PAYEE) { $html .= 'Payée'; } elseif ($this->getStatutPaiement() == Commande::STATUT_IMPAYEE) { $html .= 'Non payée
Reste ' . $this->getMontantRestant(true) . ' à payer'; } elseif ($this->getStatutPaiement() == Commande::STATUT_SURPLUS) { $html .= 'Payée'; } } else { $html .= 'À régler sur place'; } return $html; } /** * Retourne une chaine de caractère décrivant l'utilisateur lié à la commande. * * @return string */ public function getStrUser() { if (isset($this->user)) { return Html::encode($this->user->prenom . ' ' . $this->user->nom); } elseif (strlen($this->username)) { return Html::encode($this->username); } else { return 'Client introuvable'; } } /** * Recherche dans les commandes suivant les paramètres suivants : * - id_etablissement * - condition * - date * - type * - orderby * - limit * * @param array $params * @return array */ public static function findBy($params = []) { if (!isset($params['id_etablissement'])) $params['id_etablissement'] = Yii::$app->user->identity->id_etablissement; $commandes = Commande::find() ->with('commandeProduits', 'creditHistorique', 'pointVente') ->joinWith(['production', 'user','user.userEtablissement']) ->where(['production.id_etablissement' => $params['id_etablissement']]); if (isset($params['condition'])) $commandes = $commandes->andWhere($params['condition']); if (isset($params['date'])) $commandes = $commandes->andWhere(['production.date' => $params['date']]); if (isset($params['type'])) $commandes = $commandes->andWhere(['commande.type' => $params['type']]); if (isset($params['orderby'])) $commandes = $commandes->orderBy($params['orderby']); else $commandes = $commandes->orderBy('date ASC'); if (isset($params['limit'])) $commandes = $commandes->limit($params['limit']); $commandes = $commandes->all(); return $commandes; } /** * Retourne l'état de la commande (livrée, modifiable ou en préparation) * * @return string */ public function getEtat() { $delai_commande = Etablissement::getConfig('delai_commande', $this->production->id_etablissement); $heure_limite = Etablissement::getConfig('heure_limite_commande', $this->production->id_etablissement); $date_commande = strtotime($this->production->date); $date_today = strtotime(date('Y-m-d')); $heure_today = date('G'); $nb_jours = (int) (($date_commande - $date_today) / (24 * 60 * 60)); if ($nb_jours <= 0) { return self::ETAT_LIVREE; } elseif ($nb_jours >= $delai_commande && ($nb_jours != $delai_commande || ($nb_jours == $delai_commande && $heure_today < $heure_limite))) { return self::ETAT_MODIFIABLE; } return self::ETAT_PREPARATION; } /** * Retourne le type de la commande (client, automatique ou admin) sous forme * texte ou HTML. * * @param boolean $with_label * @return string */ public function getStrType($with_label = false) { $class_label = ''; $str = ''; if ($this->type == self::TYPE_USER) { $class_label = 'success'; $str = 'Client'; } elseif ($this->type == self::TYPE_AUTO) { $class_label = 'default'; $str = 'Auto'; } elseif ($this->type == self::TYPE_ADMIN) { $class_label = 'warning'; $str = 'Vous'; } if ($with_label) return '' . $str . ''; else return $str; } /** * Retourne l'historique de la commande (ajoutée, modifiée, supprimée) au format * HTML. * * @return string */ public function getStrHistorique() { $arr = [ 'class' => 'create', 'glyphicon' => 'plus', 'str' => 'Ajoutée', 'date' => $this->date ] ; if(!is_null($this->date_update)) { $arr = [ 'class' => 'update', 'glyphicon' => 'pencil', 'str' => 'Modifiée', 'date' => $this->date_update ] ; } if(!is_null($this->date_delete)) { $arr = [ 'class' => 'delete', 'glyphicon' => 'remove', 'str' => 'Annulée', 'date' => $this->date_delete ] ; } $html = '
'.$arr['str'].' le '.date('d/m/Y à G\hi', strtotime($arr['date'])).'
' ; return $html ; } /** * Retourne une classe identifiant l'historique de la commande (ajoutée, * modifiée, supprimée). * * @return string */ public function getClassHistorique() { if(!is_null($this->date_delete)) { return 'commande-delete' ; } if(!is_null($this->date_update)) { return 'commande-update' ; } return 'commande-create' ; } }