Files
portal-api/app/Models/Invoice.php
T

73 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use App\Enums\Invoice\InvoiceStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
{
use HasFactory, SoftDeletes;
protected $table = 'invoices';
protected $guarded = [];
protected $casts = [
'due_date' => 'date',
'paid_at' => 'datetime',
'auto_renew' => 'boolean',
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
/** The order that originated this invoice. */
public function order()
{
return $this->belongsTo(Order::class, 'order_id');
}
/** Line items on this invoice. */
public function invoiceItems()
{
return $this->hasMany(InvoiceItem::class, 'invoice_id');
}
/** Uploaded payment-proof or supporting documents. */
public function attachments()
{
return $this->hasMany(InvoiceAttachment::class);
}
/** Services whose active invoice is this one (updated on each renewal). */
public function services()
{
return $this->hasMany(Service::class, 'invoice_id');
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
public function getStatus(): string
{
return match ($this->status) {
1 => '<span class="bg-red-100 text-red-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-red-900 dark:text-red-300">Chưa thanh toán</span>',
2 => '<span class="bg-lime-100 text-lime-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-lime-900 dark:text-lime-300">Đã thanh toán</span>',
3 => '<span class="bg-gray-100 text-gray-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-gray-900 dark:text-gray-300">Đã hủy</span>',
4 => '<span class="bg-orange-100 text-orange-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-orange-900 dark:text-orange-300">Quá hạn</span>',
default => '<span class="bg-red-100 text-red-800 text-xs font-medium me-2 px-2.5 py-0.5 rounded-sm dark:bg-red-900 dark:text-red-300">Không xác định</span>',
};
}
public function getStatusLabelAttribute(): string
{
return InvoiceStatus::from($this->status)->label();
}
}