use Yii; | use Yii; | ||||
use yii\base\Model; | use yii\base\Model; | ||||
use common\models\CreditHistorique ; | |||||
use common\models\CreditHistory ; | |||||
use common\models\User ; | use common\models\User ; | ||||
use common\models\Etablissement ; | |||||
use common\models\UserEtablissement ; | |||||
use common\models\Prtoducer ; | |||||
use common\models\UserProducer ; | |||||
use common\helpers\Mail ; | use common\helpers\Mail ; | ||||
/** | /** | ||||
public $id_user ; | public $id_user ; | ||||
public $id_user_action ; | public $id_user_action ; | ||||
public $id_etablissement ; | |||||
public $id_producer ; | |||||
public $type ; | public $type ; | ||||
public $montant ; | |||||
public $moyen_paiement ; | |||||
public $commentaire ; | |||||
public $amount ; | |||||
public $means_payment ; | |||||
public $comment ; | |||||
public $send_mail ; | public $send_mail ; | ||||
/** | /** | ||||
public function rules() | public function rules() | ||||
{ | { | ||||
return [ | return [ | ||||
[['montant'], 'required'], | |||||
[['id_user', 'id_user_action', 'id_etablissement'], 'integer'], | |||||
[['amount'], 'required'], | |||||
[['id_user', 'id_user_action', 'id_producer'], 'integer'], | |||||
[['date','send_mail'], 'safe'], | [['date','send_mail'], 'safe'], | ||||
[['montant'], 'double'], | |||||
[['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255], | |||||
[['amount'], 'double'], | |||||
[['type', 'means_payment', 'comment'], 'string', 'max' => 255], | |||||
]; | ]; | ||||
} | } | ||||
'id_user' => 'Utilisateur', | 'id_user' => 'Utilisateur', | ||||
'id_user_action' => 'Utilisateur', | 'id_user_action' => 'Utilisateur', | ||||
'date' => 'Date', | 'date' => 'Date', | ||||
'montant' => 'Montant', | |||||
'amount' => 'Montant', | |||||
'type' => 'Type', | 'type' => 'Type', | ||||
'id_etablissement' => 'Établissement', | |||||
'moyen_paiement' => 'Moyen de paiement', | |||||
'commentaire' => 'Commentaire', | |||||
'id_producer' => 'Producteur', | |||||
'means_payment' => 'Moyen de paiement', | |||||
'comment' => 'Commentaire', | |||||
'send_mail' => 'Prévenir l\'utilisateur', | 'send_mail' => 'Prévenir l\'utilisateur', | ||||
]; | ]; | ||||
} | } | ||||
public function save() | public function save() | ||||
{ | { | ||||
if ($this->validate()) { | 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(); | |||||
$creditHistory = new CreditHistory; | |||||
$creditHistory->id_user = $this->id_user; | |||||
$creditHistory->id_user_action = Yii::$app->user->identity->id; | |||||
$creditHistory->id_producer = Producer::getCurrentId() ; | |||||
$creditHistory->type = $this->type ; | |||||
$creditHistory->comment = $this->comment ; | |||||
$creditHistory->amount = $this->amount ; | |||||
$creditHistory->means_payment = $this->means_payment ; | |||||
$creditHistory->save(); | |||||
// on prévient l'utilisateur que son compte vient d'être crédité | // on prévient l'utilisateur que son compte vient d'être crédité | ||||
if($this->send_mail) { | if($this->send_mail) { | ||||
$user = User::findOne($this->id_user) ; | $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]); | |||||
$producer = Producer::getCurrent() ; | |||||
$userProducer = UserProducer::searchOne([ | |||||
'id_user' => $this->id_user | |||||
]); | |||||
Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [ | Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [ | ||||
'user' => $user, | 'user' => $user, | ||||
'etablissement' => $etablissement, | |||||
'user_etablissement' => $user_etablissement, | |||||
'credit_form' => $this | |||||
'producer' => $producer, | |||||
'userProducer' => $userProducer, | |||||
'creditForm' => $this | |||||
]) ; | ]) ; | ||||
} | } | ||||
} | } |
{ | { | ||||
return Yii::$app->mailer->compose() | return Yii::$app->mailer->compose() | ||||
->setTo($email) | ->setTo($email) | ||||
->setFrom(['matthieu@lechatdesnoisettes.com' => 'Le Chat des Noisettes']) | |||||
->setFrom(['contact@laboiteapain.net' => 'Laboîte à pain']) | |||||
->setSubject($this->subject) | ->setSubject($this->subject) | ||||
->setTextBody($this->body) | ->setTextBody($this->body) | ||||
->send(); | ->send(); |
use Yii; | use Yii; | ||||
use yii\base\Model; | use yii\base\Model; | ||||
use common\models\UserEtablissement; | |||||
use common\models\Etablissement; | |||||
use common\models\UserProducer; | |||||
use common\models\Producer; | |||||
use yii\helpers\Html; | use yii\helpers\Html; | ||||
/** | /** | ||||
* ContactForm is the model behind the contact form. | * ContactForm is the model behind the contact form. | ||||
*/ | */ | ||||
class AddEtablissementForm extends Model | |||||
class AddProducerForm extends Model | |||||
{ | { | ||||
public $id_etablissement; | |||||
public $id_producer; | |||||
public $code; | public $code; | ||||
/** | /** | ||||
public function rules() | public function rules() | ||||
{ | { | ||||
return [ | return [ | ||||
['id_etablissement', 'integer'], | |||||
['id_etablissement', 'required'], | |||||
['id_etablissement', function($attribute, $params) { | |||||
['id_producer', 'integer'], | |||||
['id_producer', 'required'], | |||||
['id_producer', function($attribute, $params) { | |||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
if (!$etablissement) { | |||||
$this->addError($attribute, 'Cet établissement n\'existe pas.'); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if (!$producer) { | |||||
$this->addError($attribute, 'Ce producteur n\'existe pas.'); | |||||
} | } | ||||
$userProducerExist = UserProducer::searchOne([ | |||||
'id_user' => Yii::$app->user->identity->id, | |||||
'active' => 1 | |||||
]) ; | |||||
$user_etablissement_exist = UserEtablissement::find() | |||||
->where([ | |||||
'id_user' => Yii::$app->user->identity->id, | |||||
'id_etablissement' => $this->id_etablissement, | |||||
'actif' => 1 | |||||
]) | |||||
->one(); | |||||
if ($user_etablissement_exist) { | |||||
$this->addError($attribute, 'Cet établissement est déjà sur votre tableau de bord.'); | |||||
if ($userProducerExist) { | |||||
$this->addError($attribute, 'Ce producteur est déjà sur votre tableau de bord.'); | |||||
} | } | ||||
}], | }], | ||||
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | ||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
if ($etablissement) { | |||||
return strlen($etablissement->code); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if ($producer) { | |||||
return strlen($producer->code); | |||||
} else { | } else { | ||||
return false; | return false; | ||||
} | } | ||||
}], | }], | ||||
['code', function($attribute, $params) { | ['code', function($attribute, $params) { | ||||
$code = $this->$attribute; | $code = $this->$attribute; | ||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if ($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code))) { | |||||
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | |||||
$this->addError($attribute, 'Code incorrect'); | $this->addError($attribute, 'Code incorrect'); | ||||
} | } | ||||
}], | }], | ||||
public function attributeLabels() | public function attributeLabels() | ||||
{ | { | ||||
return [ | return [ | ||||
'id_etablissement' => 'Établissement', | |||||
'id_producer' => 'Producteur', | |||||
'code' => 'Code', | 'code' => 'Code', | ||||
]; | ]; | ||||
} | } | ||||
*/ | */ | ||||
public function add() | public function add() | ||||
{ | { | ||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
$user_etablissement_exist = UserEtablissement::find() | |||||
->where([ | |||||
'id_user' => Yii::$app->user->identity->id, | |||||
'id_etablissement' => $this->id_etablissement, | |||||
'actif' => 0 | |||||
]) | |||||
->one(); | |||||
if ($user_etablissement_exist) { | |||||
$user_etablissement_exist->actif = 1; | |||||
$user_etablissement_exist->save(); | |||||
$userProducerExist = UserProducer::searchOne([ | |||||
'id_user' => User::getCurrentId(), | |||||
'active' => 0 | |||||
]) ; | |||||
if ($userProducerExist) { | |||||
$userProducerExist->active = 1; | |||||
$userProducerExist->save(); | |||||
} else { | } else { | ||||
$user_etablissement = new UserEtablissement(); | |||||
$user_etablissement->id_user = Yii::$app->user->identity->id; | |||||
$user_etablissement->id_etablissement = $this->id_etablissement; | |||||
$user_etablissement->credit = 0; | |||||
$user_etablissement->actif = 1; | |||||
$user_etablissement->save(); | |||||
$userProducer = new UserProducer(); | |||||
$userProducer->id_user = User::getCurrentId(); | |||||
$userProducer->id_producer = $this->id_producr; | |||||
$userProducer->credit = 0; | |||||
$userProducer->actif = 1; | |||||
$userProducer->save(); | |||||
} | } | ||||
Yii::$app->session->setFlash('success', 'Le producteur <strong>' . Html::encode($etablissement->nom) . '</strong> a bien été ajoutée à votre tableau de bord.'); | |||||
Yii::$app->session->setFlash('success', 'Le producteur <strong>' . Html::encode($producer->name) . '</strong> a bien été ajoutée à votre tableau de bord.'); | |||||
} | } | ||||
} | } |
['id_producer', 'integer'], | ['id_producer', 'integer'], | ||||
['id_producer', function($attribute, $params) { | ['id_producer', function($attribute, $params) { | ||||
if ($this->id_producer) { | if ($this->id_producer) { | ||||
$producer = Etablissement::findOne($this->id_producer); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if (!$producer) { | if (!$producer) { | ||||
$this->addError($attribute, 'Ce producteur n\'existe pas.'); | $this->addError($attribute, 'Ce producteur n\'existe pas.'); | ||||
} | } | ||||
} | } | ||||
}], | }], | ||||
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | ||||
$producer = Etablissement::findOne($this->id_producer); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if ($producer) { | if ($producer) { | ||||
return strlen($producer->code); | return strlen($producer->code); | ||||
} else { | } else { | ||||
}], | }], | ||||
['code', function($attribute, $params) { | ['code', function($attribute, $params) { | ||||
$code = $this->$attribute; | $code = $this->$attribute; | ||||
$producer = Etablissement::findOne($this->id_producer); | |||||
$producer = Producer::findOne($this->id_producer); | |||||
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | ||||
$this->addError($attribute, 'Code incorrect'); | $this->addError($attribute, 'Code incorrect'); | ||||
{ | { | ||||
return [ | return [ | ||||
'id_producer' => 'Producteur', | 'id_producer' => 'Producteur', | ||||
'code' => 'Code de l\'établissement', | |||||
'code' => 'Code du producteur', | |||||
]; | ]; | ||||
} | } | ||||
public $username; | public $username; | ||||
public $email; | public $email; | ||||
public $password; | public $password; | ||||
public $nom; | |||||
public $prenom; | |||||
public $telephone; | |||||
public $is_boulanger; | |||||
public $name; | |||||
public $lastname; | |||||
public $phone; | |||||
public $is_producer; | |||||
public $siret; | public $siret; | ||||
public $nom_magasin; | public $nom_magasin; | ||||
public $code_postal; | public $code_postal; |