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.

130 lines
3.9KB

  1. <?php
  2. namespace common\logic\User\CreditHistory;
  3. use common\helpers\MeanPayment;
  4. use common\logic\SolverInterface;
  5. use yii\helpers\Html;
  6. class CreditHistorySolver implements SolverInterface
  7. {
  8. public function isTypeDebit(CreditHistory $creditHistory): bool
  9. {
  10. return in_array($creditHistory->getType(), [
  11. CreditHistory::TYPE_DEBIT,
  12. CreditHistory::TYPE_PAYMENT,
  13. ]);
  14. }
  15. public function isTypeCredit(CreditHistory $creditHistory): bool
  16. {
  17. return in_array($creditHistory->getType(), [
  18. CreditHistory::TYPE_CREDIT,
  19. CreditHistory::TYPE_INITIAL_CREDIT,
  20. CreditHistory::TYPE_REFUND
  21. ]);
  22. }
  23. public function getAmount(CreditHistory $creditHistory, bool $format = false): string
  24. {
  25. if ($format) {
  26. return number_format($creditHistory->getAmount(), 2) . '&nbsp;€';
  27. } else {
  28. return $creditHistory->getAmount();
  29. }
  30. }
  31. /**
  32. * Retourne le libellé du CreditHistory informant de son type et
  33. * éventuellement de la date de sa commande associée.
  34. *
  35. */
  36. public function getStrWording(CreditHistory $creditHistory): string
  37. {
  38. $str = '';
  39. $type = $creditHistory->getType();
  40. if (CreditHistory::TYPE_INITIAL_CREDIT == $type) {
  41. $str = 'Crédit initial';
  42. } elseif (CreditHistory::TYPE_CREDIT == $type) {
  43. $str = 'Crédit';
  44. } elseif (CreditHistory::TYPE_PAYMENT == $type) {
  45. $str = 'Paiement';
  46. } elseif (CreditHistory::TYPE_REFUND == $type) {
  47. $str = 'Remboursement';
  48. } elseif (CreditHistory::TYPE_DEBIT == $type) {
  49. $str = 'Débit';
  50. }
  51. if (CreditHistory::TYPE_PAYMENT == $type || CreditHistory::TYPE_REFUND == $type) {
  52. $order = $creditHistory->getOrderObject();
  53. if ($order && $order->distribution) {
  54. $str .= '<br />Commande : ' . date('d/m/Y', strtotime($order->distribution->date));
  55. } else {
  56. $str .= '<br />Commande supprimée';
  57. }
  58. }
  59. return $str;
  60. }
  61. /**
  62. * Retourne les informations à ajouter au commentaire du CreditHistorique
  63. * (libellé, montant, client, action) au format HTML.
  64. *
  65. */
  66. public function getStrComment(CreditHistory $creditHistory): string
  67. {
  68. $str = '';
  69. if (strlen($creditHistory->getComment())) {
  70. $str .= '<br />';
  71. }
  72. $str .= $this->getStrWording($creditHistory);
  73. $order = $creditHistory->getOrderObject();
  74. if ($order) {
  75. $str .= '<br />Montant de la commande : ' . $order->getAmountWithTax(Order::AMOUNT_TOTAL, true);
  76. }
  77. $user = $creditHistory->getUserObject();
  78. if ($user) {
  79. $str .= '<br />Client : ' . Html::encode($user->getName() . ' CreditHistorySolver.php' . $user->getLastname());
  80. }
  81. $userAction = $creditHistory->getUserActionObject();
  82. if ($userAction) {
  83. $str .= '<br />Action : ' . Html::encode($userAction->getName() . ' CreditHistorySolver.php' . $userAction->getLastname());
  84. }
  85. return $str;
  86. }
  87. public function getDate(CreditHistory $creditHistory, bool $format = false): string
  88. {
  89. $date = $creditHistory->getDate();
  90. if ($format) {
  91. return date('d/m/Y à H:i:s', strtotime($date));
  92. } else {
  93. return $date;
  94. }
  95. }
  96. public function getStrMeanPayment(CreditHistory $creditHistory): string
  97. {
  98. return MeanPayment::getStrBy($creditHistory->getMeanPayment());
  99. }
  100. // strUserAction
  101. public function getStrUserAction(CreditHistory $creditHistory): string
  102. {
  103. $userAction = $creditHistory->getUserActionObject();
  104. if ($userAction) {
  105. return $userAction->getName() . ' CreditHistorySolver.php' . $userAction->getlastname();
  106. } else {
  107. return 'Système';
  108. }
  109. }
  110. }