Ви не можете вибрати більше 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 $amount;
  8. private string $currencyCode;
  9. private ?int $convertedAmount = null;
  10. public function __construct(int $amount, string $currencyCode)
  11. {
  12. $this->amount = $amount;
  13. $this->currencyCode = $currencyCode;
  14. }
  15. public function getAmount(): int
  16. {
  17. return $this->amount;
  18. }
  19. public function getCurrencyCode(): string
  20. {
  21. return $this->currencyCode;
  22. }
  23. public function getEffectiveAmount(): int
  24. {
  25. return $this->convertedAmount ?? $this->amount;
  26. }
  27. public function getConvertedAmount(): ?int
  28. {
  29. return $this->convertedAmount;
  30. }
  31. public function getValue(): float
  32. {
  33. return money($this->amount, $this->currencyCode)->getValue();
  34. }
  35. public function format(): string
  36. {
  37. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->format();
  38. }
  39. public function formatSimple(): string
  40. {
  41. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->formatSimple();
  42. }
  43. public function formatWithCode(bool $codeBefore = false): string
  44. {
  45. return money($this->getEffectiveAmount(), $this->getCurrencyCode())->formatWithCode($codeBefore);
  46. }
  47. public function convert(): self
  48. {
  49. // The journal entry sums are stored in the default currency not the account currency (transaction amounts are stored in the account currency)
  50. $fromCurrency = CurrencyAccessor::getDefaultCurrency();
  51. $toCurrency = $this->currencyCode;
  52. if ($fromCurrency !== $toCurrency) {
  53. $this->convertedAmount = CurrencyConverter::convertBalance($this->amount, $fromCurrency, $toCurrency);
  54. }
  55. return $this;
  56. }
  57. public function __toString(): string
  58. {
  59. return $this->formatSimple();
  60. }
  61. }