|
- <?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\Producer\Producer\Repository\ProducerRepository;
- use common\logic\User\CreditHistory\Model\CreditHistory;
- use common\logic\User\User\Model\User;
- use common\logic\User\User\Repository\UserRepository;
- use common\logic\UtilsInterface;
- use yii\base\ErrorException;
-
- class CreditUtils extends AbstractService implements UtilsInterface
- {
- protected CreditHistoryBuilder $creditHistoryBuilder;
- protected OrderSolver $orderSolver;
- protected ProducerRepository $producerRepository;
- protected UserRepository $userRepository;
-
- public function loadDependencies(): void
- {
- $this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class);
- $this->orderSolver = $this->loadService(OrderSolver::class);
- $this->producerRepository = $this->loadService(ProducerRepository::class);
- $this->userRepository = $this->loadService(UserRepository::class);
- }
-
- public function creditUser(User $user, float $amount, string $meanPayment, User $userAction): void
- {
- $this->creditHistoryBuilder->createCreditHistory(
- CreditHistory::TYPE_CREDIT,
- $amount,
- $this->getProducerContext(),
- $user,
- $userAction,
- $meanPayment
- );
- }
-
- public function debitUser(User $user, float $amount, string $meanPayment, User $userAction): void
- {
- $this->creditHistoryBuilder->createCreditHistory(
- CreditHistory::TYPE_DEBIT,
- $amount,
- $this->getProducerContext(),
- $user,
- $userAction,
- $meanPayment
- );
- }
-
- 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(
- CreditHistory::TYPE_REFUND,
- $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID),
- $this->getProducerContext(),
- $order->user,
- $userAction,
- MeanPayment::CREDIT,
- $order
- );
- }
-
- public function refundSurplusOrder(Order $order, User $userAction): void
- {
- $this->creditHistoryBuilder->createCreditHistory(
- CreditHistory::TYPE_REFUND,
- $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_SURPLUS),
- $this->getProducerContext(),
- $order->user,
- $userAction,
- MeanPayment::CREDIT,
- $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');
- }
- }
- }
|