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

EditTransactionAction.php 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\EditAction;
  7. use Filament\Actions\StaticAction;
  8. use Filament\Forms\Form;
  9. use Filament\Support\Enums\MaxWidth;
  10. class EditTransactionAction extends EditAction
  11. {
  12. use HasTransactionAction;
  13. protected function setUp(): void
  14. {
  15. parent::setUp();
  16. $this->transactionType = $this->getRecord()->type;
  17. $this->label(function () {
  18. return match ($this->transactionType) {
  19. TransactionType::Transfer => 'Edit transfer',
  20. TransactionType::Journal => 'Edit journal entry',
  21. default => 'Edit transaction',
  22. };
  23. });
  24. $this->slideOver();
  25. $this->visible(static fn (Transaction $transaction) => ! $transaction->transactionable_id);
  26. $this->modalWidth(function (): MaxWidth {
  27. return match ($this->transactionType) {
  28. TransactionType::Journal => MaxWidth::Screen,
  29. default => MaxWidth::ThreeExtraLarge,
  30. };
  31. });
  32. $this->extraModalWindowAttributes(function (): array {
  33. if ($this->transactionType === TransactionType::Journal) {
  34. return ['class' => 'journal-transaction-modal'];
  35. }
  36. return [];
  37. });
  38. $this->modalHeading(function (): string {
  39. return match ($this->transactionType) {
  40. TransactionType::Journal => 'Journal Entry',
  41. TransactionType::Transfer => 'Edit Transfer',
  42. default => 'Edit Transaction',
  43. };
  44. });
  45. $this->form(function (Form $form) {
  46. return match ($this->transactionType) {
  47. TransactionType::Transfer => $this->transferForm($form),
  48. TransactionType::Journal => $this->journalTransactionForm($form),
  49. default => $this->transactionForm($form),
  50. };
  51. });
  52. $this->afterFormFilled(function (Transaction $record) {
  53. if ($this->transactionType === TransactionType::Journal) {
  54. $debitAmounts = $record->journalEntries->sumDebits()->getAmount();
  55. $creditAmounts = $record->journalEntries->sumCredits()->getAmount();
  56. $this->setDebitAmount($debitAmounts);
  57. $this->setCreditAmount($creditAmounts);
  58. }
  59. });
  60. $this->modalSubmitAction(function (StaticAction $action) {
  61. if ($this->transactionType === TransactionType::Journal) {
  62. $action->disabled(! $this->isJournalEntryBalanced());
  63. }
  64. return $action;
  65. });
  66. $this->after(function (Transaction $transaction) {
  67. if ($this->transactionType === TransactionType::Journal) {
  68. $transaction->updateAmountIfBalanced();
  69. }
  70. });
  71. }
  72. }