您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DefaultSetting.php 6.5KB

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