Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

198 lines
6.8KB

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