- <?php
-
- namespace common\models;
-
- use Yii;
- use yii\db\ActiveRecord ;
- use common\models\User ;
- use common\models\Commande ;
-
- /**
- * This is the model class for table "credit_historique".
- *
- * @property integer $id
- * @property integer $id_user
- * @property integer $id_commande
- * @property string $date
- * @property double $montant
- * @property string $type
- * @property integer $id_etablissement
- * @property string $moyen_paiement
- */
- class CreditHistorique extends ActiveRecord
- {
- const TYPE_CREDIT_INITIAL = 'credit-initial' ;
- const TYPE_CREDIT = 'credit' ;
- const TYPE_PAIEMENT = 'paiement' ;
- const TYPE_REMBOURSEMENT = 'remboursement' ;
- const TYPE_DEBIT = 'debit' ;
-
- const MOYEN_CB = 'cb' ;
- const MOYEN_ESPECES = 'especes' ;
- const MOYEN_CHEQUE = 'cheque' ;
- const MOYEN_AUTRE = 'autre' ;
-
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'credit_historique';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['montant'], 'required'],
- [['id_user','id_user_action', 'id_commande', 'id_etablissement'], 'integer'],
- [['date'], 'safe'],
- [['montant'], 'double'],
- [['type', 'moyen_paiement','commentaire'], 'string', 'max' => 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) {
- parent::save($runValidation, $attributeNames) ;
-
- $user_etablissement = UserEtablissement::findOne([
- 'id_user' => $this->id_user,
- 'id_etablissement' => $this->id_etablissement
- ]) ;
- if($user_etablissement)
- {
- if($this->type == self::TYPE_CREDIT ||
- $this->type == self::TYPE_CREDIT_INITIAL ||
- $this->type == self::TYPE_REMBOURSEMENT)
- {
- $user_etablissement->credit += $this->montant ;
- $user_etablissement->save() ;
- }
- elseif($this->type == self::TYPE_PAIEMENT)
- {
- $user_etablissement->credit -= $this->montant ;
- }
-
- $user_etablissement->save() ;
- }
- }
- }
|