Ajout du crédit de l'utilisateur dans la liste des utilisateurs + un bouton menant au formulaire de crédit. Création du formulaire de crédit avec l'affichage de l'historique des opérations concernant l'utilisateur.prodstable
use common\models\UserEtablissement ; | use common\models\UserEtablissement ; | ||||
use common\models\Etablissement ; | use common\models\Etablissement ; | ||||
use yii\base\UserException ; | use yii\base\UserException ; | ||||
use common\models\CreditHistorique; | |||||
/** | /** | ||||
* UserController implements the CRUD actions for User model. | * UserController implements the CRUD actions for User model. | ||||
} | } | ||||
public function actionCredit($id) | |||||
{ | |||||
$user = User::find()->with('userEtablissement')->where(['id' => $id])->one() ; | |||||
$user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ; | |||||
if(($user_appartient_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_etablissement = Yii::$app->user->identity->id_etablissement ; | |||||
$credit_historique->type = CreditHistorique::TYPE_CREDIT ; | |||||
$credit_historique->save() ; | |||||
$this->redirect(['user/index']) ; | |||||
} | |||||
$historique = CreditHistorique::find() | |||||
->where([ | |||||
'id_user' => $user->id, | |||||
'id_etablissement' => Yii::$app->user->identity->id_etablissement, | |||||
]) | |||||
->orderBy('date DESC') | |||||
->all() ; | |||||
return $this->render('credit', [ | |||||
'user' => $user, | |||||
'credit_historique' => $credit_historique, | |||||
'historique' => $historique | |||||
]) ; | |||||
} | |||||
else { | |||||
throw new UserException("Vous ne pouvez pas créditer un utilisateur qui n'est pas associé à votre boulangerie."); | |||||
} | |||||
} | |||||
/** | /** | ||||
* Finds the User model based on its primary key value. | * Finds the User model based on its primary key value. | ||||
* If the model is not found, a 404 HTTP exception will be thrown. | * If the model is not found, a 404 HTTP exception will be thrown. |
<?php | |||||
use yii\helpers\Html; | |||||
use yii\widgets\ActiveForm; | |||||
use common\models\CreditHistorique; | |||||
$this->title = 'Créditer <small>'.Html::encode($user->nom.' '.$user->prenom).'</small>'; | |||||
$this->params['breadcrumbs'][] = ['label' => 'Utilisateurs', 'url' => ['index']]; | |||||
$this->params['breadcrumbs'][] = ['label' => Html::encode($user->nom.' '.$user->prenom)]; | |||||
$this->params['breadcrumbs'][] = 'Créditer'; | |||||
?> | |||||
<div class="user-credit"> | |||||
<div class="col-md-6"> | |||||
<h1><?= $this->title ?></h1> | |||||
<?php $form = ActiveForm::begin(); ?> | |||||
<?= $form->field($credit_historique, 'montant')->textInput() ?> | |||||
<?= $form->field($credit_historique, 'moyen_paiement')->dropDownList([ | |||||
CreditHistorique::MOYEN_ESPECES => 'Espèces', | |||||
CreditHistorique::MOYEN_CB => 'Carte bancaire', | |||||
CreditHistorique::MOYEN_CHEQUE => 'Chèque', | |||||
]) ?> | |||||
<div class="form-group"> | |||||
<?= Html::submitButton( 'Créditer', ['class' => 'btn btn-primary']) ?> | |||||
</div> | |||||
<?php ActiveForm::end(); ?> | |||||
</div> | |||||
<div class="col-md-6"> | |||||
<h2>Historique</h2> | |||||
<table class="table table-bordered"> | |||||
<thead> | |||||
<tr> | |||||
<th>Date</th> | |||||
<th>Type</th> | |||||
<th>Montant</th> | |||||
<th>Paiement</th> | |||||
</tr> | |||||
</thead> | |||||
<tbody> | |||||
<?php if(count($historique)): ?> | |||||
<?php foreach($historique as $ch): ?> | |||||
<tr> | |||||
<td><?= date('d/m/Y à H:i:s',strtotime($ch->date)) ?></td> | |||||
<td> | |||||
<?php if($ch->type == CreditHistorique::TYPE_CREDIT_INITIAL): ?>Crédit initial<?php endif; ?> | |||||
<?php if($ch->type == CreditHistorique::TYPE_CREDIT): ?>Crédit<?php endif; ?> | |||||
<?php if($ch->type == CreditHistorique::TYPE_PAIEMENT_COMMANDE): ?>Paiement commande<?php endif; ?> | |||||
</td> | |||||
<td> | |||||
<?= $ch->montant ; ?> € | |||||
</td> | |||||
<td> | |||||
<?php if($ch->moyen_paiement == CreditHistorique::MOYEN_ESPECES): ?>Espèces<?php endif; ?> | |||||
<?php if($ch->moyen_paiement == CreditHistorique::MOYEN_CB): ?>Carte bancaire<?php endif; ?> | |||||
<?php if($ch->moyen_paiement == CreditHistorique::MOYEN_CHEQUE): ?>Chèque<?php endif; ?> | |||||
</td> | |||||
</tr> | |||||
<?php endforeach; ?> | |||||
<?php else: ?> | |||||
<tr><td colspan="4">Aucun résultat</td></tr> | |||||
<?php endif; ?> | |||||
</tbody> | |||||
</table> | |||||
</div> | |||||
</div> |
'prenom', | 'prenom', | ||||
'telephone', | 'telephone', | ||||
'email', | 'email', | ||||
[ | |||||
'attribute' => 'credit', | |||||
'format' => 'raw', | |||||
'value' => function($model) { | |||||
$html = $model['credit'].' € ' ; | |||||
$html = '<div class="input-group"> | |||||
<input type="text" class="form-control input-credit" readonly="readonly" value="'.$model['credit'].' €" placeholder=""> | |||||
<span class="input-group-btn"> | |||||
'.Html::a( | |||||
'<span class="glyphicon glyphicon-euro"></span> Crédit', | |||||
Yii::$app->urlManager->createUrl(['user/credit','id' => $model['id']]), | |||||
[ | |||||
'title' => 'Crédit', | |||||
'class' => 'btn btn-default' | |||||
] | |||||
).' | |||||
</span> | |||||
</div>' ; | |||||
/*$html = '<div class="btn-group" role="group" aria-label=""> | |||||
<input type="text" readonly="readonly" class="btn btn-default">'.$model['credit'].' € </button> | |||||
'.Html::a( | |||||
'<span class="glyphicon glyphicon-euro"></span> Créditer', | |||||
Yii::$app->urlManager->createUrl(['user/credit','id' => $model['id']]), | |||||
[ | |||||
'title' => 'Créditer', | |||||
'class' => 'btn btn-default' | |||||
] | |||||
).' | |||||
</div>' ;*/ | |||||
/*$html .= Html::a( | |||||
'<span class="glyphicon glyphicon-euro"></span> Créditer', | |||||
Yii::$app->urlManager->createUrl(['user/credit','id' => $model['id']]), | |||||
[ | |||||
'title' => 'Créditer', | |||||
'class' => 'btn btn-default btn-xs' | |||||
] | |||||
) ;*/ | |||||
return $html ; | |||||
} | |||||
], | |||||
[ | [ | ||||
'class' => 'yii\grid\ActionColumn', | 'class' => 'yii\grid\ActionColumn', | ||||
'template' => '{update}', | 'template' => '{update}', |
font-weight: normal; | font-weight: normal; | ||||
display: block; | display: block; | ||||
} | } | ||||
/* utilisateurs */ | |||||
/* line 946, ../sass/screen.scss */ | |||||
.user-index .input-group { | |||||
width: 180px; | |||||
} | |||||
/* line 949, ../sass/screen.scss */ | |||||
.user-index .input-group .input-credit { | |||||
text-align: center; | |||||
} |
} | } | ||||
} | } | ||||
} | } | ||||
/* utilisateurs */ | |||||
.user-index { | |||||
.input-group { | |||||
width: 180px ; | |||||
.input-credit { | |||||
text-align: center ; | |||||
} | |||||
} | |||||
} |
use Yii; | use Yii; | ||||
use yii\db\ActiveRecord ; | use yii\db\ActiveRecord ; | ||||
use common\models\User ; | |||||
/** | /** | ||||
* This is the model class for table "credit_historique". | * This is the model class for table "credit_historique". | ||||
*/ | */ | ||||
class CreditHistorique extends ActiveRecord | class CreditHistorique extends ActiveRecord | ||||
{ | { | ||||
const TYPE_CREDIT_INITIAL = 'credit' ; | |||||
const TYPE_CREDIT_INITIAL = 'credit-initial' ; | |||||
const TYPE_CREDIT = 'credit' ; | const TYPE_CREDIT = 'credit' ; | ||||
const TYPE_PAIEMENT_COMMANDE = 'paiement-commande' ; | const TYPE_PAIEMENT_COMMANDE = 'paiement-commande' ; | ||||
public function rules() | public function rules() | ||||
{ | { | ||||
return [ | return [ | ||||
[['id_user', 'type'], 'required'], | |||||
[['montant', 'moyen_paiement'], 'required'], | |||||
[['id_user', 'id_commande', 'id_etablissement'], 'integer'], | [['id_user', 'id_commande', 'id_etablissement'], 'integer'], | ||||
[['date'], 'safe'], | [['date'], 'safe'], | ||||
[['montant'], 'number'], | [['montant'], 'number'], | ||||
'moyen_paiement' => 'Moyen de paiement', | 'moyen_paiement' => 'Moyen de paiement', | ||||
]; | ]; | ||||
} | } | ||||
public function save($runValidation = true, $attributeNames = NULL) { | |||||
parent::save($runValidation, $attributeNames) ; | |||||
$user_etablissement = UserEtablissement::findOne([ | |||||
'id_user' => $this->id_user, | |||||
'id_etablissement' => $this->id_etablissement | |||||
]) ; | |||||
if($user_etablissement) | |||||
{ | |||||
if($this->type == self::TYPE_CREDIT || | |||||
$this->type == self::TYPE_CREDIT_INITIAL) | |||||
{ | |||||
$user_etablissement->credit += $this->montant ; | |||||
$user_etablissement->save() ; | |||||
} | |||||
elseif($this->type == self::TYPE_PAIEMENT_COMMANDE) | |||||
{ | |||||
$user_etablissement->credit -= $this->montant ; | |||||
$user_etablissement->save() ; | |||||
} | |||||
} | |||||
} | |||||
} | } |