Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CompanyProfile.php 8.2KB

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