Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CompanySettingsService.php 3.3KB

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