|
- <?php
-
-
-
- namespace backend\models;
-
- use Yii;
- use yii\base\Model;
- use common\models\CreditHistory ;
- use common\models\User ;
- use common\models\Producer ;
- use common\models\UserProducer ;
- use common\helpers\Mail ;
-
-
- class CreditForm extends Model
- {
-
- public $id_user ;
- public $id_user_action ;
- public $id_producer ;
- public $type ;
- public $amount ;
- public $mean_payment ;
- public $comment ;
- public $send_mail ;
-
-
-
- public function rules()
- {
- return [
- [['amount'], 'required'],
- [['id_user', 'id_user_action', 'id_producer'], 'integer'],
- [['date','send_mail'], 'safe'],
- [['amount'], 'double'],
- [['type', 'mean_payment', 'comment'], 'string', 'max' => 255],
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'id_user' => 'Utilisateur',
- 'id_user_action' => 'Utilisateur',
- 'date' => 'Date',
- 'amount' => 'Montant',
- 'type' => 'Type',
- 'id_producer' => 'Producteur',
- 'mean_payment' => 'Moyen de paiement',
- 'comment' => 'Commentaire',
- 'send_mail' => 'Prévenir l\'utilisateur',
- ];
- }
-
-
-
- public function save()
- {
- if ($this->validate()) {
- $creditHistory = new CreditHistory;
- $creditHistory->id_user = $this->id_user;
- $creditHistory->id_user_action = Yii::$app->user->identity->id;
- $creditHistory->id_producer = Producer::getId() ;
- $creditHistory->type = $this->type ;
- $creditHistory->comment = $this->comment ;
- $creditHistory->amount = $this->amount ;
- $creditHistory->mean_payment = $this->mean_payment ;
- $creditHistory->save();
-
-
- if($this->send_mail) {
- $user = User::findOne($this->id_user) ;
- $producer = Producer::getCurrent() ;
- $userProducer = UserProducer::searchOne([
- 'id_user' => $this->id_user
- ]);
- Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [
- 'user' => $user,
- 'producer' => $producer,
- 'userProducer' => $userProducer,
- 'creditForm' => $this
- ]) ;
- }
- }
- }
-
- }
|