99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\InteractsWithUuid;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
|
|
class Transaction extends Model
|
|
{
|
|
use HasFactory, InteractsWithUuid;
|
|
|
|
protected $table = 't_transaction';
|
|
protected $primaryKey = 'id';
|
|
protected $keyType = 'string';
|
|
public $incrementing = false;
|
|
|
|
protected $fillable = [
|
|
'invoice_number',
|
|
'registration_id',
|
|
'patient_id',
|
|
'insurance_id',
|
|
'cashier_id',
|
|
'transaction_datetime',
|
|
'payment_datetime',
|
|
'due_date',
|
|
'subtotal',
|
|
'tax_amount',
|
|
'discount_amount',
|
|
'discount_reason',
|
|
'grand_total',
|
|
'paid_amount',
|
|
'change_amount',
|
|
'insurance_covered_amount',
|
|
'patient_responsibility',
|
|
'payment_method',
|
|
'payment_reference',
|
|
'status',
|
|
'notes',
|
|
];
|
|
|
|
protected $casts = [
|
|
'transaction_datetime' => 'datetime',
|
|
'payment_datetime' => 'datetime',
|
|
'due_date' => 'date',
|
|
'subtotal' => 'decimal:2',
|
|
'tax_amount' => 'decimal:2',
|
|
'discount_amount' => 'decimal:2',
|
|
'grand_total' => 'decimal:2',
|
|
'paid_amount' => 'decimal:2',
|
|
'change_amount' => 'decimal:2',
|
|
'insurance_covered_amount' => 'decimal:2',
|
|
'patient_responsibility' => 'decimal:2',
|
|
];
|
|
|
|
// Relationships
|
|
public function registration(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Registration::class, 'registration_id');
|
|
}
|
|
|
|
public function patient(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Patient::class, 'patient_id');
|
|
}
|
|
|
|
public function insurance(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Insurance::class, 'insurance_id');
|
|
}
|
|
|
|
public function cashier(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'cashier_id');
|
|
}
|
|
|
|
public function details(): HasMany
|
|
{
|
|
return $this->hasMany(TransactionDetail::class, 'transaction_id');
|
|
}
|
|
|
|
// helper untuk generate invoice number
|
|
public static function generateInvoiceNumber(): string
|
|
{
|
|
$datePart = now()->format('Ymd');
|
|
$lastInvoice = self::where('invoice_number', 'like', "INV-{$datePart}-%")
|
|
->orderBy('invoice_number', 'desc')
|
|
->first();
|
|
|
|
$sequence = $lastInvoice
|
|
? (int) substr($lastInvoice->invoice_number, -3) + 1
|
|
: 1;
|
|
|
|
return sprintf('INV-%s-%03d', $datePart, $sequence);
|
|
}
|
|
}
|