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.

CompanyProfile.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace App\Filament\Company\Clusters\Settings\Pages;
  3. use App\Enums\Setting\EntityType;
  4. use App\Filament\Company\Clusters\Settings;
  5. use App\Models\Locale\City;
  6. use App\Models\Locale\Country;
  7. use App\Models\Locale\State;
  8. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  9. use App\Utilities\Localization\Timezone;
  10. use Filament\Actions\Action;
  11. use Filament\Actions\ActionGroup;
  12. use Filament\Forms\Components\Component;
  13. use Filament\Forms\Components\FileUpload;
  14. use Filament\Forms\Components\Group;
  15. use Filament\Forms\Components\Section;
  16. use Filament\Forms\Components\Select;
  17. use Filament\Forms\Components\TextInput;
  18. use Filament\Forms\Form;
  19. use Filament\Forms\Get;
  20. use Filament\Forms\Set;
  21. use Filament\Notifications\Notification;
  22. use Filament\Pages\Concerns\InteractsWithFormActions;
  23. use Filament\Pages\Page;
  24. use Filament\Support\Enums\MaxWidth;
  25. use Filament\Support\Exceptions\Halt;
  26. use Illuminate\Auth\Access\AuthorizationException;
  27. use Illuminate\Contracts\Support\Htmlable;
  28. use Illuminate\Database\Eloquent\Model;
  29. use Illuminate\Support\Facades\Auth;
  30. use Livewire\Attributes\Locked;
  31. use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
  32. use function Filament\authorize;
  33. /**
  34. * @property Form $form
  35. */
  36. class CompanyProfile extends Page
  37. {
  38. use InteractsWithFormActions;
  39. protected static ?string $title = 'Company Profile';
  40. protected static string $view = 'filament.company.pages.setting.company-profile';
  41. protected static ?string $cluster = Settings::class;
  42. public ?array $data = [];
  43. #[Locked]
  44. public ?CompanyProfileModel $record = null;
  45. public function getTitle(): string | Htmlable
  46. {
  47. return translate(static::$title);
  48. }
  49. public static function getNavigationLabel(): string
  50. {
  51. return translate(static::$title);
  52. }
  53. public function getMaxContentWidth(): MaxWidth | string | null
  54. {
  55. return MaxWidth::ScreenTwoExtraLarge;
  56. }
  57. public function mount(): void
  58. {
  59. $this->record = CompanyProfileModel::firstOrNew([
  60. 'company_id' => auth()->user()->currentCompany->id,
  61. ]);
  62. abort_unless(static::canView($this->record), 404);
  63. $this->fillForm();
  64. }
  65. public function fillForm(): void
  66. {
  67. $data = $this->record->attributesToArray();
  68. $this->form->fill($data);
  69. }
  70. public function save(): void
  71. {
  72. try {
  73. $data = $this->form->getState();
  74. $this->handleRecordUpdate($this->record, $data);
  75. } catch (Halt $exception) {
  76. return;
  77. }
  78. $countryChanged = $this->record->wasChanged('country');
  79. $stateChanged = $this->record->wasChanged('state_id');
  80. $this->getSavedNotification()->send();
  81. if ($countryChanged || $stateChanged) {
  82. if ($countryChanged) {
  83. $this->updateTimezone($this->record->country);
  84. }
  85. $this->getTimezoneChangeNotification()->send();
  86. }
  87. }
  88. protected function updateTimezone(string $countryCode): void
  89. {
  90. $model = \App\Models\Setting\Localization::firstOrFail();
  91. $timezones = Timezone::getTimezonesForCountry($countryCode);
  92. if (! empty($timezones)) {
  93. $model->update([
  94. 'timezone' => $timezones[0],
  95. ]);
  96. }
  97. }
  98. protected function getTimezoneChangeNotification(): Notification
  99. {
  100. return Notification::make()
  101. ->info()
  102. ->title('Timezone Update Required')
  103. ->body('You have changed your country or state. Please update your timezone to ensure accurate date and time information.')
  104. ->actions([
  105. \Filament\Notifications\Actions\Action::make('updateTimezone')
  106. ->label('Update Timezone')
  107. ->url(Localization::getUrl()),
  108. ])
  109. ->persistent()
  110. ->send();
  111. }
  112. protected function getSavedNotification(): Notification
  113. {
  114. return Notification::make()
  115. ->success()
  116. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  117. }
  118. public function form(Form $form): Form
  119. {
  120. return $form
  121. ->schema([
  122. $this->getIdentificationSection(),
  123. $this->getLocationDetailsSection(),
  124. $this->getLegalAndComplianceSection(),
  125. ])
  126. ->model($this->record)
  127. ->statePath('data')
  128. ->operation('edit');
  129. }
  130. protected function getIdentificationSection(): Component
  131. {
  132. return Section::make('Identification')
  133. ->schema([
  134. Group::make()
  135. ->schema([
  136. TextInput::make('email')
  137. ->email()
  138. ->localizeLabel()
  139. ->maxLength(255)
  140. ->required(),
  141. TextInput::make('phone_number')
  142. ->tel()
  143. ->nullable()
  144. ->localizeLabel(),
  145. ])->columns(1),
  146. FileUpload::make('logo')
  147. ->openable()
  148. ->maxSize(2048)
  149. ->localizeLabel()
  150. ->visibility('public')
  151. ->disk('public')
  152. ->directory('logos/company')
  153. ->imageResizeMode('contain')
  154. ->imageCropAspectRatio('1:1')
  155. ->panelAspectRatio('1:1')
  156. ->panelLayout('integrated')
  157. ->removeUploadedFileButtonPosition('center bottom')
  158. ->uploadButtonPosition('center bottom')
  159. ->uploadProgressIndicatorPosition('center bottom')
  160. ->getUploadedFileNameForStorageUsing(
  161. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  162. ->prepend(Auth::user()->currentCompany->id . '_'),
  163. )
  164. ->extraAttributes(['class' => 'w-32 h-32'])
  165. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  166. ])->columns();
  167. }
  168. protected function getLocationDetailsSection(): Component
  169. {
  170. return Section::make('Location Details')
  171. ->schema([
  172. Select::make('country')
  173. ->searchable()
  174. ->localizeLabel()
  175. ->live()
  176. ->options(Country::getAvailableCountryOptions())
  177. ->afterStateUpdated(static function (Set $set) {
  178. $set('state_id', null);
  179. $set('city_id', null);
  180. })
  181. ->required(),
  182. Select::make('state_id')
  183. ->localizeLabel('State / Province')
  184. ->searchable()
  185. ->live()
  186. ->options(static fn (Get $get) => State::getStateOptions($get('country')))
  187. ->afterStateUpdated(static fn (Set $set) => $set('city_id', null))
  188. ->nullable(),
  189. TextInput::make('address')
  190. ->localizeLabel('Street Address')
  191. ->maxLength(255)
  192. ->nullable(),
  193. Select::make('city_id')
  194. ->localizeLabel('City / Town')
  195. ->searchable()
  196. ->options(static fn (Get $get) => City::getCityOptions($get('country'), $get('state_id')))
  197. ->nullable(),
  198. TextInput::make('zip_code')
  199. ->localizeLabel('Zip / Postal Code')
  200. ->maxLength(20)
  201. ->nullable(),
  202. ])->columns();
  203. }
  204. protected function getLegalAndComplianceSection(): Component
  205. {
  206. return Section::make('Legal & Compliance')
  207. ->schema([
  208. Select::make('entity_type')
  209. ->localizeLabel()
  210. ->options(EntityType::class)
  211. ->required(),
  212. TextInput::make('tax_id')
  213. ->localizeLabel('Tax ID')
  214. ->maxLength(50)
  215. ->nullable(),
  216. ])->columns();
  217. }
  218. protected function handleRecordUpdate(CompanyProfileModel $record, array $data): CompanyProfileModel
  219. {
  220. $record->fill($data);
  221. $keysToWatch = [
  222. 'logo',
  223. ];
  224. if ($record->isDirty($keysToWatch)) {
  225. $this->dispatch('companyProfileUpdated');
  226. }
  227. $record->save();
  228. return $record;
  229. }
  230. /**
  231. * @return array<Action | ActionGroup>
  232. */
  233. protected function getFormActions(): array
  234. {
  235. return [
  236. $this->getSaveFormAction(),
  237. ];
  238. }
  239. protected function getSaveFormAction(): Action
  240. {
  241. return Action::make('save')
  242. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  243. ->submit('save')
  244. ->keyBindings(['mod+s']);
  245. }
  246. public static function canView(Model $record): bool
  247. {
  248. try {
  249. return authorize('update', $record)->allowed();
  250. } catch (AuthorizationException $exception) {
  251. return $exception->toResponse()->allowed();
  252. }
  253. }
  254. }