No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

133 líneas
3.4KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord ;
  5. use common\models\User ;
  6. use common\models\Commande ;
  7. /**
  8. * This is the model class for table "credit_historique".
  9. *
  10. * @property integer $id
  11. * @property integer $id_user
  12. * @property integer $id_commande
  13. * @property string $date
  14. * @property double $montant
  15. * @property string $type
  16. * @property integer $id_etablissement
  17. * @property string $moyen_paiement
  18. */
  19. class CreditHistorique extends ActiveRecord
  20. {
  21. const TYPE_CREDIT_INITIAL = 'credit-initial' ;
  22. const TYPE_CREDIT = 'credit' ;
  23. const TYPE_PAIEMENT = 'paiement' ;
  24. const TYPE_REMBOURSEMENT = 'remboursement' ;
  25. const TYPE_DEBIT = 'debit' ;
  26. const MOYEN_CB = 'cb' ;
  27. const MOYEN_ESPECES = 'especes' ;
  28. const MOYEN_CHEQUE = 'cheque' ;
  29. const MOYEN_AUTRE = 'autre' ;
  30. /**
  31. * @inheritdoc
  32. */
  33. public static function tableName()
  34. {
  35. return 'credit_historique';
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules()
  41. {
  42. return [
  43. [['montant'], 'required'],
  44. [['id_user','id_user_action', 'id_commande', 'id_etablissement'], 'integer'],
  45. [['date'], 'safe'],
  46. [['montant'], 'double'],
  47. [['type', 'moyen_paiement','commentaire'], 'string', 'max' => 255],
  48. ];
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'id_user' => 'Utilisateur',
  58. 'id_user_action' => 'Utilisateur',
  59. 'id_commande' => 'Commande',
  60. 'date' => 'Date',
  61. 'montant' => 'Montant',
  62. 'type' => 'Type',
  63. 'id_etablissement' => 'Établissement',
  64. 'moyen_paiement' => 'Moyen de paiement',
  65. 'commentaire' => 'Commentaire',
  66. ];
  67. }
  68. public function getUser()
  69. {
  70. return $this->hasOne(User::className(),['id' => 'id_user']) ;
  71. }
  72. public function getUserAction()
  73. {
  74. return $this->hasOne(User::className(),['id' => 'id_user_action']) ;
  75. }
  76. public function getCommande()
  77. {
  78. return $this->hasOne(Commande::className(),['id' => 'id_commande']) ;
  79. }
  80. public function getLibelleType()
  81. {
  82. switch($this->type)
  83. {
  84. case 'paiement':
  85. return 'Paiement' ;
  86. break ;
  87. case 'remboursement':
  88. return 'Remboursement' ;
  89. break ;
  90. case 'debit':
  91. return 'Débit' ;
  92. break ;
  93. }
  94. }
  95. public function save($runValidation = true, $attributeNames = NULL) {
  96. parent::save($runValidation, $attributeNames) ;
  97. $user_etablissement = UserEtablissement::findOne([
  98. 'id_user' => $this->id_user,
  99. 'id_etablissement' => $this->id_etablissement
  100. ]) ;
  101. if($user_etablissement)
  102. {
  103. if($this->type == self::TYPE_CREDIT ||
  104. $this->type == self::TYPE_CREDIT_INITIAL ||
  105. $this->type == self::TYPE_REMBOURSEMENT)
  106. {
  107. $user_etablissement->credit += $this->montant ;
  108. $user_etablissement->save() ;
  109. }
  110. elseif($this->type == self::TYPE_PAIEMENT)
  111. {
  112. $user_etablissement->credit -= $this->montant ;
  113. }
  114. $user_etablissement->save() ;
  115. }
  116. }
  117. }