Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace domain\Payment;
  3. use common\helpers\MeanPayment;
  4. use domain\Order\Order\Order;
  5. use domain\Order\Order\OrderSolver;
  6. use domain\Producer\Producer\ProducerSolver;
  7. use domain\User\User\User;
  8. use domain\User\User\UserRepository;
  9. use domain\_\AbstractService;
  10. use domain\_\ManagerInterface;
  11. use yii\base\ErrorException;
  12. class PaymentManager extends AbstractService implements ManagerInterface
  13. {
  14. protected PaymentBuilder $paymentBuilder;
  15. protected OrderSolver $orderSolver;
  16. protected UserRepository $userRepository;
  17. protected ProducerSolver $producerSolver;
  18. public function loadDependencies(): void
  19. {
  20. $this->paymentBuilder = $this->loadService(PaymentBuilder::class);
  21. $this->orderSolver = $this->loadService(OrderSolver::class);
  22. $this->userRepository = $this->loadService(UserRepository::class);
  23. $this->producerSolver = $this->loadService(ProducerSolver::class);
  24. }
  25. public function creditUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void
  26. {
  27. $this->paymentBuilder->createPayment(
  28. Payment::TYPE_CREDIT,
  29. $amount,
  30. $this->getProducerContext(),
  31. $user,
  32. $userAction,
  33. $meanPayment,
  34. $comment,
  35. $dateTransaction
  36. );
  37. }
  38. public function debitUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void
  39. {
  40. $this->paymentBuilder->createPayment(
  41. Payment::TYPE_DEBIT,
  42. $amount,
  43. $this->getProducerContext(),
  44. $user,
  45. $userAction,
  46. $meanPayment,
  47. $comment,
  48. $dateTransaction
  49. );
  50. }
  51. public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction, string $comment, string $dateTransaction = null): void
  52. {
  53. if($type == Payment::TYPE_CREDIT) {
  54. $this->creditUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction);
  55. }
  56. elseif($type == Payment::TYPE_DEBIT) {
  57. $this->debitUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction);
  58. }
  59. else {
  60. throw new ErrorException('$type a une valeur incorrect');
  61. }
  62. }
  63. public function payOrder(Order $order, string $meanPayment, User $userAction, bool $checkCreditLimit)
  64. {
  65. if($meanPayment == MeanPayment::CREDIT) {
  66. $this->payOrderByCredit($order, $userAction, $checkCreditLimit);
  67. }
  68. elseif(key_exists($meanPayment, MeanPayment::getAll())) {
  69. $this->paymentBuilder->createPayment(
  70. Payment::TYPE_PAYMENT,
  71. $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING),
  72. $this->getProducerContext(),
  73. $order->user,
  74. $userAction,
  75. $meanPayment,
  76. null,
  77. null,
  78. $order
  79. );
  80. }
  81. else {
  82. throw new ErrorException('Moyen de paiement inconnu : '.$meanPayment);
  83. }
  84. }
  85. public function payOrderByCredit(Order $order, User $userAction, bool $checkCreditLimit): void
  86. {
  87. $amountRemaining = round($this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING), 2);
  88. if($checkCreditLimit) {
  89. $creditLimit = $this->producerSolver->getConfig('credit_limit');
  90. $creditUser = $this->userRepository->getCredit($order->user);
  91. if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
  92. $amountRemaining = $creditUser - $creditLimit;
  93. }
  94. }
  95. if($amountRemaining > 0) {
  96. $this->paymentBuilder->createPayment(
  97. Payment::TYPE_PAYMENT,
  98. $amountRemaining,
  99. $this->getProducerContext(),
  100. $order->user,
  101. $userAction,
  102. MeanPayment::CREDIT,
  103. null,
  104. null,
  105. $order
  106. );
  107. }
  108. }
  109. public function refundOrder(Order $order, string $meanPayment, User $userAction): void
  110. {
  111. $amountPaid = round($this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID), 2);
  112. if ($amountPaid >= 0.01 && $order->id_user) {
  113. $amount = $this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID);
  114. if($meanPayment == MeanPayment::CREDIT) {
  115. $amount = $this->orderSolver->getOrderAmountPaidByCredit($order);
  116. }
  117. $this->paymentBuilder->createPayment(
  118. Payment::TYPE_REFUND,
  119. $amount,
  120. $this->getProducerContext(),
  121. $order->user,
  122. $userAction,
  123. $meanPayment,
  124. null,
  125. null,
  126. $order
  127. );
  128. }
  129. }
  130. public function refundOrderCredit(Order $order, User $userAction): void
  131. {
  132. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  133. if($amountPaidByCredit >= 0.01 && $order->id_user) {
  134. $this->paymentBuilder->createPayment(
  135. Payment::TYPE_REFUND,
  136. $amountPaidByCredit,
  137. $this->getProducerContext(),
  138. $order->user,
  139. $userAction,
  140. MeanPayment::CREDIT,
  141. null,
  142. null,
  143. $order
  144. );
  145. }
  146. }
  147. public function refundSurplusOrderCredit(Order $order, User $userAction): void
  148. {
  149. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  150. $amount = $this->orderSolver->getOrderAmountWithTax($order);
  151. if($amountPaidByCredit > $amount) {
  152. $this->paymentBuilder->createPayment(
  153. Payment::TYPE_REFUND,
  154. $amountPaidByCredit - $amount,
  155. $this->getProducerContext(),
  156. $order->user,
  157. $userAction,
  158. MeanPayment::CREDIT,
  159. null,
  160. null,
  161. $order
  162. );
  163. }
  164. }
  165. public function payOrRefundOrder(string $type, Order $order, string $meanPayment, User $userAction, bool $checkCreditLimit = false): void
  166. {
  167. if($type == Payment::TYPE_PAYMENT) {
  168. $this->payOrder($order, $meanPayment, $userAction, $checkCreditLimit);
  169. }
  170. elseif($type == Payment::TYPE_REFUND) {
  171. $this->refundOrder($order, $meanPayment, $userAction);
  172. }
  173. else {
  174. throw new ErrorException('$type a une valeur incorrect');
  175. }
  176. }
  177. }