Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CompanySettingsService.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Services;
  3. use App\Enums\Setting\DateFormat;
  4. use App\Enums\Setting\WeekStart;
  5. use App\Models\Company;
  6. use App\Models\Setting\Currency;
  7. use Illuminate\Support\Facades\Cache;
  8. class CompanySettingsService
  9. {
  10. protected static array $requestCache = [];
  11. public static function getSettings(?int $companyId = null): array
  12. {
  13. $companyId ??= session('current_company_id');
  14. if (! $companyId) {
  15. return self::getDefaultSettings();
  16. }
  17. if (isset(self::$requestCache[$companyId])) {
  18. return self::$requestCache[$companyId];
  19. }
  20. $cacheKey = "company_settings_{$companyId}";
  21. $settings = Cache::rememberForever($cacheKey, function () use ($companyId) {
  22. $company = Company::with(['locale'])->find($companyId);
  23. if (! $company) {
  24. return self::getDefaultSettings();
  25. }
  26. $defaultCurrency = Currency::query()
  27. ->where('company_id', $companyId)
  28. ->where('enabled', true)
  29. ->value('code') ?? 'USD';
  30. return [
  31. 'default_language' => $company->locale->language ?? config('transmatic.source_locale'),
  32. 'default_timezone' => $company->locale->timezone ?? config('app.timezone'),
  33. 'default_currency' => $defaultCurrency,
  34. 'default_date_format' => $company->locale->date_format->value ?? DateFormat::DEFAULT,
  35. 'default_week_start' => $company->locale->week_start->value ?? WeekStart::DEFAULT,
  36. ];
  37. });
  38. self::$requestCache[$companyId] = $settings;
  39. return $settings;
  40. }
  41. public static function invalidateSettings(int $companyId): void
  42. {
  43. $cacheKey = "company_settings_{$companyId}";
  44. Cache::forget($cacheKey);
  45. unset(self::$requestCache[$companyId]);
  46. }
  47. public static function getDefaultSettings(): array
  48. {
  49. return [
  50. 'default_language' => config('transmatic.source_locale'),
  51. 'default_timezone' => config('app.timezone'),
  52. 'default_currency' => 'USD',
  53. 'default_date_format' => DateFormat::DEFAULT,
  54. 'default_week_start' => WeekStart::DEFAULT,
  55. ];
  56. }
  57. public static function getSpecificSetting(?int $companyId, string $key, $default = null)
  58. {
  59. $settings = self::getSettings($companyId);
  60. return $settings[$key] ?? $default;
  61. }
  62. public static function getDefaultLanguage(?int $companyId = null): string
  63. {
  64. return self::getSpecificSetting($companyId, 'default_language', config('transmatic.source_locale'));
  65. }
  66. public static function getDefaultTimezone(?int $companyId = null): string
  67. {
  68. return self::getSpecificSetting($companyId, 'default_timezone', config('app.timezone'));
  69. }
  70. public static function getDefaultCurrency(?int $companyId = null): string
  71. {
  72. return self::getSpecificSetting($companyId, 'default_currency', 'USD');
  73. }
  74. public static function getDefaultDateFormat(?int $companyId = null): string
  75. {
  76. return self::getSpecificSetting($companyId, 'default_date_format', DateFormat::DEFAULT);
  77. }
  78. public static function getDefaultWeekStart(?int $companyId = null): string
  79. {
  80. return self::getSpecificSetting($companyId, 'default_week_start', WeekStart::DEFAULT);
  81. }
  82. }