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

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