You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewTransaction.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Filament\Company\Resources\Accounting\TransactionResource\Pages;
  3. use App\Filament\Actions\EditTransactionAction;
  4. use App\Filament\Company\Resources\Accounting\TransactionResource;
  5. use App\Filament\Company\Resources\Purchases\BillResource\Pages\ViewBill;
  6. use App\Filament\Company\Resources\Purchases\VendorResource;
  7. use App\Filament\Company\Resources\Sales\ClientResource;
  8. use App\Filament\Company\Resources\Sales\InvoiceResource\Pages\ViewInvoice;
  9. use App\Models\Accounting\Bill;
  10. use App\Models\Accounting\Invoice;
  11. use App\Models\Accounting\JournalEntry;
  12. use App\Models\Accounting\Transaction;
  13. use App\Models\Common\Client;
  14. use App\Models\Common\Vendor;
  15. use Filament\Actions;
  16. use Filament\Infolists\Components\IconEntry;
  17. use Filament\Infolists\Components\Section;
  18. use Filament\Infolists\Components\TextEntry;
  19. use Filament\Infolists\Infolist;
  20. use Filament\Resources\Pages\ViewRecord;
  21. use Filament\Support\Enums\IconPosition;
  22. use function Filament\Support\get_model_label;
  23. class ViewTransaction extends ViewRecord
  24. {
  25. protected static string $resource = TransactionResource::class;
  26. protected function getHeaderActions(): array
  27. {
  28. return [
  29. EditTransactionAction::make()
  30. ->outlined(),
  31. Actions\ViewAction::make('viewAssociatedDocument')
  32. ->outlined()
  33. ->icon('heroicon-o-document-text')
  34. ->hidden(static fn (Transaction $record): bool => ! $record->transactionable_id)
  35. ->label(static function (Transaction $record) {
  36. if (! $record->transactionable_type) {
  37. return 'View document';
  38. }
  39. return 'View ' . get_model_label($record->transactionable_type);
  40. })
  41. ->url(static function (Transaction $record) {
  42. return match ($record->transactionable_type) {
  43. Bill::class => ViewBill::getUrl(['record' => $record->transactionable_id]),
  44. Invoice::class => ViewInvoice::getUrl(['record' => $record->transactionable_id]),
  45. default => null,
  46. };
  47. }),
  48. Actions\ActionGroup::make([
  49. Actions\ActionGroup::make([
  50. Actions\Action::make('markAsReviewed')
  51. ->label(static fn (Transaction $record) => $record->reviewed ? 'Mark as unreviewed' : 'Mark as reviewed')
  52. ->icon(static fn (Transaction $record) => $record->reviewed ? 'heroicon-s-check-circle' : 'heroicon-o-check-circle')
  53. ->disabled(fn (Transaction $record): bool => $record->isUncategorized())
  54. ->action(fn (Transaction $record) => $record->update(['reviewed' => ! $record->reviewed])),
  55. Actions\ReplicateAction::make()
  56. ->excludeAttributes(['created_by', 'updated_by', 'created_at', 'updated_at'])
  57. ->modal(false)
  58. ->beforeReplicaSaved(static function (Transaction $replica) {
  59. $replica->description = '(Copy of) ' . $replica->description;
  60. })
  61. ->hidden(static fn (Transaction $transaction) => $transaction->transactionable_id)
  62. ->after(static function (Transaction $original, Transaction $replica) {
  63. $original->journalEntries->each(function (JournalEntry $entry) use ($replica) {
  64. $entry->replicate([
  65. 'transaction_id',
  66. ])->fill([
  67. 'transaction_id' => $replica->id,
  68. ])->save();
  69. });
  70. }),
  71. ])->dropdown(false),
  72. Actions\DeleteAction::make(),
  73. ])
  74. ->label('Actions')
  75. ->button()
  76. ->outlined()
  77. ->dropdownPlacement('bottom-end')
  78. ->icon('heroicon-m-chevron-down')
  79. ->iconPosition(IconPosition::After),
  80. ];
  81. }
  82. public function infolist(Infolist $infolist): Infolist
  83. {
  84. return $infolist
  85. ->schema([
  86. Section::make('Transaction Details')
  87. ->columns(3)
  88. ->schema([
  89. TextEntry::make('posted_at')
  90. ->label('Date')
  91. ->date(),
  92. TextEntry::make('type')
  93. ->badge(),
  94. IconEntry::make('is_payment')
  95. ->label('Payment')
  96. ->boolean(),
  97. TextEntry::make('description')
  98. ->label('Description'),
  99. TextEntry::make('bankAccount.account.name')
  100. ->label('Account')
  101. ->hidden(static fn (Transaction $record): bool => ! $record->bankAccount),
  102. TextEntry::make('payeeable.name')
  103. ->label('Payee')
  104. ->hidden(static fn (Transaction $record): bool => ! $record->payeeable_type)
  105. ->url(static function (Transaction $record): ?string {
  106. if (! $record->payeeable_type) {
  107. return null;
  108. }
  109. return match ($record->payeeable_type) {
  110. Vendor::class => VendorResource::getUrl('view', ['record' => $record->payeeable_id]),
  111. Client::class => ClientResource::getUrl('view', ['record' => $record->payeeable_id]),
  112. default => null,
  113. };
  114. })
  115. ->link(),
  116. TextEntry::make('account.name')
  117. ->label('Category')
  118. ->hidden(static fn (Transaction $record): bool => ! $record->account),
  119. TextEntry::make('amount')
  120. ->label('Amount')
  121. ->currency(static fn (Transaction $record) => $record->bankAccount?->account->currency_code ?? 'USD'),
  122. TextEntry::make('reviewed')
  123. ->label('Status')
  124. ->badge()
  125. ->formatStateUsing(static fn (bool $state): string => $state ? 'Reviewed' : 'Not Reviewed')
  126. ->color(static fn (bool $state): string => $state ? 'success' : 'warning'),
  127. TextEntry::make('notes')
  128. ->label('Notes')
  129. ->columnSpan(2)
  130. ->visible(static fn (Transaction $record): bool => filled($record->notes)),
  131. ]),
  132. ]);
  133. }
  134. protected function getAllRelationManagers(): array
  135. {
  136. return [
  137. TransactionResource\RelationManagers\JournalEntriesRelationManager::class,
  138. ];
  139. }
  140. }