@@ -16,6 +16,7 @@ use common\models\UserEtablissement; | |||
use common\models\Etablissement; | |||
use yii\base\UserException; | |||
use common\models\CreditHistorique; | |||
use backend\models\CreditForm; | |||
use common\models\Commande; | |||
use common\helpers\Mail; | |||
@@ -189,27 +190,14 @@ class UserController extends BackendController { | |||
public function actionCredit($id) { | |||
$user = User::find()->with('userEtablissement')->where(['id' => $id])->one(); | |||
$user_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]); | |||
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ; | |||
if (($user_etablissement) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) { | |||
$credit_historique = new CreditHistorique; | |||
if ($credit_historique->load(Yii::$app->request->post()) && $credit_historique->validate()) { | |||
$credit_historique->id_user = $user->id; | |||
$credit_historique->id_user_action = Yii::$app->user->identity->id; | |||
$credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement; | |||
$credit_historique->save(); | |||
// on prévient l'utilisateur que son compte vient d'être crédité | |||
if($credit_historique->type == CreditHistorique::TYPE_CREDIT) { | |||
Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [ | |||
'user' => $user, | |||
'etablissement' => $etablissement, | |||
'user_etablissement' => $user_etablissement, | |||
'credit_historique' => $credit_historique | |||
]) ; | |||
} | |||
$credit_historique = new CreditHistorique; | |||
$credit_form = new CreditForm; | |||
if ($credit_form->load(Yii::$app->request->post()) && $credit_form->validate()) { | |||
$credit_form->id_user = $id ; | |||
$credit_form->save(); | |||
$credit_form = new CreditForm; | |||
} | |||
$historique = CreditHistorique::find() | |||
@@ -223,7 +211,7 @@ class UserController extends BackendController { | |||
return $this->render('credit', [ | |||
'user' => $user, | |||
'credit_historique' => $credit_historique, | |||
'credit_form' => $credit_form, | |||
'historique' => $historique | |||
]); | |||
} |
@@ -0,0 +1,86 @@ | |||
<?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 | |||
]) ; | |||
} | |||
} | |||
} | |||
} |
@@ -28,18 +28,20 @@ $this->params['breadcrumbs'][] = 'Créditer'; | |||
<div class="col-md-12"> | |||
<h1><?= $this->title ?></h1> | |||
<?php $form = ActiveForm::begin(); ?> | |||
<?= $form->field($credit_historique, 'type')->dropDownList([ | |||
<?= $form->field($credit_form, 'type')->dropDownList([ | |||
CreditHistorique::TYPE_CREDIT => 'Crédit', | |||
CreditHistorique::TYPE_DEBIT => 'Débit', | |||
]) ?> | |||
<?= $form->field($credit_historique, 'montant')->textInput() ?> | |||
<?= $form->field($credit_historique, 'moyen_paiement')->dropDownList([ | |||
<?= $form->field($credit_form, 'montant')->textInput() ?> | |||
<?= $form->field($credit_form, 'moyen_paiement')->dropDownList([ | |||
CreditHistorique::MOYEN_ESPECES => 'Espèces', | |||
CreditHistorique::MOYEN_CB => 'Carte bancaire', | |||
CreditHistorique::MOYEN_CHEQUE => 'Chèque', | |||
CreditHistorique::MOYEN_AUTRE => 'Autre', | |||
]) ?> | |||
<?= $form->field($credit_historique, 'commentaire')->textarea() ?> | |||
<?= $form->field($credit_form, 'commentaire')->textarea() ?> | |||
<?= $form->field($credit_form, 'send_mail')->checkbox() ?> | |||
<div class="form-group"> | |||
<?= Html::submitButton( 'Créditer', ['class' => 'btn btn-primary']) ?> | |||
</div> |
@@ -2,12 +2,14 @@ | |||
use yii\helpers\Html; | |||
use common\helpers\Price; | |||
use common\models\CreditHistorique; | |||
?> | |||
<p>Bonjour <?= Html::encode($user->prenom); ?>,</p> | |||
<p>Votre producteur <strong><?= Html::encode($etablissement->nom); ?></strong> vient | |||
de créditer votre compte de <strong><?= Price::format($credit_historique->montant); ?></strong> sur le site <a href="http://www.laboiteapain.net/">La boîte à pain</a>.</p> | |||
de <?php if($credit_form->type == CreditHistorique::TYPE_CREDIT): ?>créditer<?php else: ?>débiter<?php endif; ?> votre compte de <strong><?= Price::format($credit_form->montant); ?></strong> sur le site <a href="http://www.laboiteapain.net/">La boîte à pain</a>.</p> | |||
<p>Votre compte est désormais à <strong><?= Price::format($user_etablissement->credit); ?></strong></p> | |||
@@ -1,11 +1,12 @@ | |||
<?php | |||
use common\helpers\Price ; | |||
use common\models\CreditHistorique; | |||
?> | |||
Bonjour <?= $user->prenom; ?>,</p> | |||
Votre producteur <?= $etablissement->nom; ?> vient de créditer votre compte de <?= Price::format($credit_historique->montant); ?> sur le site http://www.laboiteapain.net/ | |||
Votre producteur <?= $etablissement->nom; ?> vient de <?php if($credit_form->type == CreditHistorique::TYPE_CREDIT): ?>créditer<?php else: ?>débiter<?php endif; ?> votre compte de <?= Price::format($credit_form->montant); ?> sur le site http://www.laboiteapain.net/ | |||
Votre compte est désormais à : <?= Price::format($user_etablissement->credit); ?>. | |||
@@ -27,6 +27,7 @@ class CreditHistorique extends ActiveRecord { | |||
const TYPE_PAIEMENT = 'paiement'; | |||
const TYPE_REMBOURSEMENT = 'remboursement'; | |||
const TYPE_DEBIT = 'debit'; | |||
const MOYEN_CB = 'cb'; | |||
const MOYEN_ESPECES = 'especes'; | |||
const MOYEN_CHEQUE = 'cheque'; |