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.

138 lines
3.8KB

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