Browse Source

[Backend] Crédit : refactoring via CreditUtils

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
f7dc65fb26
7 changed files with 81 additions and 92 deletions
  1. +1
    -9
      backend/controllers/CronController.php
  2. +2
    -10
      backend/controllers/OrderController.php
  3. +4
    -12
      backend/models/CreditForm.php
  4. +66
    -7
      common/logic/User/CreditHistory/Service/CreditUtils.php
  5. +2
    -2
      common/logic/User/CreditHistory/Wrapper/CreditHistoryManager.php
  6. +4
    -17
      producer/controllers/CreditController.php
  7. +2
    -35
      producer/controllers/OrderController.php

+ 1
- 9
backend/controllers/CronController.php View File

$orderManager->initOrder($order); $orderManager->initOrder($order);
if ($order->auto_payment && $configCredit) { if ($order->auto_payment && $configCredit) {
if ($orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING) > 0) { if ($orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING) > 0) {
$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING),
$order->distribution->producer,
$order->user,
$userManager->findOneUserById(User::ID_USER_SYSTEM),
MeanPayment::CREDIT,
$order
);
$creditHistoryManager->payOrder($order, $userManager->findOneUserById(User::ID_USER_SYSTEM), false);
$countOrders++; $countOrders++;
} }
} }

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



$orderManager = $this->getOrderManager(); $orderManager = $this->getOrderManager();
$creditHistoryManager = $this->getCreditHistoryManager(); $creditHistoryManager = $this->getCreditHistoryManager();

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


if ($order) { if ($order) {
$creditHistoryManager->createCreditHistory(
$type,
$amount,
GlobalParam::getCurrentProducer(),
$order->user,
GlobalParam::getCurrentUser(),
MeanPayment::CREDIT,
$order
);
$creditHistoryManager->payOrRefundOrder($type, $order, $this->getUserCurrent());
} }


return ['success']; return ['success'];

+ 4
- 12
backend/models/CreditForm.php View File

use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\User\Wrapper\UserManager; use common\logic\User\User\Wrapper\UserManager;
use common\logic\User\UserProducer\Model\UserProducer; use common\logic\User\UserProducer\Model\UserProducer;
use common\logic\User\UserProducer\Wrapper\UserProducerManager;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;


public function save() public function save()
{ {
$userManager = UserManager::getInstance(); $userManager = UserManager::getInstance();
$userProducerManager = UserProducerManager::getInstance();
$creditHistoryManager = CreditHistoryManager::getInstance(); $creditHistoryManager = CreditHistoryManager::getInstance();
$producerManager = ProducerManager::getInstance(); $producerManager = ProducerManager::getInstance();


if ($this->validate()) { if ($this->validate()) {
$user = $userManager->findOneUserById($this->id_user); $user = $userManager->findOneUserById($this->id_user);
$creditHistoryManager->createCreditHistory(
$this->type,
$this->amount,
GlobalParam::getCurrentProducer(),
$user,
Yii::$app->user->identity,
$this->mean_payment
);
$creditHistoryManager->creditOrDebitUser($this->type, $user, $this->amount, $this->mean_payment, $userManager->getCurrent());
// on prévient l'utilisateur que son compte vient d'être crédité // on prévient l'utilisateur que son compte vient d'être crédité
if($this->send_mail) { if($this->send_mail) {
$user = User::findOne($this->id_user) ;
$producer = GlobalParam::getCurrentProducer() ; $producer = GlobalParam::getCurrentProducer() ;
$userProducer = UserProducer::searchOne([
'id_user' => $this->id_user
]);
$userProducer = $userProducerManager->findOneUserProducer($user);


$paramsEmail = [ $paramsEmail = [
'from_email' => $producerManager->getEmailOpendistrib($producer), 'from_email' => $producerManager->getEmailOpendistrib($producer),

+ 66
- 7
common/logic/User/CreditHistory/Service/CreditUtils.php View File

use common\logic\AbstractService; use common\logic\AbstractService;
use common\logic\Order\Order\Model\Order; use common\logic\Order\Order\Model\Order;
use common\logic\Order\Order\Service\OrderSolver; use common\logic\Order\Order\Service\OrderSolver;
use common\logic\Producer\Producer\Repository\ProducerRepository;
use common\logic\User\CreditHistory\Model\CreditHistory; use common\logic\User\CreditHistory\Model\CreditHistory;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use common\logic\User\User\Repository\UserRepository;
use common\logic\UtilsInterface; use common\logic\UtilsInterface;
use yii\base\ErrorException;


class CreditUtils extends AbstractService implements UtilsInterface class CreditUtils extends AbstractService implements UtilsInterface
{ {
protected CreditHistoryBuilder $creditHistoryBuilder; protected CreditHistoryBuilder $creditHistoryBuilder;
protected OrderSolver $orderSolver; protected OrderSolver $orderSolver;
protected ProducerRepository $producerRepository;
protected UserRepository $userRepository;


public function loadDependencies(): void public function loadDependencies(): void
{ {
$this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class); $this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class);
$this->orderSolver = $this->loadService(OrderSolver::class); $this->orderSolver = $this->loadService(OrderSolver::class);
$this->producerRepository = $this->loadService(ProducerRepository::class);
$this->userRepository = $this->loadService(UserRepository::class);
} }


public function creditCredit(float $amount, User $user, User $userAction, string $meanPayment): void
public function creditUser(User $user, float $amount, string $meanPayment, User $userAction): void
{ {
$this->creditHistoryBuilder->createCreditHistory( $this->creditHistoryBuilder->createCreditHistory(
CreditHistory::TYPE_CREDIT, CreditHistory::TYPE_CREDIT,
); );
} }


public function debitCredit(float $amount, User $user, User $userAction, string $meanPayment): void
public function debitUser(User $user, float $amount, string $meanPayment, User $userAction): void
{ {
$this->creditHistoryBuilder->createCreditHistory( $this->creditHistoryBuilder->createCreditHistory(
CreditHistory::TYPE_DEBIT, CreditHistory::TYPE_DEBIT,
); );
} }


public function payOrder(Order $order, User $userAction): void
public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction): void
{
if($type == CreditHistory::TYPE_CREDIT) {
$this->creditUser($user, $amount, $meanPayment, $userAction);
}
elseif($type == CreditHistory::TYPE_DEBIT) {
$this->debitUser($user, $amount, $meanPayment, $userAction);
}
else {
throw new ErrorException('$type a une valeur incorrect');
}
}

public function payOrder(Order $order, User $userAction, bool $checkCreditLimit): void
{
$amountRemaining = $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING);

if($checkCreditLimit) {
$creditLimit = $this->producerRepository->getConfig('credit_limit');
$creditUser = $this->userRepository->getCredit($order->user);

if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
$amountRemaining = $creditUser - $creditLimit;
}
}

if($amountRemaining > 0) {
$this->creditHistoryBuilder->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$amountRemaining,
$this->getProducerContext(),
$order->user,
$userAction,
MeanPayment::CREDIT,
$order
);
}
}

public function refundOrder(Order $order, User $userAction): void
{ {
$this->creditHistoryBuilder->createCreditHistory( $this->creditHistoryBuilder->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$this->orderSolver->getOrderAmountWithTax($order),
CreditHistory::TYPE_REFUND,
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID),
$this->getProducerContext(), $this->getProducerContext(),
$order->user, $order->user,
$userAction, $userAction,
); );
} }


public function refundOrder(Order $order, User $userAction): void
public function refundSurplusOrder(Order $order, User $userAction): void
{ {
$this->creditHistoryBuilder->createCreditHistory( $this->creditHistoryBuilder->createCreditHistory(
CreditHistory::TYPE_REFUND, CreditHistory::TYPE_REFUND,
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID),
$this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_SURPLUS),
$this->getProducerContext(), $this->getProducerContext(),
$order->user, $order->user,
$userAction, $userAction,
$order $order
); );
} }

public function payOrRefundOrder(string $type, Order $order, User $userAction, bool $checkCreditLimit = false): void
{
if($type == CreditHistory::TYPE_PAYMENT) {
$this->payOrder($order, $userAction, $checkCreditLimit);
}
elseif($type == CreditHistory::TYPE_REFUND) {
$this->refundOrder($order, $userAction);
}
else {
throw new ErrorException('$type a une valeur incorrect');
}
}
} }

+ 2
- 2
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;
use common\logic\User\CreditHistory\Service\CreditUtils;


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

+ 4
- 17
producer/controllers/CreditController.php View File



public function actionStripeVerification() public function actionStripeVerification()
{ {
$orderManager = $this->getOrderManager();
$creditHistoryManager = $this->getCreditHistoryManager(); $creditHistoryManager = $this->getCreditHistoryManager();
$producerManager = $this->getProducerManager(); $producerManager = $this->getProducerManager();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();


if (isset($paymentIntentMetadata->order_id)) { if (isset($paymentIntentMetadata->order_id)) {
$order = $this->getOrderManager()->findOneOrderById($paymentIntentMetadata->order_id); $order = $this->getOrderManager()->findOneOrderById($paymentIntentMetadata->order_id);
$orderManager->initOrder($order);
$pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale); $pointSale = $this->getPointSaleManager()->findOnePointSaleById($order->id_point_sale);
$distribution = $this->getDistributionManager()->findOneDistributionById($order->id_distribution); $distribution = $this->getDistributionManager()->findOneDistributionById($order->id_distribution);
} }


if (!$creditHistoryExist) { if (!$creditHistoryExist) {


$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_CREDIT,
$amount,
$producer,
$user,
$user,
MeanPayment::CREDIT_CARD
);
$creditHistoryManager->creditUser($user, $amount, MeanPayment::CREDIT_CARD, $user);


if (isset($order) && $order) { if (isset($order) && $order) {


$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$amount,
$producer,
$user,
$user,
MeanPayment::CREDIT,
$order
);
$creditHistoryManager->payOrder($order, $user, true);


// client : envoi d'un email de confirmation de paiement // client : envoi d'un email de confirmation de paiement
$paramsEmail = [ $paramsEmail = [

+ 2
- 35
producer/controllers/OrderController.php View File



// credit // credit
$credit = $producerManager->getConfig('credit'); $credit = $producerManager->getConfig('credit');
$creditLimit = $producerManager->getConfig('credit_limit');
$creditFunctioning = $producerManager->getPointSaleCreditFunctioning($pointSale); $creditFunctioning = $producerManager->getPointSaleCreditFunctioning($pointSale);
$creditUser = $userManager->getCredit($user);
$order = $orderManager->findOneOrderById($order->id); $order = $orderManager->findOneOrderById($order->id);
$orderManager->initOrder($order); $orderManager->initOrder($order);
$amountRemaining = $orderManager->getOrderAmount($order, Order::AMOUNT_REMAINING);


if ($credit && $pointSale->credit && if ($credit && $pointSale->credit &&
(($creditFunctioning == Producer::CREDIT_FUNCTIONING_OPTIONAL && $posts['use_credit']) || (($creditFunctioning == Producer::CREDIT_FUNCTIONING_OPTIONAL && $posts['use_credit']) ||
$creditFunctioning == Producer::CREDIT_FUNCTIONING_MANDATORY || $creditFunctioning == Producer::CREDIT_FUNCTIONING_MANDATORY ||
($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER && $userProducer->credit_active) ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER && $userProducer->credit_active)
)) { )) {
$orderManager->updateOrderStatus($order, 'waiting-paiement-by-credit', 'user');

// à payer // à payer
if ($orderManager->getPaymentStatus($order) == Order::PAYMENT_UNPAID) { if ($orderManager->getPaymentStatus($order) == Order::PAYMENT_UNPAID) {
if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
$amountRemaining = $creditUser - $creditLimit;
}

if ($amountRemaining > 0) {
$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_PAYMENT,
$amountRemaining,
$producer,
GlobalParam::getCurrentUser(),
GlobalParam::getCurrentUser(),
MeanPayment::CREDIT,
$order
);
$orderManager->updateOrderStatus($order, 'paid-by-credit', 'user');
} else {
$orderManager->updateOrderStatus($order, 'waiting-paiement-on-delivery', 'user');
}
$creditHistoryManager->payOrder($order, $this->getUserCurrent(), true);
} // surplus à rembourser } // surplus à rembourser
elseif ($orderManager->getPaymentStatus($order) == Order::PAYMENT_SURPLUS) { elseif ($orderManager->getPaymentStatus($order) == Order::PAYMENT_SURPLUS) {
$amountSurplus = $orderManager->getOrderAmount($order, Order::AMOUNT_SURPLUS);
$creditHistoryManager->createCreditHistory(
CreditHistory::TYPE_REFUND,
$amountSurplus,
$producer,
GlobalParam::getCurrentUser(),
GlobalParam::getCurrentUser(),
MeanPayment::CREDIT,
$order
);
$creditHistoryManager->refundSurplusOrder($order, $this->getUserCurrent());
} }
} else {
$orderManager->updateOrderStatus($order, 'waiting-paiement-on-delivery', 'user');
} }


$paramsEmail = [ $paramsEmail = [

Loading…
Cancel
Save