Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

122 lines
3.1KB

  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 MOYEN_CB = 'cb' ;
  26. const MOYEN_ESPECES = 'especes' ;
  27. const MOYEN_CHEQUE = 'cheque' ;
  28. /**
  29. * @inheritdoc
  30. */
  31. public static function tableName()
  32. {
  33. return 'credit_historique';
  34. }
  35. /**
  36. * @inheritdoc
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['montant'], 'required'],
  42. [['id_user','id_user_action', 'id_commande', 'id_etablissement'], 'integer'],
  43. [['date'], 'safe'],
  44. [['montant'], 'number'],
  45. [['type', 'moyen_paiement'], 'string', 'max' => 255],
  46. ];
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function attributeLabels()
  52. {
  53. return [
  54. 'id' => 'ID',
  55. 'id_user' => 'Utilisateur',
  56. 'id_user_action' => 'Utilisateur',
  57. 'id_commande' => 'Commande',
  58. 'date' => 'Date',
  59. 'montant' => 'Montant',
  60. 'type' => 'Type',
  61. 'id_etablissement' => 'Établissement',
  62. 'moyen_paiement' => 'Moyen de paiement',
  63. ];
  64. }
  65. public function getUser()
  66. {
  67. return $this->hasOne(User::className(),['id' => 'id_user']) ;
  68. }
  69. public function getUserAction()
  70. {
  71. return $this->hasOne(User::className(),['id' => 'id_user_action']) ;
  72. }
  73. public function getCommande()
  74. {
  75. return $this->hasOne(Commande::className(),['id' => 'id_commande']) ;
  76. }
  77. public function getLibelleType()
  78. {
  79. if($this->type == 'paiement')
  80. return 'Paiement' ;
  81. elseif($this->type == 'remboursement')
  82. return 'Remboursement' ;
  83. }
  84. public function save($runValidation = true, $attributeNames = NULL) {
  85. parent::save($runValidation, $attributeNames) ;
  86. $user_etablissement = UserEtablissement::findOne([
  87. 'id_user' => $this->id_user,
  88. 'id_etablissement' => $this->id_etablissement
  89. ]) ;
  90. if($user_etablissement)
  91. {
  92. if($this->type == self::TYPE_CREDIT ||
  93. $this->type == self::TYPE_CREDIT_INITIAL ||
  94. $this->type == self::TYPE_REMBOURSEMENT)
  95. {
  96. $user_etablissement->credit += $this->montant ;
  97. $user_etablissement->save() ;
  98. }
  99. elseif($this->type == self::TYPE_PAIEMENT)
  100. {
  101. $user_etablissement->credit -= $this->montant ;
  102. }
  103. $user_etablissement->save() ;
  104. }
  105. }
  106. }