<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class OrdersHeader extends Model
{
    use HasFactory;

    protected $table = 'orders_header';

    protected $fillable = [
        'order_no',
        'tanggal',
        'type',
        'id_meja',
        'meja_tambahan',
        'id_waiters',
        'created_by',
        'nama_pelanggan',
        'tipe_pembayaran',
        'no_transaksi_pembayaran',
        'bank_account_id',
        'discount_id',
        'discount_name',
        'discount_type',
        'discount_value',
        'discount_amount',
        'grand_total_after_discount',
        'status',
        'grand_total',
        'amount_paid',
        'change_amount',
    ];

    protected $casts = [
        'tanggal' => 'datetime',
        'grand_total' => 'decimal:2',
        'discount_value' => 'decimal:2',
        'discount_amount' => 'decimal:2',
        'grand_total_after_discount' => 'decimal:2',
        'amount_paid' => 'decimal:2',
        'change_amount' => 'decimal:2',
    ];

    public function details(): HasMany
    {
        return $this->hasMany(OrdersDetail::class, 'id_order_headers');
    }

    public function meja(): BelongsTo
    {
        return $this->belongsTo(MasterMeja::class, 'id_meja');
    }

    public function waiter(): BelongsTo
    {
        return $this->belongsTo(MasterWaiter::class, 'id_waiters');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function bankAccount(): BelongsTo
    {
        return $this->belongsTo(MasterBankAccount::class, 'bank_account_id');
    }
}
