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

DefaultSetting.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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->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 & Discounts')
  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. Select::make('sales_discount_id')
  68. ->label('Sales Discount')
  69. ->options(Defaults::getSalesDiscounts())
  70. ->default(Defaults::getDefaultSalesDiscount())
  71. ->searchable()
  72. ->validationAttribute('Sales Discount')
  73. ->required(),
  74. Select::make('purchase_discount_id')
  75. ->label('Purchase Discount')
  76. ->options(Defaults::getPurchaseDiscounts())
  77. ->default(Defaults::getDefaultPurchaseDiscount())
  78. ->searchable()
  79. ->validationAttribute('Purchase Discount')
  80. ->required(),
  81. ])->columns(),
  82. Section::make('Categories')
  83. ->schema([
  84. Select::make('income_category_id')
  85. ->label('Income Category')
  86. ->options(Defaults::getIncomeCategories())
  87. ->default(Defaults::getDefaultIncomeCategory())
  88. ->searchable()
  89. ->validationAttribute('Income Category')
  90. ->required(),
  91. Select::make('expense_category_id')
  92. ->label('Expense Category')
  93. ->options(Defaults::getExpenseCategories())
  94. ->default(Defaults::getDefaultExpenseCategory())
  95. ->searchable()
  96. ->validationAttribute('Expense Category')
  97. ->required(),
  98. ])->columns(),
  99. ];
  100. }
  101. public function create(): void
  102. {
  103. $data = $this->form->getState();
  104. $data = $this->mutateFormDataBeforeCreate($data);
  105. $this->record = $this->handleRecordCreation($data);
  106. $this->form->model($this->record)->saveRelationships();
  107. $this->getSavedNotification()?->send();
  108. }
  109. protected function mutateFormDataBeforeCreate(array $data): array
  110. {
  111. $data['company_id'] = Auth::user()->currentCompany->id;
  112. $data['updated_by'] = Auth::id();
  113. return $data;
  114. }
  115. protected function getRelatedEntities(): array
  116. {
  117. return [
  118. 'account_id' => [Account::class, 'id'],
  119. 'currency_code' => [Currency::class, 'code'],
  120. 'sales_tax_id' => [Tax::class, 'id', 'sales'],
  121. 'purchase_tax_id' => [Tax::class, 'id', 'purchase'],
  122. 'sales_discount_id' => [Tax::class, 'id', 'sales'],
  123. 'purchase_discount_id' => [Tax::class, 'id', 'purchase'],
  124. 'income_category_id' => [Category::class, 'id', 'income'],
  125. 'expense_category_id' => [Category::class, 'id', 'expense'],
  126. ];
  127. }
  128. protected function getFormModel(): string
  129. {
  130. return Defaults::class;
  131. }
  132. protected function getSavedNotification():?Notification
  133. {
  134. $title = $this->getSavedNotificationTitle();
  135. if (blank($title)) {
  136. return null;
  137. }
  138. return Notification::make()
  139. ->success()
  140. ->title($title);
  141. }
  142. protected function getSavedNotificationTitle(): ?string
  143. {
  144. return __('filament::resources/pages/edit-record.messages.saved');
  145. }
  146. public function render(): View
  147. {
  148. return view('livewire.default-setting');
  149. }
  150. }