選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

EditTransactionAction.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Filament\Tables\Actions;
  3. use App\Concerns\HasTransactionAction;
  4. use App\Enums\Accounting\TransactionType;
  5. use App\Models\Accounting\Transaction;
  6. use Filament\Actions\StaticAction;
  7. use Filament\Forms\Form;
  8. use Filament\Support\Enums\MaxWidth;
  9. use Filament\Tables\Actions\EditAction;
  10. class EditTransactionAction extends EditAction
  11. {
  12. use HasTransactionAction;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->modalWidth(function (): MaxWidth {
  17. return match ($this->transactionType) {
  18. TransactionType::Journal => MaxWidth::Screen,
  19. default => MaxWidth::ThreeExtraLarge,
  20. };
  21. });
  22. $this->extraModalWindowAttributes(function (): array {
  23. if ($this->transactionType === TransactionType::Journal) {
  24. return ['class' => 'journal-transaction-modal'];
  25. }
  26. return [];
  27. });
  28. $this->modalHeading(function (): string {
  29. return match ($this->transactionType) {
  30. TransactionType::Journal => 'Journal Entry',
  31. TransactionType::Transfer => 'Edit Transfer',
  32. default => 'Edit Transaction',
  33. };
  34. });
  35. $this->form(function (Form $form) {
  36. return match ($this->transactionType) {
  37. TransactionType::Transfer => $this->transferForm($form),
  38. TransactionType::Journal => $this->journalTransactionForm($form),
  39. default => $this->transactionForm($form),
  40. };
  41. });
  42. $this->afterFormFilled(function (Transaction $record) {
  43. if ($this->transactionType === TransactionType::Journal) {
  44. $debitAmounts = $record->journalEntries->sumDebits()->getAmount();
  45. $creditAmounts = $record->journalEntries->sumCredits()->getAmount();
  46. $this->setDebitAmount($debitAmounts);
  47. $this->setCreditAmount($creditAmounts);
  48. }
  49. });
  50. $this->modalSubmitAction(function (StaticAction $action) {
  51. if ($this->transactionType === TransactionType::Journal) {
  52. $action->disabled(! $this->isJournalEntryBalanced());
  53. }
  54. return $action;
  55. });
  56. $this->after(function (Transaction $transaction) {
  57. if ($this->transactionType === TransactionType::Journal) {
  58. $transaction->updateAmountIfBalanced();
  59. }
  60. });
  61. }
  62. }