您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ViewTransaction.php 7.8KB

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