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.

87 line
2.8KB

  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\models\CreditHistorique ;
  6. use common\models\User ;
  7. use common\models\Etablissement ;
  8. use common\models\UserEtablissement ;
  9. use common\helpers\Mail ;
  10. /**
  11. * ContactForm is the model behind the contact form.
  12. */
  13. class CreditForm extends Model {
  14. public $id_user ;
  15. public $id_user_action ;
  16. public $id_etablissement ;
  17. public $type ;
  18. public $montant ;
  19. public $moyen_paiement ;
  20. public $commentaire ;
  21. public $send_mail ;
  22. /**
  23. * @inheritdoc
  24. */
  25. public function rules() {
  26. return [
  27. [['montant'], 'required'],
  28. [['id_user', 'id_user_action', 'id_etablissement'], 'integer'],
  29. [['date','send_mail'], 'safe'],
  30. [['montant'], 'double'],
  31. [['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255],
  32. ];
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function attributeLabels() {
  38. return [
  39. 'id_user' => 'Utilisateur',
  40. 'id_user_action' => 'Utilisateur',
  41. 'date' => 'Date',
  42. 'montant' => 'Montant',
  43. 'type' => 'Type',
  44. 'id_etablissement' => 'Établissement',
  45. 'moyen_paiement' => 'Moyen de paiement',
  46. 'commentaire' => 'Commentaire',
  47. 'send_mail' => 'Prévenir l\'utilisateur',
  48. ];
  49. }
  50. public function save() {
  51. if ($this->validate()) {
  52. $credit_historique = new CreditHistorique ;
  53. $credit_historique->id_user = $this->id_user;
  54. $credit_historique->id_user_action = Yii::$app->user->identity->id;
  55. $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement;
  56. $credit_historique->type = $this->type ;
  57. $credit_historique->commentaire = $this->commentaire ;
  58. $credit_historique->montant = $this->montant ;
  59. $credit_historique->moyen_paiement = $this->moyen_paiement ;
  60. $credit_historique->save();
  61. // on prévient l'utilisateur que son compte vient d'être crédité
  62. if($this->send_mail) {
  63. $user = User::findOne($this->id_user) ;
  64. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  65. $user_etablissement = UserEtablissement::findOne(['id_user' => $this->id_user, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
  66. Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [
  67. 'user' => $user,
  68. 'etablissement' => $etablissement,
  69. 'user_etablissement' => $user_etablissement,
  70. 'credit_form' => $this
  71. ]) ;
  72. }
  73. }
  74. }
  75. }