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.

149 lines
4.4KB

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