You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.5KB

  1. <?php
  2. namespace common\logic\User\CreditHistory\Service;
  3. use common\helpers\MeanPayment;
  4. use common\logic\AbstractService;
  5. use common\logic\Order\Order\Model\Order;
  6. use common\logic\Order\Order\Service\OrderSolver;
  7. use common\logic\Producer\Producer\Repository\ProducerRepository;
  8. use common\logic\User\CreditHistory\Model\CreditHistory;
  9. use common\logic\User\User\Model\User;
  10. use common\logic\User\User\Repository\UserRepository;
  11. use common\logic\UtilsInterface;
  12. use yii\base\ErrorException;
  13. class CreditUtils extends AbstractService implements UtilsInterface
  14. {
  15. protected CreditHistoryBuilder $creditHistoryBuilder;
  16. protected OrderSolver $orderSolver;
  17. protected ProducerRepository $producerRepository;
  18. protected UserRepository $userRepository;
  19. public function loadDependencies(): void
  20. {
  21. $this->creditHistoryBuilder = $this->loadService(CreditHistoryBuilder::class);
  22. $this->orderSolver = $this->loadService(OrderSolver::class);
  23. $this->producerRepository = $this->loadService(ProducerRepository::class);
  24. $this->userRepository = $this->loadService(UserRepository::class);
  25. }
  26. public function creditUser(User $user, float $amount, string $meanPayment, User $userAction): void
  27. {
  28. $this->creditHistoryBuilder->createCreditHistory(
  29. CreditHistory::TYPE_CREDIT,
  30. $amount,
  31. $this->getProducerContext(),
  32. $user,
  33. $userAction,
  34. $meanPayment
  35. );
  36. }
  37. public function debitUser(User $user, float $amount, string $meanPayment, User $userAction): void
  38. {
  39. $this->creditHistoryBuilder->createCreditHistory(
  40. CreditHistory::TYPE_DEBIT,
  41. $amount,
  42. $this->getProducerContext(),
  43. $user,
  44. $userAction,
  45. $meanPayment
  46. );
  47. }
  48. public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction): void
  49. {
  50. if($type == CreditHistory::TYPE_CREDIT) {
  51. $this->creditUser($user, $amount, $meanPayment, $userAction);
  52. }
  53. elseif($type == CreditHistory::TYPE_DEBIT) {
  54. $this->debitUser($user, $amount, $meanPayment, $userAction);
  55. }
  56. else {
  57. throw new ErrorException('$type a une valeur incorrect');
  58. }
  59. }
  60. public function payOrder(Order $order, User $userAction, bool $checkCreditLimit): void
  61. {
  62. $amountRemaining = $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING);
  63. if($checkCreditLimit) {
  64. $creditLimit = $this->producerRepository->getConfig('credit_limit');
  65. $creditUser = $this->userRepository->getCredit($order->user);
  66. if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
  67. $amountRemaining = $creditUser - $creditLimit;
  68. }
  69. }
  70. if($amountRemaining > 0) {
  71. $this->creditHistoryBuilder->createCreditHistory(
  72. CreditHistory::TYPE_PAYMENT,
  73. $amountRemaining,
  74. $this->getProducerContext(),
  75. $order->user,
  76. $userAction,
  77. MeanPayment::CREDIT,
  78. $order
  79. );
  80. }
  81. }
  82. public function refundOrder(Order $order, User $userAction): void
  83. {
  84. $this->creditHistoryBuilder->createCreditHistory(
  85. CreditHistory::TYPE_REFUND,
  86. $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_PAID),
  87. $this->getProducerContext(),
  88. $order->user,
  89. $userAction,
  90. MeanPayment::CREDIT,
  91. $order
  92. );
  93. }
  94. public function refundSurplusOrder(Order $order, User $userAction): void
  95. {
  96. $this->creditHistoryBuilder->createCreditHistory(
  97. CreditHistory::TYPE_REFUND,
  98. $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_SURPLUS),
  99. $this->getProducerContext(),
  100. $order->user,
  101. $userAction,
  102. MeanPayment::CREDIT,
  103. $order
  104. );
  105. }
  106. public function payOrRefundOrder(string $type, Order $order, User $userAction, bool $checkCreditLimit = false): void
  107. {
  108. if($type == CreditHistory::TYPE_PAYMENT) {
  109. $this->payOrder($order, $userAction, $checkCreditLimit);
  110. }
  111. elseif($type == CreditHistory::TYPE_REFUND) {
  112. $this->refundOrder($order, $userAction);
  113. }
  114. else {
  115. throw new ErrorException('$type a une valeur incorrect');
  116. }
  117. }
  118. }