$comment = $request->post('comment'); | $comment = $request->post('comment'); | ||||
$order = $orderManager->findOneOrderById($idOrder); | $order = $orderManager->findOneOrderById($idOrder); | ||||
$orderManager->initOrder($order); | |||||
$user = $userManager->findOneUserById($idUser); | $user = $userManager->findOneUserById($idUser); | ||||
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale); | $pointSale = $pointSaleManager->findOnePointSaleById($idPointSale); | ||||
if ($order | if ($order | ||||
&& $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) { | && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) { | ||||
// Si changement d'user : on rembourse l'ancien user | |||||
$oldIdUser = $order->id_user; | $oldIdUser = $order->id_user; | ||||
$oldUser = $userManager->findOneUserById($oldIdUser); | |||||
$amountPaid = $orderManager->getOrderAmountWithTax($order, Order::AMOUNT_PAID); | |||||
if ($oldIdUser != $idUser && $amountPaid > 0) { | |||||
$creditHistoryManager->refundOrder($order, $this->getUserCurrent()); | |||||
$order = $orderManager->findOneOrderById($idOrder); | |||||
$orderManager->initOrder($order); | |||||
} | |||||
if ($idUser) { | if ($idUser) { | ||||
$order->username = ''; | $order->username = ''; | ||||
$order->id_user = $idUser; | $order->id_user = $idUser; | ||||
$order = Order::searchOne(['id' => $order->id]); | $order = Order::searchOne(['id' => $order->id]); | ||||
$orderManager->initOrder($order); | $orderManager->initOrder($order); | ||||
if ($order && $orderManager->isCreditAutoPayment($order)) { | if ($order && $orderManager->isCreditAutoPayment($order)) { | ||||
// Si changement d'user : on rembourse l'ancien user | |||||
$amountPaid = $orderManager->getOrderAmount($order, Order::AMOUNT_PAID); | |||||
if ($oldIdUser != $idUser && $amountPaid > 0) { | |||||
$creditHistoryManager->createCreditHistory( | |||||
CreditHistory::TYPE_REFUND, | |||||
$amountPaid, | |||||
$this->getProducerCurrent(), | |||||
$oldUser, | |||||
$this->getUserCurrent(), | |||||
MeanPayment::CREDIT, | |||||
$order | |||||
); | |||||
$order = Order::searchOne(['id' => $order->id]); | |||||
$orderManager->initOrder($order); | |||||
} | |||||
$orderManager->processCredit($order); | $orderManager->processCredit($order); | ||||
} | } | ||||
} | } |
<?php | |||||
namespace common\logic\User\CreditHistory\Service; | |||||
use common\helpers\MeanPayment; | |||||
use common\logic\AbstractService; | |||||
use common\logic\Order\Order\Model\Order; | |||||
use common\logic\Order\Order\Service\OrderSolver; | |||||
use common\logic\User\CreditHistory\Model\CreditHistory; | |||||
use common\logic\User\User\Model\User; | |||||
use common\logic\UtilsInterface; | |||||
class CreditUtils extends AbstractService implements UtilsInterface | |||||
{ | |||||
protected CreditHistoryBuilder $creditHistoryBuilder; | |||||
protected OrderSolver $orderSolver; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class); | |||||
$this->orderSolver = $this->loadService(OrderSolver::class); | |||||
} | |||||
public function creditCredit(float $amount, User $user, User $userAction, string $meanPayment): void | |||||
{ | |||||
$this->creditHistoryBuilder->createCreditHistory( | |||||
CreditHistory::TYPE_CREDIT, | |||||
$amount, | |||||
$this->getProducerContext(), | |||||
$user, | |||||
$userAction, | |||||
$meanPayment | |||||
); | |||||
} | |||||
public function debitCredit(float $amount, User $user, User $userAction, string $meanPayment): void | |||||
{ | |||||
$this->creditHistoryBuilder->createCreditHistory( | |||||
CreditHistory::TYPE_DEBIT, | |||||
$amount, | |||||
$this->getProducerContext(), | |||||
$user, | |||||
$userAction, | |||||
$meanPayment | |||||
); | |||||
} | |||||
public function payOrder(Order $order, User $userAction): void | |||||
{ | |||||
$this->creditHistoryBuilder->createCreditHistory( | |||||
CreditHistory::TYPE_PAYMENT, | |||||
$this->orderSolver->getOrderAmountWithTax($order), | |||||
$this->getProducerContext(), | |||||
$order->user, | |||||
$userAction, | |||||
MeanPayment::CREDIT, | |||||
$order | |||||
); | |||||
} | |||||
public function refundOrder(Order $order, User $userAction): void | |||||
{ | |||||
$this->creditHistoryBuilder->createCreditHistory( | |||||
CreditHistory::TYPE_REFUND, | |||||
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID), | |||||
$this->getProducerContext(), | |||||
$order->user, | |||||
$userAction, | |||||
MeanPayment::CREDIT, | |||||
$order | |||||
); | |||||
} | |||||
} |
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | ||||
use common\logic\User\CreditHistory\Service\CreditHistoryDefinition; | use common\logic\User\CreditHistory\Service\CreditHistoryDefinition; | ||||
use common\logic\User\CreditHistory\Service\CreditHistorySolver; | use common\logic\User\CreditHistory\Service\CreditHistorySolver; | ||||
use common\logic\User\CreditHistory\Service\CreditUtils; | |||||
class CreditHistoryContainer extends AbstractContainer | class CreditHistoryContainer extends AbstractContainer | ||||
{ | { | ||||
CreditHistorySolver::class, | CreditHistorySolver::class, | ||||
CreditHistoryBuilder::class, | CreditHistoryBuilder::class, | ||||
CreditHistoryRepository::class, | CreditHistoryRepository::class, | ||||
CreditUtils::class, | |||||
]; | ]; | ||||
} | } | ||||
{ | { | ||||
return CreditHistoryRepository::getInstance(); | return CreditHistoryRepository::getInstance(); | ||||
} | } | ||||
public function getCreditUtils(): CreditUtils | |||||
{ | |||||
return CreditUtils::getInstance(); | |||||
} | |||||
} | } |
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | use common\logic\User\CreditHistory\Service\CreditHistoryBuilder; | ||||
use common\logic\User\CreditHistory\Service\CreditHistoryDefinition; | use common\logic\User\CreditHistory\Service\CreditHistoryDefinition; | ||||
use common\logic\User\CreditHistory\Service\CreditHistorySolver; | use common\logic\User\CreditHistory\Service\CreditHistorySolver; | ||||
use common\logic\User\CreditHistory\Service\CreditService; | |||||
/** | /** | ||||
* @mixin CreditHistoryDefinition | * @mixin CreditHistoryDefinition | ||||
* @mixin CreditHistorySolver | * @mixin CreditHistorySolver | ||||
* @mixin CreditHistoryRepository | * @mixin CreditHistoryRepository | ||||
* @mixin CreditHistoryBuilder | * @mixin CreditHistoryBuilder | ||||
* @mixin CreditService | |||||
*/ | */ | ||||
class CreditHistoryManager extends AbstractManager | class CreditHistoryManager extends AbstractManager | ||||
{ | { |
$user = $userRepository->findOneUserById($creditHistory->id_user); | $user = $userRepository->findOneUserById($creditHistory->id_user); | ||||
$producer = $producerRepository->findOneProducerById($creditHistory->id_producer); | $producer = $producerRepository->findOneProducerById($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(); | |||||
if($user && $user->email && strlen($user->email) > 0) { | |||||
\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(); | |||||
} | |||||
} | } | ||||
} | } | ||||