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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Models\Setting\DocumentDefault;
  4. use Filament\Forms\ComponentContainer;
  5. use Filament\Forms\Components\Section;
  6. use Filament\Forms\Components\Select;
  7. use Filament\Forms\Components\Textarea;
  8. use Filament\Forms\Components\TextInput;
  9. use Filament\Forms\Concerns\InteractsWithForms;
  10. use Filament\Notifications\Notification;
  11. use Illuminate\Contracts\View\View;
  12. use Illuminate\Support\Facades\Auth;
  13. use Livewire\Component;
  14. use Filament\Forms\Contracts\HasForms;
  15. /**
  16. * @property ComponentContainer $form
  17. */
  18. class Invoice extends Component implements HasForms
  19. {
  20. use InteractsWithForms;
  21. public DocumentDefault $invoice;
  22. public $data;
  23. public $record;
  24. public function mount(): void
  25. {
  26. $this->invoice = DocumentDefault::where('type', 'invoice')->firstOrNew();
  27. $this->form->fill([
  28. 'document_number_prefix' => $this->invoice->document_number_prefix,
  29. 'document_number_digits' => $this->invoice->document_number_digits,
  30. 'document_number_next' => $this->invoice->document_number_next,
  31. 'payment_terms' => $this->invoice->payment_terms,
  32. 'template' => $this->invoice->template,
  33. 'title' => $this->invoice->title,
  34. 'subheading' => $this->invoice->subheading,
  35. 'notes' => $this->invoice->notes,
  36. 'footer' => $this->invoice->footer,
  37. 'terms' => $this->invoice->terms,
  38. ]);
  39. }
  40. protected function getFormSchema(): array
  41. {
  42. return [
  43. Section::make('General')
  44. ->schema([
  45. TextInput::make('document_number_prefix')
  46. ->label('Number Prefix')
  47. ->default('INV-')
  48. ->required(),
  49. Select::make('document_number_digits')
  50. ->label('Number Digits')
  51. ->options(DocumentDefault::getDocumentNumberDigits())
  52. ->default(DocumentDefault::getDefaultDocumentNumberDigits())
  53. ->reactive()
  54. ->afterStateUpdated(static function (callable $set, $state) {
  55. $numDigits = $state;
  56. $nextNumber = DocumentDefault::getDefaultDocumentNumberNext($numDigits);
  57. return $set('document_number_next', $nextNumber);
  58. })
  59. ->searchable()
  60. ->required(),
  61. TextInput::make('document_number_next')
  62. ->label('Next Number')
  63. ->default(DocumentDefault::getDefaultDocumentNumberNext(DocumentDefault::getDefaultDocumentNumberDigits()))
  64. ->required(),
  65. Select::make('payment_terms')
  66. ->label('Payment Terms')
  67. ->options(DocumentDefault::getPaymentTerms())
  68. ->default(DocumentDefault::getDefaultPaymentTerms())
  69. ->searchable()
  70. ->required(),
  71. ])->columns(),
  72. Section::make('Template')
  73. ->schema([
  74. Select::make('template')
  75. ->label('Template')
  76. ->options([
  77. 'default' => 'Default',
  78. 'simple' => 'Simple',
  79. 'modern' => 'Modern',
  80. ])
  81. ->default('default')
  82. ->searchable()
  83. ->required(),
  84. ])->columns(),
  85. Section::make('Content')
  86. ->schema([
  87. TextInput::make('title')
  88. ->label('Title')
  89. ->default('Invoice')
  90. ->nullable(),
  91. TextInput::make('subheading')
  92. ->label('Subheading')
  93. ->nullable(),
  94. Textarea::make('notes')
  95. ->label('Notes')
  96. ->nullable(),
  97. Textarea::make('footer')
  98. ->label('Footer')
  99. ->nullable(),
  100. Textarea::make('terms')
  101. ->label('Terms')
  102. ->nullable()
  103. ->columnSpanFull(),
  104. ])->columns(),
  105. ];
  106. }
  107. public function save(): void
  108. {
  109. $data = $this->form->getState();
  110. $data = $this->mutateFormDataBeforeSave($data);
  111. $this->record = $this->invoice->update($data);
  112. $this->form->model($this->record)->saveRelationships();
  113. $this->getSavedNotification()?->send();
  114. }
  115. protected function mutateFormDataBeforeSave(array $data): array
  116. {
  117. $data['company_id'] = Auth::user()->currentCompany->id;
  118. $data['type'] = 'invoice';
  119. return $data;
  120. }
  121. protected function getSavedNotification():?Notification
  122. {
  123. $title = $this->getSavedNotificationTitle();
  124. if (blank($title)) {
  125. return null;
  126. }
  127. return Notification::make()
  128. ->success()
  129. ->title($title);
  130. }
  131. protected function getSavedNotificationTitle(): ?string
  132. {
  133. return __('filament::resources/pages/edit-record.messages.saved');
  134. }
  135. public function render(): View
  136. {
  137. return view('livewire.invoice');
  138. }
  139. }