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.

CompanySettingsService.php 3.5KB

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