No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

178 líneas
6.1KB

  1. <?php
  2. namespace common\logic\Payment\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\Payment\Model\Payment;
  8. use common\logic\Producer\Producer\Repository\ProducerRepository;
  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 PaymentUtils extends AbstractService implements UtilsInterface
  14. {
  15. protected PaymentBuilder $paymentBuilder;
  16. protected OrderSolver $orderSolver;
  17. protected ProducerRepository $producerRepository;
  18. protected UserRepository $userRepository;
  19. public function loadDependencies(): void
  20. {
  21. $this->paymentBuilder = $this->loadService(PaymentBuilder::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->paymentBuilder->createPayment(
  29. Payment::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->paymentBuilder->createPayment(
  40. Payment::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 == Payment::TYPE_CREDIT) {
  51. $this->creditUser($user, $amount, $meanPayment, $userAction);
  52. }
  53. elseif($type == Payment::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, string $meanPayment, User $userAction, bool $checkCreditLimit)
  61. {
  62. if($meanPayment == MeanPayment::CREDIT) {
  63. $this->payOrderByCredit($order, $userAction, $checkCreditLimit);
  64. }
  65. elseif(key_exists($meanPayment, MeanPayment::getAll())) {
  66. $this->paymentBuilder->createPayment(
  67. Payment::TYPE_PAYMENT,
  68. $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING),
  69. $this->getProducerContext(),
  70. $order->user,
  71. $userAction,
  72. $meanPayment,
  73. $order
  74. );
  75. }
  76. else {
  77. throw new ErrorException('Moyen de paiement inconnu : '.$meanPayment);
  78. }
  79. }
  80. public function payOrderByCredit(Order $order, User $userAction, bool $checkCreditLimit): void
  81. {
  82. $amountRemaining = round($this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING), 2);
  83. if($checkCreditLimit) {
  84. $creditLimit = $this->producerRepository->getConfig('credit_limit');
  85. $creditUser = $this->userRepository->getCredit($order->user);
  86. if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
  87. $amountRemaining = $creditUser - $creditLimit;
  88. }
  89. }
  90. if($amountRemaining > 0) {
  91. $this->paymentBuilder->createPayment(
  92. Payment::TYPE_PAYMENT,
  93. $amountRemaining,
  94. $this->getProducerContext(),
  95. $order->user,
  96. $userAction,
  97. MeanPayment::CREDIT,
  98. $order
  99. );
  100. }
  101. }
  102. public function refundOrder(Order $order, string $meanPayment, User $userAction): void
  103. {
  104. $amountPaid = round($this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID), 2);
  105. if ($amountPaid >= 0.01 && $order->id_user) {
  106. $this->paymentBuilder->createPayment(
  107. Payment::TYPE_REFUND,
  108. $this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID),
  109. $this->getProducerContext(),
  110. $order->user,
  111. $userAction,
  112. $meanPayment,
  113. $order
  114. );
  115. }
  116. }
  117. public function refundOrderCredit(Order $order, User $userAction): void
  118. {
  119. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  120. if($amountPaidByCredit >= 0.01 && $order->id_user) {
  121. $this->paymentBuilder->createPayment(
  122. Payment::TYPE_REFUND,
  123. $amountPaidByCredit,
  124. $this->getProducerContext(),
  125. $order->user,
  126. $userAction,
  127. MeanPayment::CREDIT,
  128. $order
  129. );
  130. }
  131. }
  132. public function refundSurplusOrderCredit(Order $order, User $userAction): void
  133. {
  134. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  135. $amount = $this->orderSolver->getOrderAmountWithTax($order);
  136. if($amountPaidByCredit > $amount) {
  137. $this->paymentBuilder->createPayment(
  138. Payment::TYPE_REFUND,
  139. $amountPaidByCredit - $amount,
  140. $this->getProducerContext(),
  141. $order->user,
  142. $userAction,
  143. MeanPayment::CREDIT,
  144. $order
  145. );
  146. }
  147. }
  148. public function payOrRefundOrder(string $type, Order $order, string $meanPayment, User $userAction, bool $checkCreditLimit = false): void
  149. {
  150. if($type == Payment::TYPE_PAYMENT) {
  151. $this->payOrder($order, $meanPayment, $userAction, $checkCreditLimit);
  152. }
  153. elseif($type == Payment::TYPE_REFUND) {
  154. $this->refundOrder($order, $meanPayment, $userAction);
  155. }
  156. else {
  157. throw new ErrorException('$type a une valeur incorrect');
  158. }
  159. }
  160. }