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.

CreateCompany.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Filament\Company\Pages;
  3. use App\Enums\EntityType;
  4. use App\Events\CompanyGenerated;
  5. use App\Models\Setting\CompanyProfile;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Components\TextInput;
  8. use Filament\Forms\Form;
  9. use Illuminate\Database\Eloquent\Model;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Gate;
  12. use Wallo\FilamentCompanies\Events\AddingCompany;
  13. use Wallo\FilamentCompanies\FilamentCompanies;
  14. use Wallo\FilamentCompanies\Pages\Company\CreateCompany as FilamentCreateCompany;
  15. class CreateCompany extends FilamentCreateCompany
  16. {
  17. public function form(Form $form): Form
  18. {
  19. return $form
  20. ->schema([
  21. TextInput::make('name')
  22. ->label(__('filament-companies::default.labels.company_name'))
  23. ->autofocus()
  24. ->maxLength(255)
  25. ->required(),
  26. TextInput::make('profile.email')
  27. ->label('Company Email')
  28. ->email()
  29. ->required(),
  30. Select::make('profile.entity_type')
  31. ->label('Entity Type')
  32. ->native(false)
  33. ->options(EntityType::class)
  34. ->required(),
  35. Select::make('profile.country')
  36. ->label('Country')
  37. ->native(false)
  38. ->searchable()
  39. ->options(CompanyProfile::getAvailableCountryOptions())
  40. ->required(),
  41. ])
  42. ->model(FilamentCompanies::companyModel())
  43. ->statePath('data');
  44. }
  45. protected function handleRegistration(array $data): Model
  46. {
  47. $user = Auth::user();
  48. Gate::forUser($user)->authorize('create', FilamentCompanies::newCompanyModel());
  49. AddingCompany::dispatch($user);
  50. $personalCompany = $user?->personalCompany() === null;
  51. $company = $user?->ownedCompanies()->create([
  52. 'name' => $data['name'],
  53. 'personal_company' => $personalCompany,
  54. ]);
  55. $company->profile()->create([
  56. 'email' => $data['profile']['email'],
  57. 'entity_type' => $data['profile']['entity_type'],
  58. 'country' => $data['profile']['country'],
  59. ]);
  60. $user?->switchCompany($company);
  61. $name = $data['name'];
  62. CompanyGenerated::dispatch($user, $company, $data['profile']['country']);
  63. $this->companyCreated($name);
  64. return $company;
  65. }
  66. }