laravel-rs/app/Filament/Resources/PaymentResource.php
Uchiha Bayu Senju 4654a2e350
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
Upload
2025-04-27 23:55:09 +07:00

144 lines
5.4 KiB
PHP

<?php
namespace App\Filament\Resources;
use Filament\Forms;
use Filament\Tables;
use App\Models\Payment;
use Filament\Forms\Form;
use Filament\Tables\Table;
use App\Models\PatientTreatment;
use Filament\Resources\Resource;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Builder;
use Filament\Forms\Components\DateTimePicker;
use App\Filament\Resources\PaymentResource\Pages;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\PaymentResource\RelationManagers;
class PaymentResource extends Resource
{
protected static ?string $model = Payment::class;
protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';
public static function form(Form $form): Form
{
return $form
->schema([
Select::make('registration_id')
->relationship('registration', 'id')
->required()
->reactive() // Menambahkan reactive agar form ini bisa merespon perubahan
->afterStateUpdated(fn($state, $set) => $set('total_amount', self::calculateTotalAmount($state))), // Kalkulasi total_amount setelah pemilihan Registration
// Tampilkan informasi pasien dan dokter
Forms\Components\TextInput::make('patient_name')
->disabled()
->label('Patient')
->default(fn($get) => $get('registration.patient.name')),
Forms\Components\TextInput::make('doctor_name')
->disabled()
->label('Doctor')
->default(fn($get) => $get('registration.doctor.name')),
// Tampilkan tindakan yang sudah dilakukan
Forms\Components\Repeater::make('patient_treatments')
->schema([
Forms\Components\TextInput::make('treatment_name')
->disabled()
->label('Treatment')
->default(fn($get) => $get('treatment.name')),
Forms\Components\TextInput::make('cost')
->disabled()
->label('Cost')
->default(fn($get) => $get('treatment.cost')),
Forms\Components\TextInput::make('quantity')
->numeric()
->default(1)
->label('Quantity'),
])
->columns(1)
->reactive()
->afterStateUpdated(fn($state, $set, $get) => $set('total_amount', self::calculateTotalAmount($get('registration_id')))),
TextInput::make('total_amount')->numeric()->required()->disabled(),
Select::make('payment_method')
->options([
'cash' => 'Cash',
'transfer' => 'Transfer',
'insurance' => 'Insurance',
'bpjs' => 'BPJS',
])
->required(),
TextInput::make('insurance_provider')->nullable(),
TextInput::make('insurance_number')->nullable(),
DateTimePicker::make('payment_date')->required(),
]);
}
// Fungsi untuk menghitung total amount berdasarkan tindakan yang terkait dengan registrasi
public static function calculateTotalAmount($registrationId)
{
// Ambil semua tindakan yang terkait dengan registrasi
$patientTreatments = PatientTreatment::where('registration_id', $registrationId)->get();
// Hitung total biaya
$total = 0;
foreach ($patientTreatments as $patientTreatment) {
$total += $patientTreatment->treatment->cost * $patientTreatment->quantity; // Asumsikan Treatment punya field 'cost'
}
return $total;
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('no')
->label('No.')
->getStateUsing(function ($rowLoop, $record) {
return $rowLoop->iteration;
}),
TextColumn::make('registration.patient.name')->label('Patient')->searchable(),
TextColumn::make('registration.doctor.name')->label('Doctor')->searchable(),
TextColumn::make('total_amount')->money('IDR'),
TextColumn::make('payment_method'),
TextColumn::make('payment_date')->dateTime(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListPayments::route('/'),
'create' => Pages\CreatePayment::route('/create'),
'edit' => Pages\EditPayment::route('/{record}/edit'),
];
}
}