201 lines
8.0 KiB
PHP
201 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Resources;
|
|
|
|
use App\Admin\Components\UserComponent;
|
|
use App\Admin\Resources\InvoiceResource\Pages;
|
|
use App\Admin\Resources\InvoiceResource\RelationManagers;
|
|
use App\Models\Currency;
|
|
use App\Models\Invoice;
|
|
use App\Models\Service;
|
|
use App\Models\ServiceUpgrade;
|
|
use Filament\Forms;
|
|
use Filament\Forms\Form;
|
|
use Filament\Forms\Get;
|
|
use Filament\Resources\Resource;
|
|
use Filament\Support\RawJs;
|
|
use Filament\Tables;
|
|
use Filament\Tables\Filters\SelectFilter;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class InvoiceResource extends Resource
|
|
{
|
|
protected static ?string $model = Invoice::class;
|
|
|
|
protected static ?string $navigationIcon = 'ri-receipt-line';
|
|
|
|
protected static ?string $activeNavigationIcon = 'ri-receipt-fill';
|
|
|
|
public static function getNavigationBadge(): ?string
|
|
{
|
|
return static::getModel()::where('status', 'pending')->count() ?: null;
|
|
}
|
|
|
|
public static function getNavigationBadgeColor(): ?string
|
|
{
|
|
return 'warning';
|
|
}
|
|
|
|
public static ?string $navigationGroup = 'Administration';
|
|
|
|
public static function form(Form $form): Form
|
|
{
|
|
return $form
|
|
->schema([
|
|
UserComponent::make('user_id'),
|
|
Forms\Components\TextInput::make('number')
|
|
->label('Invoice Number')
|
|
->helperText('The invoice number will be generated automatically')
|
|
->disabled(),
|
|
Forms\Components\DatePicker::make('created_at')
|
|
->label('Issued At')
|
|
->required()
|
|
->default(now())
|
|
->placeholder('Select the date and time the invoice was issued'),
|
|
Forms\Components\DatePicker::make('due_at')
|
|
->label('Due At')
|
|
->required()
|
|
->default(now()->addDays(7))
|
|
->placeholder('Select the date and time the invoice is due'),
|
|
Forms\Components\Select::make('status')
|
|
->label('Status')
|
|
->required()
|
|
->options([
|
|
'paid' => 'Paid',
|
|
'pending' => 'Pending',
|
|
'cancelled' => 'Cancelled',
|
|
])
|
|
->default('pending')
|
|
->placeholder('Select the status of the invoice'),
|
|
Forms\Components\Select::make('currency_code')
|
|
->label('Currency')
|
|
->required()
|
|
->relationship('currency', 'code')
|
|
->placeholder('Select the currency'),
|
|
Forms\Components\Toggle::make('send_email')
|
|
->label('Send Email')
|
|
->hiddenOn('edit')
|
|
->default(true),
|
|
Forms\Components\Repeater::make('items')
|
|
->relationship('items')
|
|
->label('Items')
|
|
->columnSpanFull()
|
|
->columns(2)
|
|
->schema([
|
|
Forms\Components\TextInput::make('price')
|
|
->label('Price')
|
|
// Grab invoice currency
|
|
->prefix(fn (Get $get): ?string => Currency::where('code', $get('../../currency_code'))->first()?->prefix)
|
|
->suffix(fn (Get $get): ?string => Currency::where('code', $get('../../currency_code'))->first()?->suffix)
|
|
->required()
|
|
->numeric()
|
|
->mask(RawJs::make(
|
|
<<<'JS'
|
|
$money($input, '.', '', 2)
|
|
JS
|
|
))
|
|
->placeholder('Enter the price of the product'),
|
|
Forms\Components\TextInput::make('quantity')
|
|
->label('Quantity')
|
|
->required()
|
|
->numeric()
|
|
->placeholder('Enter the quantity of the product'),
|
|
Forms\Components\TextInput::make('description')
|
|
->label('Description')
|
|
->required()
|
|
->hintAction(
|
|
Forms\Components\Actions\Action::make('View Service')
|
|
->url(function (Get $get) {
|
|
return ServiceResource::getUrl('edit', ['record' => $get('reference_id')]);
|
|
})
|
|
->label('View Service')
|
|
->hidden(fn (Get $get): bool => !in_array($get('reference_type'), [Service::class, ServiceUpgrade::class]))
|
|
)
|
|
->placeholder('Enter the description of the product'),
|
|
Forms\Components\Hidden::make('reference_type'),
|
|
Forms\Components\Hidden::make('reference_id'),
|
|
]),
|
|
|
|
]);
|
|
}
|
|
|
|
public static function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
Tables\Columns\TextColumn::make('id')
|
|
->label('ID')
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('number')
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('user.name')
|
|
->label('User')
|
|
->searchable(true, fn (Builder $query, string $search) => $query->whereHas('user', fn (Builder $query) => $query->where('first_name', 'like', "%$search%")->orWhere('last_name', 'like', "%$search%"))),
|
|
Tables\Columns\TextColumn::make('user.email')
|
|
->label('Email')
|
|
->searchable(),
|
|
Tables\Columns\TextColumn::make('status')
|
|
->label('Status')
|
|
// Make first letter uppercase
|
|
->formatStateUsing(fn (string $state): string => ucfirst($state))
|
|
->badge()
|
|
->color(fn (string $state): string => match ($state) {
|
|
'paid' => 'success',
|
|
'pending' => 'warning',
|
|
default => 'danger',
|
|
})
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('created_at')
|
|
->label('Issued At')
|
|
->date()
|
|
->searchable()
|
|
->sortable(),
|
|
Tables\Columns\TextColumn::make('formattedTotal')
|
|
->label('Total'),
|
|
Tables\Columns\TextColumn::make('formattedRemaining')
|
|
->label('Remaining'),
|
|
])
|
|
->defaultSort(function (Builder $query): Builder {
|
|
return $query
|
|
->orderBy('id', 'desc');
|
|
})
|
|
->filters([
|
|
SelectFilter::make('status')
|
|
->label('Status')
|
|
->options([
|
|
'paid' => 'Paid',
|
|
'pending' => 'Pending',
|
|
'cancelled' => 'Cancelled',
|
|
]),
|
|
])
|
|
->actions([
|
|
Tables\Actions\EditAction::make(),
|
|
])
|
|
->bulkActions([
|
|
Tables\Actions\BulkActionGroup::make([
|
|
Tables\Actions\DeleteBulkAction::make(),
|
|
]),
|
|
]);
|
|
}
|
|
|
|
public static function getRelations(): array
|
|
{
|
|
return [
|
|
RelationManagers\TransactionsRelationManager::class,
|
|
];
|
|
}
|
|
|
|
public static function getPages(): array
|
|
{
|
|
return [
|
|
'index' => Pages\ListInvoices::route('/'),
|
|
'create' => Pages\CreateInvoice::route('/create'),
|
|
'edit' => Pages\EditInvoice::route('/{record}/edit'),
|
|
];
|
|
}
|
|
}
|