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

CreateTransactionAction.php 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Filament\Actions;
  3. use App\Concerns\HasTransactionAction;
  4. use App\Enums\Accounting\TransactionType;
  5. use App\Models\Accounting\Transaction;
  6. use Filament\Actions\CreateAction;
  7. use Filament\Actions\StaticAction;
  8. use Filament\Forms\Form;
  9. use Filament\Support\Enums\MaxWidth;
  10. class CreateTransactionAction extends CreateAction
  11. {
  12. use HasTransactionAction;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->label(null);
  17. $this->groupedIcon(null);
  18. $this->slideOver();
  19. $this->modalWidth(function (): MaxWidth {
  20. return match ($this->getTransactionType()) {
  21. TransactionType::Journal => MaxWidth::Screen,
  22. default => MaxWidth::ThreeExtraLarge,
  23. };
  24. });
  25. $this->extraModalWindowAttributes(function (): array {
  26. if ($this->getTransactionType() === TransactionType::Journal) {
  27. return ['class' => 'journal-transaction-modal'];
  28. }
  29. return [];
  30. });
  31. $this->modalHeading(function (): string {
  32. return match ($this->getTransactionType()) {
  33. TransactionType::Journal => 'Create journal entry',
  34. default => 'Create transaction',
  35. };
  36. });
  37. $this->fillForm(fn (): array => $this->getFormDefaultsForType($this->getTransactionType()));
  38. $this->form(function (Form $form) {
  39. return match ($this->getTransactionType()) {
  40. TransactionType::Transfer => $this->transferForm($form),
  41. TransactionType::Journal => $this->journalTransactionForm($form),
  42. default => $this->transactionForm($form),
  43. };
  44. });
  45. $this->afterFormFilled(function () {
  46. if ($this->getTransactionType() === TransactionType::Journal) {
  47. $this->resetJournalEntryAmounts();
  48. }
  49. });
  50. $this->modalSubmitAction(function (StaticAction $action) {
  51. if ($this->getTransactionType() === TransactionType::Journal) {
  52. $action->disabled(! $this->isJournalEntryBalanced());
  53. }
  54. return $action;
  55. });
  56. $this->after(function (Transaction $transaction) {
  57. if ($this->getTransactionType() === TransactionType::Journal) {
  58. $transaction->updateAmountIfBalanced();
  59. }
  60. });
  61. $this->mutateFormDataUsing(function (array $data) {
  62. if ($this->getTransactionType() === TransactionType::Journal) {
  63. $data['type'] = TransactionType::Journal;
  64. }
  65. return $data;
  66. });
  67. $this->outlined(fn () => ! $this->getGroup());
  68. }
  69. }