Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PaymentSolver.php 3.9KB

7 månader sedan
7 månader sedan
7 månader sedan
7 månader sedan
7 månader sedan
7 månader sedan
7 månader sedan
7 månader sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace domain\Payment;
  3. use common\helpers\MeanPayment;
  4. use domain\Order\Order\Order;
  5. use domain\_\AbstractService;
  6. use domain\_\SolverInterface;
  7. use domain\User\User\UserSolver;
  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 cagnotte';
  54. } elseif (Payment::TYPE_CREDIT == $type) {
  55. $str = 'Crédit cagnotte';
  56. } elseif (Payment::TYPE_PAYMENT == $type) {
  57. $str = 'Paiement';
  58. } elseif (Payment::TYPE_REFUND == $type) {
  59. $str = 'Remboursement';
  60. } elseif (Payment::TYPE_DEBIT == $type) {
  61. $str = 'Débit cagnotte';
  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 /><small>commande du <a href="'.\Yii::$app->urlManager->createUrl(['distribution/index', 'date' => $order->distribution->date]).'">' . date('d/m/Y', strtotime($order->distribution->date)).'</a></small>';
  70. $str .= '<br /><small>Commande du ' . date('d/m/Y', strtotime($order->distribution->date)).'</small>';
  71. } else {
  72. $str .= '<br /><small>Commande supprimée</small>';
  73. }
  74. }
  75. return $str;
  76. }
  77. public function getDate(Payment $payment, bool $format = false): string
  78. {
  79. $date = $payment->getDate();
  80. if ($format) {
  81. return date('d/m/Y à H:i:s', strtotime($date));
  82. } else {
  83. return $date;
  84. }
  85. }
  86. public function isMeanPaymentCredit(Payment $payment): bool
  87. {
  88. return $payment->mean_payment == MeanPayment::CREDIT;
  89. }
  90. public function isMeanPayment(Payment $payment, string $meanPayment): bool
  91. {
  92. return $payment->mean_payment == $meanPayment;
  93. }
  94. public function getStrMeanPayment(Payment $payment): string
  95. {
  96. return MeanPayment::getStrBy($payment->getMeanPayment());
  97. }
  98. public function getStrType(Payment $payment): string
  99. {
  100. switch($payment->type) {
  101. case Payment::TYPE_PAYMENT:
  102. return 'Paiement';
  103. case Payment::TYPE_DEBIT:
  104. return 'Débit';
  105. case Payment::TYPE_CREDIT:
  106. return 'Crédit';
  107. case Payment::TYPE_REFUND:
  108. return 'Remboursement';
  109. case Payment::TYPE_INITIAL_CREDIT:
  110. return 'Crédit initial';
  111. }
  112. return 'Type de paiement inconnu';
  113. }
  114. }