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.

CurrencyRepository.php 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Repositories\Setting;
  3. use App\Models\Company;
  4. use App\Models\Setting\Currency;
  5. class CurrencyRepository
  6. {
  7. public function ensureCurrencyExists(Company $company, string $currencyCode): Currency
  8. {
  9. $hasDefaultCurrency = $this->hasDefaultCurrency($company);
  10. $currency = currency($currencyCode);
  11. return $company->currencies()
  12. ->firstOrCreate([
  13. 'code' => $currencyCode,
  14. ], [
  15. 'name' => $currency->getName(),
  16. 'rate' => $currency->getRate(),
  17. 'precision' => $currency->getPrecision(),
  18. 'symbol' => $currency->getSymbol(),
  19. 'symbol_first' => $currency->isSymbolFirst(),
  20. 'decimal_mark' => $currency->getDecimalMark(),
  21. 'thousands_separator' => $currency->getThousandsSeparator(),
  22. 'enabled' => ! $hasDefaultCurrency,
  23. ]);
  24. }
  25. public function getDefaultCurrency(Company $company): ?Currency
  26. {
  27. return $company->currencies()
  28. ->where('enabled', true)
  29. ->first();
  30. }
  31. public function hasDefaultCurrency(Company $company): bool
  32. {
  33. return $this->getDefaultCurrency($company) !== null;
  34. }
  35. }