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.

93 lines
2.4KB

  1. <?php
  2. namespace common\models;
  3. use Yii;
  4. use yii\db\ActiveRecord ;
  5. use common\models\User ;
  6. /**
  7. * This is the model class for table "credit_historique".
  8. *
  9. * @property integer $id
  10. * @property integer $id_user
  11. * @property integer $id_commande
  12. * @property string $date
  13. * @property double $montant
  14. * @property string $type
  15. * @property integer $id_etablissement
  16. * @property string $moyen_paiement
  17. */
  18. class CreditHistorique extends ActiveRecord
  19. {
  20. const TYPE_CREDIT_INITIAL = 'credit-initial' ;
  21. const TYPE_CREDIT = 'credit' ;
  22. const TYPE_PAIEMENT_COMMANDE = 'paiement-commande' ;
  23. const MOYEN_CB = 'cb' ;
  24. const MOYEN_ESPECES = 'especes' ;
  25. const MOYEN_CHEQUE = 'cheque' ;
  26. /**
  27. * @inheritdoc
  28. */
  29. public static function tableName()
  30. {
  31. return 'credit_historique';
  32. }
  33. /**
  34. * @inheritdoc
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['montant', 'moyen_paiement'], 'required'],
  40. [['id_user', 'id_commande', 'id_etablissement'], 'integer'],
  41. [['date'], 'safe'],
  42. [['montant'], 'number'],
  43. [['type', 'moyen_paiement'], 'string', 'max' => 255],
  44. ];
  45. }
  46. /**
  47. * @inheritdoc
  48. */
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'id_user' => 'Utilisateur',
  54. 'id_commande' => 'Commande',
  55. 'date' => 'Date',
  56. 'montant' => 'Montant',
  57. 'type' => 'Type',
  58. 'id_etablissement' => 'Établissement',
  59. 'moyen_paiement' => 'Moyen de paiement',
  60. ];
  61. }
  62. public function save($runValidation = true, $attributeNames = NULL) {
  63. parent::save($runValidation, $attributeNames) ;
  64. $user_etablissement = UserEtablissement::findOne([
  65. 'id_user' => $this->id_user,
  66. 'id_etablissement' => $this->id_etablissement
  67. ]) ;
  68. if($user_etablissement)
  69. {
  70. if($this->type == self::TYPE_CREDIT ||
  71. $this->type == self::TYPE_CREDIT_INITIAL)
  72. {
  73. $user_etablissement->credit += $this->montant ;
  74. $user_etablissement->save() ;
  75. }
  76. elseif($this->type == self::TYPE_PAIEMENT_COMMANDE)
  77. {
  78. $user_etablissement->credit -= $this->montant ;
  79. $user_etablissement->save() ;
  80. }
  81. }
  82. }
  83. }