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

EditTransactionAction.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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->visible(static fn (Transaction $transaction) => ! $transaction->transactionable_id);
  25. $this->modalWidth(function (): MaxWidth {
  26. return match ($this->transactionType) {
  27. TransactionType::Journal => MaxWidth::Screen,
  28. default => MaxWidth::ThreeExtraLarge,
  29. };
  30. });
  31. $this->extraModalWindowAttributes(function (): array {
  32. if ($this->transactionType === TransactionType::Journal) {
  33. return ['class' => 'journal-transaction-modal'];
  34. }
  35. return [];
  36. });
  37. $this->modalHeading(function (): string {
  38. return match ($this->transactionType) {
  39. TransactionType::Journal => 'Journal Entry',
  40. TransactionType::Transfer => 'Edit Transfer',
  41. default => 'Edit Transaction',
  42. };
  43. });
  44. $this->form(function (Form $form) {
  45. return match ($this->transactionType) {
  46. TransactionType::Transfer => $this->transferForm($form),
  47. TransactionType::Journal => $this->journalTransactionForm($form),
  48. default => $this->transactionForm($form),
  49. };
  50. });
  51. $this->afterFormFilled(function (Transaction $record) {
  52. if ($this->transactionType === TransactionType::Journal) {
  53. $debitAmounts = $record->journalEntries->sumDebits()->getAmount();
  54. $creditAmounts = $record->journalEntries->sumCredits()->getAmount();
  55. $this->setDebitAmount($debitAmounts);
  56. $this->setCreditAmount($creditAmounts);
  57. }
  58. });
  59. $this->modalSubmitAction(function (StaticAction $action) {
  60. if ($this->transactionType === TransactionType::Journal) {
  61. $action->disabled(! $this->isJournalEntryBalanced());
  62. }
  63. return $action;
  64. });
  65. $this->after(function (Transaction $transaction) {
  66. if ($this->transactionType === TransactionType::Journal) {
  67. $transaction->updateAmountIfBalanced();
  68. }
  69. });
  70. }
  71. }