<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorkBGTrans extends Model
{
    use SoftDeletes;

    protected $table = 'workbg_trans';
    protected $primaryKey = 'WBT_Id';

    protected $fillable = [
        'WBT_Date',
        'WBT_Type',
        'SL_Id',
        'WA_Id',
        'WNE_Id',
        'BG_Amt',
        'WBT_HOD',
        'WBT_HDT',
        'WBT_CD',
        'WBT_BillNo',
          'Status',
    'Remarks'
    ];

    protected static function boot()
    {
        parent::boot();

        // Auto timestamp when HOD set
        static::updating(function ($model) {

            if ($model->isDirty('WBT_HOD') && $model->WBT_HOD) {
                $model->WBT_HDT = now();
            }
        });
    }

    // Relationships

    public function subledger()
    {
        return $this->belongsTo(SubLedgerMaster::class, 'SL_Id', 'SL_Id');
    }

    public function workAllocation()
    {
        return $this->belongsTo(WorkAllocationBook::class, 'WA_Id', 'WA_Id');
    }

    public function noc()
    {
        return $this->belongsTo(WONOCEntry::class, 'WNE_Id', 'WNE_Id');
    }

    public function hod()
    {
        return $this->belongsTo(UserMaster::class, 'WBT_HOD', 'Usr_Id');
    }
}
