Просмотр исходного кода

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

feature/souke
Guillaume Bourgeois 1 год назад
Родитель
Сommit
01c2804fdd
5 измененных файлов: 109 добавлений и 32 удалений
  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 Просмотреть файл

@@ -901,14 +901,23 @@ class OrderController extends BackendController
$comment = $request->post('comment');

$order = $orderManager->findOneOrderById($idOrder);
$orderManager->initOrder($order);

$user = $userManager->findOneUserById($idUser);
$pointSale = $pointSaleManager->findOnePointSaleById($idPointSale);

if ($order
&& $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {

// Si changement d'user : on rembourse l'ancien 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) {
$order->username = '';
$order->id_user = $idUser;
@@ -994,22 +1003,6 @@ class OrderController extends BackendController
$order = Order::searchOne(['id' => $order->id]);
$orderManager->initOrder($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);
}
}

+ 73
- 0
common/logic/User/CreditHistory/Service/CreditUtils.php Просмотреть файл

@@ -0,0 +1,73 @@
<?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 Просмотреть файл

@@ -7,6 +7,7 @@ use common\logic\User\CreditHistory\Repository\CreditHistoryRepository;
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder;
use common\logic\User\CreditHistory\Service\CreditHistoryDefinition;
use common\logic\User\CreditHistory\Service\CreditHistorySolver;
use common\logic\User\CreditHistory\Service\CreditUtils;

class CreditHistoryContainer extends AbstractContainer
{
@@ -17,6 +18,7 @@ class CreditHistoryContainer extends AbstractContainer
CreditHistorySolver::class,
CreditHistoryBuilder::class,
CreditHistoryRepository::class,
CreditUtils::class,
];
}

@@ -39,4 +41,9 @@ class CreditHistoryContainer extends AbstractContainer
{
return CreditHistoryRepository::getInstance();
}

public function getCreditUtils(): CreditUtils
{
return CreditUtils::getInstance();
}
}

+ 2
- 0
common/logic/User/CreditHistory/Wrapper/CreditHistoryManager.php Просмотреть файл

@@ -7,12 +7,14 @@ use common\logic\User\CreditHistory\Repository\CreditHistoryRepository;
use common\logic\User\CreditHistory\Service\CreditHistoryBuilder;
use common\logic\User\CreditHistory\Service\CreditHistoryDefinition;
use common\logic\User\CreditHistory\Service\CreditHistorySolver;
use common\logic\User\CreditHistory\Service\CreditService;

/**
* @mixin CreditHistoryDefinition
* @mixin CreditHistorySolver
* @mixin CreditHistoryRepository
* @mixin CreditHistoryBuilder
* @mixin CreditService
*/
class CreditHistoryManager extends AbstractManager
{

+ 17
- 15
common/logic/User/UserProducer/Service/UserProducerBuilder.php Просмотреть файл

@@ -111,21 +111,23 @@ class UserProducerBuilder extends AbstractBuilder
$user = $userRepository->findOneUserById($creditHistory->id_user);
$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();
}
}
}


Загрузка…
Отмена
Сохранить