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.4KB

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