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.

преди 3 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. Copyright Guillaume Bourgeois (2018)
  4. contact@souke.fr
  5. Ce logiciel est un programme informatique servant à aider les producteurs
  6. à distribuer leur production en circuits courts.
  7. Ce logiciel est régi par la licence CeCILL soumise au droit français et
  8. respectant les principes de diffusion des logiciels libres. Vous pouvez
  9. utiliser, modifier et/ou redistribuer ce programme sous les conditions
  10. de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  11. sur le site "http://www.cecill.info".
  12. En contrepartie de l'accessibilité au code source et des droits de copie,
  13. de modification et de redistribution accordés par cette licence, il n'est
  14. offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  15. seule une responsabilité restreinte pèse sur l'auteur du programme, le
  16. titulaire des droits patrimoniaux et les concédants successifs.
  17. A cet égard l'attention de l'utilisateur est attirée sur les risques
  18. associés au chargement, à l'utilisation, à la modification et/ou au
  19. développement et à la reproduction du logiciel par l'utilisateur étant
  20. donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  21. manipuler et qui le réserve donc à des développeurs et des professionnels
  22. avertis possédant des connaissances informatiques approfondies. Les
  23. utilisateurs sont donc invités à charger et tester l'adéquation du
  24. logiciel à leurs besoins dans des conditions permettant d'assurer la
  25. sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  26. à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  27. Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  28. pris connaissance de la licence CeCILL, et que vous en avez accepté les
  29. termes.
  30. */
  31. namespace domain\Payment;
  32. use common\helpers\MeanPayment;
  33. use domain\Order\Order\Order;
  34. use domain\Order\Order\OrderSolver;
  35. use domain\Producer\Producer\ProducerSolver;
  36. use domain\User\User\User;
  37. use domain\User\User\UserRepository;
  38. use domain\_\AbstractService;
  39. use domain\_\ManagerInterface;
  40. use yii\base\ErrorException;
  41. class PaymentManager extends AbstractService implements ManagerInterface
  42. {
  43. protected PaymentBuilder $paymentBuilder;
  44. protected OrderSolver $orderSolver;
  45. protected UserRepository $userRepository;
  46. protected ProducerSolver $producerSolver;
  47. public function loadDependencies(): void
  48. {
  49. $this->paymentBuilder = $this->loadService(PaymentBuilder::class);
  50. $this->orderSolver = $this->loadService(OrderSolver::class);
  51. $this->userRepository = $this->loadService(UserRepository::class);
  52. $this->producerSolver = $this->loadService(ProducerSolver::class);
  53. }
  54. public function creditUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void
  55. {
  56. $this->paymentBuilder->createPayment(
  57. Payment::TYPE_CREDIT,
  58. $amount,
  59. $this->getProducerContext(),
  60. $user,
  61. $userAction,
  62. $meanPayment,
  63. $comment,
  64. $dateTransaction
  65. );
  66. }
  67. public function debitUser(User $user, float $amount, string $meanPayment, User $userAction, string $comment = null, string $dateTransaction = null): void
  68. {
  69. $this->paymentBuilder->createPayment(
  70. Payment::TYPE_DEBIT,
  71. $amount,
  72. $this->getProducerContext(),
  73. $user,
  74. $userAction,
  75. $meanPayment,
  76. $comment,
  77. $dateTransaction
  78. );
  79. }
  80. public function creditOrDebitUser(string $type, User $user, float $amount, string $meanPayment, User $userAction, string $comment, string $dateTransaction = null): void
  81. {
  82. if($type == Payment::TYPE_CREDIT) {
  83. $this->creditUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction);
  84. }
  85. elseif($type == Payment::TYPE_DEBIT) {
  86. $this->debitUser($user, $amount, $meanPayment, $userAction, $comment, $dateTransaction);
  87. }
  88. else {
  89. throw new ErrorException('$type a une valeur incorrect');
  90. }
  91. }
  92. public function payOrder(Order $order, string $meanPayment, User $userAction, bool $checkCreditLimit)
  93. {
  94. if($meanPayment == MeanPayment::CREDIT) {
  95. $this->payOrderByCredit($order, $userAction, $checkCreditLimit);
  96. }
  97. elseif(key_exists($meanPayment, MeanPayment::getAll())) {
  98. $this->paymentBuilder->createPayment(
  99. Payment::TYPE_PAYMENT,
  100. $this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING),
  101. $this->getProducerContext(),
  102. $order->user,
  103. $userAction,
  104. $meanPayment,
  105. null,
  106. null,
  107. $order
  108. );
  109. }
  110. else {
  111. throw new ErrorException('Moyen de paiement inconnu : '.$meanPayment);
  112. }
  113. }
  114. public function payOrderByCredit(Order $order, User $userAction, bool $checkCreditLimit): void
  115. {
  116. $amountRemaining = round($this->orderSolver->getOrderAmountWithTax($order, Order::AMOUNT_REMAINING), 2);
  117. if($checkCreditLimit) {
  118. $creditLimit = $this->producerSolver->getConfig('credit_limit');
  119. $creditUser = $this->userRepository->getCredit($order->user);
  120. if (!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
  121. $amountRemaining = $creditUser - $creditLimit;
  122. }
  123. }
  124. if($amountRemaining > 0) {
  125. $this->paymentBuilder->createPayment(
  126. Payment::TYPE_PAYMENT,
  127. $amountRemaining,
  128. $this->getProducerContext(),
  129. $order->user,
  130. $userAction,
  131. MeanPayment::CREDIT,
  132. null,
  133. null,
  134. $order
  135. );
  136. }
  137. }
  138. public function refundOrder(Order $order, string $meanPayment, User $userAction): void
  139. {
  140. $amountPaid = round($this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID), 2);
  141. if ($amountPaid >= 0.01 && $order->id_user) {
  142. $amount = $this->orderSolver->getOrderAmount($order, Order::AMOUNT_PAID);
  143. if($meanPayment == MeanPayment::CREDIT) {
  144. $amount = $this->orderSolver->getOrderAmountPaidByCredit($order);
  145. }
  146. $this->paymentBuilder->createPayment(
  147. Payment::TYPE_REFUND,
  148. $amount,
  149. $this->getProducerContext(),
  150. $order->user,
  151. $userAction,
  152. $meanPayment,
  153. null,
  154. null,
  155. $order
  156. );
  157. }
  158. }
  159. public function refundOrderCredit(Order $order, User $userAction): void
  160. {
  161. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  162. if($amountPaidByCredit >= 0.01 && $order->id_user) {
  163. $this->paymentBuilder->createPayment(
  164. Payment::TYPE_REFUND,
  165. $amountPaidByCredit,
  166. $this->getProducerContext(),
  167. $order->user,
  168. $userAction,
  169. MeanPayment::CREDIT,
  170. null,
  171. null,
  172. $order
  173. );
  174. }
  175. }
  176. public function refundSurplusOrderCredit(Order $order, User $userAction): void
  177. {
  178. $amountPaidByCredit = $this->orderSolver->getOrderAmountPaidByCredit($order);
  179. $amount = $this->orderSolver->getOrderAmountWithTax($order);
  180. if($amountPaidByCredit > $amount) {
  181. $this->paymentBuilder->createPayment(
  182. Payment::TYPE_REFUND,
  183. $amountPaidByCredit - $amount,
  184. $this->getProducerContext(),
  185. $order->user,
  186. $userAction,
  187. MeanPayment::CREDIT,
  188. null,
  189. null,
  190. $order
  191. );
  192. }
  193. }
  194. public function payOrRefundOrder(string $type, Order $order, string $meanPayment, User $userAction, bool $checkCreditLimit = false): void
  195. {
  196. if($type == Payment::TYPE_PAYMENT) {
  197. $this->payOrder($order, $meanPayment, $userAction, $checkCreditLimit);
  198. }
  199. elseif($type == Payment::TYPE_REFUND) {
  200. $this->refundOrder($order, $meanPayment, $userAction);
  201. }
  202. else {
  203. throw new ErrorException('$type a une valeur incorrect');
  204. }
  205. }
  206. }