<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Branch;
use App\Models\Carmodel;
use App\Models\Carvarient;

class rto_master extends Model
{
    use HasFactory;
    protected $table = 'rto_master';
    
    protected $fillable = [
        'branch_id',
        'car_model_id',
        'car_varient_id',
        'pricelist_id',
        'amount',
        'rto_type',
        'rto_tax_individual',
        'rto_tax_company',
        'temporary_registration',
        'smart_card_fee',
        'hypothecation',
        'fresh_permit',
        'new_registion',
        'crtem',
        'agent_charges',
        'rto_heandling_charges',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id','id');
    }

    public function carModel()
    {
        return $this->belongsTo(Carmodel::class, 'car_model_id', 'id');
    }

    public function carVariant()
    {
        return $this->belongsTo(Carvarient::class, 'car_varient_id', 'id');
    }
    public function export($slug){
        $collection = rto_master::select('rto_master.id','car_model.name as car_model_name','car_varient.name as car_varient_name','pricelist_master.price_list_name','rto_master.amount','branches.name as branch_name','rto_master.status')->leftjoin('car_model','car_model.id','=','rto_master.car_model_id')->leftjoin('car_varient','car_varient.id','=','rto_master.car_varient_id')->leftjoin('pricelist_master','pricelist_master.id','=','rto_master.pricelist_id')->leftjoin('branches','branches.id','=','rto_master.branch_id');

        if($slug != 'all')
            $collection->where(['rto_master.status' => $slug]);
        
        $data = $collection->get();

        if($data->isNotEmpty()){
            return $data;
        }else{
            return null;
        }
     }
}
