<?php



namespace App\Http\Controllers;

 

use Illuminate\Http\Request; 

use App\Models\Accessory;

use App\Models\Allocation; 

use App\Models\Approval;

use App\Models\Branch;

use App\Models\CarExchange; 

use App\Models\ExtandWarranty;

use App\Models\Fasttag;

use App\Models\Finance;

use App\Models\Insurance;

use App\Models\Lead;

use App\Models\Sublead;

use App\Models\OBF;

use App\Models\User;

use App\Models\Product;

use App\Models\Carmodel;

use App\Models\Interiorcolor;

use App\Models\Exteriorcolor;

use App\Models\Carvarient;

use App\Models\pricelist;

use App\Models\Mcp;

use App\Models\Tax;

use App\Models\OtherOne;

use App\Models\OtherTwo;

use App\Models\Inventory;

use App\Models\CashReceipt;

use App\Models\Texmaster;

use App\Models\Fuletype;

use App\Models\ObfInsurance;

use App\Models\CustomerTypeMaster;

use App\Models\RSA;

use App\Models\Setting;

use App\Models\rto_master;

use App\Models\offer;

use App\Models\MakeDeal;

use Spatie\Permission\Models\Role;

use Spatie\Permission\Models\Permission;

use Maatwebsite\Excel\Facades\Excel; 

use App\Imports\ImportObf;

use App\Exports\ExportObf;

use Carbon\Carbon;

use Auth, DB, Mail, Validator, File, DataTables;

use Session; 

use PDF;



class ObfController extends Controller{

    /** construct */
        public function __construct(){

            $this->middleware('permission:obf-create', ['only' => ['create']]);

            $this->middleware('permission:obf-edit', ['only' => ['edit']]);

            $this->middleware('permission:obf-view', ['only' => ['view']]);

            $this->middleware('permission:obf-insuranceview', ['only' => ['insuranceview']]);

            $this->middleware('permission:obf-rtoview', ['only' => ['rtoview']]);

            $this->middleware('permission:obf-fasttagview', ['only' => ['fasttagview']]);

            $this->middleware('permission:obf-delete', ['only' => ['delete']]);

            $this->middleware('permission:obf-finance', ['only' => ['finance']]);

            $this->middleware('permission:obf-policy_view', ['only' => ['finance']]);

        }
    /** construct */

    /** index */
        public function index(Request $request){
            $permission_get = '';
            $data_v = auth()->user()->roles->pluck( 'data_permission' );

            if(!empty($data_v)){
                $permission_get = $data_v[0];
            }

            $customer_name = OBF::select('obf.id','obf.customer_name')->get();

            $kec_name = User::select(DB::raw("CONCAT(users.first_name,' ',users.last_name) As kec_name",'users.id'))->get();

            $product = Carmodel::select('name','id')->where('status','active')->get();

            $price = OBF::select('on_road_price','id')->get();

            $varient = Carvarient::select('name','id')->where('status','active')->get();

            $branch = Branch::select('name','id')->where('status','active')->get();

            $date = OBF::select('booking_date','id')->get();

            $cash_status = CashReceipt::select('cash_receipt.status')->join('obf','obf.id','=','cash_receipt.obf_id')->get();

            $cust_name = $request->client;
            $kec = $request->kec;
            $products = $request->product;
            $road_price = $request->road_price;
            $variants = $request->varient;
            $locations = $request->location;
            $startdates = $request->start_date;
            $enddates = $request->end_date;

            if($request->ajax()){

                $data = OBF::select('obf.id', 
                                    'obf.customer_name', 
                                    'obf.booking_date',
                                    'car_model.name AS product',
                                    'obf.status','obf.ew_status',
                                    'obf.mcp_status',
                                    'obf.rsa_status',
                                    'obf.yodepartment_status',
                                    'obf.rto_status',
                                    'obf.accesories_status',
                                    'obf.on_road_price',
                                    'car_varient.name AS car_varient_name',
                                    'branches.name as branch_name',
                                    'cash_receipt.status as receipt_status',
                                    'orders.status as order_status',
                                    'inventory.vin_number',
                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),
                                    DB::raw('ROUND(obf.on_road_price - sum(cash_receipt.amount)) as due_amount'),
                                    DB::raw('ROUND(sum(cash_receipt.amount)) as received_amount'),
                                    'obf.gst',
                                    'exteriorcolor.name as exterior_color',
                                    'obf.address',
                                    'obf.registration',
                                    'obf_finance.loan_amount',
                                    'obf_finance.finance_discount',
                                    'obf_finance.net_disbusment', 
                                    // DB::raw('SUM(IF(cash_receipt.receipt_type = "booking_receipt",cash_receipt.amount,0)) as booking_amount'),
                                    // DB::raw('SUM(IF(cash_receipt.receipt_type = "downpayment_receipt",cash_receipt.amount,null)) as dp_amount'),
                                    DB::raw('(SELECT SUM(amount) FROM cash_receipt WHERE cash_receipt.obf_id = obf.id AND cash_receipt.receipt_type = "booking_receipt") as booking_amount'),
                                    DB::raw('SUM(DISTINCT IF(cash_receipt.receipt_type = "downpayment_receipt", cash_receipt.amount, 0)) as dp_amount'),
                                    'Team_master.team_name',
                                    'exchange_purchase.total_exchange_amount')

                        ->leftjoin('products', 'obf.product_id', 'products.id')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('users','users.id','=','obf.sales_person_id')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('branches','branches.id','=','obf.branch_id')

                        ->leftjoin('cash_receipt','cash_receipt.obf_id','=','obf.id')

                        ->leftjoin('orders','orders.obf_id','=','obf.id')

                        ->leftJoin('inventory','inventory.name','=','products.id')

                        ->leftJoin('exteriorcolor','exteriorcolor.id','=','products.exterior_color')

                        ->leftJoin('interiorcolor','interiorcolor.id','=','products.interior_color')

                        ->leftJoin('obf_finance','obf_finance.obf_id','=','obf.id')

                        ->leftjoin('Team_master','Team_master.id','users.team_name')

                        ->leftjoin('exchange_purchase','exchange_purchase.obf_id','=','obf.id');


                if(auth()->user()->roles && auth()->user()->roles[0]['id'] == 33) {

                    $data = $data->groupBy('orders.obf_id')->where('orders.status', 'all_payment_done')->whereNotNull('obf.extanded_warranty_id');

                }elseif(auth()->user()->roles && auth()->user()->roles[0]['id'] == 36){

                    $data = $data->groupBy('orders.obf_id')->where('orders.status', 'all_payment_done')->whereNotNull('obf.convinience_program');

                }elseif(auth()->user()->roles && auth()->user()->roles[0]['id'] == 37){

                    $data = $data->groupBy('orders.obf_id')->where('orders.status', 'all_payment_done')->whereNotNull('obf.rsa_master_obf');

                }elseif(auth()->user()->roles && auth()->user()->roles[0]['id'] == 32){

                    $data = $data->groupBy('orders.obf_id')->where('orders.status', 'all_payment_done')->whereNotNull('obf.accessory_id');

                }elseif(auth()->user()->roles && auth()->user()->roles[0]['id'] == 22){

                    $data = $data->groupBy('orders.obf_id')->where('orders.status','all_payment_done');

                }else {

                    $data = $data->groupBy('obf.id');

                }

                if($permission_get != '' && $permission_get == 'ONLY_MY_DATA'){

                    $data = $data->whereRaw('FIND_IN_SET(obf.created_by ,"'.auth()->id().'")');

                }                    

                if(!empty(auth()->user()->branch)){

                    $data =$data->whereRaw('FIND_IN_SET(obf.branch_id ,"'.auth()->user()->branch.'")');

                }

                if($permission_get != '' && $permission_get == 'MY_TEAM_DATA'){

                    if(auth()->user()->team_name != NULL){

                        $team_name_ids = User::select('id')->where('team_name',auth()->user()->team_name)->get()->toArray();   

                        $data = $data->whereIn('obf.created_by',$team_name_ids);  

                    }    

                }

                $data->orderBy('id', 'desc');
    

                return Datatables::of($data)
                        ->addIndexColumn()

                        ->addColumn('action', function ($data) {

                            $return = '<div class="btn-group">';

                            if (auth()->user()->can('obf-view')) {
                                $return .=  '<a href="'.route('obf.view', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }

                            if (auth()->user()->can('obf-fasttagview') && !(auth()->user()->can('obf-view'))) {
                                $return .=  '<a href="'.route('obf.fasttag', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }

                            if (auth()->user()->can('obf-rtoview') && !(auth()->user()->can('obf-view'))) {
                                $return .=  '<a href="'.route('obf.rto', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }

                            if (auth()->user()->can('obf-insuranceview') && !(auth()->user()->can('obf-view'))) {
                                $return .=  '<a href="'.route('obf.insurance', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-eye"></i>
                                            </a> &nbsp;';
                            }

                            if (auth()->user()->can('obf-edit')) {
                                $return .= '<a href="'.route('obf.edit', ['id' => base64_encode($data->id)]).'" class="btn btn-default btn-xs">
                                                <i class="fa fa-edit"></i>
                                            </a> &nbsp;';
                            }
                            
                            $return .= '<a href="javascript:;" class="btn btn-default btn-xs dropdown-toggle" data-toggle="dropdown">
                                            <i class="fa fa-bars"></i>
                                        </a> &nbsp;

                                        <ul class="dropdown-menu">';

                                            if (auth()->user()->can('obf-delete')) {

                                                $return .= '<li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="pending" data-id="' . base64_encode($data->id) . '">Pending</a></li>

                                                <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="delete" data-id="' . base64_encode($data->id) . '">Delete</a></li>

                                                <li><a class="dropdown-item" href="' . route('obf.obf_print', ['id' => base64_encode($data->id)]) . '" target="_blank">OBF Print</a></li>

                                                <li><a class="dropdown-item" href="' . route('obf.obf_summary', ['id' => base64_encode($data->id)]) . '" target="_blank">OBF Summary</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process EW') && $data->ew_status != 'EW COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processew', ['id' => base64_encode($data->id)]) . '" target="_blank">Process EW</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process MCP') && $data->mcp_status != 'MCP COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processmcp', ['id' => base64_encode($data->id)]) . '" target="_blank">Process MCP</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process RSA') && $data->rsa_status != 'RSA COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processrsa', ['id' => base64_encode($data->id)]) . '" target="_blank">Process RSA</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process FastTag')) {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processfasttag', ['id' => base64_encode($data->id)]) . '" target="_blank">Process FastTag</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process YO Department')) {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processyodepartment', ['id' => base64_encode($data->id)]) . '" target="_blank">Process YO Department</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process Accessories') && $data->accesories_status != 'ACCESORIES COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processaccesories', ['id' => base64_encode($data->id)]) . '" target="_blank">Process Accessories</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process RTO') && $data->rto_status != 'RTO COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.processrto', ['id' => base64_encode($data->id)]) . '" target="_blank">Process RTO</a></li>';

                                            } 
                                            
                                            if (auth()->user()->can('Process EW') && $data->ew_status != 'EW COMPLETED' || auth()->user()->can('Process MCP') && $data->mcp_status != 'MCP COMPLETED' || auth()->user()->can('Process RSA') && $data->rsa_status != 'RSA COMPLETED' || auth()->user()->can('Process FastTag') || auth()->user()->can('Process YO Department') || auth()->user()->can('Process Accessories') && $data->accesories_status != 'ACCESORIES COMPLETED' || auth()->user()->can('Process RTO') && $data->rto_status != 'RTO COMPLETED') {

                                                $return .= '<li><a class="dropdown-item" href="javascript:;">Cancel</a></li>';

                                            }
                                            
                                            if (auth()->user()->can('obf-make-deal')) {
                                                
                                                $return .= '<li><a class="dropdown-item" href="' . route('obf.make-deal', ['id' => base64_encode($data->id)]) . '" target="_blank">Make Deal</a></li>';

                                            }

                            $return .= '</ul></div>';

                            return $return;
                        })

                        ->editColumn('booking_date',function($data){

                            return (isset($data->booking_date) && !empty($data->booking_date))?\Carbon\Carbon::createFromFormat('Y-m-d',$data->booking_date)->format('d-m-Y') : '--';

                        })

                        ->editColumn('final_total',function($data){

                            $book_amt = $data->booking_amount + $data->dp_amount + $data->net_disbusment + $data->total_exchange_amount;

                            return $book_amt;

                        })

                        ->editColumn('net_total',function($data){

                            $book_amt = $data->booking_amount + $data->dp_amount + $data->net_disbusment + $data->total_exchange_amount;

                            $net_amt = $book_amt - $data->finance_discount;

                            return $net_amt;

                        })

                        ->editColumn('due_amount',function($data){

                            $book_amt = $data->booking_amount + $data->dp_amount + $data->net_disbusment + $data->total_exchange_amount;

                            $net_amt = $book_amt - $data->finance_discount;

                            $due_amount = $data->on_road_price - $net_amt;

                            return $due_amount;

                        })

                        ->editColumn('status', function ($data) {

                            if($data->receipt_status == 'accepted' && $data->status == 'account_accepted' && $data->order_status == 'waiting'){

                                return '<span class="badge badge-pill badge-success">Order'. '-'.$data->order_status .'</span>';

                            }elseif($data->status == 'account_accepted' && $data->order_status == 'finance_apporve'){

                                return '<span class="badge badge-pill badge-success">Finance Apporved</span>';

                            }elseif($data->status == 'account_accepted' && $data->order_status == 'disbursement'){

                                return '<span class="badge badge-pill badge-success">Disbursement</span>';

                            }elseif($data->status == 'account_accepted' && $data->order_status == 'all_payment_done'){

                                return '<span class="badge badge-pill badge-success">All Payment Done</span>';

                            }elseif($data->order_status == 'pdi_approve'){

                                return '<span class="badge badge-pill badge-success">Invoice Created</span>';

                            }elseif($data->order_status == 'ready_to_deliver'){

                                return '<span class="badge badge-pill badge-success">Gate Pass Approved</span>';

                            }elseif($data->order_status == 'delivered'){

                                return '<span class="badge badge-pill badge-success">Delivered</span>';

                            }elseif($data->receipt_status == 'accepted' && $data->status == 'account_accepted'){

                                return '<span class="badge badge-pill badge-success">Receipt Created</span>';

                            }elseif ($data->status == 'accepted') {

                                return '<span class="badge badge-pill badge-success">Active</span>';

                            }elseif ($data->status == 'pending') {

                                return '<span class="badge badge-pill badge-warning">Pending</span>';

                            }elseif ($data->status == 'deleted') {

                                return '<span class="badge badge-pill badge-danger">Deleted</span>';

                            }elseif ($data->status == 'account_rejected') {

                                return '<span class="badge badge-pill badge-danger">Account Rejected</span>';

                            }elseif ($data->status == 'obf_rejected') {

                                return '<span class="badge badge-pill badge-danger">OBF Rejected</span>';

                            }elseif ($data->status == 'rejected') {

                                return '<span class="badge badge-pill badge-danger">Rejected</span>';

                            }elseif ($data->status == 'obf_accepted') {

                                return '<span class="badge badge-pill badge-success">Obf Accepted</span>';

                            }elseif ($data->status == 'account_accepted') {

                                return '<span class="badge badge-pill badge-success">Account Accepted</span>';

                            }elseif($data->status == 'completed'){

                                return '<span class="badge badge-pill badge-success">Completed</span>';

                            }else{

                                return '-';

                            }                            

                        })

                        ->rawColumns(['action' ,'status'])

                        ->filter(function ($query){
                            if (request()->get('booking_date',false)) {
                                $date = \Carbon\Carbon::parse(request()->get("booking_date"))->format('Y-m-d');
                                $query->where('obf.booking_date', "like", "%".$date."%");
                            }
                            if (request()->get('branch_name',false)) {
                                $query->where('branches.name', "like", "%".request()->get("branch_name")."%");
                            }
                            if (request()->get('customer_name',false)) {
                                $query->where('obf.customer_name', "like", "%".request()->get("customer_name")."%");
                            }
                            if (request()->get('gst',false)) {
                                $query->where('obf.gst', "like", "%".request()->get("gst")."%");
                            }
                            if (request()->get('team_name',false)) {
                                $query->where('Team_master.team_name', "like", "%".request()->get("team_name")."%");
                            }
                            if (request()->get('sales_person_name',false)) {
                                $salesPersonName = request()->get('sales_person_name');
                                $query->whereRaw("CONCAT(users.first_name, ' ', users.last_name) LIKE ?", ["%{$salesPersonName}%"]);
                            }
                            if (request()->get('product',false)) {
                                $query->where('car_model.name', "like", "%".request()->get("product")."%");
                            }
                            if (request()->get('car_varient_name',false)) {
                                $query->where('car_varient.name', "like", "%".request()->get("car_varient_name")."%");
                            }
                            if (request()->get('exterior_color',false)) {
                                $query->where('exteriorcolor.name', "like", "%".request()->get("exterior_color")."%");
                            }
                            if (request()->get('vin_number',false)) {
                                $query->where('inventory.vin_number', "like", "%".request()->get("vin_number")."%");
                            }
                            if (request()->get('on_road_price',false)) {
                                $query->where('obf.on_road_price', "like", "%".request()->get("on_road_price")."%");
                            }
                            if (request()->get('booking_amount', false)) {
                                $query->whereRaw(
                                    "SUM(IF(cash_receipt.receipt_type = 'booking_receipt', cash_receipt.amount, 0)) LIKE ?",
                                    ["%" . request()->get("booking_amount") . "%"]
                                );
                            }
                            if (request()->get('dp_amount',false)) {
                                $query->whereRaw(
                                    "SUM(IF(cash_receipt.receipt_type = 'downpayment_receipt', cash_receipt.amount, 0)) LIKE ?",
                                    ["%" . request()->get("dp_amount") . "%"]
                                );
                            }
                            if (request()->get('loan_amount',false)) {
                                $query->where('obf_finance.loan_amount', "like", "%".request()->get("loan_amount")."%");
                            }
                            if (request()->get('net_disbusment',false)) {
                                $query->where('obf_finance.net_disbusment', "like", "%".request()->get("net_disbusment")."%");
                            }
                            if (request()->get('total_exchange_amount',false)) {
                                $query->where('exchange_purchase.total_exchange_amount', "like", "%".request()->get("total_exchange_amount")."%");
                            }
                            if (request()->get('final_total',false)) {
                                $query->havingRaw("final_total LIKE ?", ["%" . request()->get("final_total") . "%"]);
                            }
                            if (request()->get('finance_discount',false)) {
                                $query->where('obf_finance.finance_discount', "like", "%".request()->get("finance_discount")."%");
                            }
                            if (request()->get('net_total',false)) {
                                $query->havingRaw("net_total LIKE ?", ["%" . request()->get("net_total") . "%"]);
                            }
                            if (request()->get('due_amount',false)) {
                                $query->havingRaw("ROUND(obf.on_road_price - SUM(cash_receipt.amount)) LIKE ?",["%" . request()->get("due_amount") . "%"]);
                            }
                        })   

                        ->make(true);

                }

                

                $role = auth()->user()->roles;

                $roleName = "";

                if ($role) {

                    $roleName = $role[0]->name;

                }

            return view('obf.index')->with(['customer_name'=>$customer_name,'kec_name'=>$kec_name,'product'=>$product,'price'=>$price,'varient'=>$varient,'branch'=>$branch,'date'=>$date,'roleName'=>$roleName]);

        }
    /** index */

    /** Create */
        public function create(Request $request){
            $sales = User::role(['sales','KEC'])->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();

            $product = Product::select('id' , 'name' ,'veriant' ,'is_applicable_for_mcp')->where(['status' => 'active'])->get();

            $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

            $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

            $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

            $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

            $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

            $fasttag = Fasttag::select('id' , 'brand_name' ,'amount')->where(['status' => 'active'])->get();

            $branch = Branch::select('*');

            if(!empty(auth()->user()->branch) && auth()->user()->branch != '' && auth()->user()->branch != NULL){

                $branch_arr = explode(',', auth()->user()->branch);

                if(!empty($branch_arr)){

                    $branch = $branch->whereIn('id',$branch_arr);    

                }

            }                        

            $branch = $branch->where(['status' => 'active'])->get(); 

            //branch selection





            $pricelist_master = DB::table('pricelist_master')->select('id','car_model')->get();

            $occupation_lists = DB::table('occupation')->select('id','occupation_title')->get();

            $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

            $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

            $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

            $car_exchange = CarExchange::select('car_exchange.id','car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

            





            $Cardata = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                       ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();



            $Carmodel = Carmodel::where(['status' => 'active'])->get();

            $Carvarient = Carvarient::where(['status' => 'active'])->get();

            

            $user = auth()->user();

            $user_role = $user->getRoleNames();



            $Carmodel_arr = Carmodel::where(['status' => 'active'])->get();  

            

            $mcp = Mcp::where(['status' => 'active'])->get();  

            

            $OtherOne = OtherOne::where(['status' => 'active'])->get();  

            

            $OtherTwo = OtherTwo::where(['status' => 'active'])->get();  

            

            $finance = Finance::where(['status' => 'active'])->get();  

            

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  



            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

            $auth = User::select('department.name')->join('department','department.id','users.department_id')->where('users.id',auth()->user()->id)->first();

            $depart = !empty($auth->name) ? $auth->name : "";

            

            $rto_master = rto_master::select('amount')->where('status','=','active')->first();

            $rto_amount = $rto_master->amount;

            $fuel_type = Fuletype::select('*')->where('status','=','active')->get();

            return view('obf.create')->with(['sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'Cardata' => $Cardata,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'finance' => $finance ,'lead' => $lead,'pricelist_master' =>$pricelist_master,'occupation_lists' =>$occupation_lists,'Carmodel'=>$Carmodel,'Carvarient'=>$Carvarient,'user_role'=> $user_role[0],'Carmodel_arr' => $Carmodel_arr,'mcp' => $mcp,'OtherOne' => $OtherOne,'OtherTwo' => $OtherTwo, 'finance' => $finance, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'depart'=>$depart,'rto_amount'=>$rto_amount,'fuel_type'=>$fuel_type]);

        }
    /** Create */

    /**carvarient */
        public function car_drop_down_get(Request $request){ 

            

            $id = ($request->id);

            //dd($id);

            $fuel = $request->fuel;

            $data = [];



            $Carvarient_arr = [];

            $ExtandWarranty_arr = [];

            $Mcp_arr_new = [];

            if($request->option_check == 'Carvarient'){

                $Carvarient = Carvarient::where(['car_model' => $request->id,'fule_type'=>$fuel,'status' => 'active'])->get();

                

                foreach ($Carvarient as $value) {

                    $color_data = [];

                    $color_data['id'] = $value->id;

                    $color_data['name'] = $value->name;

                    $Carvarient_arr[] = $color_data;                    

                }  



                $ExtandWarranty_data = ExtandWarranty::where(['car_model' => $request->id,'status' => 'active'])->orderBy('years', 'asc')->get();

                foreach ($ExtandWarranty_data as $value) {

                    $arr_sub = [];

                    $arr_sub['id'] = $value->id;

                    $arr_sub['amount'] = $value->amount;

                    $arr_sub['years'] = $value->years;

                    $ExtandWarranty_arr[] = $arr_sub;                    

                }



                $Mcp_data = Mcp::where(['car_model' => $request->id,'status' => 'active'])->get();

                foreach ($Mcp_data as $value) {

                    $arr_sub = [];

                    $arr_sub['id'] = $value->id;

                    $arr_sub['amount'] = $value->amount;

                    $arr_sub['name'] = $value->name;

                    $Mcp_arr_new[] = $arr_sub;                    

                }  

            }

            $data['Carvarient_arr'] = $Carvarient_arr;  

            $data['ExtandWarranty_arr'] = $ExtandWarranty_arr;  



          /*  */

            $data['Mcp_arr_new'] = $Mcp_arr_new;  



            // var_dump($data['Mcp_arr']);

                        // die('s');



            $Pricelist_arr = [];

            if($request->pricelist == 'pricelist_master'){

                if(!empty(auth()->user()->branch) && auth()->user()->branch != '' && auth()->user()->branch != NULL){

                $branch_arr = explode(',', auth()->user()->branch);

                if(!empty($branch_arr)){

                    $branch = Branch::select('id')->whereIn('id',$branch_arr);    

                }

            }    

                $pricelist = pricelist::where(['is_csd'=>$request->is_csd,'car_variant' => $request->id,'status' => 'active'])->get();



                foreach ($pricelist as $value) {

                    $price = [];

                    $price['id'] = $value->id;

                    $price['price_list_name'] = $value->price_list_name;

                    $Pricelist_arr[] = $price;                    

                }

            }



            $data['Pricelist_arr'] = $Pricelist_arr;



            $carname_arr = [];

            $tex_master_gst = 1;

            if($request->option_check == 'carname'){

                $Carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                   ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                    ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                    ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                    ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                    ->where('products.veriant', '=', $request->id)

                    ->where('products.status', '=', 'active')

                    ->get();

                

                foreach ($Carmodel as $value) {

                    $color_data = [];

                    $color_data['id'] = $value->car_id;

                    $color_data['car_name'] = $value->car_name;

                    $color_data['varient_name'] = $value->varient_name;

                    $color_data['exterior_color'] = $value->exterior_color;

                    $color_data['interior_color'] = $value->interior_color;

                    $carname_arr[] = $color_data;                    

                }  

                $Carvarient_data = Carvarient::where(['id' => $request->id,'status' => 'active'])->first();



                $tex_master_id = 0;

                if($Carvarient_data)

                    $tex_master_id = $Carvarient_data->tex_master_id;



                if($tex_master_id > 0){

                    $Texmaster_data = Texmaster::where(['id' => $tex_master_id,'status' => 'active'])->first();    

                    if($Texmaster_data->gst != null) 

                         $tex_master_gst = $Texmaster_data->gst;

                }

                

            }





             $Mcp_arr = [];



            if($request->new_mcp == 'Mcp'){

                $new_mcp = Mcp::where(['car_model' => $request->id,'status' => 'active'])->get();



                foreach ($new_mcp as $value) {

                    $mcp = [];

                    $mcp['id'] = $value->id;

                    $mcp['name'] = $value->name;

                    $mcp['amount'] = $value->amount;

                    $Mcp_arr[] = $mcp;                    

                }

            }



            $data['Mcp_arr'] = $Mcp_arr;



            $data['carname_arr'] = $carname_arr;  

            $data['tex_master_gst'] = $tex_master_gst;  



            // var_dump($data);

            // die('stop');

            

            return json_encode(array('status'=> true ,'data' => $data));

        }
    /** carvarient */

    public function sub_lead_html(Request $request){ 

       $id = ($request->id);



       $data = [];



       $sub_lead_html = [];

       

       $lead_data = Sublead::where(['lead_id' => $id,'status' => 'active'])->get();

       

       foreach ($lead_data as $value) {

           $color_data_arr = [];

           $color_data_arr['id'] = $value->id;

           $color_data_arr['name'] = $value->name;

           $sub_lead_html[] = $color_data_arr;                    

       }  

      

       $data['sub_lead_html'] = $sub_lead_html;  

       return json_encode(array('status'=> true ,'data' => $data));

    }

    public function color_select(Request $request){ 

        $data = [];



        $exterior_color_arr = [];

        if($request->option_check == 'exterior_color'){



            $arr = Product::select('exterior_color')

                ->where('products.car_model', '=', $request->car_model)

                ->where('products.veriant', '=', $request->id)

                ->where('products.status', '=', 'active')

                ->distinct()

                ->get();

            

            foreach ($arr as $value) {

                $color_data = [];

                $color_data['id'] = $value->exterior_color;

                $Exteriorcolor_data = Exteriorcolor::select('name')->where('id', $value->exterior_color)->first();



                $color_data['name'] = '';

                if($Exteriorcolor_data){

                    $color_data['name'] = $Exteriorcolor_data->name;

                }

                $exterior_color_arr[] = $color_data;                    

            }  

        }

        $data['exterior_color_arr'] = $exterior_color_arr;  



        $interior_color_arr = [];

        if($request->option_check == 'interior_color'){

            // die('stop');



           /* var_dump($request->car_model);

            var_dump($request->veriant);

            var_dump($request->id);*/



            $arr = Product::select('products.id','interiorcolor.name as interior_color','interiorcolor.id as interior_color_id')

                    ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                    ->where('products.car_model', '=', $request->car_model)

                    ->where('products.veriant', '=', $request->veriant)

                    ->where('products.exterior_color', '=', $request->id)

                    ->where('products.status', '=', 'active')

                    ->get();

            

            foreach ($arr as $value) {

                $color_data = [];

                $color_data['id'] = $value->interior_color_id;

                $color_data['name'] = $value->interior_color;

                $color_data['product_id'] = $value->id;

                $interior_color_arr[] = $color_data;                    

            }  

        }

        $data['interior_color_arr'] = $interior_color_arr;  

        

        return json_encode(array('status'=> true ,'data' => $data));    

    }

    /**carvarient */
        public function carvarient(Request $request){ 

                $id = ($request->id);



           /* $cardeails = Carmodel::select('car_model.name as car_name','car_model.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                        ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                        ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                        ->where('car_model.id', '=', $id)

                        ->get();*/



            $Cardata = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                       ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->where('products.id', '=', $id)

                        ->get();



                

                $data_new = Product::select('products.*','pricelist_master.id as price_id','pricelist_master.price_list_name as price_list_name','pricelist_master.transmission as transmission','pricelist_master.car_variant as price_car_var')

                        ->leftjoin('pricelist_master', 'pricelist_master.car_variant', 'products.veriant')

                        ->where('products.status', '=', 'active')

                        ->where('products.id', '=', $id)

                        ->get();



                $pricelist_arr = [];

                foreach ($data_new as $value) {

                    $color_data = [];

                    $color_data['id'] = $value->price_id;

                    $color_data['price_list_name'] = $value->price_list_name;

                    $color_data['transmission'] = $value->transmission;

                    $pricelist_arr[] = $color_data;

                    

                }

                $data = [];

                $data['pricelist_arr'] = $pricelist_arr;

            

            return json_encode(array('status'=> true ,'cardeails' => $Cardata,'data' => $data));

        }
    /** carvarient */

    public function insert(Request $request){

        if(request()->ajax()){

            return true;

        } 

            $obf = OBF::count();

            $temp_number = sprintf('%04d',$obf+1);





            

            /*$product_id = 0;

            if($request->product_id != null && $request->product_id != ''){

                $product_id = $request->product_id;

            }



              DB::enableQueryLog();



              

           



              dd(DB::getQueryLog());*/







            if(empty($request->accessories)){

                $request->accessories = [];

            }



            if(empty($request->priclist_detail)){

                $request->priclist_detail = [];

            }



            if(empty($request->other_details_1)){

                $request->other_details_1 = [];

            }



            if(empty($request->other_details_2)){

                $request->other_details_2 = [];

            }



            $detail_value_arr = [];

           if(!empty($request->priclist_detail)){

               foreach ($request->priclist_detail as $value) {

                   $arr_value = explode('__', $value);

                   $detail_value_sub_arr = [];

                   $detail_value_sub_arr['detail_field'] = $arr_value[0];

                   $detail_value_sub_arr['value_field'] = $arr_value[1]; 

                   $detail_value_arr[] = $detail_value_sub_arr; 

               }

           }

           $detail_value_arr_json = json_encode($detail_value_arr);



            /*echo "<pre>";

            var_dump($detail_value_arr_json);

            die('s');*/



            if(!empty($request->dob) && $request->dob != '' && $request->dob != NULL)

            {

               $request->dob = date("Y-m-d",strtotime($request->dob));        

            }



            if(!empty($request->nominee_dob) && $request->nominee_dob != '' && $request->nominee_dob != NULL)

            {

               $request->nominee_dob = date("Y-m-d",strtotime($request->nominee_dob));        

            }



            //offer type



            $offer_type = $request->offer_type;



            $offer_kin = $request->offer_kin;                

            if($offer_kin != null){

                $offerkin = implode(',',$offer_kin);

            }

            else{

                $offerkin = null;

            }



            $offer_ib = $request->offer_ib;

            if($offer_ib != null){

                $offerib = implode(',',$offer_ib);

            }

            else{

                $offerib = null;

            }

            if($request->offer_amount != null){

               // $ex_showroom_price = $request->with_offer_ex_showroom_price;

                $ex_showroom_price = $request->deal_price;

            }else{

                $ex_showroom_price = $request->ex_showroom_price;

            }



            $crud = [

                'temporary_id' => $temp_number,

                'booking_date' => $request->booking_date,

                'customer_name' => strtoupper($request->customer_name),

                'customer_birthdate' => $request->customer_birthdate ?? null,

                'customer_type' => $request->customer_type,

                'customer_type_new' => $request->customer_type_new,

                'branch_id' => $request->branch,

                'company_name' => $request->company_name ?? null,

                'einvoicing_applicable' => $request->einvoapp ?? null,
                
                'gst_required' => $request->gst_required === 'yes' ? 'yes' : 'no',

                'gst' => $request->gst ?? null,

                'address' => $request->address_1 ?? null,

                'registration' => $request->address_2 ?? null,

                'email' => $request->email ?? null,

                'pan_number' => $request->pan_number ?? null,

                'adhar_number' => $request->adhar_number ?? null,

                'licance_number' => $request->license_number ?? null,

                'contact_number' => $request->contact_number ?? null,

                'dob' => $request->dob ?? null,

                'nominee_dob' => $request->nominee_dob ?? null,

                'nominee_name' => $request->nominee_name ?? null,

                'nominee_reletion' => $request->nominee_relation ?? null,

                'nominee_age' => $request->nominee_age ?? null,

                'occupation' => $request->occupation ?? null,

                'sales_person_id' => $request->sales_person_name ?? null,

                //'product_id' => $request->name ?? null,

                'product_id' => $request->product_id ?? null,

                'fuel_type' => $request->fuel_type ?? null,

               /* 'varient_id' => $request->veriant ?? null,

                'exterior_color' => $request->exterior_color ?? null,

                'interior_color' => $request->interior_color ?? null,*/

                'offer_type' => $offer_type ?? null,

                'offer_kin'=>$offerkin ?? null,

                'offer_ib'=>$offerib ?? null,

                'offer_ib_amount'=>$request->offers_ib_amount ?? null,

                'offer_kin_amount'=>$request->offers_kin_amount ?? null,

                'offer_amount' => $request->offe_amount ?? null,

                'deal_price' =>$request->deal_price ?? null,

                'way_off'=>$request->way_off ?? null,
                
                'final_on_road_price_after_way_off'=>$request->final_on_road_price_after_way_off ?? null,

                'ex_showroom_price' => $request->ex_showroom_price ?? null,

                'registration_tax_id' => $request->registration_tax ?? null,

                'insurance_id' => $request->insurance ?? null,

                'municipal_tax_id' => $request->municipal_tax ?? null,

                'municipal_tax_applicable' => $request->municipal_tax_applicable ?? 2,

                'tcs_tax_id' => $request->tcs_tax ?? null,

                'accessory_id' => implode(" ,",$request->accessories) ?? null,

                'priclist_detail' => $detail_value_arr_json,

                'extanded_warranty_id' => $request->extended_warranty ?? null,

                'fasttag_brand_name' => $request->fasttag_brand_name ?? null,

                'trade_car_name' => $request->trade_car_name ?? null,

                'trade_car_value' => $request->trade_car_value ?? null,

                'on_road_price' => $request->on_road_price ?? null,

                'on_road_price_word' => $request->on_road_price_word ?? null,

                // 'finance_id' => $request->finance_name ?? null,

                // 'finance_branch_id' => $request->finance_branch_name ?? null,

                'lead_id' => $request->lead ?? null,

                'sub_lead_id' => $request->sub_lead_id ?? null,

                'booking_amount' => $request->booking_amount ?? null,

                'mode_of_payment' => $request->mode_of_payment ?? null,

                'pricelist_id' => $request->pricelist_manager_id ?? null,                    

                'convinience_program' => $request->convinience_program ?? null,                    

                'other_details_1' => implode(" ,",$request->other_details_1) ?? null,

                'other_details_2' => implode(" ,",$request->other_details_2) ?? null,

                'rsa_master_obf' => $request->rsa_master_obf ?? null,

                'step1_remarks' => $request->step1_remarks ?? null,

                'step2_remarks' => $request->step2_remarks ?? null,

                'step3_remarks' => $request->step3_remarks ?? null,

                'step4_remarks' => $request->step4_remarks ?? null,

                'step5_remarks' => $request->step5_remarks ?? null,

                'status' => 'pending',
                
                'aadhar_pan_link' => $request->aadhar_pan_link === 'yes' ? 'yes' : 'no',

                'is_csd' => $request->is_csd == 'Yes'?1:0,

                'number_plate'=>$request->number_plate??null,

                'municipal_tax_handling' => $request->municipal_tax_handling??null,

                'created_at' => date('Y-m-d H:i:s'),

                'created_by' => auth()->user()->id,

                'updated_at' => date('Y-m-d H:i:s'),

                'updated_by' => auth()->user()->id

            ];



            

            // dd($request->file('licance_image'));

            // Pan Image

                /*if(!empty($request->file('pan_image'))){

                    $file = $request->file('pan_image');

                    $filenameWithExtension = $request->file('pan_image')->getClientOriginalName();

                    $filename = pathinfo($filenameWithExtension, PATHINFO_FILENAME);

                    $extension = $request->file('pan_image')->getClientOriginalExtension();

                    $filenameToStore = "pan_".time()."_".$filename.'.'.$extension;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['pan_image'] = $filenameToStore;

                }else{

                    $crud['pan_image'] = 'default.jpg';

                }*/

            // Pan Image



            // Aadhar Image

                /*if(!empty($request->file('adhar_image'))){

                    $file_two = $request->file('adhar_image');

                    $filenameWithExtension_two = $request->file('adhar_image')->getClientOriginalName();

                    $filename_two = pathinfo($filenameWithExtension_two, PATHINFO_FILENAME);

                    $extension_two = $request->file('adhar_image')->getClientOriginalExtension();

                    $filenameToStore_two = "adhar_".time()."_".$filename_two.'.'.$extension_two;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['adhar_image'] = $filenameToStore_two;

                }else{

                    $crud['adhar_image'] = 'default.jpg';

                }*/

            // Aadhar Image



            // Licance Image

                /*if(!empty($request->file('licance_image'))){

                    $file_three = $request->file('licance_image');

                    $filenameWithExtension_three = $request->file('licance_image')->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $request->file('licance_image')->getClientOriginalExtension();

                    $filenameToStore_three = "licance_".time()."_".$filename_three.'.'.$extension_three;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['licance_image'] = $filenameToStore_three;

                }else{

                    $crud['licance_image'] = 'default.jpg';

                }*/



                // other documents

               /* $other_documents = '';

                $other_documents_name_arr = [];

                if(!empty($request->file('other_documents'))){

                    $other_documents_arr = $request->file('other_documents');

                    foreach($other_documents_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "other_documents_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($other_documents_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                }else{

                }

                if(!empty($other_documents_name_arr)){

                    $other_documents = implode(",",$other_documents_name_arr);

                }

                $crud['other_documents'] = $other_documents;*/



            // other documents

            $other_documents = '';

            $other_documents_name_arr = [];

            if(!empty($request->file('other_documents'))){

                $other_documents_arr = $request->file('other_documents');

                foreach($other_documents_arr as $key => $file_value){

                    $filenameWithExtension_three = $file_value->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $file_value->getClientOriginalExtension();

                    $filenameToStore_three_new = "other_documents_".time()."_".$filename_three.'.'.$extension_three;



                    array_push($other_documents_name_arr, $filenameToStore_three_new);                        

                    $folder_to_upload_sn = public_path().'/uploads/kyc/';

                    

                    if(!\File::exists($folder_to_upload_sn))

                        \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                     $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                }

                if(!empty($other_documents_name_arr)){

                    $other_documents = implode(",",$other_documents_name_arr);

                }

                $crud['other_documents'] = $other_documents;

            }else{

               // $other_documents_name_arr = [];

            }





            // pan_image  

            if(!empty($request->file('pan_image'))){

                $pan_image = '';

                $pan_image_name_arr = [];



                $pan_image_arr = $request->file('pan_image');

                foreach($pan_image_arr as $key => $file_value){

                    $filenameWithExtension_three = $file_value->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $file_value->getClientOriginalExtension();

                    $filenameToStore_three_new = "pan_image_".time()."_".$filename_three.'.'.$extension_three;



                    array_push($pan_image_name_arr, $filenameToStore_three_new);                        

                    $folder_to_upload_sn = public_path().'/uploads/kyc/';

                    

                    if(!\File::exists($folder_to_upload_sn))

                        \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                     $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                }

                if(!empty($pan_image_name_arr)){

                    $pan_image = implode(",",$pan_image_name_arr);

                }

                $crud['pan_image'] = $pan_image;

            }



            // adhar_image        

            if(!empty($request->file('adhar_image'))){

                $adhar_image = '';

                $adhar_image_name_arr = [];



                $adhar_image_arr = $request->file('adhar_image');

                foreach($adhar_image_arr as $key => $file_value){

                    $filenameWithExtension_three = $file_value->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $file_value->getClientOriginalExtension();

                    $filenameToStore_three_new = "adhar_image_".time()."_".$filename_three.'.'.$extension_three;



                    array_push($adhar_image_name_arr, $filenameToStore_three_new);                        

                    $folder_to_upload_sn = public_path().'/uploads/kyc/';

                    

                    if(!\File::exists($folder_to_upload_sn))

                        \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                     $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                }

                if(!empty($adhar_image_name_arr)){

                    $adhar_image = implode(",",$adhar_image_name_arr);

                }

                $crud['adhar_image'] = $adhar_image;

            }



            // licance_image        

            if(!empty($request->file('licance_image'))){

                $licance_image = '';

                $licance_image_name_arr = [];



                $licance_image_arr = $request->file('licance_image');

                foreach($licance_image_arr as $key => $file_value){

                    $filenameWithExtension_three = $file_value->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $file_value->getClientOriginalExtension();

                    $filenameToStore_three_new = "licance_image_".time()."_".$filename_three.'.'.$extension_three;



                    array_push($licance_image_name_arr, $filenameToStore_three_new);                        

                    $folder_to_upload_sn = public_path().'/uploads/kyc/';

                    

                    if(!\File::exists($folder_to_upload_sn))

                        \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                     $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                }

                if(!empty($licance_image_name_arr)){

                    $licance_image = implode(",",$licance_image_name_arr);

                }

                $crud['licance_image'] = $licance_image;

            }

            // Licance Image
            
            
            //Aadhar Pan Image
            
            if(!empty($request->file('aadhar_pan_image'))){

                $aadhar_pan_image = '';

                $aadhar_pan_image_name_arr = [];



                $aadhar_pan_image_arr = $request->file('aadhar_pan_image');

                foreach($aadhar_pan_image_arr as $key => $file_value){

                    $filenameWithExtension_three = $file_value->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $file_value->getClientOriginalExtension();

                    $filenameToStore_three_new = "aadhar_pan_image_".time()."_".$filename_three.'.'.$extension_three;



                    array_push($aadhar_pan_image_name_arr, $filenameToStore_three_new);                        

                    $folder_to_upload_sn = public_path().'/uploads/kyc/';

                    

                    if(!\File::exists($folder_to_upload_sn))

                        \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                     $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                }

                if(!empty($aadhar_pan_image_name_arr)){

                    $aadhar_pan_image = implode(",",$aadhar_pan_image_name_arr);

                }

                $crud['aadhar_pan_image'] = $aadhar_pan_image;

            }
            

            DB::beginTransaction();

            try {

                $last_id = '';

                  // \DB::enableQueryLog(); 

                // DB::enableQueryLog();

                $last_id = OBF::insertGetId($crud);                     

                if($last_id) {



                    //notification insert

                    $notification_template_master_data = DB::table('notification_template_master')->select('*')->where(['template_name' => 'obf_created','status' => 'active'])->first();

                    if(!empty($notification_template_master_data)){

                        $customer_name = $request->customer_name ?? null;

                        

                        $car_name_get = '';

                        $product_id = 0;

                        if($request->product_id != null && $request->product_id != ''){

                            $product_id = $request->product_id;

                        }



                        if($product_id != null){

                            $Cardata_get = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                         ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                          ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                          ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                          ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                          ->where('products.status', '=', 'active')

                          ->where('products.id', '=', $product_id)  

                          ->first();

                            

                            if(!empty($Cardata_get)){

                                if(!empty($Cardata_get->car_name) && $Cardata_get->car_name != '' && $Cardata_get->car_name != NULL){

                                    $car_name_get .= '('.$Cardata_get->car_name;    

                                    $car_name_get .= ' , '.$Cardata_get->varient_name.')';   

                                }

                            }    

                        }



                        $customer_name_with_car_detail = '';     



                        if($customer_name != null){

                            $customer_name_with_car_detail = $customer_name;

                        }   



                        if($customer_name != null){

                            $customer_name_with_car_detail .= ' '.$car_name_get;

                        }                  

                        

                        $data_new = str_replace("[customer_name_with_car_detail]",$customer_name_with_car_detail,$notification_template_master_data->template_data);



                        $notification_data = [

                            'user_id' => auth()->user()->id,

                            'notification_text' => $data_new,

                            'notification_link' => route('obf.view', ['id' => base64_encode($last_id)]),

                            'created_by' => auth()->user()->id,

                        ];

                        DB::table('notifications')->insert($notification_data);

                    }

                    //notification insert











                    // insert rto data

                    // die('stpo');

                    $rto = [

                    'obf_id' => $last_id,

                    'rto_type' => $request->rto_type ?? null,

                    'registration_date' => $request->registration_date ?? null,

                    'application_number' => $request->application_number ?? null,

                    'temporary_number' => $request->temporary_number ?? null,

                    'final_amount' => $request->final_amount ?? null,

                    'vehicle_number' => $request->vehicle_number ?? null,

                    'rto_type' => $request->rto_type ?? null,

                    ];

                    DB::table('obf_rto')->insert($rto);

                    

                    // insert Insurance data



                    $insurance = [

                    'obf_id' => $last_id,

                    'insurance_exist' => $request->insurance_exist ?? 1,

                    'ncb' => $request->ncb ?? null,

                    // 'documents' => $request->documents ?? null,

                    'date_of_insurance' => $request->date_of_insurance ?? null,

                    'time_of_insurance' => $request->time_of_insurance ?? null,

                    'last_time_of_insurance' => $request->last_time_of_insurance ?? null,

                    'company_name' => $request->insurance_company_name ?? null,

                    'ncb_amount' => $request->ncb_amount ?? null,

                    'policy_number' => $request->policy_number ?? null,     
                    'tyre_no1' => $request->tyre_no1 ?? null, 
                    'tyre_no2' => $request->tyre_no2 ?? null, 
                    'tyre_no3' => $request->tyre_no3 ?? null, 
                    'tyre_no4' => $request->tyre_no4 ?? null, 
                     'tyre_no5' => $request->tyre_no5 ?? null, 
                    ];





                    // documents Image

                    /*if(!empty($request->file('documents'))){

                        $documents_file = $request->file('documentws');

                        $filenameWithExtension_three12 = $request->file('documents')->getClientOriginalName();

                        $filename_three12 = pathinfo($filenameWithExtension_three12, PATHINFO_FILENAME);

                        $extension_three12 = $request->file('documents')->getClientOriginalExtension();

                        $filenameToStore_three12 = time()."_".$filename_three12.'.'.$extension_three12;

                    

                        $folder_to_upload12 = public_path().'/uploads/kyc/';

                    

                        if(!\File::exists($folder_to_upload12))

                            \File::makeDirectory($folder_to_upload12, 0777, true, true);



                        $documents_file->move($folder_to_upload12, $filenameToStore_three12);                    

                        $insurance['documents'] = $filenameToStore_three12;

                    }*/

                    // 'documents' => $request->documents ?? null,



                    if(!empty($request->file('documents'))){

                        $documents = '';

                        $documents_name_arr = [];



                        $documents_arr = $request->file('documents');

                        foreach($documents_arr as $key => $file_value){

                            $filenameWithExtension_three = $file_value->getClientOriginalName();

                            $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                            $extension_three = $file_value->getClientOriginalExtension();

                            $filenameToStore_three_new = "documents_".time()."_".$filename_three.'.'.$extension_three;



                            array_push($documents_name_arr, $filenameToStore_three_new);                        

                            $folder_to_upload_sn = public_path().'/uploads/kyc/';

                            

                            if(!\File::exists($folder_to_upload_sn))

                                \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                             $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                        }

                        if(!empty($documents_name_arr)){

                            $documents = implode(",",$documents_name_arr);

                        }

                        $insurance['documents'] = $documents;

                    }



                     DB::table('obf_insurance')->insert($insurance);

                    

                    $fasttag = [

                    'obf_id'=>$last_id,

                    'fasttag_number' => $request->fasttag_number ?? null,

                    'fasttag_brand' => $request->fasttag_brand ?? null,

                    'date_of_issue' => $request->date_of_issue ?? null,

                    'fasttag_amount' => $request->fasttag_amount ?? null,

                    'opening_stock' => $request->opening_stock ?? null,

                    ];

                    // DB::enableQueryLog();

                    

                        DB::table('obf_fasttag')->insert($fasttag);



                     $obf_finance = [

                    'obf_id'=>$last_id,

                    //'finance_required' => $request->finance_required ?? null,

                    'finance_name' => $request->finance_name ?? null,

                    'repo_rate' => $request->repo_rate ?? null,

                    'net_disbusment' => $request->net_disbusment ?? null,

                    'loan_amount' => $request->loan_amount ?? null,

                    'processing_fee' => $request->processing_fee ?? null,

                    'finance_discount' => $request->finance_discount ?? null,

                    'payout' => $request->payout ?? null,

                    'pay_booster' => $request->pay_booster ?? null,

                    'booster_percentage' => $request->booster_percentage ?? null,

                    'finance_type' => $request->finance_type ?? null,

                    'delivery_on_d_o' => $request->delivery_on_d_o ?? null,

                    'disbursement' => $request->disbursement ?? null,

                    'payout_amount'=>$request->payout_amount ?? null,

                    'lumsum_amount'=>$request->lumsum_amount ?? null,

                    'total_payout_amount'=>$request->total_payout_amount ?? null,

                    'boostamt'=>$request->boostamt ?? null,

                    ];

                    // DB::enableQueryLog();

                    

                        DB::table('obf_finance')->insert($obf_finance);



                    // Move Files to Folder

                       /* if (!empty($request->file('pan_image'))){

                            $file->move($folder_to_upload, $filenameToStore);

                        }

                        if (!empty($request->file('adhar_image'))){

                            $file_two->move($folder_to_upload, $filenameToStore_two);

                        }

                        if (!empty($request->file('licance_image'))){

                            $file_three->move($folder_to_upload, $filenameToStore_three);

                        }*/

                        // Move Files to Folder

                        DB::commit();

                        return redirect()->route('obf')->with('success', 'Record inserted successfully');



                       // var_dump($insurance);

            //die();

                } else {

                    DB::rollback();

                    return redirect()->back()->with('error', 'Failed to insert record')->withInput();

                }

            }catch (\Throwable $th){

                DB::rollback();

                // die('s');

                return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();

            }

        

    }
/** insert */



    /** view */
        public function view(Request $request){

            if(auth()->user()->can('obf-view')){

                $id = base64_decode($request->id);               

                $path = URL('/uploads/kyc').'/';



                

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();



                $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

                

                        

                $data = OBF::select('obf.id','obf.sales_person_id',

                                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_name' ,'obf.einvoicing_applicable', 'obf.gst' ,'obf.address' ,'obf.registration_tax_id' ,'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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','obf.gst_required', 'obf.aadhar_pan_link','obf.aadhar_pan_image',

                'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status','obf.municipal_tax_id','obf.tcs_tax_id','obf.registration',



                'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',



                'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf'



                               /* DB::Raw("CASE

                                            WHEN ".'obf_insurance.documents'." != '' THEN CONCAT("."'".$path."'".", ".'obf_insurance.documents'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as documents"),

                                DB::Raw("CASE

                                            WHEN ".'pan_image'." != '' THEN CONCAT("."'".$path."'".", ".'pan_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as pan_image"),

                                DB::Raw("CASE

                                            WHEN ".'adhar_image'." != '' THEN CONCAT("."'".$path."'".", ".'adhar_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as adhar_image"),

                                DB::Raw("CASE

                                            WHEN ".'licance_image'." != '' THEN CONCAT("."'".$path."'".", ".'licance_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as licance_image"),*/

                                )

                    

                            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                            ->leftjoin('products' ,'obf.product_id' ,'products.id')

                            ->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('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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                            ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                            ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                            ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                            ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                            ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                            ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                            ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                            ->where(['obf.id' => $id])

                            ->first();      

            }





          /*  'obf_insurance.ncb','obf_insurance.documents','obf_insurance.date_of_insurance','obf_insurance.last_time_of_insurance','obf_insurance.time_of_insurance','obf_insurance.company_name','obf_insurance.ncb_amount','obf_insurance.policy_number','obf_insurance.status as insurance_status','obf_insurance.documents',*/







             $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                    ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                    ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                    ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                    ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                    ->where('products.status', '=', 'active')

                    ->get();



                    // dd($carmodel);





        $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                            ->leftJoin('car_model','car_model.id','=','obf.product_id')

                            ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                            ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                            ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                            ->where('obf.id', '=', $id)  

                            ->first();



        $fule = Obf::select('fule_type.title as fule_name')

        ->leftJoin('products','products.id','=','obf.product_id')

        ->leftJoin('car_varient','car_varient.id','=','products.veriant')

        ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

        ->where('obf.id', '=', $id)  

        ->first();



         $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();



         $OtherOne = OtherOne::where(['status' => 'active'])->get();  

            

         $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 



         $Inventory_data = [];

         if($data->inventory_id != NULL){

             $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

         }



        $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  



        $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  



        $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();



        $accessory_sum = 0;

        if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

            $accessory_id_arr = explode(' ,', $data->accessory_id);

            if(!empty($accessory_id_arr)){

                $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

            }

        }



        // $sales = User::role('sales')->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();



         $sales = User::role(['sales','KEC'])->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();



         $user = auth()->user();

         $user_role = $user->getRoleNames();



         



        return view('obf.view')->with(['data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'user_role'=> $user_role[0],'accessory_sum' => $accessory_sum]);

        }

    /** view */  

    

    /** insurance view */

        public function insuranceview(Request $request){

            if(auth()->user()->can('obf-insuranceview')){

                $id = base64_decode($request->id);

                $path = URL('/uploads/kyc').'/';



                $sales = User::role('sales')->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id' , 'brand_name' ,'amount')->where(['status' => 'active'])->get();

                $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

                $data = OBF::select('obf.id','obf.sales_person_id',

                                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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' ,'fasttags.brand_name AS fasttag_brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                                DB::Raw("CASE

                                            WHEN ".'pan_image'." != '' THEN CONCAT("."'".$path."'".", ".'pan_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as pan_image"),

                                DB::Raw("CASE

                                            WHEN ".'adhar_image'." != '' THEN CONCAT("."'".$path."'".", ".'adhar_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as adhar_image"),

                                DB::Raw("CASE

                                            WHEN ".'licance_image'." != '' THEN CONCAT("."'".$path."'".", ".'licance_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as licance_image"),

                                )

                            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                            ->leftjoin('products' ,'obf.product_id' ,'products.id')

                            ->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')

                            ->where(['obf.id' => $id])

                            ->first();      

            }

            return view('obf.insurance')->with(['data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'finance' => $finance ,'lead' => $lead]);

        }

    /** insurance view */

    

    /** RTO view */

        public function rtoview(Request $request){

            if(auth()->user()->can('obf-rtoview')){

                $id = base64_decode($request->id);

                $path = URL('/uploads/kyc').'/';



                $sales = User::role('sales')->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id' , 'brand_name' ,'amount')->where(['status' => 'active'])->get();

                $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

                $data = OBF::select('obf.id','obf.sales_person_id',

                                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new','branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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' ,'fasttags.brand_name AS fasttag_brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                                DB::Raw("CASE

                                            WHEN ".'pan_image'." != '' THEN CONCAT("."'".$path."'".", ".'pan_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as pan_image"),

                                DB::Raw("CASE

                                            WHEN ".'adhar_image'." != '' THEN CONCAT("."'".$path."'".", ".'adhar_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as adhar_image"),

                                DB::Raw("CASE

                                            WHEN ".'licance_image'." != '' THEN CONCAT("."'".$path."'".", ".'licance_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as licance_image"),

                                )

                            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                            ->leftjoin('products' ,'obf.product_id' ,'products.id')

                            ->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')

                            ->where(['obf.id' => $id])

                            ->first();      

            }

            return view('obf.rto')->with(['data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'finance' => $finance ,'lead' => $lead]);

        }

    /** RTO view */

    

    /** fasttag view */

        public function fasttagview(Request $request){

            if(auth()->user()->can('obf-fasttagview')){

                $id = base64_decode($request->id);

                $path = URL('/uploads/kyc').'/';



                $sales = User::role('sales')->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id' , 'brand_name' ,'amount')->where(['status' => 'active'])->get();

                $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

                $data = OBF::select('obf.id','obf.sales_person_id',

                                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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' ,'fasttags.brand_name AS fasttag_brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                                DB::Raw("CASE

                                            WHEN ".'pan_image'." != '' THEN CONCAT("."'".$path."'".", ".'pan_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as pan_image"),

                                DB::Raw("CASE

                                            WHEN ".'adhar_image'." != '' THEN CONCAT("."'".$path."'".", ".'adhar_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as adhar_image"),

                                DB::Raw("CASE

                                            WHEN ".'licance_image'." != '' THEN CONCAT("."'".$path."'".", ".'licance_image'.")

                                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                                        END as licance_image"),

                                )

                            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                            ->leftjoin('products' ,'obf.product_id' ,'products.id')

                            ->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')

                            ->where(['obf.id' => $id])

                            ->first();      

            }

            return view('obf.fasttag')->with(['data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'finance' => $finance ,'lead' => $lead]);

        }

    /** fasttag view */



    /** Edit */

        public function edit(Request $request){

                

            $id = base64_decode($request->id);

            $path = URL('/uploads/kyc').'/';



            $sales = User::role(['sales','KEC'])->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();



            

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id' , 'brand_name' ,'amount')->where(['status' => 'active'])->get();

                //$branch = Branch::select('id' , 'name' ,'city','tax_percentage')->where(['status' => 'active'])->get();

                //$branch = Branch::select('*')->where(['status' => 'active'])->get();



                //branch selection

                $branch = Branch::select('*');

                if(!empty(auth()->user()->branch) && auth()->user()->branch != '' && auth()->user()->branch != NULL){

                    $branch_arr = explode(',', auth()->user()->branch);

                    if(!empty($branch_arr)){

                        $branch = $branch->whereIn('id',$branch_arr);    

                    }

                }                        

                $branch = $branch->where(['status' => 'active'])->get(); 

                //branch selection







                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();    

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $pricelist_master = DB::table('pricelist_master')->select('id','car_model')->get();

                $occupation_lists = DB::table('occupation')->select('id','occupation_title')->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                $data = OBF::select('obf.id','obf.sales_person_id',

                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id','obf.fuel_type','products.veriant','products.car_model','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'obf.fasttag_brand_name AS fasttag_brand_name','registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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' ,'fasttags.id AS fasttag_id','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','obf.offer_kin','obf.offer_ib', 'obf.offer_ib_amount', 'obf.offer_kin_amount','obf.gst_required', 'obf.aadhar_pan_link','obf.aadhar_pan_image', 

                'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status','obf.way_off','obf.final_on_road_price_after_way_off',

                'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.pricelist_id','obf.registration_tax_id','obf.municipal_tax_id','obf.municipal_tax_applicable','obf.tcs_tax_id','obf.nominee_dob','convinience_program',



                    'obf.step1_remarks','obf.step3_remarks','obf.step4_remarks','obf.step5_remarks','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',



                 'obf_finance.finance_name','obf_finance.repo_rate','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.rsa_master_obf','obf_finance.disbursement','obf_finance.finance_type','obf.step2_remarks','obf.step1_remarks','obf_finance.payout_amount','obf_finance.lumsum_amount','obf_finance.total_payout_amount'



               /* DB::Raw("CASE

                            WHEN ".'obf_insurance.documents'." != '' THEN CONCAT("."'".$path."'".", ".'obf_insurance.documents'.")

                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                        END as documents"),*/

               /* DB::Raw("CASE

                            WHEN ".'pan_image'." != '' THEN CONCAT("."'".$path."'".", ".'pan_image'.")

                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                        END as pan_image"),*/

             /*   DB::Raw("CASE

                            WHEN ".'adhar_image'." != '' THEN CONCAT("."'".$path."'".", ".'adhar_image'.")

                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                        END as adhar_image"),

                DB::Raw("CASE

                            WHEN ".'licance_image'." != '' THEN CONCAT("."'".$path."'".", ".'licance_image'.")

                            ELSE CONCAT("."'".$path."'".", 'default.jpg')

                        END as licance_image"),*/

                )

                        ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                        ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                        ->leftjoin('products' ,'obf.product_id' ,'products.id')

                        ->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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                        ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                        ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                        ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                        ->where(['obf.id' => $id])

                        ->first();    



            $fule_data = Fuletype::select('*')->where('status', '=', 'active')->get();





                        /*'obf_insurance.ncb','obf_insurance.date_of_insurance','obf_insurance.last_time_of_insurance','obf_insurance.time_of_insurance','obf_insurance.company_name AS insurance_company_name','obf_insurance.ncb_amount','obf_insurance.policy_number','obf_insurance.status as insurance_status',

                        'obf_insurance.documents',

                */

                $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  



                /*echo "<pre>";

                print_r($ObfInsurance);

                die('s');*/







                        

               // $Carmodel = Carmodel::where(['status' => 'active'])->get();

           //$Carvarient = Carvarient::where(['status' => 'active'])->get();  



                /*$Carmodel = Carmodel::select('car_model.name as car_name','car_model.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                    ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                    ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                    ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                    ->where('car_model.status', '=', 'active')

                    ->get();*/



                   /* car_model

                    Variant*/







            $Carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                            ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                            ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                            ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                            ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                            ->where('products.status', '=', 'active')

                            ->get();



            $Product_data = Product::where('id',$data->product_id)->where('status', '=', 'active')->first();    

            

            $Carvarient = Carvarient::where(['status' => 'active','car_model'=>$data->car_model,'fule_type'=>$data->fuel_type])->get(); 

            //dd($data->car_model);

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color','car_varient.id as varient_id')

                        ->leftJoin('products','products.id','=','obf.product_id')

                        ->leftJoin('car_model','car_model.id','=','products.car_model')

                        ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                        ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                        ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                        ->where('obf.id', '=', $id)  

                        ->first();



            $Inventory_data = [];

            if($data->inventory_id != NULL){

                $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

            }



            $Carmodel_arr = Carmodel::where(['status' => 'active'])->get();  



            $user = auth()->user();

            

            $user_role = $user->getRoleNames();

        

            $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->first();



            $OtherOne = OtherOne::where(['status' => 'active'])->get();  

        

            $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 



            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  



            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();



            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

            

            $auth = User::select('department.name')->join('department','department.id','users.department_id')->where('users.id',auth()->user()->id)->first();

            $depart = $auth->name;

            $rto_master = rto_master::select('amount')->where('status','=','active')->first();

            $rto_amount = $rto_master->amount;

            $offers = OBF::select('offer_kin','offer_ib')->where('obf.id','=',$id)->first();

            //dd($offers->offer_ib);

            $kin = $offers->offer_kin;

            $kin_o = explode(',', $kin);

            //dd($kin);

            //dd(explode(',', $kin));

            foreach($kin_o as $k){

                $kin_title = Offer::select('title')->where('id','=',$k)->first();           

            }

            //dd($kin_title->title);



            $ib = $offers->offer_ib;

            $ib_o = explode(',', $ib);



            foreach($ib_o as $i){

              $ib_title = Offer::select('title')->where('id','=',$i)->first();

            }
            
            // Code For Make-deal Data Show Only in Insurance & RTO Form
            $make_deal_data = OBF::select('obf.id','obf.sales_person_id',

            DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'obf.booking_date','branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id','products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'obf.fasttag_brand_name AS fasttag_brand_name','registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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_new' ,'fasttags.id AS fasttag_id','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','allocation.tentetive_delivery_date','Team_master.team_name','obf.other_accesssories_prise',
    
            'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',
    
            'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.pricelist_id','obf.registration_tax_id','obf.municipal_tax_id','obf.municipal_tax_applicable','obf.tcs_tax_id','obf.nominee_dob','convinience_program',
    
            'obf.step1_remarks','obf.step3_remarks','obf.step4_remarks','obf.step5_remarks','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',
    
            'obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.rsa_master_obf',
    
            'car_model.name AS car_model_name','car_varient.name AS car_varient_name','interiorcolor.name AS interiorcolor_name','exteriorcolor.name AS exteriorcolor_name','inventory.key_number','inventory.engine_number','inventory.chassis_number','inventory.vin_number', 'obf_insurance.company_name AS insurance_company_name','obf_insurance.ncb_amount','obf.offer_ib_amount','obf.offer_kin_amount','obf.offer_amount','inventory.purchase_price','inventory.aging_days','inventory.remark',
            
            'make_deal.obf_id','make_deal.purchase_gst_tax_deal','make_deal.final_deal_amt','make_deal.deal_amt','make_deal.gst_tax_deal','make_deal.cess_tax_deal','make_deal.tcs_tax_deal','make_deal.municipal_tax_deal','make_deal.rto_deal','make_deal.insurance_deal','make_deal.created_by')
    
            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')
            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')
            ->leftjoin('products', 'obf.product_id', 'products.id')
            ->leftjoin('car_model', 'car_model.id', 'products.car_model')
            ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')
            ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')
            ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_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('allocation' ,'obf.id' ,'allocation.obf_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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')
            ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')
            ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')
            ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')
            ->leftjoin('inventory' ,'obf.inventory_id' ,'inventory.id')
            ->leftjoin('Team_master','Team_master.id','users.team_name')
            ->leftjoin('make_deal','obf.id','make_deal.obf_id')
            ->where(['obf.id' => $id])->first(); 
    
            $make_deal_branch = Branch::select('start_value1', 'end_value1', 'tax_percentage1', 'start_value2', 'end_value2', 'tax_percentage2')->where(['status' => 'active','id' => $make_deal_data->branch_id])->first();
           
            $make_deal_price_list_name = DB::table('pricelist_master')->select('basic','price_list_name','gst_percentage','gst_amount','cess_percentage','cess_amount')->where('id',  $make_deal_data->pricelist_id)->first();
    
            $make_deal_rto_data = DB::table('rto_master')->select('rto_type', 'rto_tax_individual', 'rto_tax_company', 'totali_individual_charges','totalcharges')->Where('rto_type', 'like', $make_deal_data->customer_type . '%')->orderBy('id', 'DESC')->first();
    
            $make_deal_accessory_sum = 0;
    
            if(!empty($make_deal_data->accessory_id) && $make_deal_data->accessory_id != NULL && $make_deal_data->accessory_id != ''){
                $accessory_id_arr = explode(' ,', $make_deal_data->accessory_id);
                if(!empty($accessory_id_arr)){
                    $make_deal_accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');           
                }
            }

            return view('obf.edit')->with(['data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'finance' => $finance ,'lead' => $lead,'pricelist_master' => $pricelist_master,'Carmodel'=>$Carmodel,'Cardata'=>$Cardata,'user_role'=> $user_role[0],'Carmodel_arr'=>$Carmodel_arr,'Product_data' => $Product_data, 'mcp' => $mcp, 'OtherOne' => $OtherOne, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule_data' => $fule_data, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum,'depart'=>$depart,'rto_amount'=>$rto_amount,'kin_title'=>$kin_title,'ib_title'=>$ib_title,'Carvarient'=>$Carvarient, 'make_deal_data'=>$make_deal_data, 'make_deal_branch' => $make_deal_branch, 'make_deal_price_list_name' => $make_deal_price_list_name, 'make_deal_rto_data'=>$make_deal_rto_data, 'make_deal_accessory_sum'=>$make_deal_accessory_sum]);

        }

    /** Edit */



    /** update */

        public function update(Request $request){

            if($request->ajax()){ 

                return true;

            }



            if(empty($request->accessories)){

                $request->accessories = [];

            }



            if(empty($request->priclist_detail)){

                $request->priclist_detail = [];

            }



            if(empty($request->other_details_1)){

                $request->other_details_1 = [];

            }



            if(empty($request->other_details_2)){

                $request->other_details_2 = [];

            }

            $detail_value_arr = [];

            if(!empty($request->priclist_detail)){

                foreach ($request->priclist_detail as $value) {

                    $arr_value = explode('__', $value);

                    $detail_value_sub_arr = [];

                    $detail_value_sub_arr['detail_field'] = $arr_value[0];

                    $detail_value_sub_arr['value_field'] = $arr_value[1]; 

                    $detail_value_arr[] = $detail_value_sub_arr; 

                }

            }

            $detail_value_arr_json = json_encode($detail_value_arr);



            /*echo "<pre>";

            var_dump($detail_value_arr_json);

            die('s');*/

            

            if(isset($request->id)){

                $ext_record = OBF::find($request->id);

            }



            if(!empty($request->dob) && $request->dob != '' && $request->dob != NULL)

            {

               $request->dob = date("Y-m-d",strtotime($request->dob));        

            }



            if(!empty($request->nominee_dob) && $request->nominee_dob != '' && $request->nominee_dob != NULL)

            {

               $request->nominee_dob = date("Y-m-d",strtotime($request->nominee_dob));        

            }



            /*var_dump($request->convinience_program );

            die('s');*/


            //offer type



                $offer_type = $request->offer_type;



                $offer_kin = $request->offer_kin;                

                if($offer_kin != null){

                    $offerkin = implode(',',$offer_kin);

                }

                else{

                    $offerkin = null;

                }



                $offer_ib = $request->offer_ib;

                if($offer_ib != null){

                    $offerib = implode(',',$offer_ib);

                }

                else{

                    $offerib = null;

                }

                if($request->offer_amount != null){

                   // $ex_showroom_price = $request->with_offer_ex_showroom_price;

                    $ex_showroom_price = $request->deal_price;

                }else{

                    $ex_showroom_price = $request->ex_showroom_price;

                }
            

            $crud = [

                'booking_date' => $request->booking_date,

                'customer_name' => strtoupper($request->customer_name),

                'customer_birthdate' => $request->customer_birthdate ?? null,

                'customer_type' => $request->customer_type,

                'customer_type_new' => $request->customer_type_new,

                'branch_id' => $request->branch,

                'company_name' => $request->company_name ?? null,

                'einvoicing_applicable' => $request->einvoapp ?? null,
                
                'gst_required' => $request->gst_required === 'yes' ? 'yes' : 'no',

                'gst' => $request->gst ?? null,

                'address' => $request->address_1 ?? null,

                'registration' => $request->address_2 ?? null,

                'email' => $request->email ?? null,

                'pan_number' => $request->pan_number ?? null,

                'adhar_number' => $request->adhar_number ?? null,

                'licance_number' => $request->license_number ?? null,

                'contact_number' => $request->contact_number ?? null,

                'dob' => $request->dob ?? null,

                'nominee_dob' => $request->nominee_dob ?? null,

                'nominee_name' => $request->nominee_name ?? null,

                'nominee_reletion' => $request->nominee_relation ?? null,

                'nominee_age' => $request->nominee_age ?? null,

                'occupation' => $request->occupation ?? null,

                'sales_person_id' => $request->sales_person_name ?? null,

                'product_id' => $request->product_id ?? null,

                'fuel_type' => $request->fuel_type ?? null,

                'offer_type' => $offer_type ?? null,

                //'offer_kin'=>$offerkin ?? null,
                'offer_kin' => $offerkin ?? $request->offers_kin_id ?? null,

                //'offer_ib'=>$offerib ?? null,
                'offer_ib' => $offerib ?? $request->offers_ib_id ?? null,

                'offer_ib_amount'=>$request->offers_ib_amount ?? null,

                'offer_kin_amount'=>$request->offers_kin_amount ?? null,

                'offer_amount' => $request->offe_amount ?? null,

                'deal_price' =>$request->deal_price ?? null,

                'way_off'=>$request->way_off ?? null,

                'final_on_road_price_after_way_off' =>$request->final_on_road_price_after_way_off ?? null,

                'ex_showroom_price' => $request->ex_showroom_price ?? null,

                'registration_tax_id' => $request->registration_tax ?? null,

                'insurance_id' => $request->insurance ?? null,

                'municipal_tax_id' => $request->municipal_tax ?? null,

                'municipal_tax_applicable' => $request->municipal_tax_applicable ?? 2,

                'tcs_tax_id' => $request->tcs_tax ?? null,

                'accessory_id' => implode(" ,",$request->accessories) ?? null,

                'priclist_detail' => $detail_value_arr_json,

                'extanded_warranty_id' => $request->extended_warranty ?? null,

                'fasttag_brand_name' => $request->fasttag_brand_name ?? null,

                'trade_car_name' => $request->trade_car_name ?? null,

                'trade_car_value' => $request->trade_car_value ?? null,

                'convinience_program' => $request->convinience_program ?? null,

                'other_details_1' => implode(" ,",$request->other_details_1) ?? null,

                'other_details_2' => implode(" ,",$request->other_details_2) ?? null,

                'rsa_master_obf' => $request->rsa_master_obf ?? null,

                'on_road_price' => $request->on_road_price ?? null,

                'on_road_price_word' => $request->on_road_price_word ?? null,

                // 'finance_id' => $request->finance_name ?? null,

                // 'finance_branch_id' => $request->finance_branch_name ?? null,

                'lead_id' => $request->lead ?? null,

                'sub_lead_id' => $request->sub_lead_id ?? null,

                'booking_amount' => $request->booking_amount ?? null,

                'mode_of_payment' => $request->mode_of_payment ?? null,

                'pricelist_id' => $request->pricelist_manager_id ?? null,

                'step1_remarks' => $request->step1_remarks ?? null,

                'step2_remarks' => $request->step2_remarks ?? null,

                'step3_remarks' => $request->step3_remarks ?? null,

                'step4_remarks' => $request->step4_remarks ?? null,

                'step5_remarks' => $request->step5_remarks ?? null,

                //'status' => 'finance_apporve',

                'status'=> $request->obf_status,
                
                'aadhar_pan_link' => $request->aadhar_pan_link === 'yes' ? 'yes' : 'no',

                'other_accesssories_detail' => $request->other_accesssories_detail,

                 'other_accesssories_prise' => $request->other_accesssories_prise,

                'created_at' => date('Y-m-d H:i:s'),

                'created_by' => auth()->user()->id,

                'updated_at' => date('Y-m-d H:i:s'),

                'updated_by' => auth()->user()->id

            ];
            
        

            // dd($request->file('pan_image'));

            // Pan Image

                /*if(!empty($request->file('pan_image'))){

                    $file = $request->file('pan_image');

                    $filenameWithExtension = $request->file('pan_image')->getClientOriginalName();

                    $filename = pathinfo($filenameWithExtension, PATHINFO_FILENAME);

                    $extension = $request->file('pan_image')->getClientOriginalExtension();

                    $filenameToStore = time()."_".$filename.'.'.$extension;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['pan_image'] = $filenameToStore;

                }else{

                    $crud['pan_image'] = $ext_record->pan_image;

                }*/

               /* var_dump($data['pan_image']);

                die('s');*/

            // Pan Image



            // Aadhar Image

                /*if(!empty($request->file('adhar_image'))){

                    $file_two = $request->file('adhar_image');

                    $filenameWithExtension_two = $request->file('adhar_image')->getClientOriginalName();

                    $filename_two = pathinfo($filenameWithExtension_two, PATHINFO_FILENAME);

                    $extension_two = $request->file('adhar_image')->getClientOriginalExtension();

                    $filenameToStore_two = time()."_".$filename_two.'.'.$extension_two;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['adhar_image'] = $filenameToStore_two;

                }else{

                    $crud['adhar_image'] = $ext_record->adhar_image;

                }*/





            // Aadhar Image



            // Licance Image

               /* if(!empty($request->file('licance_image'))){

                    $file_three = $request->file('licance_image');

                    $filenameWithExtension_three = $request->file('licance_image')->getClientOriginalName();

                    $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                    $extension_three = $request->file('licance_image')->getClientOriginalExtension();

                    $filenameToStore_three = time()."_".$filename_three.'.'.$extension_three;

    

                    $folder_to_upload = public_path().'/uploads/kyc/';

    

                    if(!\File::exists($folder_to_upload))

                        \File::makeDirectory($folder_to_upload, 0777, true, true);

    

                    $crud['licance_image'] = $filenameToStore_three;

                }else{

                    $crud['licance_image'] = $ext_record->licance_image;

                }*/



                // other documents

                $other_documents = '';

                $other_documents_name_arr = [];

                if(!empty($request->file('other_documents'))){

                    $other_documents_arr = $request->file('other_documents');

                    foreach($other_documents_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "other_documents_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($other_documents_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                    if(!empty($other_documents_name_arr)){

                        $other_documents = implode(",",$other_documents_name_arr);

                    }

                    $crud['other_documents'] = $other_documents;

                }else{

                   // $other_documents_name_arr = [];

                }



                // pan_image  

                if(!empty($request->file('pan_image'))){

                    $pan_image = '';

                    $pan_image_name_arr = [];



                    $pan_image_arr = $request->file('pan_image');

                    foreach($pan_image_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "pan_image_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($pan_image_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                    if(!empty($pan_image_name_arr)){

                        $pan_image = implode(",",$pan_image_name_arr);

                    }

                    $crud['pan_image'] = $pan_image;

                }



                // adhar_image        

                if(!empty($request->file('adhar_image'))){

                    $adhar_image = '';

                    $adhar_image_name_arr = [];



                    $adhar_image_arr = $request->file('adhar_image');

                    foreach($adhar_image_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "adhar_image_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($adhar_image_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                    if(!empty($adhar_image_name_arr)){

                        $adhar_image = implode(",",$adhar_image_name_arr);

                    }

                    $crud['adhar_image'] = $adhar_image;

                }



                // licance_image        

                if(!empty($request->file('licance_image'))){

                    $licance_image = '';

                    $licance_image_name_arr = [];



                    $licance_image_arr = $request->file('licance_image');

                    foreach($licance_image_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "licance_image_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($licance_image_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                    if(!empty($licance_image_name_arr)){

                        $licance_image = implode(",",$licance_image_name_arr);

                    }

                    $crud['licance_image'] = $licance_image;

                }


                //Aadhar Pan Image
                
                if(!empty($request->file('aadhar_pan_image'))){

                    $aadhar_pan_image = '';

                    $aadhar_pan_image_name_arr = [];



                    $aadhar_pan_image_arr = $request->file('aadhar_pan_image');

                    foreach($aadhar_pan_image_arr as $key => $file_value){

                        $filenameWithExtension_three = $file_value->getClientOriginalName();

                        $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                        $extension_three = $file_value->getClientOriginalExtension();

                        $filenameToStore_three_new = "aadhar_pan_image_".time()."_".$filename_three.'.'.$extension_three;



                        array_push($aadhar_pan_image_name_arr, $filenameToStore_three_new);                        

                        $folder_to_upload_sn = public_path().'/uploads/kyc/';

                        

                        if(!\File::exists($folder_to_upload_sn))

                            \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                         $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                    }

                    if(!empty($aadhar_pan_image_name_arr)){

                        $aadhar_pan_image = implode(",",$aadhar_pan_image_name_arr);

                    }

                    $crud['aadhar_pan_image'] = $aadhar_pan_image;

                }
                    



                

                        $rto = [

                        'obf_id'=>$request->id,

                        'rto_type' => $request->rto_type ?? null,

                        'registration_date' => $request->registration_date ?? null,

                        'application_number' => $request->application_number ?? null,

                        'temporary_number' => $request->temporary_number ?? null,

                        'final_amount' => $request->final_amount ?? null,

                        'vehicle_number' => $request->vehicle_number ?? null,

                        'rto_type' => $request->rto_type ?? null,

                        'status' => $request->rto_status ?? null,

                        ];

                        // DB::enableQueryLog();

                        if(DB::table('obf_rto')->where("obf_id", "=", $request->id)->exists()){

                            DB::table('obf_rto')->where(['obf_id' => $request->id])->update($rto);

                        }else{

                            DB::table('obf_rto')->insert($rto);

                        }

                        //  dd(DB::getQueryLog());



                        $insurance = [

                            'obf_id'=>$request->id,

                            'insurance_exist' => $request->insurance_exist ?? 1,

                            'ncb' => $request->ncb ?? null,

                            'date_of_insurance' => $request->date_of_insurance ?? null,

                            'time_of_insurance' => $request->time_of_insurance ?? null,

                            'last_time_of_insurance' => $request->last_time_of_insurance ?? null,

                            'company_name' => $request->insurance_company_name ?? null,

                            'ncb_amount' => $request->ncb_amount ?? null,

                            'policy_number' => $request->policy_number ?? null,
                            'tyre_no1' => $request->tyre_no1 ?? null, 
                            'tyre_no2' => $request->tyre_no2 ?? null, 
                            'tyre_no3' => $request->tyre_no3 ?? null, 
                            'tyre_no4' => $request->tyre_no4 ?? null, 
                            'tyre_no5' => $request->tyre_no5 ?? null, 

                            'status' => $request->insurance_status ?? null,

                        ];



                        // documents Image

                        /*if(!empty($request->file('documents'))){

                            $documents_file = $request->file('documents');

                            $filenameWithExtension_three12 = $request->file('documents')->getClientOriginalName();

                            $filename_three12 = pathinfo($filenameWithExtension_three12, PATHINFO_FILENAME);

                            $extension_three12 = $request->file('documents')->getClientOriginalExtension();

                            $filenameToStore_three12 = time()."_".$filename_three12.'.'.$extension_three12;

                    

                            $folder_to_upload12 = public_path().'/uploads/kyc/';

                    

                            if(!\File::exists($folder_to_upload12))

                                \File::makeDirectory($folder_to_upload12, 0777, true, true);



                            $documents_file->move($folder_to_upload12, $filenameToStore_three12);                    

                            $insurance['documents'] = $filenameToStore_three12;

                        }*/

                        // 'documents' => $request->documents ?? null,

                        

                        // insert Insurance data



                        // documents        

                        if(!empty($request->file('documents'))){

                            $documents = '';

                            $documents_name_arr = [];



                            $documents_arr = $request->file('documents');

                            foreach($documents_arr as $key => $file_value){

                                $filenameWithExtension_three = $file_value->getClientOriginalName();

                                $filename_three = pathinfo($filenameWithExtension_three, PATHINFO_FILENAME);

                                $extension_three = $file_value->getClientOriginalExtension();

                                $filenameToStore_three_new = "documents_".time()."_".$filename_three.'.'.$extension_three;



                                array_push($documents_name_arr, $filenameToStore_three_new);                        

                                $folder_to_upload_sn = public_path().'/uploads/kyc/';

                                

                                if(!\File::exists($folder_to_upload_sn))

                                    \File::makeDirectory($folder_to_upload_sn, 0777, true, true);

                                 $file_value->move($folder_to_upload_sn, $filenameToStore_three_new);

                            }

                            if(!empty($documents_name_arr)){

                                $documents = implode(",",$documents_name_arr);

                            }

                            $insurance['documents'] = $documents;

                        }

                        

                        

                        // DB::enableQueryLog();

                         // DB::enableQueryLog();

                       

                         if(DB::table('obf_insurance')->where("obf_id", "=", $request->id)->exists()){

                            if(DB::table('obf_insurance')->where("id", "=", $request->obf_insurance_id)->exists()){

                                DB::table('obf_insurance')->where(['id' => $request->obf_insurance_id])->update($insurance);

                            }else{

                                DB::table('obf_insurance')->where(['obf_id' => $request->id])->update($insurance);

                            }

                        }else{

                            DB::table('obf_insurance')->insert($insurance);

                        }

                          // dd(DB::getQueryLog());

                        

                        $fasttag = [

                        'obf_id'=>$request->id,

                        'fasttag_number' => $request->fasttag_number ?? null,

                        'fasttag_brand' => $request->fasttag_brand ?? null,

                        'date_of_issue' => $request->date_of_issue ?? null,

                        'fasttag_amount' => $request->fasttag_amount ?? null,

                        'opening_stock' => $request->opening_stock ?? null,

                        'status' => $request->fasttag_status ?? null,

                        ];

                        // DB::enableQueryLog();

                        if(DB::table('obf_fasttag')->where("obf_id", "=", $request->id)->exists()){

                            DB::table('obf_fasttag')->where(['obf_id' => $request->id])->update($fasttag);

                        }else{

                            DB::table('obf_fasttag')->insert($fasttag);

                        }

                        //  dd(DB::getQueryLog());



                         $obf_finance = [

                        'obf_id'=>$request->id,

                        'finance_name' => $request->finance_name ?? null,

                        'finance_required' => $request->finance_required ?? null,

                        'repo_rate' => $request->repo_rate ?? null,

                        'net_disbusment' => $request->net_disbusment ?? null,

                        'loan_amount' => $request->loan_amount ?? null,

                        'processing_fee' => $request->processing_fee ?? null,

                        'finance_discount' => $request->finance_discount ?? null,

                        'payout' => $request->payout ?? null,

                        'pay_booster' => $request->pay_booster ?? null,

                        'booster_percentage' => $request->booster_percentage ?? null,

                        'finance_type' => $request->finance_type ?? null,

                        'delivery_on_d_o' => $request->delivery_on_d_o ?? null,

                        'disbursement' => $request->disbursement ?? null,

                        'payout_amount'=>$request->payout_amount ?? null,

                        'lumsum_amount'=>$request->lumsum_amount ?? null,

                        'total_payout_amount'=>$request->total_payout_amount ?? null,

                        'boostamt'=>$request->boostamt ?? null,

                        ];

                        // DB::enableQueryLog();

                         if(DB::table('obf_finance')->where("obf_id", "=", $request->id)->exists()){

                            DB::table('obf_finance')->where(['obf_id' => $request->id])->update($obf_finance);

                        }else{

                            DB::table('obf_finance')->insert($obf_finance);

                        }



                DB::beginTransaction();

                try {

                    /*echo "<pre>";

                    print_r($crud);

                    die('s');*/



                      // DB::enableQueryLog();
                    
                    $update = OBF::where(['id' => $request->id])->update($crud);

                     // dd(DB::getQueryLog());

                    

                    if ($update) {

                        // Move Files to Folder





                            /*if (!empty($request->file('pan_image'))){

                                $file->move($folder_to_upload, $filenameToStore);

                            }*/

                           /* if (!empty($request->file('adhar_image'))){

                                $file_two->move($folder_to_upload, $filenameToStore_two);

                            }

                            if (!empty($request->file('licance_image'))){

                                $file_three->move($folder_to_upload, $filenameToStore_three);

                            }*/

                            /* var_dump($filenameToStore);

                        die('s');*/



                            // Move Files to Folder

                            DB::commit();

                        return redirect()->route('obf')->with('success', 'Record updated successfully');

                    } else {

                        DB::rollback();

                        return redirect()->back()->with('error', 'Failed to update record')->withInput();

                    }

                }catch (\Throwable $th){

                    DB::rollback();

                    return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();

                }

        }

    /** update */

    



    /** change-status */

        public function change_status(Request $request){

            if (!$request->ajax()) { exit('No direct script access allowed'); }

            $id = base64_decode($request->id);

            $data = OBF::where(['id' => $id])->first();



            if (!empty($data)) {

                $update = OBF::where(['id' => $id])->update(['status' => $request->status, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth()->user()->id]);

                if ($update) {

                    return response()->json(['code' => 200]);

                } else {

                    return response()->json(['code' => 201]);

                }

            } else {

                return response()->json(['code' => 201]);

            }

            

        }

    /** change-status */



    /** remove-profile */

        public function obf_profile_remove(Request $request){

            if(!$request->ajax()) { exit('No direct script access allowed'); }



            if(!empty($request->all())){

                $id = base64_decode($request->id);

                $data = OBF::find($id);



                if($data){

                    if($data->pan_image != ''){

                        $file_path = public_path().'/uploads/kyc/'.$data->pan_image;



                        if(File::exists($file_path) && $file_path != ''){

                            if($data->pan_image != 'user-icon.jpg') {

                                @unlink($file_path);

                            }

                        }



                        $update = OBF::where(['id' => $id])->update(['pan_image' => '']);



                        if($update)

                            return response()->json(['code' => 200]);

                        else

                            return response()->json(['code' => 201]);

                    }else{

                        return response()->json(['code' => 200]);

                    }

                }else{

                    return response()->json(['code' => 201]);

                }

            }else{

                return response()->json(['code' => 201]);

            }

        }

    /** remove-profile */



    /** Import */

        public function import(Request $request){

                /*$import = Excel::import(new ImportObf(), $request->file('file'));    

                if($import){

                    Excel::store(new ImportObf(), 'OBF_'.Date('YmdHis').'.xlsx' ,'excel_import');

                    return redirect()->route('obf')->with('sucess' ,'File Imported Sucessfully');

                }*/



                if(Excel::import(new ImportObf, $request->file('file')->store('file'))){

                    return redirect()->route('obf')->with('sucess' ,'File Imported Sucessfully');

                }else{

                    return redirect()->route('obf')->with('error' ,'Faild To Import File!');

                }

        }

    /** Import */



    /** Export */

        public function export(Request $request){

            $slug = $request->slug;

            $name = 'Obf'.Date('YmdHis').'.xlsx';



            try {

                return Excel::download(new ExportOBF($slug), $name);

            }catch(\Exception $e){

                return redirect()->back()->with('error' ,$e->getMessage());

            }

           

        } 

    /** Export */

    /** Get pricelist */

        public function getpricelist(Request $request)
        {
            $pricelist_data = DB::table('pricelist_master')->where('id', $request->id)->first();
            $rto_custome_handling_charges = $pricelist_data->rto_custome_handling_charges ?? 0;
        
            if (isset($request->customer_type)) {
        
                if ($rto_custome_handling_charges == 0) {
                    $pricelist_data = DB::table('rto_master')
                        ->select('rto_type', 'rto_tax_individual', 'rto_tax_company', 'totali_individual_charges', 'totalcharges')
                        ->where('rto_type', 'like', $request->customer_type . '%')
                        ->orderBy('id', 'DESC')
                        ->first();
                } else {
                    $rto_data = DB::table('rto_master')
                        ->select('temporary_registration', 'smart_card_fee', 'hypothecation', 'fresh_permit', 'new_registion', 'crtem', 'agent_charges')
                        ->where('rto_type', 'like', $request->customer_type . '%')
                        ->orderBy('id', 'DESC')
                        ->first();
        
                    $totalcharges = ($rto_data->temporary_registration ?? 0) +
                                    ($rto_data->smart_card_fee ?? 0) +
                                    ($rto_data->hypothecation ?? 0) +
                                    ($rto_data->fresh_permit ?? 0) +
                                    ($rto_data->new_registion ?? 0) +
                                    ($rto_data->crtem ?? 0) +
                                    ($rto_data->agent_charges ?? 0) +
                                    $rto_custome_handling_charges;
        
                    $pricelist_data = [
                        'rto_type' => $rto_data->rto_type,
                        'rto_tax_individual' => $rto_data->rto_tax_individual,
                        'rto_tax_company' => $rto_data->rto_tax_company,
                        'totali_individual_charges' => $rto_data->totali_individual_charges,
                        'totalcharges' => $totalcharges,
                    ];
                }
            }
        
            return response()->json($pricelist_data);
        }
        

         public function rtolist(Request $request)

         {

              return "xcdc";

            //$rto_master_data=DB::table('rto_master')->select('rto_type', 'rto_tax_individual', 'rto_tax_company', 'totali_individual_charges','totalcharges')->Where('rto_type', 'like', $request->customer_type . '%')->orderBy('id', 'DESC')->first();



              //return $rto_master_data;

         }

    

    /** Get Pricelist */



    /**product_data_get */

        public function product_data_get(Request $request){ 

            $model = ($request->model);

            $veriant = ($request->veriant);



            $exterior_color = ($request->exterior_color);

            $interior_color = ($request->interior_color);

            /*echo "<pre>";

            print_r($_REQUEST);

            die('S');*/

            $Product = Product::where(['car_model' => $model])

            ->where(['veriant' => $veriant])

            ->where(['interior_color' => $interior_color])

            ->where(['exterior_color' => $exterior_color])

            ->where(['status' => 'active'])->get();



            $data = [];

            if(!empty($Product) && $Product->count() > 0){

                $data['ex_showroom_price'] = $Product[0]->ex_showroom_price;        

            }

            return json_encode(array('status'=> true ,'data' => $data));

        }

    /** carvarient */



    /** OBF validation */

        public function obf_validation(Request $request)
        {
            $response = array();
            $response['status'] = 200;
            $response['message'] = '';
            $response['data'] = (object)array();
        
            $data = $request->input('data');
            $content = json_decode($data);
        
            $contact_number = isset($content->contact_number) ? $content->contact_number : '';
            $pan_number = isset($content->pan_number) ? $content->pan_number : '';
            $aadhar_number = isset($content->aadhar_number) ? $content->aadhar_number : '';
            $customer_type = isset($content->customer_type) ? $content->customer_type : '';
            $branch = isset($content->branch) ? $content->branch : '';
            $customer_name = isset($content->customer_name) ? $content->customer_name : '';
            $einvoapp = isset($content->einvoapp) ? $content->einvoapp : '';
            $gst_required = isset($content->gst_required) ? $content->gst_required : '';
            $gst = isset($content->gst) ? $content->gst : '';
            $address_1 = isset($content->address_1) ? $content->address_1 : '';
            $pancard = isset($content->pancard) ? $content->pancard : '';
            $aadhar = isset($content->aadhar) ? $content->aadhar : '';
            $aadhar_pan_link = isset($content->aadhar_pan_link) ? $content->aadhar_pan_link : '';
            $aadhar_pan = isset($content->aadhar_pan) ? $content->aadhar_pan : '';
        
            $params = [
                'contact_number' => $contact_number,
                'pan_number' => $pan_number,
                'aadhar_number' => $aadhar_number,
                'rto_type' => $customer_type,
                'branch' => $branch,
                'customer_name' => $customer_name,
                'e-Invoice_Applicable' => $einvoapp,
                'gst_required' => $gst_required,
                'gst' => $gst,
                'Address_Correspondence' => $address_1,
                'pan_image' => $pancard,
                'adhar_image' => $aadhar,
                'aadhar_pan_link' => $aadhar_pan_link,
                'aadhar_pan_image' => $aadhar_pan,
            ];
        
            // Define validation rules
            $rules = [
                'contact_number' => 'required|digits:10',
                'pan_number' => [
                    'required',
                    'regex:/^[A-Z]{5}[0-9]{4}[A-Z]{1}$/',
                ],
                'aadhar_number' => [
                    'required',
                    'regex:/^\d{12}$/',
                ],
                'rto_type' => 'required',
                'branch' => 'required',
                'customer_name' => 'required',
                'e-Invoice_Applicable' => 'required',
                'gst_required' => 'required|in:yes,no',
                'gst' => [
                    'required_if:gst_required,yes',
                    'regex:/^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/',
                ],
                'Address_Correspondence' => 'required',
                'aadhar_pan_link' => 'required|in:yes,no',
                // 'aadhar_pan_image' => [
                //     'required_if:aadhar_pan_link,yes',
                // ],
            ];
        
            // Add pan_image validation only if it is present in the request
            if ($request->hasFile('pan_image')) {
                $rules['pan_image'] = 'required';
            }
            
            // Add adhar_image validation only if it is present in the request
            if ($request->hasFile('adhar_image')) {
                $rules['adhar_image'] = 'required';
            }
            
            if ($request->hasFile('aadhar_pan_image')) {
                $rules['aadhar_pan_image'] = [
                    'required_if:aadhar_pan_link,yes',
                ];
            }
        
            $validator = Validator::make($params, $rules);
        
            if ($validator->fails()) {
                $response['status'] = 404;
                $response['message'] = $validator->errors()->first();
                return response($response, 200);
            }
        
            return response($response, 200);
        }

    /** OBF validation */



    /** obf_validation_step_two */

        public function obf_validation_step_two(Request $request)

        {

            $response = array();

            $response['status'] = 200;

            $response['message'] = '';

            $response['data'] = (object)array();



            $data = $request->input('data');

            $content = json_decode($data);

            

            $sales_person_name = isset($content->sales_person_name) ? $content->sales_person_name : '';

            $car_name = isset($content->car_name) ? $content->car_name : '';

            $veriant = isset($content->veriant) ? $content->veriant : '';

            $exterior_color = isset($content->exterior_color) ? $content->exterior_color : '';

            $interior_color = isset($content->interior_color) ? $content->interior_color : '';

            $pricelist_manager_id = isset($content->pricelist_manager_id) ? $content->pricelist_manager_id : '';



            $params = [

                'sales_person_name' => $sales_person_name,

                'car_name' => $car_name,

                'veriant' => $veriant,

                'exterior_color' => $exterior_color,

                'interior_color' => $interior_color,

                'pricelist_manager_id' => $pricelist_manager_id,

            ];



            $validator = Validator::make($params, [

                'sales_person_name' => 'required',

                'car_name' => 'required',

                'veriant' => 'required',

                'exterior_color' => 'required',

                'interior_color' => 'required',

                'pricelist_manager_id' => 'required',

            ],

                ['car_name.required' => 'The car model field is required',

                'veriant.required' => 'The Car Variant field is required',

                'pricelist_manager_id.required' => 'The Pricelist field is required',

                ],

            );



            if ($validator->fails()) {

                $response['status'] = 404;

                $response['message'] = $validator->errors()->first();

                return response($response, 200);

            }

            return response($response, 200);

        }

    /** obf_validation_step_two */





    /** OBF Print */

    public function obf_print(Request $request)

    {       

        ini_set('memory_limit', '48M');



        $obf_id = base64_decode($request->id);



        $data = OBF::select('obf.id','obf.sales_person_id',

        DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'obf.booking_date','branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id','products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'obf.fasttag_brand_name AS fasttag_brand_name','registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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_new' ,'fasttags.id AS fasttag_id','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','allocation.tentetive_delivery_date',

       

        'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

        'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.pricelist_id','obf.registration_tax_id','obf.municipal_tax_id','obf.municipal_tax_applicable','obf.tcs_tax_id','obf.nominee_dob','convinience_program',

            'obf.step1_remarks','obf.step3_remarks','obf.step4_remarks','obf.step5_remarks','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',



         'obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.rsa_master_obf',

         'car_model.name AS car_model_name',

                'car_varient.name AS car_varient_name',

                'interiorcolor.name AS interiorcolor_name',

                'exteriorcolor.name AS exteriorcolor_name',

            'inventory.key_number','inventory.engine_number','inventory.chassis_number','inventory.vin_number', 'obf_insurance.company_name AS insurance_company_name','obf_insurance.ncb_amount','obf.offer_ib_amount','obf.offer_kin_amount','obf.offer_amount'           

        )

        ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

        ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

        ->leftjoin('products', 'obf.product_id', 'products.id')

        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_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('allocation' ,'obf.id' ,'allocation.obf_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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

        ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

        ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

        ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

        ->leftjoin('inventory' ,'obf.inventory_id' ,'inventory.id')

        ->where(['obf.id' => $obf_id])

        ->first();

           $userBranch = auth()->user()->branch;

           $branchArray = explode(',', $userBranch);

           $img = DB::table('global_configuration')->select('*')->whereIn('branch_id', $branchArray)->where('status','active')->first();

            $img1 = $img->obf_print1;

            $img2 = $img->obf_print2;

           /*if($img == null){

               $img1 = "http://erp.ibgroup.in/public/assets/img/111.jpg";

               $img2 = "http://erp.ibgroup.in/public/assets/img/222.jpg";

           }

           else{

               $img1 = $img->obf_print1;

               $img2 = $img->obf_print2;

           }*/

           

         /*'obf_insurance.ncb','obf_insurance.date_of_insurance','obf_insurance.last_time_of_insurance','obf_insurance.time_of_insurance',,'obf_insurance.policy_number','obf_insurance.status as insurance_status','obf_insurance.documents',*/



        $data->customer_name = isset($data->customer_name) ? $data->customer_name : "N/A";

        //$data->cash_receipt_created_at = isset($data->cash_receipt_created_at) ? date("d/m/Y",strtotime($data->cash_receipt_created_at)) : "N/A";

        $data->contact_number = isset($data->contact_number) ? $data->contact_number : "N/A";

        $data->address = isset($data->address) ? $data->address : "N/A";

        $data->registration = isset($data->registration) ? $data->registration : "N/A";

        $data->finance_name = isset($data->finance_name) ? $data->finance_name : "N/A";

        $data->car_model_name = isset($data->car_model_name) ? $data->car_model_name : "N/A";

        $data->car_varient_name = isset($data->car_varient_name) ? $data->car_varient_name : "N/A";

        $data->interiorcolor_name = isset($data->interiorcolor_name) ? $data->interiorcolor_name : "N/A";

        $data->exteriorcolor_name = isset($data->exteriorcolor_name) ? $data->exteriorcolor_name : "N/A";

        $data->sales_person_name = isset($data->sales_person_name) ? $data->sales_person_name : "N/A";

        

        $data->company_name = isset($data->company_name) ? $data->company_name : "N/A";

        $data->gst = isset($data->gst) ? $data->gst : "N/A";

        $data->pan_number = isset($data->pan_number) ? $data->pan_number : "N/A";

        $data->contact_number = isset($data->contact_number) ? $data->contact_number : "N/A";

 

        $data->nominee_name = isset($data->nominee_name) ? $data->nominee_name : "N/A";

        $data->nominee_reletion = isset($data->nominee_reletion) ? $data->nominee_reletion : "N/A";

        $data->nominee_age = isset($data->nominee_age) ? $data->nominee_age : "N/A";

        $data->occupation = isset($data->occupation) ? $data->occupation : "N/A";

        $data->ex_showroom_price = isset($data->ex_showroom_price) ? $data->ex_showroom_price : "N/A";

        $data->registration_tax_id = isset($data->registration_tax_id) ? $data->registration_tax_id : "N/A";

        $data->insurance_id = isset($data->insurance_id) ? $data->insurance_id : "N/A";

        $data->municipal_tax_id = isset($data->municipal_tax_id) ? $data->municipal_tax_id : "N/A";

        $data->tcs_tax_id = isset($data->tcs_tax_id) ? $data->tcs_tax_id : "N/A";

        $data->trade_car_value = isset($data->trade_car_value) ? $data->trade_car_value : "N/A";

        $data->on_road_price = isset($data->on_road_price) ? $data->on_road_price : "N/A";

        $data->on_road_price_word = isset($data->on_road_price_word) ? $data->on_road_price_word : "N/A";

        $data->dob = isset($data->dob) ? date("d-M-Y",strtotime($data->dob)) : "N/A";

        $data->extand_warranties_amount = isset($data->extand_warranties_amount) ? $data->extand_warranties_amount : "N/A";

        $data->fasttag_amount_new = isset($data->fasttag_amount_new) ? $data->fasttag_amount_new : "N/A";

        $data->lead_name = isset($data->lead_name) ? $data->lead_name : "N/A";

        $data->accessory_id = isset($data->accessory_id) ? $data->accessory_id : "";

        $data->branch_name = isset($data->branch_name) ? $data->branch_name : "N/A";

        $data->insurance_company_name = isset($data->insurance_company_name) ? $data->insurance_company_name : "N/A";

        $data->ncb_amount = isset($data->ncb_amount) ? $data->ncb_amount : "N/A";

        $data->vin_number = isset($data->vin_number) ? $data->vin_number : "N/A";

        $data->engine_number = isset($data->engine_number) ? $data->engine_number : "N/A";

        $data->key_number = isset($data->key_number) ? $data->key_number : "N/A";

        $data->booking_date = isset($data->booking_date) ? $data->booking_date : "N/A";

        $data->tentetive_delivery_date = isset($data->tentetive_delivery_date) ? $data->tentetive_delivery_date : "N/A";

    

        

        

        

        



        $data->accessory_sum = 0;

        if($data->accessory_id != ''){

            $data->accessory_id = explode(",",$data->accessory_id); 

             $data->accessory_sum = Accessory::whereIn('id',$data->accessory_id)->sum('price');

        }



        $cashreceipt_data = CashReceipt::where('obf_id',$data->id)->where('status','accepted')->get();

        /*echo "<pre>";

        print_r($cashreceipt_data);

        die('stp[');*/





        PDF::SetAuthor('OBF PRINT');

        PDF::SetTitle('OBF PRINT');

        PDF::SetSubject('OBF PRINT');

        PDF::setImageScale(PDF_IMAGE_SCALE_RATIO);

        PDF::AddPage('P', 'A3');

        PDF::Image(public_path('uploads/global_configuration/obf_print_one/'.$img1), 15, 11, 46, 19, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);



        PDF::Image(public_path('uploads/global_configuration/obf_print_two/'.$img2), 230, 11, 46, 19, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);

        $tbl = '<div style="border:1px solid black;">

               

               

               <span><img src="{{asset(uploads/global_configuration/obf_print_one/. $img1 )}}"></span>

               <span><img src="{{asset(uploads/global_configuration/obf_print_two/. $img2 ))}}"></span>

               <br>

               

               

               

                <table cellspacing="0" cellpadding="3" border="0" nobr="true">

                    

                    

                    <tr>

                        <td colspan="5" style="width:97%;font-size:14px;color:black;text-align:center; padding-bottom:10px; border-bottom:2px solid #000;"><b>RETAIL ORDER & BOOKING FORM</b></td>

                    </tr>

                    <tr>

                        <td style="width:16%;font-size:13px;">Booking Date</td>

                        <td style="width:20%;font-size:11px;border-bottom:1px solid black;text-align:center;"><b>'.$data->booking_date.'</b></td>

                        <td style="width:10%;font-size:13px;"><b>Place : </b></td>

                        <td style="width:18%;font-size:11px;border-bottom:1px solid black;text-align:center;"><b>'.$data->branch_name.'</b></td>

                        <td style="width:13%;font-size:13px;">Delivery Date</td>

                        <td style="width:20%;font-size:11px;border-bottom:1px solid black;text-align:center;"><b>'.$data->tentetive_delivery_date.'</b></td>

                    </tr>

                    <tr>

                        <td style="width:16%;font-size:13px;">Customer Name : </td>

                        <td colspan="5" style="font-size:13px;border-bottom:1px solid black;"><b>'.$data->customer_name.'</b></td>

                    </tr>

                    <tr>

                        <td style="width:16%;font-size:13px;">Comapny Name : </td>

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;"><b>'.$data->company_name.'</b></td>

                        <td style="font-size:13px;border-bottom:1px solid black;"><b>GST No, If Any </b></td>

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;"><b>'.$data->gst.'</b></td>

                    </tr>

                     <tr>

                        <td style="width:16%;font-size:13px; text-align:right;border-right:1px solid black;border-bottom:1px solid black;">Billing Address </td>

                        <td colspan="5" style="width:81%;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;">'.$data->address.'</td>

                    </tr>

                    <tr>

                        <td style="width:16%;font-size:13px; text-align:right;border-right:1px solid black;border-bottom:1px solid black;">Address Correspondence</td>

                        <td colspan="5" style="width:81%;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;">'.$data->registration.'</td>

                    </tr>                   

                    <tr>

                        <td style="width:10%;font-size:13px;"><b>VIN No.</b></td>

                        <td style="width:27%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>'.$data->vin_number.'</b></td>

                        <td style="width:13%;font-size:13px; text-align:center;"><b>Engine No.</b></td>

                        <td style="width:19%;border-bottom:1px solid black;font-size:13px;text-align:center;"><b>'.$data->engine_number.'</b></td>

                        <td style="width:15%;"><b>Key Number</b></td>

                        <td style="width:13%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>'.$data->key_number.'</b></td>

                    </tr>

                    <tr>

                        <td style="width:10%;font-size:13px;">Model :</td>

                        <td colspan="2" style="width:39%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>'.$data->car_model_name.'</b></td>

                        <td style="width:18%;font-size:13px;text-align:left;"> &nbsp;&nbsp; MFG YEAR</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>-</b></td>

                    </tr>

                    <tr>

                        <td style="width:10%;font-size:13px;">Variant :</td>

                        <td colspan="2" style="width:39%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>'.$data->car_varient_name.'</b></td>

                        <td style="width:18%;font-size:13px;text-align:left;"> &nbsp;&nbsp; Interior Color :</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>'.$data->interiorcolor_name.'</b></td>

                    </tr>

                    <tr>

                        <td style="width:20%;font-size:13px;"><b>Sales Person Name :</b></td>

                        <td colspan="2" style="width:29%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>'.$data->sales_person_name.'</b></td>

                        <td style="width:18%;font-size:13px;text-align:left;"> &nbsp;&nbsp; Exterior Color :</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>'.$data->exteriorcolor_name.'</b></td>

                    </tr>

                    <tr>

                        <td style="width:20%;font-size:13px;"><b>Anniversary :</b></td>

                        <td colspan="2" style="width:29%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>-</b></td>

                        <td style="width:18%;font-size:13px;text-align:left;"> &nbsp;&nbsp; Email :</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;text-align:center;font-size:11px;">'.$data->email.'</td>

                    </tr>

                    <tr>

                        <td style="width:10%;font-size:13px;">Pan No. :</td>

                        <td colspan="2" style="width:39%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>'.$data->pan_number.'</b></td>

                        <td rowspan="2" style="width:18%;font-size:13px;text-align:left;border-bottom:1px solid black;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;"> &nbsp;&nbsp; Nominee<br> &nbsp;&nbsp; Relation & Age :</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;text-align:left;font-size:11px;">'.$data->nominee_name.'</td>

                    </tr>

                    <tr>

                        <td style="width:10%;font-size:13px;">Mobile. :</td>

                        <td colspan="2" style="width:39%;text-align:center;border-bottom:1px solid black;font-size:13px;">'.$data->contact_number.'</td>

                        <td style="width:15%;text-align:center;font-size:13px;border-bottom:1px solid black;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;">'.$data->nominee_reletion.'</td>

                        <td style="width:15%;border-bottom:1px solid black;border-top:1px solid black;border-left:1px solid black;border-right:1px solid black;text-align:center;font-size:13px;">'.$data->nominee_age.'</td>

                    </tr>

                    <tr>

                        <td style="width:20%;font-size:13px;">Date of Birth: </td>

                        <td colspan="2" style="width:29%;text-align:center;border-bottom:1px solid black;font-size:13px;">'.$data->dob.'</td>

                        <td style="width:18%;font-size:13px;text-align:left;">Occupation :</td>

                        <td colspan="2" style="width:30%;border-bottom:1px solid black;text-align:center;font-size:11px;">'.$data->occupation.'</td>

                    </tr>

                    <tr>

                        <td colspan="6" style="font-size:13px;border-bottom:1px solid black;">If Special Vehicle Reg. No. : </td>

                        

                    </tr>

                    <tr>

                        <td colspan="4" style="text-align:center; font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>PARTICULAR</b></td>

                        <td colspan="2" style="text-align:center;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;"><b>AMOUNT</b></td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Standard Ex. Showroom Price (Rs)</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->ex_showroom_price.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Handling Charges</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">-</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Registration Tax</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->registration_tax_id.'</td>

                    </tr>

                    <tr>

                        <td style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Insurance</td>

                        <td colspan="3" style="text-align:right;font-size:13px;border-left:1px solid black;border-right:1px solid black;border-bottom:1px solid black;">'.$data->insurance_id.'</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->insurance_id.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">RMC - JMC - JNDMT Tax</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->municipal_tax_id.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">1% for TCS Tax</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->tcs_tax_id.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Accessories</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->accessory_sum.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Extended Warranty</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->extand_warranties_amount.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Fasttag</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->fasttag_amount_new.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Road Side Assist</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">-</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Trade-In Value (if any)</td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->trade_car_value.'</td>

                    </tr>

                    <tr>

                        <td colspan="4" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>On Road Price(Rs)</b></td>

                        <td colspan="2" style="text-align:right;font-size:13px;border-right:1px solid black;border-bottom:1px solid black;">'.$data->on_road_price.'</td>

                    </tr>

                     <tr>

                        <td colspan="2" rowspan="2" style="text-align:left;font-size:13px;border-right:1px solid black;border-left:1px solid black;border-bottom:1px solid black;"><b>On Road Price (In words)</b></td>

                        <td colspan="4" rowspan="2" style="font-size:12px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>'.$data->on_road_price_word.'</b></td>

                    </tr>

                    <tr>

                        <td colspan="2"style="text-align:left;font-size:13px;"></td>

                        <td colspan="4" style="font-size:11px;"></td>

                    </tr>

                    <tr>

                        <td colspan="2" style="width:35%;font-size:13px;">Finance Arrangement Through Dealer :</td>

                        <td style="width:17%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>-</b></td>

                        <td colspan="2" style="width:25%;text-align:center;font-size:13px;"><b>Corporate - Specify If Any</b></td>

                        <td style="width:20%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>-</b></td>

                    </tr>

                    <tr>

                        <td colspan="2" style="width:35%;font-size:13px;">Insurance Arrangement Through Dealer :</td>

                        <td style="width:17%;text-align:center;border-bottom:1px solid black;font-size:13px;"><b>-</b></td>

                        <td colspan="2" style="width:25%;text-align:center;font-size:13px;">Lead Source</td>

                        <td style="width:20%;border-bottom:1px solid black;text-align:center;font-size:13px;"><b>'.$data->lead_name.'</b></td>

                    </tr>

                    <tr>

                        <td colspan="2" style="width:23%;font-size:13px;">Booking Amount</td>

                        <td style="width:15%;text-align:center;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;border-top:1px solid black;font-size:13px;"><b>'.$data->booking_amount.'</b></td>

                        <td style="width:25%;text-align:center;font-size:13px;border-bottom:2px solid black;border-right:2px solid black;border-left:2px solid black;border-top:2px solid black;">Mode of Payment / Date</td>

                        <td style="width:24%;border-bottom:2px solid black;border-right:1px solid black;border-left:2px solid black;border-top:2px solid black;text-align:center;font-size:13px;"><b>CASH</b></td>

                        <td style="width:10%;border-bottom:1px solid black;border-right:1px solid black;text-align:center;font-size:13px;">-</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="color:black;text-align:left;font-size:13px;border-top:1px solid black; border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">If Finance Disb Amt.</td>

                        <td style="color:black;text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                        <td style="color:black;text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Finance Bank / Branch / Date</td>

                        <td style="color:black;text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                        <td style="color:black;text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>';

                    

                    $tbl .= '<tr>

                    <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

                    <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>

                    <tr>

                    <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

                    <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>

                    <tr>

                    <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

                    <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>

                    <tr>

                    <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

                    <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>';



                    //code added



                    /*$tbl .= '<tr>

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>2,74,600.00</b></td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">AXIS BANK RTGS 062570</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">03-Oct-22</td>

                    </tr>';   */

                   

                    /*if(!empty($cashreceipt_data) && count($cashreceipt_data) > 0){

                        $count = 0;

                        foreach ($cashreceipt_data as $key => $value) {

                            $count++;



                            $tbl .= '<tr>

                                <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">'.$value->payment_type.'</td>

                                <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>'.$value->amount.'</b></td>

                                <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

                                <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">AXIS BANK RTGS 062570</td>

                                <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">03-Oct-22</td>

                            </tr>';   

                        }

                    }*/



                    $tbl .='<tr>

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cash Amount </td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cash Receipt No & Date</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">0</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">If Exchange of Old Car Amount </td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Exchange Car Mfg & Year</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">- | -</td>

                        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">0</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="color:black;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Fianance In Home or Not</td>

                        <td style="color:black;text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>0</b></td>

                        <td colspan="3" rowspan="2"  style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Remark</td>

                    </tr>

                    <tr>    

                        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>TOTAL AMOUNT</b></td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-right:1px solid black;border-left:1px solid black;">KIN Offers</td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">'.$data->offer_kin_amount.'</td>

                        <td style="width:25%;text-align:center;font-size:13px;">Discount Approved by /Ref </td>

                        <td style="width:34%;border-bottom:1px solid black;text-align:center;font-size:13px;">-</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-right:1px solid black;border-left:1px solid black;">SavanIB Offers</td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">'.$data->offer_ib_amount.'</td>

                        <td style="width:25%;text-align:center;font-size:13px;">F O C Approved by /Ref </td>

                        <td style="width:34%;border-bottom:1px solid black;text-align:center;font-size:13px;">-</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-right:1px solid black;border-left:1px solid black;"></td>

                        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"></td>

                        <td style="width:25%;text-align:center;font-size:13px;">Less</td>

                        <td style="width:34%;border-bottom:1px solid black;text-align:center;font-size:13px;">-</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-right:1px solid black;border-bottom:2px solid black;border-left:1px solid black;"><b>Final Total</b></td>

                        <td style="text-align:right;border-right:px solid black;font-size:13px;border-bottom:2px solid black;border-right:1px solid black;border-left:1px solid black;">'.$data->offer_amount.'</td>

                        <td style="width:25%;text-align:center;border-bottom:2px solid black;font-size:13px;"></td>

                        <td style="width:34%;border-bottom:2px solid black;text-align:center;font-size:13px;"></td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;border-right:1px solid black;border-bottom:1px solid black;border-left:1px solid black;"><b>DSA / Broker / Location</b></td>

                        <td style="text-align:right;border-right:px solid black;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;text-align;center">-</td>

                        <td style="width:25%;text-align:center;font-size:13px;">IF DSA, Commision Act</td>

                        <td style="width:34%;border-bottom:1px solid black;text-align:center;font-size:13px;border-right:1px solid black;border-left:1px solid black;"></td>

                    </tr>

                     <tr>

                        <td colspan="6" style="font-size:13px;">I/We have read the terms and conditions related to the above contract as attached in annexure and acept the same.</td>

                    </tr>

                     <tr>

                        <td colspan="6" style="font-size:13px;">I/We further understand that this agreement of sale of the above vehicle is between both of us & that KMI is not a party to this booking & is not liable in any manner.</td>

                    </tr>

                    <tr>

                        <td colspan="6" style="font-size:13px;"><b>I/We have chosen to buy insurance of M/s.<u></u> via dealer at my own free will.</b></td>

                    </tr>

                    <tr>

                        <td colspan="6" style="width:97%;font-size:13px;border-bottom:1px solid black;">Please find the enclosed terms and conditions for order and booking form for KMI in the back page.</td>

                    </tr>

                    <tr>

                        <td colspan="2" style="font-size:13px;text-align:center">Customer Signature</td>

                        <td colspan="2" style="font-size:13px;text-align:center">KEC / Sales Manager</td>

                        <td colspan="2" style="font-size:13px;text-align:center">CEO / CCM</td>

                    </tr>

                    <tr>

                        <td style="width:29%;border:1px solid black;"><br><br></td>

                        <td style="width:5%;"></td>

                        <td style="width:29%;border:1px solid black;"><br><br></td>

                        <td style="width:5%;"></td>

                        <td style="width:29%;border:1px solid black;"><br><br></td>

                        <td style="width:5%;"></td>

                    </tr>

                    <tr>

                        <td colspan="6" style="width:97%;font-size:13px;marign-top: 15px;"></td>

                    </tr>

                    <tr>

                        <td colspan="6" style="width:97%;font-size:13px;marign-top: 15px;"><b>Terms and Conditions</b></td>

                    </tr>';



                    $general_data = Setting::select('value')->where(['type' => 'general','key' => 'TERMS_AND_CONDITIONS'])->first();



                    $terms_and_conditions = '';

                    if(!empty($general_data)){

                        $terms_and_conditions = $general_data->value;

                    }



                    $tbl .='<tr>

                        <td colspan="6" style="width:97%;font-size:10px;">'.$terms_and_conditions.'</td>

                    </tr>



                </table>

                

        </div>';



        PDF::writeHTML($tbl, true, false, false, false, '');



        PDF::SetXY(10, 110);

        PDF::Output('obf.pdf', 'I');

    }

    /** OBF Print */



    /*

    <tr>

        <td style="font-size:13px; border-bottom:1px solid black;"></td>

        <td style="text-align:right;">Dist. :</td>

        <td style="font-size:13px; text-align:center;border-bottom:1px solid black;">MORBI</td>

        <td style="border-bottom:1px solid black;">Pin Code No.:</td>

        <td style="border-bottom:1px solid black;">363641</td>

        <td style=""></td>

    </tr>







    <tr>

        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_01 </td>

        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>2,74,600.00</b></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Drawn On / RTGS Details</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">AXIS BANK RTGS 062570</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">03-Oct-22</td>

    </tr>

    <tr>

        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_02 </td>

        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>1,40,000.00</b></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">CASH</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">30-Sep-22</td>

    </tr>

    <tr>

        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_03 </td>

        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>200.00</b></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">CASH</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">04-Oct-22</td>

    </tr>

    <tr>

        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_04 </td>

        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">0</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">00-Jan-00</td>

    </tr>

    <tr>

        <td colspan="2" style="font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">Cheque / RTGS / NEFT_05 </td>

        <td style="text-align:right;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"><b>-</b></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;"></td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">0</td>

        <td style="text-align:center;font-size:13px;border-bottom:1px solid black;border-right:1px solid black;border-left:1px solid black;">-</td>

    </tr>*/

    

    

    public function offer_asper_varient(Request $request){

        $car_name = $request->car_name;

        $varient = $request->varient;

        $fuel = $request->fuel;

        //dd($varient);

        $offer_type = $request->offer_type;

        $data =[];

        $title = [];

        $titles = [];



        if($offer_type = 'KIN'){



            // $offer= offer::where(['fk_car_model_id'=>$car_name, 'fk_car_varient_id'=>$varient,'fk_fuel_id'=>$fuel,'offer_type'=>$offer_type])->get();
            $offer = Offer::where([
                'fk_car_model_id' => $car_name,
                'fk_fuel_id' => $fuel,
                'offer_type' => $offer_type
            ])
            ->whereRaw("FIND_IN_SET(?, fk_car_varient_id)", [$varient])
            ->get();


        foreach ($offer as $value) {

                    $color_data = [];                    

                    $color_data['id'] = $value->id;

                    $color_data['title'] = $value->title;

                    $color_data['amount'] = $value->amount;

                    $title[] = $color_data;                    

                }  

            $data['sub_offer_html'] = $title;  

        }



        if($offer_type = 'ShivamIB'){

            //$offer= offer::where(['fk_car_model_id'=>$car_name, 'fk_car_varient_id'=>$varient,'fk_fuel_id'=>$fuel,'offer_type'=>$offer_type])->get();
            $offer = Offer::where([
                'fk_car_model_id' => $car_name,
                'fk_fuel_id' => $fuel,
                'offer_type' => $offer_type
            ])
            ->whereRaw("FIND_IN_SET(?, fk_car_varient_id)", [$varient])
            ->get();

        //dd($offer);

        foreach ($offer as $value) {

                    $color_data = [];                    

                    $color_data['id'] = $value->id;

                    $color_data['title'] = $value->title;

                    $color_data['amount'] = $value->amount;

                    $titles[] = $color_data;                    

                }  

            $data['sub_offer_ShivamIB'] = $titles;

        }





        return json_encode(array('status'=> true ,'data' => $data));        



    }



    public function amount_asper_offer(Request $request){

        $offer = $request->offer;

        //dd($offer);

        //$offer_ib = $request->offer_ib;

       // dd($offer_ib);

      //  $o_id = implode(',',$offer);

        //dd($o_id);

        $offers= offer::select(DB::raw('SUM(amount) AS sum_of_1'))->whereIn('id',$offer)->get();

        //dd($offers);

        //$offers_ib= offer::select(DB::raw('SUM(amount) AS sum_of_2'))->whereIn('id',$offer_ib)->get();

      //  dd($offers_ib);

        

        //dd($o);

        $data =[];

        //$title = '';

        $color_data = '';

        $colors_data = '';

        foreach ($offers as $value) {

                    $color_data = $value->sum_of_1;

                    //$color_data['amount'] = $value->sum_of_1;

                    //$title = $color_data;                    

                }  



       /* foreach ($offers_ib as $value) {

                    $colors_data = $value->sum_of_2;

                    //$color_data['amount'] = $value->sum_of_1;

                    //$title = $color_data;                    

                }   */     



        $data['offer_amount'] =  $color_data; /* + $colors_data;*/  

        //dd($data['offer_amount']);

        return json_encode(array('status'=> true ,'data' => $data));    

    }



    public function amount_asper_offer_ib(Request $request){

        $offer = $request->offer_ib;

        

        $offers= offer::select(DB::raw('SUM(amount) AS sum_of_1'))->whereIn('id',$offer)->get();

       

        $data =[];

        $color_data = '';

        $colors_data = '';

        foreach ($offers as $value) {

                    $color_data = $value->sum_of_1;

                }  

        $data['offer_amount_ib'] =  $color_data; 

        //dd($data['offer_amount']);

        return json_encode(array('status'=> true ,'data' => $data));    

    }

    

    public function obf_summary(Request $request){

        $id = base64_decode($request->id);               

        $path = URL('/uploads/kyc').'/';



        $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

        $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

        $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

        $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

        $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

        $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

        $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();



        $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

        $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

        $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

        $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

        $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

        $data = OBF::select('obf.id','obf.sales_person_id',

        DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'obf.booking_date','branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id','products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'obf.fasttag_brand_name AS fasttag_brand_name','registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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_new' ,'fasttags.id AS fasttag_id','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','allocation.tentetive_delivery_date',

       

        'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

        'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.pricelist_id','obf.registration_tax_id','obf.municipal_tax_id','obf.municipal_tax_applicable','obf.tcs_tax_id','obf.nominee_dob','convinience_program',

            'obf.step1_remarks','obf.step3_remarks','obf.step4_remarks','obf.step5_remarks','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',



         'obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.rsa_master_obf',

         'car_model.name AS car_model_name',

                'car_varient.name AS car_varient_name',

                'interiorcolor.name AS interiorcolor_name',

                'exteriorcolor.name AS exteriorcolor_name',

            'inventory.key_number','inventory.engine_number','inventory.chassis_number','inventory.vin_number', 'obf_insurance.company_name AS insurance_company_name','obf_insurance.ncb_amount','obf.offer_ib_amount','obf.offer_kin_amount','obf.offer_amount'           

        )

        ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

        ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

        ->leftjoin('products', 'obf.product_id', 'products.id')

        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_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('allocation' ,'obf.id' ,'allocation.obf_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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

        ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

        ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

        ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

        ->leftjoin('inventory' ,'obf.inventory_id' ,'inventory.id')

        ->where(['obf.id' => $id])

        ->first();


    
          /*  'obf_insurance.ncb','obf_insurance.documents','obf_insurance.date_of_insurance','obf_insurance.last_time_of_insurance','obf_insurance.time_of_insurance','obf_insurance.company_name','obf_insurance.ncb_amount','obf_insurance.policy_number','obf_insurance.status as insurance_status','obf_insurance.documents',*/



          $accessory_name = OBF::select('accessories.id' , 'accessories.name as accessory_name')->leftjoin('accessories' ,'accessories.id','obf.accessory_id')->where('obf.id' , $id)->get();

            //dd($accessory_name);

           /* $fast_tags = Fasttag::select('fasttags.id', 'fasttags.brand_name' ,'fasttags.amount')->where(['fasttags.status' => 'active','obf.id' => $id])->get();*/



          $leads = OBF::select('lead.id' ,'lead.name')->leftjoin('lead' ,'lead.id','obf.lead_id')->where('obf.id' , $id)->get();

         /* $leads = OBF::select('lead.id' ,'lead.name')->leftjoin('lead' ,'lead.id','obf.lead_id')->where('obf.id' , $id)->get();*/



             $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                    ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                    ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                    ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                    ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                    ->where('products.status', '=', 'active')

                    ->get();



                    // dd($carmodel);





        $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                            ->leftJoin('car_model','car_model.id','=','obf.product_id')

                            ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                            ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                            ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                            ->where('obf.id', '=', $id)  

                            ->first();



        $fule = Obf::select('fule_type.title as fule_name')

        ->leftJoin('products','products.id','=','obf.product_id')

        ->leftJoin('car_varient','car_varient.id','=','products.veriant')

        ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

        ->where('obf.id', '=', $id)  

        ->first();



         $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();



         $OtherOne = OtherOne::where(['status' => 'active'])->get();  

            

         $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 
       
        $price_list_name = DB::table('pricelist_master')
    ->select('price_list_name')
    ->where('id',  $data->pricelist_id)
    ->get();
         $Inventory_data = [];

         if($data->inventory_id != NULL){

             $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

         }



        $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  



        $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  



        $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();



        $accessory_sum = 0;

        if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

            $accessory_id_arr = explode(' ,', $data->accessory_id);

            if(!empty($accessory_id_arr)){

                $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');           

            }

        }



        // $sales = User::role('sales')->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();



         $sales = User::role(['sales','KEC'])->select('id' , DB::raw("CONCAT(first_name,' ',last_name) AS sales_person_name"))->where(['status' => 'active'])->get();



         $user = auth()->user();

         $user_role = $user->getRoleNames();



         



        return view('obf.summary')->with(['price_list_name'=>$price_list_name,'data' =>$data,'sales' => $sales ,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'user_role'=> $user_role[0],'accessory_sum' => $accessory_sum,'accessory_name'=>$accessory_name,'leads'=>$leads]);



    }

    

    public function processew(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

         

                $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();



                $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                

                

                        

                  $data = OBF::select('obf.id','obf.sales_person_id',

                                DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',



                'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',



                'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                    

                            ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                            ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                            ->leftjoin('products' ,'obf.product_id' ,'products.id')

                            ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                            ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                            ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                            ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                            ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                            ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                            ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                            ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                            ->where(['obf.id' => $id])

                            ->first();

                            

        $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                    ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                    ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                    ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                    ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                    ->where('products.status', '=', 'active')

                    ->get();



                    // dd($carmodel);





        $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                            ->leftJoin('car_model','car_model.id','=','obf.product_id')

                            ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                            ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                            ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                            ->where('obf.id', '=', $id)  

                            ->first();



        $fule = Obf::select('fule_type.title as fule_name')

        ->leftJoin('products','products.id','=','obf.product_id')

        ->leftJoin('car_varient','car_varient.id','=','products.veriant')

        ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

        ->where('obf.id', '=', $id)  

        ->first();



         $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();



         $OtherOne = OtherOne::where(['status' => 'active'])->get();  

            

         $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 



         $Inventory_data = [];

         if($data->inventory_id != NULL){

             $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

         }



        $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  



        $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  



        $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();



        $accessory_sum = 0;

        if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

            $accessory_id_arr = explode(' ,', $data->accessory_id);

            if(!empty($accessory_id_arr)){

                $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

            }

        }

        return view('EW.processew')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    public function processewdms(Request $request){

        

        $ewImage = $request->file('dms_ew_certificate');

        $ewImageName = time().'.'.$ewImage->extension();

        $ewImagePath = $ewImage->storeAs('EWIMAGE', $ewImageName, 'public');

        DB::table('ew_process_dms')->insert([

            'obf_id'=> $request->id,

            'ew_invoice' => $request->ew_invoice,

            'ew_invoice_date' => Carbon::parse($request->ew_invoice_date)->toDateString(),

            'dms_invoice_no' => $request->dms_invoice_no,

            'dms_invoice_date' => Carbon::parse($request->dms_invoice_date)->toDateString(),

            'dms_gatepass_date' => Carbon::parse($request->dms_gatepass_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_ew_create_date' => Carbon::parse($request->dms_ew_create_date)->toDateString(),

            'dms_ew_certificate' => $ewImagePath,

            'dms_invoice_no' => $request->dms_invoice_no,

            'status' => 'EW COMPLETED',

        ]);

        DB::table('obf')->where('id',$request->id)->update(['ew_status' =>'EW COMPLETED']);

        return redirect()->route('obf');

    }

    

        public function processmcp(Request $request){

             ini_set('memory_limit', '48M');

            $id = base64_decode($request->id);

             

                    $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                      $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program','mcp.name AS carName','mcp.amount AS mcpAmount','branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                                ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

            return view('MCP.processmcp')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

        }

        

        public function processmcpdms(Request $request){

            

        $mcpImage = $request->file('dms_mcp_certificate');

        $mcpImageName = time().'.'.$mcpImage->extension();

        $mcpImagePath = $mcpImage->storeAs('MCPIMAGE', $mcpImageName, 'public');    

            

        DB::table('mcp_process_dms')->insert([

            'obf_id'=> $request->id,

            'mcp_invoice' => $request->mcp_invoice,

            'mcp_invoice_date' => Carbon::parse($request->mcp_invoice_date)->toDateString(),

            'dms_invoice_no' => $request->dms_invoice_no,

            'dms_invoice_date' => Carbon::parse($request->dms_invoice_date)->toDateString(),

            'dms_gatepass_date' => Carbon::parse($request->dms_gatepass_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_mcp_create_date' => Carbon::parse($request->dms_mcp_create_date)->toDateString(),

            'dms_mcp_certificate' => $mcpImagePath,

            'dms_invoice_no' => $request->dms_invoice_no,

            'status' => 'MCP COMPLETED',

        ]);

        DB::table('obf')->where('id',$request->id)->update(['mcp_status' =>'MCP COMPLETED']);

        return redirect()->route('obf');

    }    

        

        

    

    public function processrsa(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

                    $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

    

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'obf.rsa_master_obf','rsa.name as rsaname','rsa.amount AS rsaamount','branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag','obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance','obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories','obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one','obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two','obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id','obf.convinience_program')

                                ->leftjoin('rsa' ,'rsa.id','obf.rsa_master_obf')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

        return view('RSA.processrsa')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    public function processrsadms(Request $request){

        

        $rsaImage = $request->file('dms_rsa_certificate');

        $rsaImageName = time().'.'.$rsaImage->extension();

        $rsaImagePath = $rsaImage->storeAs('RSAIMAGE', $rsaImageName, 'public');    

        DB::table('rsa_process_dms')->insert([

            'obf_id'=> $request->id,

            'rsa_invoice' => $request->rsa_invoice,

            'rsa_invoice_date' => Carbon::parse($request->rsa_invoice_date)->toDateString(),

            'dms_invoice_no' => $request->dms_invoice_no,

            'dms_invoice_date' => Carbon::parse($request->dms_invoice_date)->toDateString(),

            'dms_gatepass_date' => Carbon::parse($request->dms_gatepass_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_rsa_create_date' => Carbon::parse($request->dms_rsa_create_date)->toDateString(),

            'dms_rsa_certificate' => $rsaImagePath,

            'dms_invoice_no' => $request->dms_invoice_no,

            'status' => 'RSA COMPLETED',

        ]);

        $data = DB::table('obf')->where('id',$request->id)->update(['rsa_status' =>'RSA COMPLETED']);

        return redirect()->route('obf');

    }  

    

    public function processfastag(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

         $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

    

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                      $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                                ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

        return view('FASTTAG.processfasttag')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    public function processfastagdms(Request $request){

        DB::table('ew_process_dms')->insert([

            'obf_id'=> $request->id,

            'ew_invoice' => $request->ew_invoice,

            'ew_invoice_date' => Carbon::parse($request->ew_invoice_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_ew_create_date' => Carbon::parse($request->dms_ew_create_date)->toDateString(),

            'dms_ew_certificate' => $request->dms_ew_certificate,

            'remark' => $request->remark,

            'status' => 'FASTTAG COMPLETED',

            'dms_type' => 'FASTTAG',

        ]);

        

        return redirect()->route('obf');

    } 

    

    

    public function processyodepartment(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

         $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

    

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                      $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                                ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

        return view('YODEPARTMENT.processyoepartment')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    public function processyodepartmentdms(Request $request){

        DB::table('ew_process_dms')->insert([

            'obf_id'=> $request->id,

            'ew_invoice' => $request->ew_invoice,

            'ew_invoice_date' => Carbon::parse($request->ew_invoice_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_ew_create_date' => Carbon::parse($request->dms_ew_create_date)->toDateString(),

            'dms_ew_certificate' => $request->dms_ew_certificate,

            'remark' => $request->remark,

            'status' => 'YO DEPARTMENT COMPLETED',

            'dms_type' => 'YODEPARTMENT',

        ]);

        

        return redirect()->route('obf');

    } 

    

    public function processaccesories(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

         $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

    

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                      $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name','accessories.type AS accesories_type','accessories.price AS accesories_price','accessories.model_number AS accesories_model','obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                                ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

        return view('ACCESORIES.processaccesories')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    

    public function processaccesoriesdms(Request $request){

        $accesoryImage = $request->file('dms_accesories_certificate');

        $accesoryImageName = time().'.'.$accesoryImage->extension();

        $accesoryImagePath = $accesoryImage->storeAs('ACCESORYIMAGE', $accesoryImageName, 'public');

        

        

        DB::table('accesories_process_dms')->insert([

            'obf_id'=> $request->id,

            'accesories_invoice' => $request->accesories_invoice,

            'accesories_invoice_date' => Carbon::parse($request->accesories_invoice_date)->toDateString(),

            'dms_invoice_no' => $request->dms_invoice_no,

            'dms_invoice_date' => Carbon::parse($request->dms_invoice_date)->toDateString(),

            'dms_gatepass_date' => Carbon::parse($request->dms_gatepass_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_accesories_create_date' => Carbon::parse($request->dms_accesories_create_date)->toDateString(),

            'dms_accesories_certificate' => $accesoryImagePath,

            'status' => 'ACCESORIES COMPLETED',

        ]);

         DB::table('obf')->where('id',$request->id)->update(['accesories_status' =>'ACCESORIES COMPLETED']);

        return redirect()->route('obf');

    }

    

    public function processrto(Request $request){

        ini_set('memory_limit', '48M');

        $id = base64_decode($request->id);

         $product = Product::select('id' , 'name' ,'veriant')->where(['status' => 'active'])->get();

                    $tax_1 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'registration_tax'])->first();

                    $tax_2 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'municipal_tax'])->first();

                    $tax_3 = Tax::select('id' , 'name' ,'percentage')->where(['status' => 'active' ,'name' => 'tcs_tax'])->first();

                    $accessory = Accessory::select('id' , 'name' ,'price')->where(['status' => 'active'])->get();

                    $extanded_warranty = ExtandWarranty::select('id' , 'years' ,'amount')->where(['status' => 'active'])->get();

                    $fasttag = Fasttag::select('id', 'brand_name' ,'amount')->where(['status' => 'active'])->get();

    

                    $branch = Branch::select('id' , 'name' ,'city')->where(['status' => 'active'])->get();

                    $insurance = Insurance::select('id' , 'name')->where(['status' => 'active'])->get();

                    $finance = Finance::select('finance.id' ,'finance.name' ,'branches.name AS branch_name' ,'branches.id AS branch_id')->leftjoin('branches' ,'finance.branch_id' ,'branches.id')->where(['finance.status' => 'active'])->get();

                    $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();

                    $car_exchange = CarExchange::select('car_exchange.id' ,'car_exchange.price','cep.name AS product_name')->leftjoin('car_exchange_product AS cep' ,'car_exchange.product_id' ,'cep.id')->get();

                    

                    

                            

                      $data = OBF::select('obf.id','obf.sales_person_id',

                                    DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type' ,'obf.customer_type_new' ,'obf.convinience_program' ,'branches.name AS branch_name' ,'branches.id AS branch_id' ,'obf.company_name','obf.fasttag_brand_name AS fasttag_brand_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','obf.product_id' ,'products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name','accessories.type AS accesories_type','accessories.price AS accesories_price','accessories.model_number AS accesories_model','obf.accessory_id','obf.extanded_warranty_id' ,'extand_warranties.years AS extand_warranties_years' ,'extand_warranties.amount AS extand_warranties_amount' ,'fasttags.id AS fasttag_id' ,'fasttags.amount AS fasttag_amount' ,'fasttags.brand_name AS brand_name','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at',

                    'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

    

                    'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id','inventory.vin_number as vinnumber',

    

                    'obf_finance.finance_name','obf.other_accesssories_prise','obf.other_accesssories_detail','obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.pricelist_id','obf.rsa_master_obf')

                        

                                ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')

                                ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')

                                ->leftjoin('products' ,'obf.product_id' ,'products.id')

                                ->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('inventory' ,'obf.inventory_id' ,'inventory.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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')

                                ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')

                                ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')

                                ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')

                                ->leftjoin('accessories' ,'obf.accessory_id' ,'accessories.id')

                                ->leftjoin('other_one' ,'obf.other_details_1' ,'other_one.id')

                                ->leftjoin('other_two' ,'obf.other_details_2' ,'other_two.id')

                                ->leftjoin('mcp' ,'mcp.id' ,'obf.convinience_program')

                                ->where(['obf.id' => $id])

                                ->first();

                                

            $carmodel = Product::select('car_model.name as car_name','products.id as car_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                        ->leftjoin('car_model', 'car_model.id', 'products.car_model')

                        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')

                        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')

                        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_color')

                        ->where('products.status', '=', 'active')

                        ->get();

    

                        // dd($carmodel);

    

    

            $Cardata = Obf::select('obf.id as o_id','obf.product_id as o_product','car_model.name as car_name','car_model.id as car_id','car_varient.id as varient_id','car_varient.name as varient_name','exteriorcolor.name as exterior_color', 'interiorcolor.name as interior_color')

                                ->leftJoin('car_model','car_model.id','=','obf.product_id')

                                ->leftJoin('car_varient','car_varient.car_model','=','car_model.id')

                                ->leftJoin('exteriorcolor','exteriorcolor.id','=','car_varient.exterior_color')

                                ->leftJoin('interiorcolor','interiorcolor.id','=','car_varient.interior_color')

                                ->where('obf.id', '=', $id)  

                                ->first();

    

            $fule = Obf::select('fule_type.title as fule_name')

            ->leftJoin('products','products.id','=','obf.product_id')

            ->leftJoin('car_varient','car_varient.id','=','products.veriant')

            ->leftJoin('fule_type','fule_type.id','=','car_varient.fule_type')

            ->where('obf.id', '=', $id)  

            ->first();

    

             $mcp = Mcp::select('id' ,'name')->where(['status' => 'active'])->get();

    

             $OtherOne = OtherOne::where(['status' => 'active'])->get();  

                

             $OtherTwo = OtherTwo::where(['status' => 'active'])->get(); 

    

             $Inventory_data = [];

             if($data->inventory_id != NULL){

                 $Inventory_data = Inventory::where('id', '=', $data->inventory_id)->first();    

             }

    

            $ObfInsurance = ObfInsurance::where(['obf_id'=> $id,'is_delete' => 0])->orderBy('id', 'desc')->first();  

    

            $CustomerTypeMaster = CustomerTypeMaster::where(['status' => 'active'])->get();  

    

            $rsa = RSA::where(['status' => 'active','deleted_at' => 0])->get();

    

            $accessory_sum = 0;

            if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){

                $accessory_id_arr = explode(' ,', $data->accessory_id);

                if(!empty($accessory_id_arr)){

                    $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');;           

                }

            }

        return view('RTO.processrto')->with(['data' =>$data,'product' => $product ,'taxOne' => $tax_1,'taxTwo' => $tax_2 ,'taxThree' => $tax_3,'accessory' => $accessory ,'extanded_warranty' => $extanded_warranty ,'fasttag' => $fasttag ,'branch' => $branch ,'insurance' => $insurance ,'car_exchange' => $car_exchange ,'carmodel' => $carmodel ,'Cardata' => $Cardata, 'finance' => $finance ,'lead' => $lead, 'OtherOne' => $OtherOne, 'mcp' => $mcp, 'OtherTwo' => $OtherTwo,'Inventory_data' => $Inventory_data,'ObfInsurance' => $ObfInsurance,'fule' => $fule, 'CustomerTypeMaster' => $CustomerTypeMaster,'rsa' => $rsa,'accessory_sum' => $accessory_sum]);

    }

    

    public function processrtodms(Request $request){

        $rtoImage = $request->file('dms_rto_certificate');

        $rtoImageName = time().'.'.$rtoImage->extension();

        $rtoImagePath = $rtoImage->storeAs('RTOIMAGE', $rtoImageName, 'public');

        

        DB::table('rto_process_dms')->insert([

            'obf_id'=> $request->id,

            'rto_invoice' => $request->rto_invoice,

            'rto_invoice_date' => Carbon::parse($request->rto_invoice_date)->toDateString(),

            'dms_invoice_no' => $request->dms_invoice_no,

            'dms_invoice_date' => Carbon::parse($request->dms_invoice_date)->toDateString(),

            'dms_gatepass_date' => Carbon::parse($request->dms_gatepass_date)->toDateString(),

            'dms_location' => $request->dms_location,

            'dms_rto_create_date' => Carbon::parse($request->dms_rto_create_date)->toDateString(),

            'dms_rto_certificate' => $rtoImagePath,

            'status' => 'RTO COMPLETED',

        ]);

         DB::table('obf')->where('id',$request->id)->update(['rto_status' =>'RTO COMPLETED']);

        return redirect()->route('obf');

    }
    
    public function getObfCounts()
        {
            $modelCounts = DB::table('car_model')
                ->select('name', DB::raw('COUNT(*) as count'))
                ->groupBy('name')
                ->get();
        
            $variantCounts = DB::table('car_varient')
                ->select('name', DB::raw('COUNT(*) as count'))
                ->groupBy('name')
                ->get();
        
            $statusCounts = DB::table('obf')
                ->select('status', DB::raw('COUNT(*) as count'))
                ->groupBy('status')
                ->get();
        
            return response()->json([
                'modelCounts' => $modelCounts,
                'variantCounts' => $variantCounts,
                'statusCounts' => $statusCounts,
            ]);
        }
        
    public function makedeal(Request $request){
        $id = base64_decode($request->id);               

        $data = OBF::select('obf.id','obf.sales_person_id',

        DB::raw("CONCAT(users.first_name,' ',users.last_name) AS sales_person_name"),'obf.temporary_id' ,'obf.booking_date' ,'obf.customer_name' ,'obf.customer_type','obf.customer_type_new' ,'obf.booking_date','branches.name AS branch_name' ,'branches.id AS branch_id' ,'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','obf.product_id','products.veriant','products.is_applicable_for_mcp' ,'obf.ex_showroom_price' ,'obf.fasttag_brand_name AS fasttag_brand_name','registration_tax.percentage AS registration_tax' ,'insurance.name AS insurance' ,'obf.insurance_id' ,'municipal_tax.percentage AS municipal_tax' ,'tcs_tax.percentage AS tcs_tax' ,'accessories.name AS accessory_name' ,'obf.accessory_id','obf.extanded_warranty_id' ,'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_new' ,'fasttags.id AS fasttag_id','obf.trade_car_name', 'obf.trade_car_value', 'obf.on_road_price' ,'obf.on_road_price_word' ,'obf.on_road_price_word','obf.finance_id' ,'finance.name AS finance_name' ,'finance_branch.name AS finance_branch_name' ,'lead.name AS lead_name','obf.lead_id' ,'obf.booking_amount' ,'obf.mode_of_payment' ,'obf.status' ,'obf.created_by' ,'obf.updated_by','obf.created_at' ,'obf.updated_at','allocation.tentetive_delivery_date','Team_master.team_name','obf.other_accesssories_prise',

        'obf_rto.rto_type','obf_rto.registration_date','obf_rto.application_number','obf_rto.temporary_number','obf_rto.final_amount','obf_rto.vehicle_number','obf_rto.status as rto_status',

        'obf_fasttag.fasttag_number','obf_fasttag.fasttag_brand','obf_fasttag.date_of_issue','obf_fasttag.fasttag_amount','obf_fasttag.opening_stock','obf_fasttag.status as fasttag_status','obf.pricelist_id','obf.registration_tax_id','obf.municipal_tax_id','obf.municipal_tax_applicable','obf.tcs_tax_id','obf.nominee_dob','convinience_program',

        'obf.step1_remarks','obf.step3_remarks','obf.step4_remarks','obf.step5_remarks','obf.other_documents','obf.pan_image','obf.adhar_image','obf.licance_image','obf.other_details_1','obf.other_details_2','obf.priclist_detail','obf.inventory_id',

        'obf_finance.repo_rate','obf_finance.net_disbusment','obf_finance.loan_amount','obf_finance.processing_fee','obf_finance.finance_discount','obf_finance.payout','obf_finance.pay_booster','obf_finance.finance_required','obf.sub_lead_id','obf.rsa_master_obf',

        'car_model.name AS car_model_name','car_varient.name AS car_varient_name','interiorcolor.name AS interiorcolor_name','exteriorcolor.name AS exteriorcolor_name','inventory.key_number','inventory.engine_number','inventory.chassis_number','inventory.vin_number', 'obf_insurance.company_name AS insurance_company_name','obf_insurance.ncb_amount','obf.offer_ib_amount','obf.offer_kin_amount','obf.offer_amount','inventory.purchase_price','inventory.aging_days','inventory.remark',
        
        'make_deal.obf_id','make_deal.purchase_gst_tax_deal','make_deal.final_deal_amt','make_deal.deal_amt','make_deal.gst_tax_deal','make_deal.cess_tax_deal','make_deal.tcs_tax_deal','make_deal.municipal_tax_deal','make_deal.rto_deal','make_deal.insurance_deal','make_deal.created_by')

        ->leftjoin('users' ,'obf.sales_person_id' ,'users.id')
        ->leftjoin('branches' ,'obf.branch_id' ,'branches.id')
        ->leftjoin('products', 'obf.product_id', 'products.id')
        ->leftjoin('car_model', 'car_model.id', 'products.car_model')
        ->leftjoin('car_varient', 'car_varient.id', 'products.veriant')
        ->leftjoin('interiorcolor', 'interiorcolor.id', 'products.interior_color')
        ->leftjoin('exteriorcolor', 'exteriorcolor.id', 'products.exterior_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('allocation' ,'obf.id' ,'allocation.obf_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('obf_insurance' ,'obf.id' ,'obf_insurance.obf_id')
        ->leftjoin('obf_rto' ,'obf.id' ,'obf_rto.obf_id')
        ->leftjoin('obf_fasttag' ,'obf.id' ,'obf_fasttag.obf_id')
        ->leftjoin('obf_finance' ,'obf.id' ,'obf_finance.obf_id')
        ->leftjoin('inventory' ,'obf.inventory_id' ,'inventory.id')
        ->leftjoin('Team_master','Team_master.id','users.team_name')
        ->leftjoin('make_deal','obf.id','make_deal.obf_id')
        ->where(['obf.id' => $id])->first(); 

        $branch = Branch::select('start_value1', 'end_value1', 'tax_percentage1', 'start_value2', 'end_value2', 'tax_percentage2')->where(['status' => 'active','id' => $data->branch_id])->first();
       
        $price_list_name = DB::table('pricelist_master')->select('basic','price_list_name','gst_percentage','gst_amount','cess_percentage','cess_amount')->where('id',  $data->pricelist_id)->first();

        $rto_data = DB::table('rto_master')->select('rto_type', 'rto_tax_individual', 'rto_tax_company', 'totali_individual_charges','totalcharges')->Where('rto_type', 'like', $data->customer_type . '%')->orderBy('id', 'DESC')->first();

        $accessory_sum = 0;

        if(!empty($data->accessory_id) && $data->accessory_id != NULL && $data->accessory_id != ''){
            $accessory_id_arr = explode(' ,', $data->accessory_id);
            if(!empty($accessory_id_arr)){
                $accessory_sum = Accessory::whereIn('id', $accessory_id_arr)->sum('price');           
            }
        }

        $user = auth()->user();

        $user_role = $user->getRoleNames(); 

        return view('obf.make-deal')->with(['data' =>$data, 'branch' => $branch, 'price_list_name'=>$price_list_name, 'rto_data'=>$rto_data, 'accessory_sum' => $accessory_sum, 'user_role'=> $user_role[0], 'user'=>$user]);
    }

    public function makedealinsert(Request $request){
        $data = [
            'obf_id' => $request->id,
            'purchase_gst_tax_deal' => $request->purchase_gst_tax_deal,
            'final_deal_amt' => $request->final_deal_amt,
            'deal_amt' => $request->ex_showroom_price_deal,
            'gst_tax_deal' => $request->gst_tax_deal,
            'cess_tax_deal' => $request->cess_tax_deal,
            'tcs_tax_deal' => $request->tcs_tax_deal,
            'municipal_tax_deal' => $request->municipal_tax_deal,
            'rto_deal' => $request->rto_deal,
            'insurance_deal' => $request->insurance_deal,
            'created_at' => date('Y-m-d H:i:s'),
            'created_by' => auth()->user()->id,
            'updated_at' => date('Y-m-d H:i:s'),
        ];
            
        MakeDeal::where('obf_id', $data['obf_id'])->delete();
        
        $newEntry = MakeDeal::create($data);

        if ($newEntry) {
            return response()->json(['code' => 200]);
        } else {
            return response()->json(['code' => 201]);
        }
    }

}

