You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.3KB

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