Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Filament\Company\Pages;
  3. use App\Enums\Common\AddressType;
  4. use App\Enums\Setting\EntityType;
  5. use App\Models\Company;
  6. use App\Models\Locale\Country;
  7. use App\Models\Setting\Localization;
  8. use App\Services\CompanyDefaultService;
  9. use App\Utilities\Currency\CurrencyAccessor;
  10. use Filament\Forms\Components\Select;
  11. use Filament\Forms\Components\TextInput;
  12. use Filament\Forms\Form;
  13. use Filament\Support\Enums\MaxWidth;
  14. use Illuminate\Contracts\Support\Htmlable;
  15. use Illuminate\Database\Eloquent\Model;
  16. use Illuminate\Support\Facades\Auth;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Gate;
  19. use Wallo\FilamentCompanies\Events\AddingCompany;
  20. use Wallo\FilamentCompanies\FilamentCompanies;
  21. use Wallo\FilamentCompanies\Pages\Company\CreateCompany as FilamentCreateCompany;
  22. class CreateCompany extends FilamentCreateCompany
  23. {
  24. protected bool $hasTopbar = false;
  25. protected static string $view = 'filament.company.pages.create-company';
  26. protected static string $layout = 'components.company.layout.custom-simple';
  27. public function getHeading(): string | Htmlable
  28. {
  29. return '';
  30. }
  31. public function getMaxWidth(): MaxWidth | string | null
  32. {
  33. return MaxWidth::FourExtraLarge;
  34. }
  35. public function hasLogo(): bool
  36. {
  37. return true;
  38. }
  39. public function form(Form $form): Form
  40. {
  41. return $form
  42. ->schema([
  43. TextInput::make('name')
  44. ->label(__('filament-companies::default.labels.company_name'))
  45. ->autofocus()
  46. ->maxLength(255)
  47. ->softRequired(),
  48. TextInput::make('profile.email')
  49. ->label('Company Email')
  50. ->email()
  51. ->softRequired(),
  52. Select::make('profile.entity_type')
  53. ->label('Entity Type')
  54. ->options(EntityType::class)
  55. ->softRequired(),
  56. Select::make('profile.country')
  57. ->label('Country')
  58. ->live()
  59. ->searchable()
  60. ->options(Country::getAvailableCountryOptions())
  61. ->getSearchResultsUsing(fn (string $search): array => Country::getSearchResultsUsing($search))
  62. ->getOptionLabelUsing(fn ($value): ?string => Country::find($value)?->name . ' ' . Country::find($value)?->flag)
  63. ->softRequired(),
  64. Select::make('locale.language')
  65. ->label('Language')
  66. ->searchable()
  67. ->options(Localization::getAllLanguages())
  68. ->softRequired(),
  69. Select::make('currencies.code')
  70. ->label('Currency')
  71. ->searchable()
  72. ->options(CurrencyAccessor::getAllCurrencyOptions())
  73. ->optionsLimit(5)
  74. ->softRequired(),
  75. ])
  76. ->columns()
  77. ->model(FilamentCompanies::companyModel())
  78. ->statePath('data');
  79. }
  80. protected function handleRegistration(array $data): Model
  81. {
  82. $user = Auth::user();
  83. Gate::forUser($user)->authorize('create', FilamentCompanies::newCompanyModel());
  84. AddingCompany::dispatch($user);
  85. $personalCompany = $user?->personalCompany() === null;
  86. return DB::transaction(function () use ($user, $data, $personalCompany) {
  87. /** @var Company $company */
  88. $company = $user?->ownedCompanies()->create([
  89. 'name' => $data['name'],
  90. 'personal_company' => $personalCompany,
  91. ]);
  92. $profile = $company->profile()->create([
  93. 'email' => $data['profile']['email'],
  94. 'entity_type' => $data['profile']['entity_type'],
  95. ]);
  96. $profile->address()->create([
  97. 'company_id' => $company->id,
  98. 'type' => AddressType::General,
  99. 'country' => $data['profile']['country'],
  100. ]);
  101. $user?->switchCompany($company);
  102. $companyDefaultService = app(CompanyDefaultService::class);
  103. $user = $company->owner ?? $user;
  104. $companyDefaultService->createCompanyDefaults($company, $user, $data['currencies']['code'], $data['profile']['country'], $data['locale']['language']);
  105. $this->companyCreated($data['name']);
  106. return $company;
  107. });
  108. }
  109. }