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

ManageTransaction.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\TransactionResource\Pages;
  3. use App\Enums\Accounting\AccountCategory;
  4. use App\Filament\Company\Resources\Accounting\TransactionResource;
  5. use App\Models\Accounting\Account;
  6. use App\Models\Banking\BankAccount;
  7. use Filament\Actions;
  8. use Filament\Resources\Pages\ManageRecords;
  9. use Filament\Support\Enums\MaxWidth;
  10. class ManageTransaction extends ManageRecords
  11. {
  12. public static string $resource = TransactionResource::class;
  13. protected function getHeaderActions(): array
  14. {
  15. return [
  16. $this->createAction(
  17. name: 'addIncome',
  18. label: 'Add Income',
  19. type: 'deposit',
  20. ),
  21. $this->createAction(
  22. name: 'addExpense',
  23. label: 'Add Expense',
  24. type: 'withdrawal',
  25. ),
  26. ];
  27. }
  28. protected function createAction(string $name, string $label, string $type): Actions\CreateAction
  29. {
  30. return Actions\CreateAction::make($name)
  31. ->label($label)
  32. ->modalWidth(MaxWidth::ThreeExtraLarge)
  33. ->stickyModalHeader()
  34. ->stickyModalFooter()
  35. ->button()
  36. ->outlined()
  37. ->fillForm(static fn (): array => [
  38. 'type' => $type,
  39. 'posted_at' => now()->format('Y-m-d'),
  40. 'bank_account_id' => BankAccount::where('enabled', true)->first()->id ?? null,
  41. 'amount' => '0.00',
  42. 'account_id' => static::getUncategorizedAccountByType($type)?->id,
  43. ]);
  44. }
  45. public static function getUncategorizedAccountByType(string $type): ?Account
  46. {
  47. [$category, $accountName] = match ($type) {
  48. 'deposit' => [AccountCategory::Revenue, 'Uncategorized Income'],
  49. 'withdrawal' => [AccountCategory::Expense, 'Uncategorized Expense'],
  50. };
  51. return Account::where('category', $category)
  52. ->where('name', $accountName)
  53. ->first();
  54. }
  55. }