- <?php
-
-
-
- namespace backend\models;
-
- use common\helpers\GlobalParam;
- use common\helpers\Mailjet;
- use common\logic\Producer\Producer\Wrapper\ProducerManager;
- use common\logic\User\CreditHistory\Model\CreditHistory;
- use common\logic\User\CreditHistory\Wrapper\CreditHistoryManager;
- use common\logic\User\User\Model\User;
- use common\logic\User\User\Wrapper\UserManager;
- use common\logic\User\UserProducer\Model\UserProducer;
- use Yii;
- use yii\base\Model;
-
-
- 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'], 'string', 'max' => 255],
- [['comment'], 'string', 'max' => 2048],
- ];
- }
-
-
-
- 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()
- {
- $userManager = UserManager::getInstance();
- $creditHistoryManager = CreditHistoryManager::getInstance();
- $producerManager = ProducerManager::getInstance();
-
- if ($this->validate()) {
- $user = $userManager->findOneUserById($this->id_user);
- $creditHistoryManager->createCreditHistory(
- $this->type,
- $this->amount,
- GlobalParam::getCurrentProducer(),
- $user,
- Yii::$app->user->identity,
- $this->mean_payment
- );
-
-
- if($this->send_mail) {
- $user = User::findOne($this->id_user) ;
- $producer = GlobalParam::getCurrentProducer() ;
- $userProducer = UserProducer::searchOne([
- 'id_user' => $this->id_user
- ]);
-
- $paramsEmail = [
- 'from_email' => $producerManager->getEmailOpendistrib($producer),
- 'from_name' => $producer->name,
- 'to_email' => $user->email,
- 'to_name' => $userManager->getUsername($user),
- 'subject' => '['.$producer->name.'] Mouvement de crédit',
- 'content_view_text' => '@common/mail/creditUser-text.php',
- 'content_view_html' => '@common/mail/creditUser-html.php',
- 'content_params' => [
- 'user' => $user,
- 'producer' => $producer,
- 'userProducer' => $userProducer,
- 'creditForm' => $this
- ]
- ] ;
-
- Mailjet::sendMail($paramsEmail);
- }
- }
- }
-
- }
|