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.

ViewRecurringInvoice.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Filament\Company\Resources\Sales\RecurringInvoiceResource\Pages;
  3. use App\Enums\Accounting\DocumentType;
  4. use App\Filament\Company\Resources\Sales\ClientResource;
  5. use App\Filament\Company\Resources\Sales\InvoiceResource\Pages\ListInvoices;
  6. use App\Filament\Company\Resources\Sales\RecurringInvoiceResource;
  7. use App\Filament\Infolists\Components\BannerEntry;
  8. use App\Filament\Infolists\Components\DocumentPreview;
  9. use App\Models\Accounting\RecurringInvoice;
  10. use Filament\Actions;
  11. use Filament\Infolists\Components\Actions\Action;
  12. use Filament\Infolists\Components\Grid;
  13. use Filament\Infolists\Components\Section;
  14. use Filament\Infolists\Components\TextEntry;
  15. use Filament\Infolists\Infolist;
  16. use Filament\Resources\Pages\ViewRecord;
  17. use Filament\Support\Enums\FontWeight;
  18. use Filament\Support\Enums\IconPosition;
  19. use Illuminate\Support\HtmlString;
  20. use Illuminate\Support\Str;
  21. class ViewRecurringInvoice extends ViewRecord
  22. {
  23. protected static string $resource = RecurringInvoiceResource::class;
  24. protected function getHeaderActions(): array
  25. {
  26. return [
  27. Actions\EditAction::make()
  28. ->label('Edit recurring invoice')
  29. ->outlined(),
  30. Actions\ActionGroup::make([
  31. Actions\ActionGroup::make([
  32. RecurringInvoice::getManageScheduleAction(),
  33. RecurringInvoice::getApproveDraftAction(),
  34. RecurringInvoice::getPrintDocumentAction(),
  35. ])->dropdown(false),
  36. Actions\DeleteAction::make(),
  37. ])
  38. ->label('Actions')
  39. ->button()
  40. ->outlined()
  41. ->dropdownPlacement('bottom-end')
  42. ->icon('heroicon-m-chevron-down')
  43. ->iconPosition(IconPosition::After),
  44. ];
  45. }
  46. public function infolist(Infolist $infolist): Infolist
  47. {
  48. return $infolist
  49. ->schema([
  50. BannerEntry::make('inactiveAdjustments')
  51. ->label('Inactive adjustments')
  52. ->warning()
  53. ->icon('heroicon-o-exclamation-triangle')
  54. ->visible(fn (RecurringInvoice $record) => $record->hasInactiveAdjustments() && $record->canBeApproved())
  55. ->columnSpanFull()
  56. ->description(function (RecurringInvoice $record) {
  57. $inactiveAdjustments = collect();
  58. foreach ($record->lineItems as $lineItem) {
  59. foreach ($lineItem->adjustments as $adjustment) {
  60. if ($adjustment->isInactive() && $inactiveAdjustments->doesntContain($adjustment->name)) {
  61. $inactiveAdjustments->push($adjustment->name);
  62. }
  63. }
  64. }
  65. $adjustmentsList = $inactiveAdjustments->map(static function ($name) {
  66. return "<span class='font-medium'>{$name}</span>";
  67. })->join(', ');
  68. $output = "<p class='text-sm'>This recurring invoice contains inactive adjustments that need to be addressed before approval: {$adjustmentsList}</p>";
  69. return new HtmlString($output);
  70. }),
  71. BannerEntry::make('scheduleIsNotSet')
  72. ->info()
  73. ->title('Schedule not set')
  74. ->description('The schedule for this recurring invoice has not been set. You must set a schedule before you can approve this draft and start creating invoices.')
  75. ->visible(fn (RecurringInvoice $record) => ! $record->hasValidStartDate())
  76. ->columnSpanFull()
  77. ->actions([
  78. RecurringInvoice::getManageScheduleAction(Action::class)
  79. ->outlined(),
  80. ]),
  81. BannerEntry::make('readyToApprove')
  82. ->info()
  83. ->title('Ready to Approve')
  84. ->description('This recurring invoice is ready for approval. Review the details, and approve it when you’re ready to start generating invoices.')
  85. ->visible(fn (RecurringInvoice $record) => $record->canBeApproved() && ! $record->hasInactiveAdjustments())
  86. ->columnSpanFull()
  87. ->actions([
  88. RecurringInvoice::getApproveDraftAction(Action::class)
  89. ->outlined(),
  90. ]),
  91. Section::make('Invoice Details')
  92. ->columns(4)
  93. ->schema([
  94. Grid::make(1)
  95. ->schema([
  96. TextEntry::make('status')
  97. ->badge(),
  98. TextEntry::make('client.name')
  99. ->label('Client')
  100. ->color('primary')
  101. ->weight(FontWeight::SemiBold)
  102. ->url(static fn (RecurringInvoice $record) => ClientResource::getUrl('view', ['record' => $record->client_id]))
  103. ->link(),
  104. TextEntry::make('last_date')
  105. ->label('Last invoice')
  106. ->date()
  107. ->placeholder('Not Created'),
  108. TextEntry::make('next_date')
  109. ->label('Next invoice')
  110. ->placeholder('Not Scheduled')
  111. ->date(),
  112. TextEntry::make('schedule')
  113. ->label('Schedule')
  114. ->getStateUsing(function (RecurringInvoice $record) {
  115. return $record->getScheduleDescription();
  116. })
  117. ->helperText(function (RecurringInvoice $record) {
  118. return $record->getTimelineDescription();
  119. }),
  120. TextEntry::make('occurrences_count')
  121. ->label('Created to date')
  122. ->visible(static fn (RecurringInvoice $record) => $record->occurrences_count > 0)
  123. ->color('primary')
  124. ->weight(FontWeight::SemiBold)
  125. ->suffix(fn (RecurringInvoice $record) => Str::of(' invoice')->plural($record->occurrences_count))
  126. ->url(static function (RecurringInvoice $record) {
  127. return ListInvoices::getUrl(['recurringInvoice' => $record->id]);
  128. }),
  129. TextEntry::make('end_date')
  130. ->label('Ends on')
  131. ->date()
  132. ->visible(fn (RecurringInvoice $record) => $record->end_type?->isOn()),
  133. TextEntry::make('approved_at')
  134. ->label('Approved at')
  135. ->placeholder('Not Approved')
  136. ->date(),
  137. TextEntry::make('ended_at')
  138. ->label('Ended at')
  139. ->date()
  140. ->visible(fn (RecurringInvoice $record) => $record->ended_at),
  141. TextEntry::make('total')
  142. ->label('Invoice amount')
  143. ->currency(static fn (RecurringInvoice $record) => $record->currency_code),
  144. ])->columnSpan(1),
  145. DocumentPreview::make()
  146. ->type(DocumentType::RecurringInvoice),
  147. ]),
  148. ]);
  149. }
  150. }