<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;

class Exchange extends Model{
    use HasFactory;
    protected $table = 'exchange_purchase';
    
    protected $fillable = [
        'id',
        'obf_id',
        'seller_detail',
        'company_name',
        'variant',
        'manufacturing_year',
        'registration_number',
        'chassis_number',
        'engine_number',
        'colour',
        'time',
        'date',
        'date_of_registration',
        'purchase_date',
        'amount',
        'exchange_bonus',
        'total_exchange_amount',
        'amount_in_words',
        'HPA',
        'car_status',
        'relation_with_obf',
        'pan_image',
        'adhar_image',
        'rcbook',
        'insurance',
        'totto',
        'remarks',
        'status',
        'updated_at',
        'updated_by',
    ];
    
    public function getUsedCarReport($slug){
        
        $collection = DB::table('exchange_purchase')
            ->select(
                'exchange_purchase.id',
    			'exchange_purchase.obf_id',
    			'exchange_purchase.seller_detail',
    			'exchange_purchase.company_name',
    			'exchange_purchase.variant',
    			'exchange_purchase.manufacturing_year',
    			'exchange_purchase.registration_number',
    			'exchange_purchase.chassis_number',
    			'exchange_purchase.engine_number',
    			'exchange_purchase.colour',
    			'exchange_purchase.time',
    			'exchange_purchase.date',
    			'exchange_purchase.date_of_registration',
    			'exchange_purchase.purchase_date',
    			'exchange_purchase.amount',
    			'exchange_purchase.exchange_bonus',
    			'exchange_purchase.total_exchange_amount',
    			'exchange_purchase.amount_in_words',
    			'exchange_purchase.HPA',
    			'exchange_purchase.car_status',
    			'exchange_purchase.relation_with_obf',
    			'exchange_purchase.pan_image',
    			'exchange_purchase.adhar_image',
    			'exchange_purchase.rcbook',
    			'exchange_purchase.insurance',
    			'exchange_purchase.totto',
    			'exchange_purchase.status',
    			'exchange_purchase.created_at',
    			'exchange_purchase.created_by',
    			'exchange_purchase.updated_at',
    			'exchange_purchase.updated_by',
    			'exchange_purchase.remarks',
    			'exchange_purchase.cancel_reason',
    			'obf.customer_name as seller_name',
            )
            ->leftJoin('obf','obf.id','=','exchange_purchase.obf_id')
            ->orderBy('exchange_purchase.id', 'desc')
            ->where('exchange_purchase.status','=',"active"); 
    
            if($slug != 'all')
                $collection->where(['i.status' => $slug]);
            
            $data = $collection->get();
            if($data->isNotEmpty()){
                return $data;
            }else{
                return null;
            }
    }
    
    public function obf()
    {
        return $this->belongsTo(OBF::class, 'obf_id');
    }
}
