Browse Source

Refactoring/traduction modèle CreditHistorique > CreditHistory

dev
Guillaume Bourgeois 6 years ago
parent
commit
ca80851ee4
1 changed files with 74 additions and 56 deletions
  1. +74
    -56
      common/models/CreditHistory.php

+ 74
- 56
common/models/CreditHistory.php View File

use Yii; use Yii;
use yii\db\ActiveRecord; use yii\db\ActiveRecord;
use common\models\User; use common\models\User;
use common\models\Commande;
use common\models\Order;
use yii\helpers\Html; use yii\helpers\Html;


/** /**
* *
* @property integer $id * @property integer $id
* @property integer $id_user * @property integer $id_user
* @property integer $id_commande
* @property integer $id_order
* @property string $date * @property string $date
* @property double $montant
* @property double $amount
* @property string $type * @property string $type
* @property integer $id_etablissement
* @property string $moyen_paiement
* @property integer $id_producer
* @property string $means_payement
*/ */
class CreditHistory extends ActiveRecord class CreditHistory extends ActiveRecord
{ {


const TYPE_INITIAL_CREDIT = 'initial-credit';
const TYPE_CREDIT = 'credit';
const TYPE_PAYMENT = 'payment';
const TYPE_REFUND= 'refund';
const TYPE_DEBIT = 'debit';
const TYPE_INITIAL_CREDIT = 'initial-credit';
const TYPE_CREDIT = 'credit';
const TYPE_PAYMENT = 'payment';
const TYPE_REFUND = 'refund';
const TYPE_DEBIT = 'debit';
const PAYMENT_CREDIT_CARD = 'credit-card';
const PAYMENT_MONEY = 'money';
const PAYMENT_CHEQUE = 'cheque';
const PAYMENT_OTHER = 'other';
const MEANS_PAYMENT_CREDIT_CARD = 'credit-card';
const MEANS_PAYMENT_MONEY = 'money';
const MEANS_PAYMENT_CHEQUE = 'cheque';
const MEANS_PAYMENT_OTHER = 'other';
/** /**
* @inheritdoc * @inheritdoc
{ {
return [ return [
[['montant'], 'required'], [['montant'], 'required'],
[['id_user', 'id_user_action', 'id_commande', 'id_etablissement'], 'integer'],
[['id_user', 'id_user_action', 'id_order', 'id_producer'], 'integer'],
[['date'], 'safe'], [['date'], 'safe'],
[['montant'], 'double'],
[['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255],
[['amount'], 'double'],
[['type', 'means_payment', 'comment'], 'string', 'max' => 255],
]; ];
} }


'id' => 'ID', 'id' => 'ID',
'id_user' => 'Utilisateur', 'id_user' => 'Utilisateur',
'id_user_action' => 'Utilisateur', 'id_user_action' => 'Utilisateur',
'id_commande' => 'Commande',
'id_order' => 'Commande',
'date' => 'Date', 'date' => 'Date',
'montant' => 'Montant',
'amount' => 'Montant',
'type' => 'Type', 'type' => 'Type',
'id_etablissement' => 'Établissement',
'moyen_paiement' => 'Moyen de paiement',
'commentaire' => 'Commentaire',
'id_producer' => 'Producteur',
'means_payment' => 'Moyen de paiement',
'comment' => 'Commentaire',
]; ];
} }


/* /*
* Relations * Relations
return $this->hasOne(User::className(), ['id' => 'id_user_action']); return $this->hasOne(User::className(), ['id' => 'id_user_action']);
} }


public function getCommande()
public function getOrder()
{ {
return $this->hasOne(Commande::className(), ['id' => 'id_commande']);
return $this->hasOne(Order::className(), ['id' => 'id_order']);
}
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch() {
return [
'class' => 'CreditHistory',
'with' => [],
'join_with' => [],
'orderby' => self::tableName().'.date ASc',
'attribute_id_producer' => self::tableName().'.id_producer'
] ;
} }


/** /**
* *
* @return string * @return string
*/ */
public function getLibelleType()
public function getStrType()
{ {
switch ($this->type) { switch ($this->type) {
case 'paiement':
case self::TYPE_PAYMENT:
return 'Paiement'; return 'Paiement';
break; break;
case 'remboursement':
case self::TYPE_REFUND:
return 'Remboursement'; return 'Remboursement';
break; break;
case 'debit':
case self::TYPE_DEBIT:
return 'Débit'; return 'Débit';
break; break;
} }
public function save($runValidation = true, $attributeNames = NULL) public function save($runValidation = true, $attributeNames = NULL)
{ {
// initialisation du commentaire avant sauvegarde // initialisation du commentaire avant sauvegarde
$this->commentaire .= $this->getStrCommentaire() ;
$this->comment .= $this->getStrComment() ;
parent::save($runValidation, $attributeNames); parent::save($runValidation, $attributeNames);


// mise à jour du crédit au niveau de UserEtablissement // 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) {
$userProducer = UserProducer::searchOne([
'id_user' => $this->id_user,
'id_producer' => $this->id_producer
]) ;
if ($userProducer) {
if ($this->isTypeCredit()) { if ($this->isTypeCredit()) {
$user_etablissement->credit += $this->montant;
$userProducer->credit += $this->amount;
} elseif ($this->isTypeDebit()) { } elseif ($this->isTypeDebit()) {
$user_etablissement->credit -= $this->montant;
$userProducer->credit -= $this->amount;
} }
$user_etablissement->save();
$userProducer->save();
} }
} }
* @param boolean $format * @param boolean $format
* @return float * @return float
*/ */
public function getMontant($format = false)
public function getAmount($format = false)
{ {
if($format) if($format)
return number_format($this->montant,2) .' €' ;
return number_format($this->amount,2) .' €' ;
else else
return $this->montant ;
return $this->amount ;
} }
/** /**
* Retourne le libellé du CreditHistorique informant de son type et
* Retourne le libellé du CreditHistory informant de son type et
* éventuellement de la date de sa commande associée. * éventuellement de la date de sa commande associée.
* *
* @return string * @return string
*/ */
public function getStrLibelle()
public function getStrWording()
{ {
$str = '' ; $str = '' ;
* *
* @return string * @return string
*/ */
public function getStrCommentaire()
public function getStrComment()
{ {
$str = '' ; $str = '' ;
if(strlen($this->commentaire)) {
if(strlen($this->comment)) {
$str .= '<br />' ; $str .= '<br />' ;
} }
$str .= $this->getStrLibelle() ;
if(isset($this->commande)) {
$str .= '<br />Montant de la commande : '.$this->commande->getMontant(true) ;
$str .= $this->getStrWording() ;
if(isset($this->order)) {
$str .= '<br />Montant de la commande : '.$this->order->getAmount(Order::AMOUNT_TOTAL, true) ;
} }
if(isset($this->user)) { if(isset($this->user)) {
$str .= '<br />Client : '.Html::encode($this->user->nom. ' '.$this->user->prenom) ;
$str .= '<br />Client : '.Html::encode($this->user->name. ' '.$this->user->lastname) ;
} }
if(isset($this->userAction)) { if(isset($this->userAction)) {
$str .= '<br />Action : '.Html::encode($this->userAction->nom. ' '.$this->userAction->prenom) ;
$str .= '<br />Action : '.Html::encode($this->userAction->name. ' '.$this->userAction->lastname) ;
} }
return $str ; return $str ;
* *
* @return string * @return string
*/ */
public function getStrMoyenPaiement()
public function getStrMeansPayment()
{ {
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' ;
switch($this->means_payment) {
case CreditHistory::MEANS_PAYMENT_MONEY : return 'Espèces' ;
case CreditHistory::MEANS_PAYMENT_CHEQUE : return 'Chèque' ;
case CreditHistory::MEANS_PAYMENT_CREDIT_CARD : return 'Carte bancaire' ;
case CreditHistory::MEANS_PAYMENT_OTHER : return 'Autre' ;
default: return 'Crédit pain' ; default: return 'Crédit pain' ;
} }
} }
public function strUserAction() public function strUserAction()
{ {
if($this->userAction) { if($this->userAction) {
return $this->userAction->nom . ' ' . $this->userAction->prenom ;
return $this->userAction->name . ' ' . $this->userAction->lastname ;
} }
else { else {
return 'Système' ; return 'Système' ;

Loading…
Cancel
Save