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.9KB

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