namespace backend\controllers; | namespace backend\controllers; | ||||
use backend\models\CreditForm; | |||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use common\helpers\Mailjet; | use common\helpers\Mailjet; | ||||
use common\models\User; | use common\models\User; | ||||
if (($userProducer) || User::getCurrentStatus() == User::STATUS_ADMIN) { | if (($userProducer) || User::getCurrentStatus() == User::STATUS_ADMIN) { | ||||
$creditForm = new CreditForm; | |||||
$creditForm = new CreditForm(); | |||||
if ($creditForm->load(Yii::$app->request->post()) && $creditForm->validate()) { | if ($creditForm->load(Yii::$app->request->post()) && $creditForm->validate()) { | ||||
$creditForm->id_user = $id; | $creditForm->id_user = $id; | ||||
$creditForm->save(); | $creditForm->save(); | ||||
$creditForm = new CreditForm; | $creditForm = new CreditForm; | ||||
} | } | ||||
use common\models\Producer; | use common\models\Producer; | ||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
$creditHistoryService = $this->getLogic()->getCreditHistoryContainer()->getService(); | |||||
$this->setTitle('Créditer <small>'.Html::encode($user->lastname.' '.$user->name).'</small>', 'Créditer '.Html::encode($user->lastname.' '.$user->name)) ; | $this->setTitle('Créditer <small>'.Html::encode($user->lastname.' '.$user->name).'</small>', 'Créditer '.Html::encode($user->lastname.' '.$user->name)) ; | ||||
$this->addBreadcrumb(['label' => 'Utilisateurs', 'url' => ['index']]) ; | $this->addBreadcrumb(['label' => 'Utilisateurs', 'url' => ['index']]) ; | ||||
$this->addBreadcrumb(['label' => Html::encode($user->lastname.' '.$user->name)]) ; | $this->addBreadcrumb(['label' => Html::encode($user->lastname.' '.$user->name)]) ; | ||||
<?php foreach($history as $creditHistory): ?> | <?php foreach($history as $creditHistory): ?> | ||||
<tr> | <tr> | ||||
<td><?= $creditHistory->getDate(true) ; ?></td> | <td><?= $creditHistory->getDate(true) ; ?></td> | ||||
<td><?= Html::encode($creditHistory->strUserAction()); ?></td> | |||||
<td><?= $creditHistory->getStrWording(); ?></td> | |||||
<td><?= Html::encode($creditHistoryService->getStrUserAction($creditHistory)); ?></td> | |||||
<td><?= $creditHistoryService->getStrWording($creditHistory); ?></td> | |||||
<td> | <td> | ||||
<?php if($creditHistory->isTypeDebit()): ?> | |||||
- <?= $creditHistory->getAmount(true); ?> | |||||
<?php if($creditHistoryService->isTypeDebit($creditHistory)): ?> | |||||
- <?= $creditHistoryService->getAmount($creditHistory, true); ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
</td> | </td> | ||||
<td> | <td> | ||||
<?php if($creditHistory->isTypeCredit()): ?> | |||||
+ <?= $creditHistory->getAmount(true); ?> | |||||
<?php if($creditHistoryService->isTypeCredit($creditHistory)): ?> | |||||
+ <?= $creditHistoryService->getAmount($creditHistory, true); ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
</td> | </td> | ||||
<td> | <td> | ||||
<?= $creditHistory->getStrMeanPayment() ?> | |||||
<?= $creditHistoryService->getStrMeanPayment($creditHistory) ?> | |||||
</td> | </td> | ||||
<td> | <td> | ||||
<?php if(strlen($creditHistory->comment)): ?> | |||||
<?= nl2br($creditHistory->comment) ; ?> | |||||
<?php if(strlen($creditHistory->getComment())): ?> | |||||
<?= nl2br($creditHistory->getComment()); ?> | |||||
<?php endif; ?> | <?php endif; ?> | ||||
</td> | </td> | ||||
</tr> | </tr> |
namespace common\components; | namespace common\components; | ||||
use common\containers\CreditHistoryContainer; | |||||
use common\containers\ProducerPriceRangeContainer; | use common\containers\ProducerPriceRangeContainer; | ||||
use common\containers\UserContainer; | use common\containers\UserContainer; | ||||
use common\containers\ProducerContainer; | use common\containers\ProducerContainer; | ||||
{ | { | ||||
return new UserProducerContainer(); | return new UserProducerContainer(); | ||||
} | } | ||||
public function getCreditHistoryContainer() | |||||
{ | |||||
return new CreditHistoryContainer(); | |||||
} | |||||
} | } |
var $title ; | var $title ; | ||||
var $page_title ; | var $page_title ; | ||||
var $buttons ; | var $buttons ; | ||||
public function getLogic() | |||||
{ | |||||
return Yii::$app->logic; | |||||
} | |||||
public function setTitle($title, $page_title = '') | public function setTitle($title, $page_title = '') | ||||
{ | { | ||||
$this->title = $title ; | $this->title = $title ; |
<?php | |||||
namespace common\containers; | |||||
use common\repositories\UserRepository; | |||||
use common\services\CreditHistoryService; | |||||
use common\services\UserService; | |||||
class CreditHistoryContainer | |||||
{ | |||||
public function getService() | |||||
{ | |||||
return new CreditHistoryService(); | |||||
} | |||||
public function getRepository() | |||||
{ | |||||
return new CreditHistoryService(); | |||||
} | |||||
} |
<?php | <?php | ||||
/** | |||||
Copyright distrib (2018) | |||||
contact@opendistrib.net | |||||
Ce logiciel est un programme informatique servant à aider les producteurs | |||||
à distribuer leur production en circuits courts. | |||||
Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||||
respectant les principes de diffusion des logiciels libres. Vous pouvez | |||||
utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||||
de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||||
sur le site "http://www.cecill.info". | |||||
En contrepartie de l'accessibilité au code source et des droits de copie, | |||||
de modification et de redistribution accordés par cette licence, il n'est | |||||
offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||||
seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||||
titulaire des droits patrimoniaux et les concédants successifs. | |||||
A cet égard l'attention de l'utilisateur est attirée sur les risques | |||||
associés au chargement, à l'utilisation, à la modification et/ou au | |||||
développement et à la reproduction du logiciel par l'utilisateur étant | |||||
donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||||
manipuler et qui le réserve donc à des développeurs et des professionnels | |||||
avertis possédant des connaissances informatiques approfondies. Les | |||||
utilisateurs sont donc invités à charger et tester l'adéquation du | |||||
logiciel à leurs besoins dans des conditions permettant d'assurer la | |||||
sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||||
à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||||
Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||||
pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||||
termes. | |||||
*/ | |||||
/** | |||||
* Copyright distrib (2018) | |||||
* | |||||
* contact@opendistrib.net | |||||
* | |||||
* Ce logiciel est un programme informatique servant à aider les producteurs | |||||
* à distribuer leur production en circuits courts. | |||||
* | |||||
* Ce logiciel est régi par la licence CeCILL soumise au droit français et | |||||
* respectant les principes de diffusion des logiciels libres. Vous pouvez | |||||
* utiliser, modifier et/ou redistribuer ce programme sous les conditions | |||||
* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA | |||||
* sur le site "http://www.cecill.info". | |||||
* | |||||
* En contrepartie de l'accessibilité au code source et des droits de copie, | |||||
* de modification et de redistribution accordés par cette licence, il n'est | |||||
* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, | |||||
* seule une responsabilité restreinte pèse sur l'auteur du programme, le | |||||
* titulaire des droits patrimoniaux et les concédants successifs. | |||||
* | |||||
* A cet égard l'attention de l'utilisateur est attirée sur les risques | |||||
* associés au chargement, à l'utilisation, à la modification et/ou au | |||||
* développement et à la reproduction du logiciel par l'utilisateur étant | |||||
* donné sa spécificité de logiciel libre, qui peut le rendre complexe à | |||||
* manipuler et qui le réserve donc à des développeurs et des professionnels | |||||
* avertis possédant des connaissances informatiques approfondies. Les | |||||
* utilisateurs sont donc invités à charger et tester l'adéquation du | |||||
* logiciel à leurs besoins dans des conditions permettant d'assurer la | |||||
* sécurité de leurs systèmes et ou de leurs données et, plus généralement, | |||||
* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. | |||||
* | |||||
* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez | |||||
* pris connaissance de la licence CeCILL, et que vous en avez accepté les | |||||
* termes. | |||||
*/ | |||||
namespace common\models; | namespace common\models; | ||||
use Yii; | |||||
use common\components\ActiveRecordCommon ; | |||||
use yii\db\ActiveRecord; | |||||
use common\models\User; | |||||
use common\models\Order; | |||||
use common\models\Producer; | |||||
use yii\helpers\Html; | |||||
use common\components\ActiveRecordCommon; | |||||
/** | /** | ||||
* This is the model class for table "credit_historique". | * This is the model class for table "credit_historique". | ||||
*/ | */ | ||||
class CreditHistory extends ActiveRecordCommon | class CreditHistory extends ActiveRecordCommon | ||||
{ | { | ||||
const TYPE_INITIAL_CREDIT = 'initial-credit'; | |||||
const TYPE_CREDIT = 'credit'; | |||||
const TYPE_PAYMENT = 'payment'; | |||||
const TYPE_REFUND = 'refund'; | |||||
const TYPE_DEBIT = 'debit'; | |||||
const TYPE_INITIAL_CREDIT = 'initial-credit'; | |||||
const TYPE_CREDIT = 'credit'; | |||||
const TYPE_PAYMENT = 'payment'; | |||||
const TYPE_REFUND = 'refund'; | |||||
const TYPE_DEBIT = 'debit'; | |||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public static function tableName() | |||||
public static function tableName() | |||||
{ | { | ||||
return 'credit_history'; | return 'credit_history'; | ||||
} | } | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public function rules() | |||||
public function rules() | |||||
{ | { | ||||
return [ | return [ | ||||
[['amount'], 'required'], | [['amount'], 'required'], | ||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
*/ | */ | ||||
public function attributeLabels() | |||||
public function attributeLabels() | |||||
{ | { | ||||
return [ | return [ | ||||
'id' => 'ID', | 'id' => 'ID', | ||||
'comment' => 'Commentaire', | 'comment' => 'Commentaire', | ||||
]; | ]; | ||||
} | } | ||||
/* | /* | ||||
* Relations | * Relations | ||||
*/ | */ | ||||
public function getUser() | |||||
public function getUser() | |||||
{ | { | ||||
return $this->hasOne(User::className(), ['id' => 'id_user']); | return $this->hasOne(User::className(), ['id' => 'id_user']); | ||||
} | } | ||||
public function getUserAction() | |||||
public function getUserObject() | |||||
{ | |||||
return $this->user; | |||||
} | |||||
public function getUserAction() | |||||
{ | { | ||||
return $this->hasOne(User::className(), ['id' => 'id_user_action']); | return $this->hasOne(User::className(), ['id' => 'id_user_action']); | ||||
} | } | ||||
public function getOrder() | |||||
public function getUserActionObject() | |||||
{ | |||||
return $this->userAction; | |||||
} | |||||
public function getOrder() | |||||
{ | { | ||||
return $this->hasOne(Order::className(), ['id' => 'id_order']); | return $this->hasOne(Order::className(), ['id' => 'id_order']); | ||||
} | } | ||||
/** | |||||
* Retourne les options de base nécessaires à la fonction de recherche. | |||||
* | |||||
* @return array | |||||
*/ | |||||
public static function defaultOptionsSearch() { | |||||
return [ | |||||
'with' => [], | |||||
'join_with' => [], | |||||
'orderby' => self::tableName().'.date ASc', | |||||
'attribute_id_producer' => self::tableName().'.id_producer' | |||||
] ; | |||||
public function getOrderObject() | |||||
{ | |||||
return $this->order; | |||||
} | } | ||||
/** | |||||
* Retourne le type de CreditHistorique (paiement, remboursement ou débit). | |||||
* | |||||
* @return string | |||||
/* | |||||
* Getters / setters | |||||
*/ | */ | ||||
public function getStrType() | |||||
public function getId() | |||||
{ | { | ||||
switch ($this->type) { | |||||
case self::TYPE_PAYMENT: | |||||
return 'Paiement'; | |||||
break; | |||||
case self::TYPE_REFUND: | |||||
return 'Remboursement'; | |||||
break; | |||||
case self::TYPE_DEBIT: | |||||
return 'Débit'; | |||||
break; | |||||
} | |||||
return $this->id; | |||||
} | } | ||||
/** | |||||
* Enregistre le modèle. | |||||
* | |||||
* @param boolean $runValidation | |||||
* @param array $attributeNames | |||||
*/ | |||||
public function save($runValidation = true, $attributeNames = NULL) | |||||
public function getIdUser() | |||||
{ | { | ||||
if($this->amount > -0.01 && $this->amount < 0.01) { | |||||
return false; | |||||
} | |||||
// initialisation du commentaire avant sauvegarde | |||||
$this->comment .= $this->getStrComment() ; | |||||
parent::save($runValidation, $attributeNames); | |||||
// mise à jour du crédit au niveau de UserProducer | |||||
$userProducer = UserProducer::searchOne([ | |||||
'id_user' => $this->id_user, | |||||
'id_producer' => $this->id_producer | |||||
]) ; | |||||
$creditLimitReminder = Producer::getConfig('credit_limit_reminder') ; | |||||
$oldCredit = $userProducer->credit ; | |||||
if ($userProducer) { | |||||
if ($this->isTypeCredit()) { | |||||
$userProducer->credit += $this->amount; | |||||
} elseif ($this->isTypeDebit()) { | |||||
$userProducer->credit -= $this->amount; | |||||
} | |||||
$userProducer->save(); | |||||
// set mean payment | |||||
if($this->id_order && $this->id_order > 0) { | |||||
$order = Order::searchOne(['id' => (int) $this->id_order]) ; | |||||
if($order) { | |||||
$paymentStatus = $order->getPaymentStatus() ; | |||||
if($paymentStatus == Order::PAYMENT_PAID | |||||
|| $paymentStatus == Order::PAYMENT_SURPLUS) { | |||||
$order->mean_payment = MeanPayment::CREDIT ; | |||||
$order->save() ; | |||||
} | |||||
} | |||||
} | |||||
// seuil limite de crédit | |||||
$newCredit = $userProducer->credit ; | |||||
if(!is_null($creditLimitReminder) && | |||||
$oldCredit > $creditLimitReminder && $newCredit <= $creditLimitReminder) { | |||||
$user = User::findOne($this->id_user) ; | |||||
$producer = Producer::findOne($this->id_producer) ; | |||||
Yii::$app->mailer->compose( | |||||
[ | |||||
'html' => 'creditLimitReminder-html', | |||||
'text' => 'creditLimitReminder-text' | |||||
], | |||||
[ | |||||
'user' => $user, | |||||
'producer' => $producer, | |||||
'credit' => $newCredit | |||||
] | |||||
) | |||||
->setTo($user->email) | |||||
->setFrom(['contact@opendistrib.net' => 'distrib']) | |||||
->setSubject('[distrib] Seuil limite de crédit dépassé') | |||||
->send(); | |||||
} | |||||
} | |||||
return $this->id_user; | |||||
} | } | ||||
/** | |||||
* Retourne si le CreditHistorique est un débit ou non. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function isTypeDebit() | |||||
public function setIdUser($idUser) | |||||
{ | { | ||||
return in_array($this->type, [ | |||||
self::TYPE_DEBIT, | |||||
self::TYPE_PAYMENT, | |||||
]) ; | |||||
$this->id_user = $idUser; | |||||
return $this; | |||||
} | } | ||||
/** | |||||
* Retourne si le CreditHistorique est un crédit ou non. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function isTypeCredit() | |||||
public function getIdUserAction() | |||||
{ | { | ||||
return in_array($this->type, [ | |||||
self::TYPE_CREDIT, | |||||
self::TYPE_INITIAL_CREDIT, | |||||
self::TYPE_REFUND | |||||
]) ; | |||||
return $this->id_user_action; | |||||
} | } | ||||
/** | |||||
* Retourne le montant. | |||||
* | |||||
* @param boolean $format | |||||
* @return float | |||||
*/ | |||||
public function getAmount($format = false) | |||||
public function setIdUserAction($idUserAction) | |||||
{ | { | ||||
if($format) { | |||||
return number_format($this->amount,2) .' €' ; | |||||
} | |||||
else { | |||||
return $this->amount ; | |||||
} | |||||
$this->id_user_action = $idUserAction; | |||||
return $this; | |||||
} | } | ||||
/** | |||||
* Retourne le libellé du CreditHistory informant de son type et | |||||
* éventuellement de la date de sa commande associée. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrWording() | |||||
public function getIdOrder() | |||||
{ | { | ||||
$str = '' ; | |||||
if($this->type == self::TYPE_INITIAL_CREDIT) { | |||||
$str = 'Crédit initial' ; | |||||
} | |||||
elseif($this->type == self::TYPE_CREDIT) { | |||||
$str = 'Crédit' ; | |||||
} | |||||
elseif($this->type == self::TYPE_PAYMENT) { | |||||
$str = 'Paiement' ; | |||||
} | |||||
elseif($this->type == self::TYPE_REFUND) { | |||||
$str = 'Remboursement' ; | |||||
} | |||||
elseif($this->type == self::TYPE_DEBIT) { | |||||
$str = 'Débit' ; | |||||
} | |||||
if($this->type == self::TYPE_PAYMENT || $this->type == self::TYPE_REFUND) { | |||||
if(isset($this->order) && isset($this->order->distribution)) { | |||||
$str .= '<br />Commande : '.date('d/m/Y',strtotime($this->order->distribution->date)) ; | |||||
} | |||||
else { | |||||
$str .= '<br />Commande supprimée' ; | |||||
} | |||||
} | |||||
return $str ; | |||||
return $this->id_order; | |||||
} | } | ||||
/** | |||||
* Retourne les informations à ajouter au commentaire du CreditHistorique | |||||
* (libellé, montant, client, action) au format HTML. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrComment() | |||||
public function setIdOrder($idOrder) | |||||
{ | { | ||||
$str = '' ; | |||||
if(strlen($this->comment)) { | |||||
$str .= '<br />' ; | |||||
} | |||||
$str .= $this->getStrWording() ; | |||||
if(isset($this->order)) { | |||||
$str .= '<br />Montant de la commande : '.$this->order->getAmountWithTax(Order::AMOUNT_TOTAL, true) ; | |||||
} | |||||
if(isset($this->user)) { | |||||
$str .= '<br />Client : '.Html::encode($this->user->name. ' '.$this->user->lastname) ; | |||||
} | |||||
if(isset($this->userAction)) { | |||||
$str .= '<br />Action : '.Html::encode($this->userAction->name. ' '.$this->userAction->lastname) ; | |||||
} | |||||
return $str ; | |||||
$this->id_order = $idOrder; | |||||
return $this; | |||||
} | } | ||||
/** | |||||
* Retourne la date. | |||||
* | |||||
* @param boolean $format | |||||
* @return string | |||||
*/ | |||||
public function getDate($format = false) | |||||
public function getDate() | |||||
{ | { | ||||
if($format) | |||||
return date('d/m/Y à H:i:s',strtotime($this->date)) ; | |||||
else | |||||
return $this->date ; | |||||
return $this->date; | |||||
} | } | ||||
/** | |||||
* Retourne le libellé du moyen de paiement du CreditHistory courant. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrMeanPayment() | |||||
public function setDate($date) | |||||
{ | { | ||||
return MeanPayment::getStrBy($this->mean_payment) ; | |||||
$this->date = $date; | |||||
return $this; | |||||
} | } | ||||
/** | |||||
* Retourne le libellé de l'utilisateur ayant initié l'action. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function strUserAction() | |||||
public function getAmount() | |||||
{ | |||||
return $this->amount; | |||||
} | |||||
public function setAmount($amount) | |||||
{ | |||||
$this->amount = $amount; | |||||
return $this; | |||||
} | |||||
public function getType() | |||||
{ | |||||
return $this->type; | |||||
} | |||||
public function setType($type) | |||||
{ | { | ||||
if($this->userAction) { | |||||
return $this->userAction->name . ' ' . $this->userAction->lastname ; | |||||
} | |||||
else { | |||||
return 'Système' ; | |||||
} | |||||
$this->type = $type; | |||||
return $this; | |||||
} | |||||
public function getIdProducer() | |||||
{ | |||||
return $this->id_producer; | |||||
} | |||||
public function setIdProducer($idProducer) | |||||
{ | |||||
$this->id_producer = $idProducer; | |||||
return $this; | |||||
} | |||||
public function getMeanPayment() | |||||
{ | |||||
return $this->mean_payment; | |||||
} | } | ||||
public function setMeanPayment($meanPayment) | |||||
{ | |||||
$this->mean_payment = $meanPayment; | |||||
return $this; | |||||
} | |||||
public function getComment() | |||||
{ | |||||
return $this->comment; | |||||
} | |||||
public function setComment($comment) | |||||
{ | |||||
$this->comment = $comment; | |||||
return $this; | |||||
} | |||||
} | } |
<?php | |||||
namespace common\repositories; | |||||
use common\models\CreditHistory; | |||||
use common\models\User; | |||||
class CreditHistoryRepository | |||||
{ | |||||
/** | |||||
* Retourne les options de base nécessaires à la fonction de recherche. | |||||
* | |||||
* @return array | |||||
*/ | |||||
public static function defaultOptionsSearch() | |||||
{ | |||||
return [ | |||||
'with' => [], | |||||
'join_with' => [], | |||||
'orderby' => CreditHistory::tableName() . '.date ASc', | |||||
'attribute_id_producer' => CreditHistory::tableName() . '.id_producer' | |||||
]; | |||||
} | |||||
} |
class UserProducerRepository | class UserProducerRepository | ||||
{ | { | ||||
public function getOne($idUser, $idProducer) | |||||
{ | |||||
return UserProducer::searchOne([ | |||||
'id_user' => $idUser, | |||||
'id_producer' => $idProducer | |||||
]); | |||||
} | |||||
public function getBy($idUser, $active = 1, $bookmark = 1) | public function getBy($idUser, $active = 1, $bookmark = 1) | ||||
{ | { | ||||
return UserProducer::find() | return UserProducer::find() |
<?php | |||||
namespace common\services; | |||||
use common\helpers\MeanPayment; | |||||
use common\models\CreditHistory; | |||||
use common\models\Order; | |||||
use yii\helpers\Html; | |||||
class CreditHistoryService | |||||
{ | |||||
/** | |||||
* Enregistre le modèle. | |||||
* | |||||
* @param boolean $runValidation | |||||
* @param array $attributeNames | |||||
*/ | |||||
public function save($creditHistory) | |||||
{ | |||||
if ($creditHistory->getAmount() > -0.01 && $creditHistory->getAmount() < 0.01) { | |||||
return false; | |||||
} | |||||
// initialisation du commentaire avant sauvegarde | |||||
$creditHistory->setComment($creditHistory->getComment() . $this->getStrComment($creditHistory)); | |||||
$creditHistory->save(); | |||||
// Mise à jour du crédit au niveau de UserProducer | |||||
Yii::$app->logic->getUserProducerContainer()->getService()->updateCredit($creditHistory); | |||||
} | |||||
/** | |||||
* Retourne si le CreditHistorique est un débit ou non. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function isTypeDebit($creditHistory) | |||||
{ | |||||
return in_array($creditHistory->type, [ | |||||
CreditHistory::TYPE_DEBIT, | |||||
CreditHistory::TYPE_PAYMENT, | |||||
]); | |||||
} | |||||
/** | |||||
* Retourne si le CreditHistorique est un crédit ou non. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function isTypeCredit($creditHistory) | |||||
{ | |||||
return in_array($creditHistory->type, [ | |||||
CreditHistory::TYPE_CREDIT, | |||||
CreditHistory::TYPE_INITIAL_CREDIT, | |||||
CreditHistory::TYPE_REFUND | |||||
]); | |||||
} | |||||
/** | |||||
* Retourne le montant. | |||||
* | |||||
* @param boolean $format | |||||
* @return float | |||||
*/ | |||||
public function getAmount($creditHistory, $format = false) | |||||
{ | |||||
if ($format) { | |||||
return number_format($creditHistory->amount, 2) . ' €'; | |||||
} | |||||
else { | |||||
return $creditHistory->amount; | |||||
} | |||||
} | |||||
/** | |||||
* Retourne le libellé du CreditHistory informant de son type et | |||||
* éventuellement de la date de sa commande associée. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrWording($creditHistory) | |||||
{ | |||||
$str = ''; | |||||
if ($creditHistory->type == CreditHistory::TYPE_INITIAL_CREDIT) { | |||||
$str = 'Crédit initial'; | |||||
} | |||||
elseif ($creditHistory->type == CreditHistory::TYPE_CREDIT) { | |||||
$str = 'Crédit'; | |||||
} | |||||
elseif ($creditHistory->type == CreditHistory::TYPE_PAYMENT) { | |||||
$str = 'Paiement'; | |||||
} | |||||
elseif ($creditHistory->type == CreditHistory::TYPE_REFUND) { | |||||
$str = 'Remboursement'; | |||||
} | |||||
elseif ($creditHistory->type == CreditHistory::TYPE_DEBIT) { | |||||
$str = 'Débit'; | |||||
} | |||||
if ($creditHistory->type == CreditHistory::TYPE_PAYMENT || $creditHistory->type == CreditHistory::TYPE_REFUND) { | |||||
if (isset($creditHistory->order) && isset($creditHistory->order->distribution)) { | |||||
$str .= '<br />Commande : ' . date('d/m/Y', strtotime($creditHistory->order->distribution->date)); | |||||
} | |||||
else { | |||||
$str .= '<br />Commande supprimée'; | |||||
} | |||||
} | |||||
return $str; | |||||
} | |||||
/** | |||||
* Retourne les informations à ajouter au commentaire du CreditHistorique | |||||
* (libellé, montant, client, action) au format HTML. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrComment($creditHistory) | |||||
{ | |||||
$str = ''; | |||||
if (strlen($creditHistory->getComment())) { | |||||
$str .= '<br />'; | |||||
} | |||||
$str .= $this->getStrWording(); | |||||
if ($creditHistory->getOrder()) { | |||||
$str .= '<br />Montant de la commande : ' . $creditHistory->getOrder()->getAmountWithTax(Order::AMOUNT_TOTAL, true); | |||||
} | |||||
if ($creditHistory->getUser()) { | |||||
$str .= '<br />Client : ' . Html::encode($creditHistory->getUser()->name . ' ' . $creditHistory->user->lastname); | |||||
} | |||||
if ($creditHistory->getUserAction()) { | |||||
$str .= '<br />Action : ' . Html::encode($creditHistory->getUserAction()->name . ' ' . $creditHistory->userAction->lastname); | |||||
} | |||||
return $str; | |||||
} | |||||
/** | |||||
* Retourne la date. | |||||
* | |||||
* @param boolean $format | |||||
* @return string | |||||
*/ | |||||
public function getDate($creditHistory, $format = false) | |||||
{ | |||||
$date = $creditHistory->getDate(); | |||||
if ($format) { | |||||
return date('d/m/Y à H:i:s', strtotime($date)); | |||||
} | |||||
else { | |||||
return $date; | |||||
} | |||||
} | |||||
/** | |||||
* Retourne le libellé du moyen de paiement du CreditHistory courant. | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function getStrMeanPayment($creditHistory) | |||||
{ | |||||
return MeanPayment::getStrBy($creditHistory->getMeanPayment()); | |||||
} | |||||
/** | |||||
* Retourne le libellé de l'utilisateur ayant initié l'action. | |||||
* | |||||
* @return string | |||||
*/ | |||||
// strUserAction | |||||
public function getStrUserAction($creditHistory) | |||||
{ | |||||
$userAction = $creditHistory->getUserActionObject(); | |||||
if ($userAction) { | |||||
return $userAction->name . ' ' . $userAction->lastname; | |||||
} | |||||
else { | |||||
return 'Système'; | |||||
} | |||||
} | |||||
} |
namespace common\services; | namespace common\services; | ||||
use common\helpers\Password; | |||||
use common\helpers\MeanPayment; | |||||
use common\models\Order; | |||||
use common\models\Producer; | use common\models\Producer; | ||||
use common\models\UserProducer; | |||||
class UserProducerService | class UserProducerService | ||||
{ | { | ||||
public function updateCredit($creditHistory) | |||||
{ | |||||
$userProducer = Yii::$app->logic->getUserProducerContainer()->getRepository()->getOne($creditHistory->id_user, $creditHistory->id_producer); | |||||
if ($userProducer) { | |||||
$oldCredit = $userProducer->credit; | |||||
$this->deductCredit($userProducer, $creditHistory); | |||||
$this->initMeanPaymentOrder($creditHistory); | |||||
$this->sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit); | |||||
} | |||||
} | |||||
public function deductCredit($userProducer, $creditHistory) | |||||
{ | |||||
if ($creditHistory->isTypeCredit()) { | |||||
$userProducer->credit += $creditHistory->amount; | |||||
} elseif ($creditHistory->isTypeDebit()) { | |||||
$userProducer->credit -= $creditHistory->amount; | |||||
} | |||||
$userProducer->save(); | |||||
} | |||||
public function initMeanPaymentOrder($creditHistory) | |||||
{ | |||||
// set mean payment | |||||
if ($creditHistory->id_order && $creditHistory->id_order > 0) { | |||||
$order = Order::searchOne(['id' => (int)$creditHistory->id_order]); | |||||
if ($order) { | |||||
$paymentStatus = $order->getPaymentStatus(); | |||||
if ($paymentStatus == Order::PAYMENT_PAID | |||||
|| $paymentStatus == Order::PAYMENT_SURPLUS) { | |||||
$order->mean_payment = MeanPayment::CREDIT; | |||||
$order->save(); | |||||
} | |||||
} | |||||
} | |||||
} | |||||
public function sendCreditLimitReminder($userProducer, $creditHistory, $oldCredit) | |||||
{ | |||||
$userRepository = Yii::$app->logic->getUserContainer()->getRepository(); | |||||
$producerRepository = Yii::$app->logic->getProducerContainer()->getRepository(); | |||||
$newCredit = $userProducer->credit; | |||||
if ($this->isCreditLimitCrossed($oldCredit, $newCredit)) { | |||||
$user = $userRepository->getOneById($creditHistory->id_user); | |||||
$producer = $producerRepository->getOneById($creditHistory->id_producer); | |||||
Yii::$app->mailer->compose( | |||||
[ | |||||
'html' => 'creditLimitReminder-html', | |||||
'text' => 'creditLimitReminder-text' | |||||
], | |||||
[ | |||||
'user' => $user, | |||||
'producer' => $producer, | |||||
'credit' => $newCredit | |||||
] | |||||
) | |||||
->setTo($user->email) | |||||
->setFrom(['contact@opendistrib.net' => 'Opendistrib']) | |||||
->setSubject('[Opendistrib] Seuil limite de crédit dépassé') | |||||
->send(); | |||||
} | |||||
} | |||||
public function isCreditLimitCrossed($oldCredit, $newCredit) | |||||
{ | |||||
$creditLimitReminder = Producer::getConfig('credit_limit_reminder'); | |||||
return !is_null($creditLimitReminder) && | |||||
$oldCredit > $creditLimitReminder | |||||
&& $newCredit <= $creditLimitReminder; | |||||
} | |||||
} | } |