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
@@ -150,7 +150,11 @@ class CommandeController extends BackendController { | |||
// remboursement de la commande | |||
if ($commande->id_user && $commande->getMontantPaye() && Etablissement::getConfig('credit_pain')) { | |||
$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 | |||
); | |||
} | |||
@@ -913,21 +917,24 @@ class CommandeController extends BackendController { | |||
} | |||
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 | |||
if ($commande) { | |||
// remboursement si l'utilisateur a payé pour cette commande | |||
$montant_paye = $commande->getMontantPaye(); | |||
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(); | |||
@@ -1199,7 +1206,7 @@ class CommandeController extends BackendController { | |||
->all(); | |||
$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>'; | |||
if ($historique && is_array($historique) && count($historique)) { | |||
@@ -1207,8 +1214,9 @@ class CommandeController extends BackendController { | |||
$html .= '<tr>' | |||
. '<td>' . date('d/m/Y H:i:s', strtotime($h->date)) . '</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>'; | |||
} | |||
} else { | |||
@@ -1230,11 +1238,20 @@ class CommandeController extends BackendController { | |||
} | |||
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) { | |||
$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 | |||
); | |||
} | |||
@@ -54,7 +54,8 @@ $this->params['breadcrumbs'][] = 'Créditer'; | |||
<th>Date</th> | |||
<th>Utilisateur</th> | |||
<th>Type</th> | |||
<th>Montant</th> | |||
<th>- Débit</th> | |||
<th>+ Crédit</th> | |||
<th>Paiement</th> | |||
<th>Commentaire</th> | |||
</tr> | |||
@@ -63,37 +64,25 @@ $this->params['breadcrumbs'][] = 'Créditer'; | |||
<?php if(count($historique)): ?> | |||
<?php foreach($historique as $ch): ?> | |||
<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><?= $ch->getStrLibelle(); ?></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; ?> | |||
</td> | |||
<td> | |||
<?= number_format($ch->montant,2) ; ?> € | |||
<?php if($ch->isTypeCredit()): ?> | |||
+ <?= $ch->getMontant(true); ?> | |||
<?php endif; ?> | |||
</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> | |||
<?php if(strlen($ch->commentaire)): ?> | |||
<?= nl2br(Html::encode($ch->commentaire)) ; ?> | |||
<?= nl2br($ch->commentaire) ; ?> | |||
<?php endif; ?> | |||
</td> | |||
</tr> |
@@ -209,14 +209,16 @@ class Commande extends \yii\db\ActiveRecord { | |||
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->id_user = $this->id_user; | |||
$credit_historique->id_commande = $this->id; | |||
$credit_historique->montant = $montant; | |||
$credit_historique->type = $type; | |||
$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(); | |||
} | |||
@@ -6,6 +6,7 @@ use Yii; | |||
use yii\db\ActiveRecord; | |||
use common\models\User; | |||
use common\models\Commande; | |||
use yii\helpers\Html; | |||
/** | |||
* This is the model class for table "credit_historique". | |||
@@ -30,7 +31,7 @@ class CreditHistorique extends ActiveRecord { | |||
const MOYEN_ESPECES = 'especes'; | |||
const MOYEN_CHEQUE = 'cheque'; | |||
const MOYEN_AUTRE = 'autre'; | |||
/** | |||
* @inheritdoc | |||
*/ | |||
@@ -96,23 +97,109 @@ class CreditHistorique extends ActiveRecord { | |||
} | |||
public function save($runValidation = true, $attributeNames = NULL) { | |||
// initialisation du commentaire avant sauvegarde | |||
$this->commentaire = $this->getStrCommentaire() ; | |||
parent::save($runValidation, $attributeNames); | |||
// mise à jour du crédit au niveau de UserEtablissement | |||
$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 || | |||
$this->type == self::TYPE_REMBOURSEMENT) { | |||
if ($this->isTypeCredit()) { | |||
$user_etablissement->credit += $this->montant; | |||
$user_etablissement->save(); | |||
} elseif ($this->type == self::TYPE_PAIEMENT) { | |||
} elseif ($this->isTypeDebit()) { | |||
$user_etablissement->credit -= $this->montant; | |||
} | |||
$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' ; | |||
} | |||
} | |||
@@ -435,7 +435,11 @@ class CommandeController extends FrontendController { | |||
if ($montant_payer > 0) { | |||
$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 | |||
); | |||
} | |||
} | |||
@@ -443,7 +447,11 @@ class CommandeController extends FrontendController { | |||
elseif ($commande->getStatutPaiement() == Commande::STATUT_SURPLUS) { | |||
$montant_rembourser = $montant_paye - $commande->montant; | |||
$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 | |||
); | |||
} | |||
} | |||
@@ -478,7 +486,11 @@ class CommandeController extends FrontendController { | |||
// remboursement | |||
if ($commande->getMontantPaye()) { | |||
$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 |