Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DefaultSetting.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Http\Livewire;
  3. use App\Models\Banking\Account;
  4. use App\Models\Setting\Category;
  5. use App\Models\Setting\Currency;
  6. use App\Models\Setting\DefaultSetting as Defaults;
  7. use App\Models\Setting\Tax;
  8. use App\Traits\HandlesRecordCreation;
  9. use Filament\Forms\ComponentContainer;
  10. use Filament\Forms\Components\Section;
  11. use Filament\Forms\Components\Select;
  12. use Filament\Forms\Concerns\InteractsWithForms;
  13. use Filament\Forms\Contracts\HasForms;
  14. use Filament\Notifications\Notification;
  15. use Illuminate\Contracts\View\View;
  16. use Illuminate\Support\Facades\Auth;
  17. use Livewire\Component;
  18. /**
  19. * @property ComponentContainer $form
  20. */
  21. class DefaultSetting extends Component implements HasForms
  22. {
  23. use InteractsWithForms, HandlesRecordCreation;
  24. public Defaults $defaultSetting;
  25. public $data;
  26. public $record;
  27. public function mount():void
  28. {
  29. $this->form->fill();
  30. }
  31. protected function getFormSchema(): array
  32. {
  33. return [
  34. Section::make('General')
  35. ->schema([
  36. Select::make('account_id')
  37. ->label('Account')
  38. ->options(Defaults::getAccounts())
  39. ->default(Defaults::getDefaultAccount())
  40. ->searchable()
  41. ->validationAttribute('Account')
  42. ->required(),
  43. Select::make('currency_code')
  44. ->label('Currency')
  45. ->options(Defaults::getCurrencies())
  46. ->default(Defaults::getDefaultCurrency())
  47. ->searchable()
  48. ->validationAttribute('Currency')
  49. ->required(),
  50. ])->columns(),
  51. Section::make('Taxes')
  52. ->schema([
  53. Select::make('sales_tax_id')
  54. ->label('Sales Tax')
  55. ->options(Defaults::getSalesTaxes())
  56. ->default(Defaults::getDefaultSalesTax())
  57. ->searchable()
  58. ->validationAttribute('Sales Tax')
  59. ->required(),
  60. Select::make('purchase_tax_id')
  61. ->label('Purchase Tax')
  62. ->options(Defaults::getPurchaseTaxes())
  63. ->default(Defaults::getDefaultPurchaseTax())
  64. ->searchable()
  65. ->validationAttribute('Purchase Tax')
  66. ->required(),
  67. ])->columns(),
  68. Section::make('Categories')
  69. ->schema([
  70. Select::make('income_category_id')
  71. ->label('Income Category')
  72. ->options(Defaults::getIncomeCategories())
  73. ->default(Defaults::getDefaultIncomeCategory())
  74. ->searchable()
  75. ->validationAttribute('Income Category')
  76. ->required(),
  77. Select::make('expense_category_id')
  78. ->label('Expense Category')
  79. ->options(Defaults::getExpenseCategories())
  80. ->default(Defaults::getDefaultExpenseCategory())
  81. ->searchable()
  82. ->validationAttribute('Expense Category')
  83. ->required(),
  84. ])->columns(),
  85. ];
  86. }
  87. public function create(): void
  88. {
  89. $data = $this->form->getState();
  90. $data = $this->mutateFormDataBeforeCreate($data);
  91. $this->record = $this->handleRecordCreation($data);
  92. $this->form->model($this->record)->saveRelationships();
  93. $this->getSavedNotification()?->send();
  94. }
  95. protected function mutateFormDataBeforeCreate(array $data): array
  96. {
  97. $data['company_id'] = Auth::user()->currentCompany->id;
  98. $data['updated_by'] = Auth::id();
  99. return $data;
  100. }
  101. protected function getRelatedEntities(): array
  102. {
  103. return [
  104. 'account_id' => [Account::class, 'id'],
  105. 'currency_code' => [Currency::class, 'code'],
  106. 'sales_tax_id' => [Tax::class, 'id', 'sales'],
  107. 'purchase_tax_id' => [Tax::class, 'id', 'purchase'],
  108. 'income_category_id' => [Category::class, 'id', 'income'],
  109. 'expense_category_id' => [Category::class, 'id', 'expense'],
  110. ];
  111. }
  112. protected function getFormModel(): string
  113. {
  114. return Defaults::class;
  115. }
  116. protected function getSavedNotification():?Notification
  117. {
  118. $title = $this->getSavedNotificationTitle();
  119. if (blank($title)) {
  120. return null;
  121. }
  122. return Notification::make()
  123. ->success()
  124. ->title($title);
  125. }
  126. protected function getSavedNotificationTitle(): ?string
  127. {
  128. return __('filament::resources/pages/edit-record.messages.saved');
  129. }
  130. public function render(): View
  131. {
  132. return view('livewire.default-setting');
  133. }
  134. }