Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

139 linhas
3.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\Payment\Model\Payment;
  7. use common\logic\SolverInterface;
  8. class PaymentSolver extends AbstractService implements SolverInterface
  9. {
  10. public function isTypeDebit(Payment $payment): bool
  11. {
  12. return in_array($payment->getType(), [
  13. Payment::TYPE_DEBIT,
  14. Payment::TYPE_PAYMENT,
  15. ]);
  16. }
  17. public function isTypeCredit(Payment $payment): bool
  18. {
  19. return in_array($payment->getType(), [
  20. Payment::TYPE_CREDIT,
  21. Payment::TYPE_INITIAL_CREDIT,
  22. Payment::TYPE_REFUND
  23. ]);
  24. }
  25. public function sumAmountPaid(Payment $payment, float $amount = 0)
  26. {
  27. if($this->isTypeDebit($payment)) {
  28. $amount += $payment->amount;
  29. }
  30. else {
  31. $amount -= $payment->amount;
  32. }
  33. return $amount;
  34. }
  35. public function getAmount(Payment $payment, bool $format = false): string
  36. {
  37. if ($format) {
  38. return number_format($payment->getAmount(), 2) . '&nbsp;€';
  39. } else {
  40. return $payment->getAmount();
  41. }
  42. }
  43. /**
  44. * Retourne le libellé du CreditHistory informant de son type et
  45. * éventuellement de la date de sa commande associée.
  46. *
  47. */
  48. public function getStrWording(Payment $payment, Order $order = null): string
  49. {
  50. $str = '';
  51. $type = $payment->getType();
  52. if (Payment::TYPE_INITIAL_CREDIT == $type) {
  53. $str = 'Crédit initial';
  54. } elseif (Payment::TYPE_CREDIT == $type) {
  55. $str = 'Crédit';
  56. } elseif (Payment::TYPE_PAYMENT == $type) {
  57. $str = 'Débit';
  58. } elseif (Payment::TYPE_REFUND == $type) {
  59. $str = 'Recrédit';
  60. } elseif (Payment::TYPE_DEBIT == $type) {
  61. $str = 'Débit';
  62. }
  63. if (Payment::TYPE_PAYMENT == $type || Payment::TYPE_REFUND == $type) {
  64. // Optimisation
  65. if(!$order) {
  66. $order = $payment->order;
  67. }
  68. if ($order && $order->distribution) {
  69. $str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->distribution->date));
  70. } else {
  71. $str .= '<br />Commande supprimée';
  72. }
  73. }
  74. return $str;
  75. }
  76. public function getDate(Payment $payment, bool $format = false): string
  77. {
  78. $date = $payment->getDate();
  79. if ($format) {
  80. return date('d/m/Y à H:i:s', strtotime($date));
  81. } else {
  82. return $date;
  83. }
  84. }
  85. public function isMeanPaymentCredit(Payment $payment): bool
  86. {
  87. return $payment->mean_payment == MeanPayment::CREDIT;
  88. }
  89. public function getStrMeanPayment(Payment $payment): string
  90. {
  91. return MeanPayment::getStrBy($payment->getMeanPayment());
  92. }
  93. public function getStrType(Payment $payment): string
  94. {
  95. switch($payment->type) {
  96. case Payment::TYPE_PAYMENT:
  97. return 'Paiement';
  98. case Payment::TYPE_DEBIT:
  99. return 'Débit';
  100. case Payment::TYPE_CREDIT:
  101. return 'Crédit';
  102. case Payment::TYPE_REFUND:
  103. return 'Remboursement';
  104. case Payment::TYPE_INITIAL_CREDIT:
  105. return 'Crédit initial';
  106. }
  107. return 'Type de paiement inconnu';
  108. }
  109. public function getStrUserAction(Payment $payment): string
  110. {
  111. $userAction = $payment->getUserActionObject();
  112. if ($userAction) {
  113. return $userAction->getName() . ' ' . $userAction->getlastname();
  114. } else {
  115. return 'Système';
  116. }
  117. }
  118. }