Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CreateCompany.php 3.5KB

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