Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CreateCompany.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Utilities\Currency\CurrencyAccessor;
  8. use Filament\Forms\Components\Select;
  9. use Filament\Forms\Components\TextInput;
  10. use Filament\Forms\Form;
  11. use Filament\Forms\Get;
  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(static fn (Get $get) => Country::getLanguagesByCountryCode($get('profile.country')))
  47. ->getSearchResultsUsing(static function (string $search) {
  48. $allLanguages = Country::getLanguagesByCountryCode();
  49. return array_filter($allLanguages, static function ($language) use ($search) {
  50. return stripos($language, $search) !== false;
  51. });
  52. })
  53. ->getOptionLabelUsing(static function ($value) {
  54. $allLanguages = Country::getLanguagesByCountryCode();
  55. return $allLanguages[$value] ?? $value;
  56. })
  57. ->required(),
  58. Select::make('currencies.code')
  59. ->label('Currency')
  60. ->searchable()
  61. ->options(CurrencyAccessor::getAllCurrencyOptions())
  62. ->optionsLimit(5)
  63. ->required(),
  64. ])
  65. ->model(FilamentCompanies::companyModel())
  66. ->statePath('data');
  67. }
  68. protected function handleRegistration(array $data): Model
  69. {
  70. $user = Auth::user();
  71. Gate::forUser($user)->authorize('create', FilamentCompanies::newCompanyModel());
  72. AddingCompany::dispatch($user);
  73. $personalCompany = $user?->personalCompany() === null;
  74. /** @var Company $company */
  75. $company = $user?->ownedCompanies()->create([
  76. 'name' => $data['name'],
  77. 'personal_company' => $personalCompany,
  78. ]);
  79. $company->profile()->create([
  80. 'email' => $data['profile']['email'],
  81. 'entity_type' => $data['profile']['entity_type'],
  82. 'country' => $data['profile']['country'],
  83. ]);
  84. $user?->switchCompany($company);
  85. $name = $data['name'];
  86. CompanyGenerated::dispatch($user ?? Auth::user(), $company, $data['profile']['country'], $data['locale']['language'], $data['currencies']['code']);
  87. $this->companyCreated($name);
  88. return $company;
  89. }
  90. }