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

DefaultSetting.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\HandlesDefaultSettingRecordUpdate;
  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\Database\Eloquent\Model;
  17. use Livewire\Component;
  18. /**
  19. * @property ComponentContainer $form
  20. */
  21. class DefaultSetting extends Component implements HasForms
  22. {
  23. use InteractsWithForms, HandlesDefaultSettingRecordUpdate;
  24. public $data;
  25. public Defaults $record;
  26. public function mount():void
  27. {
  28. $this->record = Defaults::firstOrNew();
  29. $this->form->fill([
  30. 'account_id' => Defaults::getDefaultAccount(),
  31. 'currency_code' => Defaults::getDefaultCurrency(),
  32. 'sales_tax_id' => Defaults::getDefaultSalesTax(),
  33. 'purchase_tax_id' => Defaults::getDefaultPurchaseTax(),
  34. 'sales_discount_id' => Defaults::getDefaultSalesDiscount(),
  35. 'purchase_discount_id' => Defaults::getDefaultPurchaseDiscount(),
  36. 'income_category_id' => Defaults::getDefaultIncomeCategory(),
  37. 'expense_category_id' => Defaults::getDefaultExpenseCategory(),
  38. ]);
  39. }
  40. protected function getFormSchema(): array
  41. {
  42. return [
  43. Section::make('General')
  44. ->schema([
  45. Select::make('account_id')
  46. ->label('Account')
  47. ->options(Defaults::getAccounts())
  48. ->searchable()
  49. ->validationAttribute('Account')
  50. ->nullable(),
  51. Select::make('currency_code')
  52. ->label('Currency')
  53. ->options(Defaults::getCurrencies())
  54. ->searchable()
  55. ->validationAttribute('Currency')
  56. ->nullable(),
  57. ])->columns(),
  58. Section::make('Taxes & Discounts')
  59. ->schema([
  60. Select::make('sales_tax_id')
  61. ->label('Sales Tax')
  62. ->options(Defaults::getSalesTaxes())
  63. ->searchable()
  64. ->validationAttribute('Sales Tax')
  65. ->nullable(),
  66. Select::make('purchase_tax_id')
  67. ->label('Purchase Tax')
  68. ->options(Defaults::getPurchaseTaxes())
  69. ->searchable()
  70. ->validationAttribute('Purchase Tax')
  71. ->nullable(),
  72. Select::make('sales_discount_id')
  73. ->label('Sales Discount')
  74. ->options(Defaults::getSalesDiscounts())
  75. ->searchable()
  76. ->validationAttribute('Sales Discount')
  77. ->nullable(),
  78. Select::make('purchase_discount_id')
  79. ->label('Purchase Discount')
  80. ->options(Defaults::getPurchaseDiscounts())
  81. ->searchable()
  82. ->validationAttribute('Purchase Discount')
  83. ->nullable(),
  84. ])->columns(),
  85. Section::make('Categories')
  86. ->schema([
  87. Select::make('income_category_id')
  88. ->label('Income Category')
  89. ->options(Defaults::getIncomeCategories())
  90. ->searchable()
  91. ->validationAttribute('Income Category')
  92. ->nullable(),
  93. Select::make('expense_category_id')
  94. ->label('Expense Category')
  95. ->options(Defaults::getExpenseCategories())
  96. ->searchable()
  97. ->validationAttribute('Expense Category')
  98. ->nullable(),
  99. ])->columns(),
  100. ];
  101. }
  102. public function save(): void
  103. {
  104. $data = $this->form->getState();
  105. $this->handleRecordUpdate($this->getFormModel(), $data);
  106. $this->getSavedNotification()?->send();
  107. }
  108. protected function getFormModel(): Model
  109. {
  110. return $this->record;
  111. }
  112. protected function getRelatedEntities(): array
  113. {
  114. return [
  115. 'account_id' => [Account::class, 'id'],
  116. 'currency_code' => [Currency::class, 'code'],
  117. 'sales_tax_id' => [Tax::class, 'id', 'sales'],
  118. 'purchase_tax_id' => [Tax::class, 'id', 'purchase'],
  119. 'sales_discount_id' => [Tax::class, 'id', 'sales'],
  120. 'purchase_discount_id' => [Tax::class, 'id', 'purchase'],
  121. 'income_category_id' => [Category::class, 'id', 'income'],
  122. 'expense_category_id' => [Category::class, 'id', 'expense'],
  123. ];
  124. }
  125. protected function getSavedNotification(): ?Notification
  126. {
  127. $title = $this->getSavedNotificationTitle();
  128. if (blank($title)) {
  129. return null;
  130. }
  131. return Notification::make()
  132. ->success()
  133. ->title($title);
  134. }
  135. protected function getSavedNotificationTitle(): ?string
  136. {
  137. return __('filament::resources/pages/edit-record.messages.saved');
  138. }
  139. public function render(): View
  140. {
  141. return view('livewire.default-setting');
  142. }
  143. }