Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\ValueObjects;
  3. use App\Utilities\Currency\CurrencyAccessor;
  4. use App\Utilities\Currency\CurrencyConverter;
  5. class Money
  6. {
  7. private ?int $convertedAmount = null;
  8. public function __construct(
  9. private readonly int $amount,
  10. private readonly string $currencyCode
  11. ) {}
  12. public function getAmount(): int
  13. {
  14. return $this->amount;
  15. }
  16. public function getCurrencyCode(): string
  17. {
  18. return $this->currencyCode;
  19. }
  20. public function getEffectiveAmount(): int
  21. {
  22. return $this->convertedAmount ?? $this->amount;
  23. }
  24. public function getConvertedAmount(): ?int
  25. {
  26. return $this->convertedAmount;
  27. }
  28. public function getValue(): float
  29. {
  30. return money($this->amount, $this->currencyCode)->getValue();
  31. }
  32. public function format(): string
  33. {
  34. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->format();
  35. }
  36. public function formatInDefaultCurrency(): string
  37. {
  38. return money($this->getEffectiveAmount(), CurrencyAccessor::getDefaultCurrency())->format();
  39. }
  40. public function formatSimple(): string
  41. {
  42. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->formatSimple();
  43. }
  44. public function formatWithCode(bool $codeBefore = false): string
  45. {
  46. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->formatWithCode($codeBefore);
  47. }
  48. public function convert(): self
  49. {
  50. // The journal entry sums are stored in the default currency not the account currency (transaction amounts are stored in the account currency)
  51. $fromCurrency = CurrencyAccessor::getDefaultCurrency();
  52. $toCurrency = $this->currencyCode;
  53. if ($fromCurrency !== $toCurrency) {
  54. $this->convertedAmount = CurrencyConverter::convertBalance($this->amount, $fromCurrency, $toCurrency);
  55. }
  56. return $this;
  57. }
  58. public function __toString(): string
  59. {
  60. return $this->formatSimple();
  61. }
  62. }