Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

DefaultSetting.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 Livewire\Component;
  17. /**
  18. * @property ComponentContainer $form
  19. */
  20. class DefaultSetting extends Component implements HasForms
  21. {
  22. use InteractsWithForms, HandlesDefaultSettingRecordUpdate;
  23. public Defaults $defaultSetting;
  24. public $data;
  25. public $record;
  26. public function mount():void
  27. {
  28. $this->defaultSetting = 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->record = $this->handleRecordUpdate($data);
  106. $this->form->model($this->record)->saveRelationships();
  107. $this->getSavedNotification()?->send();
  108. }
  109. protected function getRelatedEntities(): array
  110. {
  111. return [
  112. 'account_id' => [Account::class, 'id'],
  113. 'currency_code' => [Currency::class, 'code'],
  114. 'sales_tax_id' => [Tax::class, 'id', 'sales'],
  115. 'purchase_tax_id' => [Tax::class, 'id', 'purchase'],
  116. 'sales_discount_id' => [Tax::class, 'id', 'sales'],
  117. 'purchase_discount_id' => [Tax::class, 'id', 'purchase'],
  118. 'income_category_id' => [Category::class, 'id', 'income'],
  119. 'expense_category_id' => [Category::class, 'id', 'expense'],
  120. ];
  121. }
  122. protected function getFormModel(): string
  123. {
  124. return Defaults::class;
  125. }
  126. protected function getSavedNotification():?Notification
  127. {
  128. $title = $this->getSavedNotificationTitle();
  129. if (blank($title)) {
  130. return null;
  131. }
  132. return Notification::make()
  133. ->success()
  134. ->title($title);
  135. }
  136. protected function getSavedNotificationTitle(): ?string
  137. {
  138. return __('filament::resources/pages/edit-record.messages.saved');
  139. }
  140. public function render(): View
  141. {
  142. return view('livewire.default-setting');
  143. }
  144. }