選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CreateClientSelect.php 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Filament\Forms\Components;
  3. use App\Filament\Company\Resources\Sales\ClientResource;
  4. use App\Models\Common\Address;
  5. use App\Models\Common\Client;
  6. use App\Models\Common\Contact;
  7. use Filament\Forms\Components\Actions\Action;
  8. use Filament\Forms\Components\Select;
  9. use Filament\Forms\Form;
  10. use Filament\Support\Enums\MaxWidth;
  11. use Illuminate\Support\Facades\DB;
  12. class CreateClientSelect extends Select
  13. {
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this
  18. ->searchable()
  19. ->preload()
  20. ->createOptionForm(fn (Form $form) => $this->createClientForm($form))
  21. ->createOptionAction(fn (Action $action) => $this->createClientAction($action));
  22. $this->relationship('client', 'name');
  23. $this->createOptionUsing(static function (array $data) {
  24. return DB::transaction(static function () use ($data) {
  25. $client = Client::create([
  26. 'name' => $data['name'],
  27. 'website' => $data['website'] ?? null,
  28. 'notes' => $data['notes'] ?? null,
  29. ]);
  30. // Create primary contact
  31. $primaryContact = $client->contacts()->create([
  32. 'first_name' => $data['primary_contact']['first_name'],
  33. 'last_name' => $data['primary_contact']['last_name'],
  34. 'email' => $data['primary_contact']['email'],
  35. 'is_primary' => true,
  36. ]);
  37. // Add phone number
  38. $primaryContact->phones()->create([
  39. 'type' => 'primary',
  40. 'number' => $data['primary_contact']['phones'][0]['number'],
  41. ]);
  42. // Create billing address
  43. $client->addresses()->create([
  44. 'type' => 'billing',
  45. ...$data['billing_address'],
  46. ]);
  47. // Create shipping address
  48. $client->addresses()->create([
  49. 'type' => 'shipping',
  50. 'recipient' => $data['shipping_address']['recipient'],
  51. 'phone' => $data['shipping_address']['phone'],
  52. ...$data['shipping_address'],
  53. ]);
  54. return $client->getKey();
  55. });
  56. });
  57. }
  58. protected function createClientForm(Form $form): Form
  59. {
  60. return ClientResource::form($form);
  61. }
  62. protected function createClientAction(Action $action): Action
  63. {
  64. return $action
  65. ->label('Add client')
  66. ->slideOver()
  67. ->modalWidth(MaxWidth::ThreeExtraLarge);
  68. }
  69. }