<?php

namespace App\Models; 

use GuzzleHttp\Psr7\Request;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Finance;
use App\Models\Inventory;
use App\Models\TransitInventory;
use App\Models\Insurance;
use App\Models\Branch;
use App\Models\Carmodel;
use App\Models\Carvarient;
use App\Models\CashReceipt;
use DB;

class OBF extends Model{ 
    use HasFactory;
    protected $table = 'obf';

    protected $fillable = [
        'temporary_id',
        'booking_date',
        'customer_name',
        'customer_type',
        'branch_id',
        'company_name',
        'einvoicing_applicable',
        'gst',
        'address',
        'registration',
        'email',
        'pan_number',
        'pan_image',
        'adhar_number',
        'adhar_image',
        'licance_number',
        'licance_image',
        'other_documents',
        'contact_number',
        'dob',
        'nominee_name',
        'nominee_reletion',
        'nominee_age',
        'occupation',
        'sales_person_id',
        'car_model',
        'product_id',
        'fule_type',
        'varient_id',
        'exterior_color',
        'interior_color',
        'registration_tax_id',
        'insurance_id',
        'municipal_tax_id',
        'municipal_tax_applicable',
        'municipal_tax_type',
        'municipal_custom_handling_charges',
        'tcs_tax_id',
        'accessory_id',
        'extanded_warranty_id',
        'convinience_program',
        'other_details_1',
        'other_details_2',
        'number_plate',
        'fasttag_brand_name',
        'trade_car_name',
        'trade_car_value',
        'on_road_price',
        'on_road_price_word',
        'finance_id',
        'finance_branch_id',
        'lead_id',
        'booking_amount',
        'mode_of_payment',
        'ex_showroom_price',
        'status',
        'other_accesssories_detail',
        'other_accesssories_prise',
        'reason',
        'created_by',
        'created_at',
        'updated_by', 
        'updated_at',
        'way_off',
        'final_on_road_price_after_way_off',
        'approval_status',
        'state_id',
        'district_id',
        'city_id',
        'ship_to_required',
        'ship_to_customer_name',
        'ship_to_address',
        'ship_to_state_id',
        'ship_to_district_id',
        'ship_to_city_id',
        'finance_image',
        'rto_image',
        'insurance_image',
        'fasttag_image',
        'issue_date',
        'risk_start_date',
        'risk_end_date',
        'handicap',
        'handicap_image',
        'tyre_company',
        'tyre_year',
        'rto_custom_handling_charges',
        'foc_accessories',
        'tentetive_delivery_date',
        'passport_document',
        'gst_certificate',
        'discount_flag',
        'tl_discount',
        'sm_discount',
        'gm_discount',
        'bh_discount',
        'ceo_discount',
        'md_discount',
        'discount_year',
        'rto_passing_id',
    ];
    
    
    
    public function inventory()
    {
        return $this->belongsTo(Inventory::class, 'inventory_id','id');
    }
    public function transitinventory()
    {
        return $this->belongsTo(TransitInventory::class, 'transit_inventory_id','id');
    }
    public function insurance()
    {
        return $this->belongsTo(Insurance::class, 'insurance_id','id');
    }
    public function finance()
    {
        return $this->belongsTo(Finance::class,'finance_id','id');
    }
    public function branch()
    {
        return $this->belongsTo(Branch::class, 'branch_id','id');
    }

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

    public function carVariant()
    {
        return $this->belongsTo(Carvarient::class, 'name', 'id');
    }
    
    public function cashReceipts()
    {
        return $this->hasMany(CashReceipt::class, 'obf_id');
    }
    public function export($slug){

        $a_id = auth()->id();

        $collection = 
                OBF::select('obf.temporary_id',DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name") ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'branches.name AS branch_name' ,'obf.company_name' ,'obf.einvoicing_applicable' ,'obf.gst' ,'obf.address' ,'obf.registration' ,'obf.email' ,'obf.pan_number','obf.adhar_number' ,'obf.licance_number','obf.contact_number' ,'obf.dob' ,'obf.nominee_name','obf.nominee_reletion' ,'obf.nominee_age' ,'obf.occupation','products.name AS product_name' ,'products.veriant','products.is_applicable_for_mcp' ,'exteriorcolor.name as exterior_color' ,'interiorcolor.name as interior_color' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.brand_name AS brand_name' ,'fasttags.amount AS fasttag_amount','obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.booking_amount' ,'obf.mode_of_payment' ,'obf.trade_car_name', 'obf.trade_car_value','mcp.name as my_name','other_one.name as other_details_1','other_two.name as other_details_2','obf.status')
                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')
                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')
                ->leftjoin('products' ,'obf.product_id' ,'products.id')
                ->leftJoin('exteriorcolor','exteriorcolor.id','=','products.exterior_color')
                ->leftJoin('interiorcolor','interiorcolor.id','=','products.interior_color')
                ->leftjoin('taxes AS registration_tax' ,'obf.registration_tax_id' ,'registration_tax.id')
                ->leftjoin('taxes AS municipal_tax' ,'obf.municipal_tax_id' ,'municipal_tax.id')
                ->leftjoin('taxes AS tcs_tax' ,'obf.tcs_tax_id' ,'tcs_tax.id')
                ->leftjoin('insurance' ,'obf.insurance_id' ,'insurance.id')
                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')
                ->leftjoin('extand_warranties' ,'obf.extanded_warranty_id' ,'extand_warranties.id')
                ->leftjoin('fasttags' ,'obf.fasttag_brand_name' ,'fasttags.id')
                ->leftjoin('finance' ,'obf.finance_id' ,'finance.id')
                ->leftjoin('branches AS finance_branch' ,'obf.finance_branch_id' ,'finance_branch.id')
                ->leftjoin('lead' ,'obf.lead_id' ,'lead.id')
                ->leftjoin('mcp' ,'obf.convinience_program' ,'mcp.id')
                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')
                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id');
                
        if($slug != 'all'){
            $collection->where(['obf.status' => $slug]);
        }
        if($a_id != 1){
            $collection->where(['obf.created_by' => $a_id]);
        }
            $data = $collection->get();
        
            if($data->isNotEmpty()){
                return $data;
            }else{
                return null;
            }
    }
}