| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 | <?php
namespace App\Filament\Company\Resources\Sales;
use App\Enums\Accounting\InvoiceStatus;
use App\Enums\Accounting\JournalEntryType;
use App\Enums\Accounting\PaymentMethod;
use App\Enums\Accounting\TransactionType;
use App\Filament\Company\Resources\Sales\InvoiceResource\Pages;
use App\Filament\Company\Resources\Sales\InvoiceResource\RelationManagers;
use App\Models\Accounting\Account;
use App\Models\Accounting\Adjustment;
use App\Models\Accounting\DocumentLineItem;
use App\Models\Accounting\Invoice;
use App\Models\Banking\BankAccount;
use App\Models\Common\Offering;
use App\Utilities\Currency\CurrencyAccessor;
use App\Utilities\Currency\CurrencyConverter;
use Awcodes\TableRepeater\Components\TableRepeater;
use Awcodes\TableRepeater\Header;
use Carbon\CarbonInterface;
use Closure;
use Filament\Forms;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Support\Enums\Alignment;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Livewire\Features\SupportFileUploads\TemporaryUploadedFile;
class InvoiceResource extends Resource
{
    protected static ?string $model = Invoice::class;
    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
    public static function form(Form $form): Form
    {
        $company = Auth::user()->currentCompany;
        return $form
            ->schema([
                Forms\Components\Section::make('Invoice Header')
                    ->collapsible()
                    ->schema([
                        Forms\Components\Split::make([
                            Forms\Components\Group::make([
                                FileUpload::make('logo')
                                    ->openable()
                                    ->maxSize(1024)
                                    ->localizeLabel()
                                    ->visibility('public')
                                    ->disk('public')
                                    ->directory('logos/document')
                                    ->imageResizeMode('contain')
                                    ->imageCropAspectRatio('3:2')
                                    ->panelAspectRatio('3:2')
                                    ->maxWidth(MaxWidth::ExtraSmall)
                                    ->panelLayout('integrated')
                                    ->removeUploadedFileButtonPosition('center bottom')
                                    ->uploadButtonPosition('center bottom')
                                    ->uploadProgressIndicatorPosition('center bottom')
                                    ->getUploadedFileNameForStorageUsing(
                                        static fn (TemporaryUploadedFile $file): string => (string) str($file->getClientOriginalName())
                                            ->prepend(Auth::user()->currentCompany->id . '_'),
                                    )
                                    ->acceptedFileTypes(['image/png', 'image/jpeg', 'image/gif']),
                            ]),
                            Forms\Components\Group::make([
                                Forms\Components\TextInput::make('header')
                                    ->default(fn () => $company->defaultInvoice->header),
                                Forms\Components\TextInput::make('subheader')
                                    ->default(fn () => $company->defaultInvoice->subheader),
                                Forms\Components\View::make('filament.forms.components.company-info')
                                    ->viewData([
                                        'company_name' => $company->name,
                                        'company_address' => $company->profile->address,
                                        'company_city' => $company->profile->city?->name,
                                        'company_state' => $company->profile->state?->name,
                                        'company_zip' => $company->profile->zip_code,
                                        'company_country' => $company->profile->state?->country->name,
                                    ]),
                            ])->grow(true),
                        ])->from('md'),
                    ]),
                Forms\Components\Section::make('Invoice Details')
                    ->schema([
                        Forms\Components\Split::make([
                            Forms\Components\Group::make([
                                Forms\Components\Select::make('client_id')
                                    ->relationship('client', 'name')
                                    ->preload()
                                    ->searchable()
                                    ->required(),
                            ]),
                            Forms\Components\Group::make([
                                Forms\Components\TextInput::make('invoice_number')
                                    ->label('Invoice Number')
                                    ->default(fn () => Invoice::getNextDocumentNumber()),
                                Forms\Components\TextInput::make('order_number')
                                    ->label('P.O/S.O Number'),
                                Forms\Components\DatePicker::make('date')
                                    ->label('Invoice Date')
                                    ->default(now()),
                                Forms\Components\DatePicker::make('due_date')
                                    ->label('Payment Due')
                                    ->default(function () use ($company) {
                                        return now()->addDays($company->defaultInvoice->payment_terms->getDays());
                                    }),
                            ])->grow(true),
                        ])->from('md'),
                        TableRepeater::make('lineItems')
                            ->relationship()
                            ->saveRelationshipsUsing(function (TableRepeater $component, Forms\Contracts\HasForms $livewire, ?array $state) {
                                if (! is_array($state)) {
                                    $state = [];
                                }
                                $relationship = $component->getRelationship();
                                $existingRecords = $component->getCachedExistingRecords();
                                $recordsToDelete = [];
                                foreach ($existingRecords->pluck($relationship->getRelated()->getKeyName()) as $keyToCheckForDeletion) {
                                    if (array_key_exists("record-{$keyToCheckForDeletion}", $state)) {
                                        continue;
                                    }
                                    $recordsToDelete[] = $keyToCheckForDeletion;
                                    $existingRecords->forget("record-{$keyToCheckForDeletion}");
                                }
                                $relationship
                                    ->whereKey($recordsToDelete)
                                    ->get()
                                    ->each(static fn (Model $record) => $record->delete());
                                $childComponentContainers = $component->getChildComponentContainers(
                                    withHidden: $component->shouldSaveRelationshipsWhenHidden(),
                                );
                                $itemOrder = 1;
                                $orderColumn = $component->getOrderColumn();
                                $translatableContentDriver = $livewire->makeFilamentTranslatableContentDriver();
                                foreach ($childComponentContainers as $itemKey => $item) {
                                    $itemData = $item->getState(shouldCallHooksBefore: false);
                                    if ($orderColumn) {
                                        $itemData[$orderColumn] = $itemOrder;
                                        $itemOrder++;
                                    }
                                    if ($record = ($existingRecords[$itemKey] ?? null)) {
                                        $itemData = $component->mutateRelationshipDataBeforeSave($itemData, record: $record);
                                        if ($itemData === null) {
                                            continue;
                                        }
                                        $translatableContentDriver ?
                                            $translatableContentDriver->updateRecord($record, $itemData) :
                                            $record->fill($itemData)->save();
                                        continue;
                                    }
                                    $relatedModel = $component->getRelatedModel();
                                    $itemData = $component->mutateRelationshipDataBeforeCreate($itemData);
                                    if ($itemData === null) {
                                        continue;
                                    }
                                    if ($translatableContentDriver) {
                                        $record = $translatableContentDriver->makeRecord($relatedModel, $itemData);
                                    } else {
                                        $record = new $relatedModel;
                                        $record->fill($itemData);
                                    }
                                    $record = $relationship->save($record);
                                    $item->model($record)->saveRelationships();
                                    $existingRecords->push($record);
                                }
                                $component->getRecord()->setRelation($component->getRelationshipName(), $existingRecords);
                                /** @var Invoice $invoice */
                                $invoice = $component->getRecord();
                                // Recalculate totals for line items
                                $invoice->lineItems()->each(function (DocumentLineItem $lineItem) {
                                    $lineItem->updateQuietly([
                                        'tax_total' => $lineItem->calculateTaxTotal()->getAmount(),
                                        'discount_total' => $lineItem->calculateDiscountTotal()->getAmount(),
                                    ]);
                                });
                                $subtotal = $invoice->lineItems()->sum('subtotal') / 100;
                                $taxTotal = $invoice->lineItems()->sum('tax_total') / 100;
                                $discountTotal = $invoice->lineItems()->sum('discount_total') / 100;
                                $grandTotal = $subtotal + $taxTotal - $discountTotal;
                                $invoice->updateQuietly([
                                    'subtotal' => $subtotal,
                                    'tax_total' => $taxTotal,
                                    'discount_total' => $discountTotal,
                                    'total' => $grandTotal,
                                ]);
                            })
                            ->headers([
                                Header::make('Items')->width('15%'),
                                Header::make('Description')->width('25%'),
                                Header::make('Quantity')->width('10%'),
                                Header::make('Price')->width('10%'),
                                Header::make('Taxes')->width('15%'),
                                Header::make('Discounts')->width('15%'),
                                Header::make('Amount')->width('10%')->align('right'),
                            ])
                            ->schema([
                                Forms\Components\Select::make('offering_id')
                                    ->relationship('sellableOffering', 'name')
                                    ->preload()
                                    ->searchable()
                                    ->required()
                                    ->live()
                                    ->afterStateUpdated(function (Forms\Set $set, Forms\Get $get, $state) {
                                        $offeringId = $state;
                                        $offeringRecord = Offering::with('salesTaxes')->find($offeringId);
                                        if ($offeringRecord) {
                                            $set('description', $offeringRecord->description);
                                            $set('unit_price', $offeringRecord->price);
                                            $set('salesTaxes', $offeringRecord->salesTaxes->pluck('id')->toArray());
                                            $set('salesDiscounts', $offeringRecord->salesDiscounts->pluck('id')->toArray());
                                        }
                                    }),
                                Forms\Components\TextInput::make('description'),
                                Forms\Components\TextInput::make('quantity')
                                    ->required()
                                    ->numeric()
                                    ->live()
                                    ->default(1),
                                Forms\Components\TextInput::make('unit_price')
                                    ->hiddenLabel()
                                    ->numeric()
                                    ->live()
                                    ->default(0),
                                Forms\Components\Select::make('salesTaxes')
                                    ->relationship('salesTaxes', 'name')
                                    ->preload()
                                    ->multiple()
                                    ->live()
                                    ->searchable(),
                                Forms\Components\Select::make('salesDiscounts')
                                    ->relationship('salesDiscounts', 'name')
                                    ->preload()
                                    ->multiple()
                                    ->live()
                                    ->searchable(),
                                Forms\Components\Placeholder::make('total')
                                    ->hiddenLabel()
                                    ->content(function (Forms\Get $get) {
                                        $quantity = max((float) ($get('quantity') ?? 0), 0);
                                        $unitPrice = max((float) ($get('unit_price') ?? 0), 0);
                                        $salesTaxes = $get('salesTaxes') ?? [];
                                        $salesDiscounts = $get('salesDiscounts') ?? [];
                                        $subtotal = $quantity * $unitPrice;
                                        // Calculate tax amount based on subtotal
                                        $taxAmount = 0;
                                        if (! empty($salesTaxes)) {
                                            $taxRates = Adjustment::whereIn('id', $salesTaxes)->pluck('rate');
                                            $taxAmount = collect($taxRates)->sum(fn ($rate) => $subtotal * ($rate / 100));
                                        }
                                        // Calculate discount amount based on subtotal
                                        $discountAmount = 0;
                                        if (! empty($salesDiscounts)) {
                                            $discountRates = Adjustment::whereIn('id', $salesDiscounts)->pluck('rate');
                                            $discountAmount = collect($discountRates)->sum(fn ($rate) => $subtotal * ($rate / 100));
                                        }
                                        // Final total
                                        $total = $subtotal + ($taxAmount - $discountAmount);
                                        return money($total, CurrencyAccessor::getDefaultCurrency(), true)->format();
                                    }),
                            ]),
                        Forms\Components\Grid::make(6)
                            ->schema([
                                Forms\Components\ViewField::make('totals')
                                    ->columnStart(5)
                                    ->columnSpan(2)
                                    ->view('filament.forms.components.invoice-totals'),
                            ]),
                        //                        Forms\Components\Repeater::make('lineItems')
                        //                            ->relationship()
                        //                            ->columns(8)
                        //                            ->schema([
                        //                                Forms\Components\Select::make('offering_id')
                        //                                    ->relationship('offering', 'name')
                        //                                    ->preload()
                        //                                    ->columnSpan(2)
                        //                                    ->searchable()
                        //                                    ->required()
                        //                                    ->live()
                        //                                    ->afterStateUpdated(function (Forms\Set $set, $state) {
                        //                                        $offeringId = $state;
                        //                                        $offeringRecord = Offering::with('salesTaxes')->find($offeringId);
                        //
                        //                                        if ($offeringRecord) {
                        //                                            $set('description', $offeringRecord->description);
                        //                                            $set('unit_price', $offeringRecord->price);
                        //                                            $set('total', $offeringRecord->price);
                        //
                        //                                            $salesTaxes = $offeringRecord->salesTaxes->map(function ($tax) {
                        //                                                return [
                        //                                                    'id' => $tax->id,
                        //                                                    'amount' => null, // Amount will be calculated dynamically
                        //                                                ];
                        //                                            })->toArray();
                        //
                        //                                            $set('taxes', $salesTaxes);
                        //                                        }
                        //                                    }),
                        //                                Forms\Components\TextInput::make('description')
                        //                                    ->columnSpan(3)
                        //                                    ->required(),
                        //                                Forms\Components\TextInput::make('quantity')
                        //                                    ->required()
                        //                                    ->numeric()
                        //                                    ->live()
                        //                                    ->default(1),
                        //                                Forms\Components\TextInput::make('unit_price')
                        //                                    ->live()
                        //                                    ->numeric()
                        //                                    ->default(0),
                        //                                Forms\Components\Placeholder::make('total')
                        //                                    ->content(function (Forms\Get $get) {
                        //                                        $quantity = $get('quantity');
                        //                                        $unitPrice = $get('unit_price');
                        //
                        //                                        if ($quantity && $unitPrice) {
                        //                                            return $quantity * $unitPrice;
                        //                                        }
                        //                                    }),
                        //                                TableRepeater::make('taxes')
                        //                                    ->relationship()
                        //                                    ->columnSpanFull()
                        //                                    ->columnStart(6)
                        //                                    ->headers([
                        //                                        Header::make('')->width('200px'),
                        //                                        Header::make('')->width('50px')->align('right'),
                        //                                    ])
                        //                                    ->defaultItems(0)
                        //                                    ->schema([
                        //                                        Forms\Components\Select::make('id') // The ID of the adjustment being attached.
                        //                                            ->label('Tax Adjustment')
                        //                                            ->options(
                        //                                                Adjustment::query()
                        //                                                    ->where('category', AdjustmentCategory::Tax)
                        //                                                    ->pluck('name', 'id')
                        //                                            )
                        //                                            ->preload()
                        //                                            ->searchable()
                        //                                            ->required()
                        //                                            ->live(),
                        //                                        Forms\Components\Placeholder::make('amount')
                        //                                            ->hiddenLabel()
                        //                                            ->content(function (Forms\Get $get) {
                        //                                                $quantity = $get('../../quantity') ?? 0; // Get parent quantity
                        //                                                $unitPrice = $get('../../unit_price') ?? 0; // Get parent unit price
                        //                                                $rate = Adjustment::find($get('id'))->rate ?? 0;
                        //
                        //                                                $total = $quantity * $unitPrice;
                        //
                        //                                                return $total * ($rate / 100);
                        //                                            }),
                        //                                    ]),
                        //                            ]),
                        Forms\Components\Textarea::make('terms')
                            ->columnSpanFull(),
                    ]),
                Forms\Components\Section::make('Invoice Footer')
                    ->collapsible()
                    ->schema([
                        Forms\Components\Textarea::make('footer')
                            ->columnSpanFull(),
                    ]),
            ]);
    }
    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('status')
                    ->badge()
                    ->searchable(),
                Tables\Columns\TextColumn::make('due_date')
                    ->label('Due')
                    ->formatStateUsing(function (Tables\Columns\TextColumn $column, mixed $state) {
                        if (blank($state)) {
                            return null;
                        }
                        $date = Carbon::parse($state)
                            ->setTimezone($timezone ?? $column->getTimezone());
                        if ($date->isToday()) {
                            return 'Today';
                        }
                        return $date->diffForHumans([
                            'options' => CarbonInterface::ONE_DAY_WORDS,
                        ]);
                    })
                    ->sortable(),
                Tables\Columns\TextColumn::make('date')
                    ->date()
                    ->sortable(),
                Tables\Columns\TextColumn::make('invoice_number')
                    ->label('Number')
                    ->searchable(),
                Tables\Columns\TextColumn::make('client.name')
                    ->sortable(),
                Tables\Columns\TextColumn::make('total')
                    ->currency(),
                Tables\Columns\TextColumn::make('amount_paid')
                    ->label('Amount Paid')
                    ->currency(),
                Tables\Columns\TextColumn::make('amount_due')
                    ->label('Amount Due')
                    ->currency(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\ActionGroup::make([
                    Tables\Actions\EditAction::make(),
                    Tables\Actions\ViewAction::make(),
                    Tables\Actions\DeleteAction::make(),
                    Tables\Actions\ReplicateAction::make()
                        ->label('Duplicate')
                        ->excludeAttributes(['status', 'amount_paid', 'amount_due', 'created_by', 'updated_by', 'created_at', 'updated_at', 'invoice_number', 'date', 'due_date'])
                        ->modal(false)
                        ->beforeReplicaSaved(function (Invoice $original, Invoice $replica) {
                            $replica->status = InvoiceStatus::Draft;
                            $replica->invoice_number = Invoice::getNextDocumentNumber();
                            $replica->date = now();
                            $replica->due_date = now()->addDays($original->company->defaultInvoice->payment_terms->getDays());
                        })
                        ->databaseTransaction()
                        ->after(function (Invoice $original, Invoice $replica) {
                            $original->lineItems->each(function (DocumentLineItem $lineItem) use ($replica) {
                                $replicaLineItem = $lineItem->replicate([
                                    'documentable_id',
                                    'documentable_type',
                                    'subtotal',
                                    'total',
                                    'created_by',
                                    'updated_by',
                                    'created_at',
                                    'updated_at',
                                ]);
                                $replicaLineItem->documentable_id = $replica->id;
                                $replicaLineItem->documentable_type = $replica->getMorphClass();
                                $replicaLineItem->save();
                                $replicaLineItem->adjustments()->sync($lineItem->adjustments->pluck('id'));
                            });
                        })
                        ->successRedirectUrl(function (Invoice $replica) {
                            return InvoiceResource::getUrl('edit', ['record' => $replica]);
                        }),
                    Tables\Actions\Action::make('approveDraft')
                        ->label('Approve')
                        ->icon('heroicon-o-check-circle')
                        ->visible(function (Invoice $record) {
                            return $record->isDraft();
                        })
                        ->databaseTransaction()
                        ->successNotificationTitle('Invoice Approved')
                        ->action(function (Invoice $record, Tables\Actions\Action $action) {
                            $transaction = $record->transactions()->create([
                                'type' => TransactionType::Journal,
                                'posted_at' => now(),
                                'amount' => $record->total,
                                'description' => 'Invoice Approval for Invoice #' . $record->invoice_number,
                            ]);
                            $transaction->journalEntries()->create([
                                'type' => JournalEntryType::Debit,
                                'account_id' => Account::getAccountsReceivableAccount()->id,
                                'amount' => $record->total,
                                'description' => $transaction->description,
                            ]);
                            foreach ($record->lineItems as $lineItem) {
                                $transaction->journalEntries()->create([
                                    'type' => JournalEntryType::Credit,
                                    'account_id' => $lineItem->offering->income_account_id,
                                    'amount' => $lineItem->subtotal,
                                    'description' => $transaction->description,
                                ]);
                                foreach ($lineItem->adjustments as $adjustment) {
                                    $transaction->journalEntries()->create([
                                        'type' => $adjustment->category->isDiscount() ? JournalEntryType::Debit : JournalEntryType::Credit,
                                        'account_id' => $adjustment->account_id,
                                        'amount' => $lineItem->calculateAdjustmentTotal($adjustment)->getAmount(),
                                        'description' => $transaction->description,
                                    ]);
                                }
                            }
                            $record->updateQuietly([
                                'status' => InvoiceStatus::Unsent,
                            ]);
                            $action->success();
                        }),
                    Tables\Actions\Action::make('markAsSent')
                        ->label('Mark as Sent')
                        ->icon('heroicon-o-paper-airplane')
                        ->visible(function (Invoice $record) {
                            return $record->status === InvoiceStatus::Unsent;
                        })
                        ->successNotificationTitle('Invoice Sent')
                        ->action(function (Invoice $record, Tables\Actions\Action $action) {
                            $record->updateQuietly([
                                'status' => InvoiceStatus::Sent,
                            ]);
                            $action->success();
                        }),
                    Tables\Actions\Action::make('recordPayment')
                        ->label(fn (Invoice $record) => $record->status === InvoiceStatus::Overpaid ? 'Refund Overpayment' : 'Record Payment')
                        ->stickyModalHeader()
                        ->stickyModalFooter()
                        ->modalFooterActionsAlignment(Alignment::End)
                        ->modalWidth(MaxWidth::TwoExtraLarge)
                        ->icon('heroicon-o-credit-card')
                        ->visible(function (Invoice $record) {
                            return $record->canRecordPayment();
                        })
                        ->mountUsing(function (Invoice $record, Form $form) {
                            $form->fill([
                                'posted_at' => now(),
                                'amount' => $record->status === InvoiceStatus::Overpaid ? ltrim($record->amount_due, '-') : $record->amount_due,
                            ]);
                        })
                        ->databaseTransaction()
                        ->successNotificationTitle('Payment Recorded')
                        ->form([
                            Forms\Components\DatePicker::make('posted_at')
                                ->label('Date'),
                            Forms\Components\TextInput::make('amount')
                                ->label('Amount')
                                ->required()
                                ->money()
                                ->live(onBlur: true)
                                ->helperText(function (Invoice $record, $state) {
                                    if (! CurrencyConverter::isValidAmount($state)) {
                                        return null;
                                    }
                                    $amountDue = $record->getRawOriginal('amount_due');
                                    $amount = CurrencyConverter::convertToCents($state);
                                    if ($amount <= 0) {
                                        return 'Please enter a valid positive amount';
                                    }
                                    if ($record->status === InvoiceStatus::Overpaid) {
                                        $newAmountDue = $amountDue + $amount;
                                    } else {
                                        $newAmountDue = $amountDue - $amount;
                                    }
                                    return match (true) {
                                        $newAmountDue > 0 => 'Amount due after payment will be ' . CurrencyConverter::formatCentsToMoney($newAmountDue),
                                        $newAmountDue === 0 => 'Invoice will be fully paid',
                                        default => 'Invoice will be overpaid by ' . CurrencyConverter::formatCentsToMoney(abs($newAmountDue)),
                                    };
                                })
                                ->rules([
                                    static fn (): Closure => static function (string $attribute, $value, Closure $fail) {
                                        if (! CurrencyConverter::isValidAmount($value)) {
                                            $fail('Please enter a valid amount');
                                        }
                                    },
                                ]),
                            Forms\Components\Select::make('payment_method')
                                ->label('Payment Method')
                                ->required()
                                ->options(PaymentMethod::class),
                            Forms\Components\Select::make('bank_account_id')
                                ->label('Account')
                                ->required()
                                ->options(BankAccount::query()
                                    ->get()
                                    ->pluck('account.name', 'id'))
                                ->searchable(),
                            Forms\Components\Textarea::make('notes')
                                ->label('Notes'),
                        ])
                        ->action(function (Invoice $record, Tables\Actions\Action $action, array $data) {
                            $record->recordPayment($data);
                            $action->success();
                        }),
                ]),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
    public static function getRelations(): array
    {
        return [
            RelationManagers\PaymentsRelationManager::class,
        ];
    }
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListInvoices::route('/'),
            'create' => Pages\CreateInvoice::route('/create'),
            'view' => Pages\ViewInvoice::route('/{record}'),
            'edit' => Pages\EditInvoice::route('/{record}/edit'),
        ];
    }
}
 |