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 3.1KB

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