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.

131 lines
4.1KB

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