Browse Source

[Backend] Crédit : rembourser client lorsqu'on attribue une commande à un autre client #1151

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
01c2804fdd
5 changed files with 109 additions and 32 deletions
  1. +10
    -17
      backend/controllers/OrderController.php
  2. +73
    -0
      common/logic/User/CreditHistory/Service/CreditUtils.php
  3. +7
    -0
      common/logic/User/CreditHistory/Wrapper/CreditHistoryContainer.php
  4. +2
    -0
      common/logic/User/CreditHistory/Wrapper/CreditHistoryManager.php
  5. +17
    -15
      common/logic/User/UserProducer/Service/UserProducerBuilder.php

+ 10
- 17
backend/controllers/OrderController.php View File

$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);
} }
} }

+ 73
- 0
common/logic/User/CreditHistory/Service/CreditUtils.php View File

<?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
);
}
}

+ 7
- 0
common/logic/User/CreditHistory/Wrapper/CreditHistoryContainer.php View File

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();
}
} }

+ 2
- 0
common/logic/User/CreditHistory/Wrapper/CreditHistoryManager.php View File

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
{ {

+ 17
- 15
common/logic/User/UserProducer/Service/UserProducerBuilder.php View File

$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();
}
} }
} }



Loading…
Cancel
Save