<?php

namespace App\Models;

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

class RSA extends Model{
    use HasFactory;
    protected $table = 'rsa';
    
    protected $fillable = [
        'rsa_ccs',
        'name',
        'car_model',
        'years',
        'amount',
        'profit',
        'gst_percentage',
        'gst',
        'total_profit',
        'status',
        'created_by',
        'created_at',
        'updated_at'
    ];

        public function carModel()
            {
                return $this->belongsTo(Carmodel::class, 'car_model');
            }
    public function export($slug){
        /*$collection = DB::table('rsa')
                        ->select('name', 'car_model', 'years', 'amount', 'status');*/
        
        $collection = DB::table('rsa')
                        ->select('rsa.rsa_ccs','rsa.name', 'car_model.name as car_model','rsa.years','rsa.amount','rsa.profit','rsa.gst_percentage','rsa.gst','rsa.total_profit')
                        ->leftjoin('car_model' ,'car_model.id' ,'rsa.car_model')
                        ->where('rsa.deleted_at', 0);

        if($slug != 'all')
            $collection->where(['status' => $slug]);
        
        $data = $collection->get();
        
        if($data->isNotEmpty()){
            return $data;
        }else{
            return null;
        }
    }
}
