您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CompanyProfile.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Models\Setting;
  3. use App\Enums\EntityType;
  4. use App\Traits\Blamable;
  5. use App\Traits\CompanyOwned;
  6. use Database\Factories\Setting\CompanyProfileFactory;
  7. use DateTime;
  8. use DateTimeZone;
  9. use Exception;
  10. use Illuminate\Database\Eloquent\Factories\Factory;
  11. use Illuminate\Database\Eloquent\Factories\HasFactory;
  12. use Illuminate\Database\Eloquent\Model;
  13. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  14. use Squire\Models\Timezone;
  15. use Wallo\FilamentCompanies\FilamentCompanies;
  16. class CompanyProfile extends Model
  17. {
  18. use Blamable, CompanyOwned, HasFactory;
  19. protected $table = 'company_profiles';
  20. protected $fillable = [
  21. 'company_id',
  22. 'logo',
  23. 'address',
  24. 'city_id',
  25. 'zip_code',
  26. 'state',
  27. 'country',
  28. 'timezone',
  29. 'phone_number',
  30. 'email',
  31. 'tax_id',
  32. 'entity_type',
  33. 'fiscal_year_start',
  34. 'fiscal_year_end',
  35. 'created_by',
  36. 'updated_by',
  37. ];
  38. protected $casts = [
  39. 'entity_type' => EntityType::class,
  40. 'fiscal_year_start' => 'date',
  41. 'fiscal_year_end' => 'date',
  42. ];
  43. public function company(): BelongsTo
  44. {
  45. return $this->belongsTo(FilamentCompanies::companyModel(), 'company_id');
  46. }
  47. public function createdBy(): BelongsTo
  48. {
  49. return $this->belongsTo(FilamentCompanies::userModel(), 'created_by');
  50. }
  51. public function updatedBy(): BelongsTo
  52. {
  53. return $this->belongsTo(FilamentCompanies::userModel(), 'updated_by');
  54. }
  55. public function getCountryName(): string
  56. {
  57. return country($this->country)?->getName();
  58. }
  59. public static function getAvailableCountryCodes(): array
  60. {
  61. return countries()->pluck('iso2')->toArray();
  62. }
  63. public static function getAvailableCountryOptions(): array
  64. {
  65. $countries = countries();
  66. return $countries->mapWithKeys(static function ($country): array {
  67. return [$country['iso2'] => $country['name'] . ' ' . $country['emoji']];
  68. })->toArray();
  69. }
  70. public static function getAvailableCountryNames(): array
  71. {
  72. return countries()->pluck('name')->toArray();
  73. }
  74. public static function getAvailableCountryEmojis(): array
  75. {
  76. return countries()->pluck('emoji')->toArray();
  77. }
  78. public static function getStateOptions(?string $countryCode = null): array
  79. {
  80. if (empty($countryCode)) {
  81. return [];
  82. }
  83. $states = states($countryCode);
  84. return $states->mapWithKeys(static function ($state): array {
  85. return [$state['state_code'] => $state['name']];
  86. })->toArray();
  87. }
  88. public static function getCityOptions(?string $countryCode = null, ?string $stateCode = null): array
  89. {
  90. if (empty($countryCode) || empty($stateCode)) {
  91. return [];
  92. }
  93. $cities = cities($countryCode, $stateCode);
  94. return $cities->mapWithKeys(static function ($city): array {
  95. return [$city['id'] => $city['name']];
  96. })->toArray();
  97. }
  98. public static function getTimezoneOptions(?string $countryCode = null): array
  99. {
  100. if (empty($countryCode)) {
  101. return [];
  102. }
  103. // convert countryCode to lowercase
  104. $countryCode = strtolower($countryCode);
  105. $timezones = Timezone::where('country_id', $countryCode)->get();
  106. if (!$timezones->isEmpty()) {
  107. return $timezones->mapWithKeys(static function ($timezone): array {
  108. $localTime = self::getLocalTime($timezone->code);
  109. $parts = explode('/', $timezone->code);
  110. $cityName = str_replace('_', ' ', end($parts));
  111. $offsetInSeconds = $timezone->getOffset(now());
  112. $hours = floor($offsetInSeconds / 3600);
  113. $minutes = floor(($offsetInSeconds / 60) % 60);
  114. $gmtOffsetName = sprintf("UTC%+d:%02d", $hours, $minutes);
  115. return [$timezone->code => $cityName . ' (' . $gmtOffsetName . ') ' . $localTime];
  116. })->toArray();
  117. }
  118. return [];
  119. }
  120. /**
  121. * @throws Exception
  122. */
  123. public static function getLocalTime(string $timezone): string
  124. {
  125. return (new DateTime('now', new DateTimeZone($timezone)))->format('g:i A');
  126. }
  127. protected static function newFactory(): Factory
  128. {
  129. return CompanyProfileFactory::new();
  130. }
  131. }