255], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id' => 'ID', 'id_user' => 'Utilisateur', 'id_user_action' => 'Utilisateur', 'id_commande' => 'Commande', 'date' => 'Date', 'montant' => 'Montant', 'type' => 'Type', 'id_etablissement' => 'Établissement', 'moyen_paiement' => 'Moyen de paiement', 'commentaire' => 'Commentaire', ]; } public function getUser() { return $this->hasOne(User::className(), ['id' => 'id_user']); } public function getUserAction() { return $this->hasOne(User::className(), ['id' => 'id_user_action']); } public function getCommande() { return $this->hasOne(Commande::className(), ['id' => 'id_commande']); } public function getLibelleType() { switch ($this->type) { case 'paiement': return 'Paiement'; break; case 'remboursement': return 'Remboursement'; break; case 'debit': return 'Débit'; break; } } public function save($runValidation = true, $attributeNames = NULL) { // initialisation du commentaire avant sauvegarde $this->commentaire .= $this->getStrCommentaire() ; parent::save($runValidation, $attributeNames); // mise à jour du crédit au niveau de UserEtablissement $user_etablissement = UserEtablissement::findOne([ 'id_user' => $this->id_user, 'id_etablissement' => $this->id_etablissement ]); if ($user_etablissement) { if ($this->isTypeCredit()) { $user_etablissement->credit += $this->montant; } elseif ($this->isTypeDebit()) { $user_etablissement->credit -= $this->montant; } $user_etablissement->save(); } } public function isTypeDebit() { return in_array($this->type, [ self::TYPE_DEBIT, self::TYPE_PAIEMENT, ]) ; } public function isTypeCredit() { return in_array($this->type, [ self::TYPE_CREDIT, self::TYPE_CREDIT_INITIAL, self::TYPE_REMBOURSEMENT ]) ; } public function getMontant($format = false) { if($format) return number_format($this->montant,2) .' €' ; else return $this->montant ; } public function getStrLibelle() { $str = '' ; if($this->type == self::TYPE_CREDIT_INITIAL) { $str = 'Crédit initial' ; } elseif($this->type == self::TYPE_CREDIT) { $str = 'Crédit' ; } elseif($this->type == self::TYPE_PAIEMENT) { $str = 'Paiement' ; } elseif($this->type == self::TYPE_REMBOURSEMENT) { $str = 'Remboursement' ; } elseif($this->type == self::TYPE_DEBIT) { $str = 'Débit' ; } if($this->type == self::TYPE_PAIEMENT || $this->type == self::TYPE_REMBOURSEMENT) { if(isset($this->commande)) { $str .= '
Commande : '.date('d/m/Y',strtotime($this->commande->production->date)) ; } else { $str .= '
Commande supprimée' ; } } return $str ; } public function getStrCommentaire() { $str = '' ; if(strlen($this->commentaire)) { $str .= '
' ; } $str .= $this->getStrLibelle() ; if(isset($this->commande)) { $str .= '
Montant de la commande : '.$this->commande->getMontant(true) ; } if(isset($this->user)) { $str .= '
Client : '.Html::encode($this->user->nom. ' '.$this->user->prenom) ; } if(isset($this->userAction)) { $str .= '
Action : '.Html::encode($this->userAction->nom. ' '.$this->userAction->prenom) ; } return $str ; } public function getDate($format = false) { if($format) return date('d/m/Y à H:i:s',strtotime($this->date)) ; else return $this->date ; } public function getStrMoyenPaiement() { switch($this->moyen_paiement) { case CreditHistorique::MOYEN_ESPECES : return 'Espèces' ; case CreditHistorique::MOYEN_CHEQUE : return 'Chèque' ; case CreditHistorique::MOYEN_CB : return 'Carte bancaire' ; case CreditHistorique::MOYEN_AUTRE : return 'Autre' ; default: return 'Crédit pain' ; } } public function strUserAction() { if($this->userAction) { return $this->userAction->nom . ' ' . $this->userAction->prenom ; } else { return 'Système' ; } } }