Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CompanyProfile.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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\CountrySelect;
  6. use App\Models\Locale\State;
  7. use App\Models\Setting\CompanyProfile as CompanyProfileModel;
  8. use App\Utilities\Localization\Timezone;
  9. use CodeWithDennis\SimpleAlert\Components\Forms\SimpleAlert;
  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\Hidden;
  16. use Filament\Forms\Components\Section;
  17. use Filament\Forms\Components\Select;
  18. use Filament\Forms\Components\TextInput;
  19. use Filament\Forms\Form;
  20. use Filament\Forms\Get;
  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. $this->getSavedNotification()->send();
  79. }
  80. protected function updateTimezone(string $countryCode): void
  81. {
  82. $model = \App\Models\Setting\Localization::firstOrFail();
  83. $timezones = Timezone::getTimezonesForCountry($countryCode);
  84. if (! empty($timezones)) {
  85. $model->update([
  86. 'timezone' => $timezones[0],
  87. ]);
  88. }
  89. }
  90. protected function getTimezoneChangeNotification(): Notification
  91. {
  92. return Notification::make()
  93. ->info()
  94. ->title('Timezone Update Required')
  95. ->body('You have changed your country or state. Please update your timezone to ensure accurate date and time information.')
  96. ->actions([
  97. \Filament\Notifications\Actions\Action::make('updateTimezone')
  98. ->label('Update Timezone')
  99. ->url(Localization::getUrl()),
  100. ])
  101. ->persistent()
  102. ->send();
  103. }
  104. protected function getSavedNotification(): Notification
  105. {
  106. return Notification::make()
  107. ->success()
  108. ->title(__('filament-panels::resources/pages/edit-record.notifications.saved.title'));
  109. }
  110. public function form(Form $form): Form
  111. {
  112. return $form
  113. ->schema([
  114. $this->getIdentificationSection(),
  115. $this->getNeedsAddressCompletionAlert(),
  116. $this->getLocationDetailsSection(),
  117. $this->getLegalAndComplianceSection(),
  118. ])
  119. ->model($this->record)
  120. ->statePath('data')
  121. ->operation('edit');
  122. }
  123. protected function getIdentificationSection(): Component
  124. {
  125. return Section::make('Identification')
  126. ->schema([
  127. Group::make()
  128. ->schema([
  129. TextInput::make('email')
  130. ->email()
  131. ->localizeLabel()
  132. ->maxLength(255)
  133. ->softRequired(),
  134. TextInput::make('phone_number')
  135. ->tel()
  136. ->localizeLabel(),
  137. ])->columns(1),
  138. FileUpload::make('logo')
  139. ->openable()
  140. ->maxSize(2048)
  141. ->localizeLabel()
  142. ->visibility('public')
  143. ->disk('public')
  144. ->directory('logos/company')
  145. ->imageResizeMode('contain')
  146. ->imageCropAspectRatio('1:1')
  147. ->panelAspectRatio('1:1')
  148. ->panelLayout('integrated')
  149. ->removeUploadedFileButtonPosition('center bottom')
  150. ->uploadButtonPosition('center bottom')
  151. ->uploadProgressIndicatorPosition('center bottom')
  152. ->getUploadedFileNameForStorageUsing(
  153. static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
  154. ->prepend(Auth::user()->currentCompany->id . '_'),
  155. )
  156. ->extraAttributes(['class' => 'w-32 h-32'])
  157. ->acceptedFileTypes(['image/png', 'image/jpeg']),
  158. ])->columns();
  159. }
  160. protected function getNeedsAddressCompletionAlert(): Component
  161. {
  162. return SimpleAlert::make('needsAddressCompletion')
  163. ->warning()
  164. ->border()
  165. ->icon('heroicon-o-exclamation-triangle')
  166. ->title('Address Information Incomplete')
  167. ->description('Please complete the required address information for proper business operations.')
  168. ->visible(fn (CompanyProfileModel $record) => $record->address->isIncomplete())
  169. ->columnSpanFull();
  170. }
  171. protected function getLocationDetailsSection(): Component
  172. {
  173. return Section::make('Address Information')
  174. ->relationship('address')
  175. ->schema([
  176. Hidden::make('type')
  177. ->default('general'),
  178. TextInput::make('address_line_1')
  179. ->label('Address Line 1')
  180. ->softRequired()
  181. ->maxLength(255),
  182. TextInput::make('address_line_2')
  183. ->label('Address Line 2')
  184. ->maxLength(255),
  185. CountrySelect::make('country')
  186. ->clearStateField()
  187. ->softRequired(),
  188. Select::make('state_id')
  189. ->localizeLabel('State / Province')
  190. ->searchable()
  191. ->options(static fn (Get $get) => State::getStateOptions($get('country'))),
  192. TextInput::make('city')
  193. ->localizeLabel('City / Town')
  194. ->softRequired()
  195. ->maxLength(255),
  196. TextInput::make('postal_code')
  197. ->label('Postal Code / Zip Code')
  198. ->maxLength(255),
  199. ])
  200. ->columns(2);
  201. }
  202. protected function getLegalAndComplianceSection(): Component
  203. {
  204. return Section::make('Legal & Compliance')
  205. ->schema([
  206. Select::make('entity_type')
  207. ->localizeLabel()
  208. ->options(EntityType::class)
  209. ->softRequired(),
  210. TextInput::make('tax_id')
  211. ->localizeLabel('Tax ID')
  212. ->maxLength(50),
  213. ])->columns();
  214. }
  215. protected function handleRecordUpdate(CompanyProfileModel $record, array $data): CompanyProfileModel
  216. {
  217. $record->fill($data);
  218. $keysToWatch = [
  219. 'logo',
  220. ];
  221. if ($record->isDirty($keysToWatch)) {
  222. $this->dispatch('companyProfileUpdated');
  223. }
  224. $record->save();
  225. return $record;
  226. }
  227. /**
  228. * @return array<Action | ActionGroup>
  229. */
  230. protected function getFormActions(): array
  231. {
  232. return [
  233. $this->getSaveFormAction(),
  234. ];
  235. }
  236. protected function getSaveFormAction(): Action
  237. {
  238. return Action::make('save')
  239. ->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
  240. ->submit('save')
  241. ->keyBindings(['mod+s']);
  242. }
  243. public static function canView(Model $record): bool
  244. {
  245. try {
  246. return authorize('update', $record)->allowed();
  247. } catch (AuthorizationException $exception) {
  248. return $exception->toResponse()->allowed();
  249. }
  250. }
  251. }