Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

BalanceValue.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace App\ValueObjects;
  3. use App\Utilities\Currency\CurrencyAccessor;
  4. use App\Utilities\Currency\CurrencyConverter;
  5. class BalanceValue
  6. {
  7. private int $value;
  8. private string $currency;
  9. private ?int $convertedValue = null;
  10. public function __construct(int $value, string $currency)
  11. {
  12. $this->value = $value;
  13. $this->currency = $currency;
  14. }
  15. public function getValue(): int
  16. {
  17. return $this->value;
  18. }
  19. public function getEffectiveValue(): int
  20. {
  21. return $this->convertedValue ?? $this->value;
  22. }
  23. public function getConvertedValue(): ?int
  24. {
  25. return $this->convertedValue;
  26. }
  27. public function getCurrency(): string
  28. {
  29. return $this->currency;
  30. }
  31. public function formatted(): string
  32. {
  33. return money($this->getEffectiveValue(), $this->getCurrency())->format();
  34. }
  35. public function formattedSimple(): string
  36. {
  37. return money($this->getEffectiveValue(), $this->getCurrency())->formatSimple();
  38. }
  39. public function formatWithCode(bool $codeBefore = false): string
  40. {
  41. return money($this->getEffectiveValue(), $this->getCurrency())->formatWithCode($codeBefore);
  42. }
  43. public function convert(): self
  44. {
  45. // The journal entry sums are stored in the default currency not the account currency (transaction amounts are stored in the account currency)
  46. $fromCurrency = CurrencyAccessor::getDefaultCurrency();
  47. $toCurrency = $this->currency;
  48. if ($fromCurrency !== $toCurrency) {
  49. $this->convertedValue = CurrencyConverter::convertBalance($this->value, $fromCurrency, $toCurrency);
  50. }
  51. return $this;
  52. }
  53. }