|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
-
- namespace backend\models;
-
- use Yii;
- use yii\base\Model;
- use common\models\CreditHistorique ;
- use common\models\User ;
- use common\models\Etablissement ;
- use common\models\UserEtablissement ;
- use common\helpers\Mail ;
-
- /**
- * ContactForm is the model behind the contact form.
- */
- class CreditForm extends Model {
-
- public $id_user ;
- public $id_user_action ;
- public $id_etablissement ;
- public $type ;
- public $montant ;
- public $moyen_paiement ;
- public $commentaire ;
- public $send_mail ;
-
- /**
- * @inheritdoc
- */
- public function rules() {
- return [
- [['montant'], 'required'],
- [['id_user', 'id_user_action', 'id_etablissement'], 'integer'],
- [['date','send_mail'], 'safe'],
- [['montant'], 'double'],
- [['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels() {
- return [
- 'id_user' => 'Utilisateur',
- 'id_user_action' => 'Utilisateur',
- 'date' => 'Date',
- 'montant' => 'Montant',
- 'type' => 'Type',
- 'id_etablissement' => 'Établissement',
- 'moyen_paiement' => 'Moyen de paiement',
- 'commentaire' => 'Commentaire',
- 'send_mail' => 'Prévenir l\'utilisateur',
- ];
- }
-
- public function save() {
- if ($this->validate()) {
- $credit_historique = new CreditHistorique ;
- $credit_historique->id_user = $this->id_user;
- $credit_historique->id_user_action = Yii::$app->user->identity->id;
- $credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement;
- $credit_historique->type = $this->type ;
- $credit_historique->commentaire = $this->commentaire ;
- $credit_historique->montant = $this->montant ;
- $credit_historique->moyen_paiement = $this->moyen_paiement ;
- $credit_historique->save();
-
- // on prévient l'utilisateur que son compte vient d'être crédité
- if($this->send_mail) {
- $user = User::findOne($this->id_user) ;
- $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
- $user_etablissement = UserEtablissement::findOne(['id_user' => $this->id_user, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
-
- Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [
- 'user' => $user,
- 'etablissement' => $etablissement,
- 'user_etablissement' => $user_etablissement,
- 'credit_form' => $this
- ]) ;
- }
-
- }
- }
-
- }
|