Affichage distinct des débits et crédits au niveau des historiques du crédit pain. Mise en place de l'enregistrement d'un commentaire "en dur" au niveau de chaque CreditHistorique pour toujours avoir des infos sur un paiement même après la suppression de la commande ou des utilisateurs qui y sont rattachés.prodstable
// remboursement de la commande | // remboursement de la commande | ||||
if ($commande->id_user && $commande->getMontantPaye() && Etablissement::getConfig('credit_pain')) { | if ($commande->id_user && $commande->getMontantPaye() && Etablissement::getConfig('credit_pain')) { | ||||
$commande->creditHistorique( | $commande->creditHistorique( | ||||
CreditHistorique::TYPE_REMBOURSEMENT, $commande->getMontantPaye(), $commande->production->id_etablissement, $commande->id_user | |||||
CreditHistorique::TYPE_REMBOURSEMENT, | |||||
$commande->getMontantPaye(), | |||||
$commande->production->id_etablissement, | |||||
$commande->id_user, | |||||
Yii::$app->user->identity->id | |||||
); | ); | ||||
} | } | ||||
} | } | ||||
public function actionAjaxDelete($date, $id_commande) { | public function actionAjaxDelete($date, $id_commande) { | ||||
$commande = Commande::find()->where(['id' => $id_commande])->one(); | |||||
$commande = Commande::find() | |||||
->with(['production', 'commandeProduits']) | |||||
->where(['id' => $id_commande]) | |||||
->one(); | |||||
$commande->init() ; | |||||
// delete | // delete | ||||
if ($commande) { | if ($commande) { | ||||
// remboursement si l'utilisateur a payé pour cette commande | // remboursement si l'utilisateur a payé pour cette commande | ||||
$montant_paye = $commande->getMontantPaye(); | $montant_paye = $commande->getMontantPaye(); | ||||
if ($montant_paye > 0.01) { | if ($montant_paye > 0.01) { | ||||
$credit_historique = new CreditHistorique; | |||||
$credit_historique->id_user = $commande->id_user; | |||||
$credit_historique->id_commande = $id_commande; | |||||
$credit_historique->montant = $montant_paye; | |||||
$credit_historique->type = CreditHistorique::TYPE_REMBOURSEMENT; | |||||
$credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement; | |||||
$credit_historique->id_user_action = Yii::$app->user->identity->id; | |||||
$credit_historique->save(); | |||||
$commande->creditHistorique( | |||||
CreditHistorique::TYPE_REMBOURSEMENT, | |||||
$montant_paye, | |||||
Yii::$app->user->identity->id_etablissement, | |||||
$commande->id_user, | |||||
Yii::$app->user->identity->id | |||||
); | |||||
} | } | ||||
$commande->delete(); | $commande->delete(); | ||||
->all(); | ->all(); | ||||
$html .= '<br /><br /><strong>Historique</strong><br /><table class="table table-condensed table-bordered">' | $html .= '<br /><br /><strong>Historique</strong><br /><table class="table table-condensed table-bordered">' | ||||
. '<thead><tr><th>Date</th><th>Utilisateur</th><th>Action</th><th>Montant</th></tr></thead>' | |||||
. '<thead><tr><th>Date</th><th>Utilisateur</th><th>Action</th><th>- Débit</th><th>+ Crédit</th></tr></thead>' | |||||
. '<tbody>'; | . '<tbody>'; | ||||
if ($historique && is_array($historique) && count($historique)) { | if ($historique && is_array($historique) && count($historique)) { | ||||
$html .= '<tr>' | $html .= '<tr>' | ||||
. '<td>' . date('d/m/Y H:i:s', strtotime($h->date)) . '</td>' | . '<td>' . date('d/m/Y H:i:s', strtotime($h->date)) . '</td>' | ||||
. '<td>' . Html::encode($h->userAction->nom . ' ' . $h->userAction->prenom) . '</td>' | . '<td>' . Html::encode($h->userAction->nom . ' ' . $h->userAction->prenom) . '</td>' | ||||
. '<td>' . $h->getLibelleType() . '</td>' | |||||
. '<td>' . number_format($h->montant, 2) . ' €</td>' | |||||
. '<td>' . $h->getStrLibelle() . '</td>' | |||||
. '<td>' . ($h->isTypeDebit() ? '- '.$h->getMontant(true) : '') . '</td>' | |||||
. '<td>' . ($h->isTypeCredit() ? '+ '.$h->getMontant(true) : '') . '</td>' | |||||
. '</tr>'; | . '</tr>'; | ||||
} | } | ||||
} else { | } else { | ||||
} | } | ||||
public function actionPaiement($id_commande, $type, $montant) { | public function actionPaiement($id_commande, $type, $montant) { | ||||
$commande = Commande::findOne($id_commande); | |||||
$commande = Commande::find() | |||||
->with('commandeProduits', 'production') | |||||
->where(['id' => $id_commande]) | |||||
->one(); | |||||
$commande->init() ; | |||||
if ($commande) { | if ($commande) { | ||||
$commande->creditHistorique( | $commande->creditHistorique( | ||||
$type, $montant, Yii::$app->user->identity->id_etablissement, Yii::$app->user->identity->id | |||||
$type, | |||||
$montant, | |||||
Yii::$app->user->identity->id_etablissement, | |||||
$commande->id_user, | |||||
Yii::$app->user->identity->id | |||||
); | ); | ||||
} | } | ||||
<th>Date</th> | <th>Date</th> | ||||
<th>Utilisateur</th> | <th>Utilisateur</th> | ||||
<th>Type</th> | <th>Type</th> | ||||
<th>Montant</th> | |||||
<th>- Débit</th> | |||||
<th>+ Crédit</th> | |||||
<th>Paiement</th> | <th>Paiement</th> | ||||
<th>Commentaire</th> | <th>Commentaire</th> | ||||
</tr> | </tr> | ||||
<?php if(count($historique)): ?> | <?php if(count($historique)): ?> | ||||
<?php foreach($historique as $ch): ?> | <?php foreach($historique as $ch): ?> | ||||
<tr> | <tr> | ||||
<td><?= date('d/m/Y à H:i:s',strtotime($ch->date)) ?></td> | |||||
<td><?= $ch->getDate(true) ; ?></td> | |||||
<td><?php if(isset($ch->userAction)): echo Html::encode($ch->userAction->nom. ' '.$ch->userAction->prenom) ; else: echo 'Administrateur' ;endif; ?></td> | <td><?php if(isset($ch->userAction)): echo Html::encode($ch->userAction->nom. ' '.$ch->userAction->prenom) ; else: echo 'Administrateur' ;endif; ?></td> | ||||
<td><?= $ch->getStrLibelle(); ?></td> | |||||
<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): ?> | |||||
Paiement commande | |||||
<?php if(isset($ch->commande)): ?> | |||||
du <?= date('d/m/Y',strtotime($ch->commande->production->date)) ?> | |||||
<?php else: ?> | |||||
(supprimée) | |||||
<?php endif; ?> | |||||
<?php endif; ?> | |||||
<?php if($ch->type == CreditHistorique::TYPE_REMBOURSEMENT): ?>Remboursement commande <?php if(isset($ch->commande)): ?>du <?= date('d/m/Y',strtotime($ch->commande->production->date)) ?><?php else: ?>(supprimée)<?php endif; ?><?php endif; ?> | |||||
<?php if($ch->type == CreditHistorique::TYPE_DEBIT): ?> | |||||
Débit | |||||
<?php if($ch->isTypeDebit()): ?> | |||||
- <?= $ch->getMontant(true); ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
</td> | </td> | ||||
<td> | <td> | ||||
<?= number_format($ch->montant,2) ; ?> € | |||||
<?php if($ch->isTypeCredit()): ?> | |||||
+ <?= $ch->getMontant(true); ?> | |||||
<?php endif; ?> | |||||
</td> | </td> | ||||
<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; ?> | |||||
<?php if($ch->moyen_paiement == CreditHistorique::MOYEN_AUTRE): ?>Autre<?php endif; ?> | |||||
<?php if(!$ch->moyen_paiement): ?>Crédit pain<?php endif; ?> | |||||
<?= $ch->getStrMoyenPaiement() ?> | |||||
</td> | </td> | ||||
<td> | <td> | ||||
<?php if(strlen($ch->commentaire)): ?> | <?php if(strlen($ch->commentaire)): ?> | ||||
<?= nl2br(Html::encode($ch->commentaire)) ; ?> | |||||
<?= nl2br($ch->commentaire) ; ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
</td> | </td> | ||||
</tr> | </tr> |
return json_encode($json_commande); | return json_encode($json_commande); | ||||
} | } | ||||
public function creditHistorique($type, $montant, $id_etablissement, $id_user) { | |||||
public function creditHistorique($type, $montant, $id_etablissement, $id_user, $id_user_action) { | |||||
$credit_historique = new CreditHistorique; | $credit_historique = new CreditHistorique; | ||||
$credit_historique->id_user = $this->id_user; | $credit_historique->id_user = $this->id_user; | ||||
$credit_historique->id_commande = $this->id; | $credit_historique->id_commande = $this->id; | ||||
$credit_historique->montant = $montant; | $credit_historique->montant = $montant; | ||||
$credit_historique->type = $type; | $credit_historique->type = $type; | ||||
$credit_historique->id_etablissement = $id_etablissement; | $credit_historique->id_etablissement = $id_etablissement; | ||||
$credit_historique->id_user_action = $id_user; | |||||
$credit_historique->id_user_action = $id_user_action; | |||||
$credit_historique->populateRelation('commande', $this) ; | |||||
$credit_historique->populateRelation('user', User::find()->where(['id' => $this->id_user])->one()) ; | |||||
$credit_historique->save(); | $credit_historique->save(); | ||||
} | } | ||||
use yii\db\ActiveRecord; | use yii\db\ActiveRecord; | ||||
use common\models\User; | use common\models\User; | ||||
use common\models\Commande; | use common\models\Commande; | ||||
use yii\helpers\Html; | |||||
/** | /** | ||||
* This is the model class for table "credit_historique". | * This is the model class for table "credit_historique". | ||||
const MOYEN_ESPECES = 'especes'; | const MOYEN_ESPECES = 'especes'; | ||||
const MOYEN_CHEQUE = 'cheque'; | const MOYEN_CHEQUE = 'cheque'; | ||||
const MOYEN_AUTRE = 'autre'; | const MOYEN_AUTRE = 'autre'; | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
} | } | ||||
public function save($runValidation = true, $attributeNames = NULL) { | public function save($runValidation = true, $attributeNames = NULL) { | ||||
// initialisation du commentaire avant sauvegarde | |||||
$this->commentaire = $this->getStrCommentaire() ; | |||||
parent::save($runValidation, $attributeNames); | parent::save($runValidation, $attributeNames); | ||||
// mise à jour du crédit au niveau de UserEtablissement | |||||
$user_etablissement = UserEtablissement::findOne([ | $user_etablissement = UserEtablissement::findOne([ | ||||
'id_user' => $this->id_user, | 'id_user' => $this->id_user, | ||||
'id_etablissement' => $this->id_etablissement | 'id_etablissement' => $this->id_etablissement | ||||
]); | ]); | ||||
if ($user_etablissement) { | if ($user_etablissement) { | ||||
if ($this->type == self::TYPE_CREDIT || | |||||
$this->type == self::TYPE_CREDIT_INITIAL || | |||||
$this->type == self::TYPE_REMBOURSEMENT) { | |||||
if ($this->isTypeCredit()) { | |||||
$user_etablissement->credit += $this->montant; | $user_etablissement->credit += $this->montant; | ||||
$user_etablissement->save(); | |||||
} elseif ($this->type == self::TYPE_PAIEMENT) { | |||||
} elseif ($this->isTypeDebit()) { | |||||
$user_etablissement->credit -= $this->montant; | $user_etablissement->credit -= $this->montant; | ||||
} | } | ||||
$user_etablissement->save(); | $user_etablissement->save(); | ||||
} | |||||
} | |||||
public function isTypeDebit() { | |||||
return in_array($this->type, [ | |||||
self::TYPE_DEBIT, | |||||
self::TYPE_PAIEMENT, | |||||
]) ; | |||||
} | |||||
public function isTypeCredit() { | |||||
return in_array($this->type, [ | |||||
self::TYPE_CREDIT, | |||||
self::TYPE_CREDIT_INITIAL, | |||||
self::TYPE_REMBOURSEMENT | |||||
]) ; | |||||
} | |||||
public function getMontant($format = false) { | |||||
if($format) | |||||
return number_format($this->montant,2) .' €' ; | |||||
else | |||||
return $this->montant ; | |||||
} | |||||
public function getStrLibelle() { | |||||
$str = '' ; | |||||
if($this->type == self::TYPE_CREDIT_INITIAL) { | |||||
$str = 'Crédit initial' ; | |||||
} | |||||
elseif($this->type == self::TYPE_CREDIT) { | |||||
$str = 'Crédit' ; | |||||
} | |||||
elseif($this->type == self::TYPE_PAIEMENT) { | |||||
$str = 'Paiement' ; | |||||
} | |||||
elseif($this->type == self::TYPE_REMBOURSEMENT) { | |||||
$str = 'Remboursement' ; | |||||
} | |||||
elseif($this->type == self::TYPE_DEBIT) { | |||||
$str = 'Débit' ; | |||||
} | |||||
if($this->type == self::TYPE_PAIEMENT || $this->type == self::TYPE_REMBOURSEMENT) { | |||||
if(isset($this->commande)) { | |||||
$str .= '<br />Commande : '.date('d/m/Y',strtotime($this->commande->production->date)) ; | |||||
} | |||||
else { | |||||
$str .= '<br />Commande supprimée' ; | |||||
} | |||||
} | |||||
return $str ; | |||||
} | |||||
public function getStrCommentaire() { | |||||
$str = $this->getStrLibelle() ; | |||||
if(isset($this->commande)) { | |||||
$str .= '<br />Montant de la commande : '.$this->commande->getMontant(true) ; | |||||
} | |||||
if(isset($this->user)) { | |||||
$str .= '<br />Client : '.Html::encode($this->user->nom. ' '.$this->user->prenom) ; | |||||
} | |||||
if(isset($this->userAction)) { | |||||
$str .= '<br />Action : '.Html::encode($this->userAction->nom. ' '.$this->userAction->prenom) ; | |||||
} | |||||
return $str ; | |||||
} | |||||
public function getDate($format = false) { | |||||
if($format) | |||||
return date('d/m/Y à H:i:s',strtotime($this->date)) ; | |||||
else | |||||
return $this->date ; | |||||
} | |||||
public function getStrMoyenPaiement() { | |||||
switch($this->moyen_paiement) { | |||||
case CreditHistorique::MOYEN_ESPECES : return 'Espèces' ; | |||||
case CreditHistorique::MOYEN_CHEQUE : return 'Chèque' ; | |||||
case CreditHistorique::MOYEN_CB : return 'Carte bancaire' ; | |||||
case CreditHistorique::MOYEN_AUTRE : return 'Autre' ; | |||||
default: return 'Crédit pain' ; | |||||
} | } | ||||
} | } | ||||
if ($montant_payer > 0) { | if ($montant_payer > 0) { | ||||
$commande->creditHistorique( | $commande->creditHistorique( | ||||
CreditHistorique::TYPE_PAIEMENT, $montant_payer, $production->id_etablissement, Yii::$app->user->identity->id | |||||
CreditHistorique::TYPE_PAIEMENT, | |||||
$montant_payer, | |||||
$production->id_etablissement, | |||||
Yii::$app->user->identity->id, | |||||
Yii::$app->user->identity->id | |||||
); | ); | ||||
} | } | ||||
} | } | ||||
elseif ($commande->getStatutPaiement() == Commande::STATUT_SURPLUS) { | elseif ($commande->getStatutPaiement() == Commande::STATUT_SURPLUS) { | ||||
$montant_rembourser = $montant_paye - $commande->montant; | $montant_rembourser = $montant_paye - $commande->montant; | ||||
$commande->creditHistorique( | $commande->creditHistorique( | ||||
CreditHistorique::TYPE_REMBOURSEMENT, $montant_rembourser, $production->id_etablissement, Yii::$app->user->identity->id | |||||
CreditHistorique::TYPE_REMBOURSEMENT, | |||||
$montant_rembourser, | |||||
$production->id_etablissement, | |||||
Yii::$app->user->identity->id, | |||||
Yii::$app->user->identity->id | |||||
); | ); | ||||
} | } | ||||
} | } | ||||
// remboursement | // remboursement | ||||
if ($commande->getMontantPaye()) { | if ($commande->getMontantPaye()) { | ||||
$commande->creditHistorique( | $commande->creditHistorique( | ||||
CreditHistorique::TYPE_REMBOURSEMENT, $commande->getMontantPaye(), $commande->production->id_etablissement, Yii::$app->user->identity->id | |||||
CreditHistorique::TYPE_REMBOURSEMENT, | |||||
$commande->getMontantPaye(), | |||||
$commande->production->id_etablissement, | |||||
Yii::$app->user->identity->id, | |||||
Yii::$app->user->identity->id | |||||
); | ); | ||||
} | } | ||||
// delete | // delete |