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.

HandlesCurrencyConversion.php 874B

1234567891011121314151617181920212223242526272829303132
  1. <?php
  2. namespace App\Concerns;
  3. use App\Utilities\Currency\CurrencyAccessor;
  4. use App\Utilities\Currency\CurrencyConverter;
  5. trait HandlesCurrencyConversion
  6. {
  7. public function convertAmountToDefaultCurrency(int $amountCents): int
  8. {
  9. $defaultCurrency = CurrencyAccessor::getDefaultCurrency();
  10. $needsConversion = $this->currency_code !== $defaultCurrency;
  11. if ($needsConversion) {
  12. return CurrencyConverter::convertBalance(
  13. $amountCents,
  14. $this->currency_code,
  15. $defaultCurrency
  16. );
  17. }
  18. return $amountCents;
  19. }
  20. public function formatAmountToDefaultCurrency(int $amountCents): string
  21. {
  22. $convertedCents = $this->convertAmountToDefaultCurrency($amountCents);
  23. return CurrencyConverter::convertCentsToFormatSimple($convertedCents);
  24. }
  25. }