<?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\CashReceipt;
use App\Models\ExtandWarranty;
use App\Models\Fasttag;
use App\Models\Finance;
use App\Models\Insurance;
use App\Models\Lead;
use App\Models\OBF;
use App\Models\Order;
use App\Models\Setting;
use App\Models\User;
use App\Models\Product;
use App\Models\Tax;
use App\Models\Carmodel;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use PDF;
use DataTables, DB;

class CashReceiptController extends Controller
{
    /** construct */
        public function __construct()
        {
            $this->middleware('permission:cash_receipt-create', ['only' => ['create']]);
            $this->middleware('permission:cash_receipt-edit', ['only' => ['edit']]);
            $this->middleware('permission:cash_receipt-view', ['only' => ['view']]);
            $this->middleware('permission:cash_receipt-delete', ['only' => ['delete']]);
            $this->middleware('permission:obf-edit', ['only' => ['edit']]);
        }
    /** 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];
            }

            /*var_dump($permission_get);
            die('s');*/

            if ($request->ajax()) {
                $a_id = auth()->id();


               /* $data = OBF::select('obf.id', 'obf.booking_date', 'obf.customer_name', 'car_model.name AS product_name', 'obf.status')
                    ->leftjoin('products', 'obf.product_id', 'products.id')
                    ->leftjoin('car_model', 'car_model.id', 'products.car_model')
                    ->where(['obf.status' => 'obf_accepted'])
                    ->orWhere(['obf.status' => 'account_accepted']);*/
                    
                    $data = OBF::select('obf.id', 'obf.booking_date', 'obf.customer_name', 'car_model.name AS product_name', 'obf.status','obf.on_road_price',DB::raw('coalesce(obf.on_road_price,0) -  coalesce(sum(cash_receipt.amount),0) as due_amount'),DB::raw('coalesce(sum(cash_receipt.amount),0) as received_amount'),DB::raw('count(cash_receipt.id)as no_of_receipt'))
                    ->leftjoin('products', 'obf.product_id', 'products.id')
                    ->leftjoin('car_model', 'car_model.id', 'products.car_model')
                    ->leftjoin('cash_receipt','cash_receipt.obf_id','obf.id')
                    ->where(['obf.status' => 'obf_accepted'])
                    ->orWhere(['obf.status' => 'account_accepted']);

                    if($permission_get != '' && $permission_get == 'ONLY_MY_DATA'){
                        $data = $data->whereRaw('FIND_IN_SET(obf.created_by ,"'.auth()->id().'")');
                    }

                    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);  
                            /*echo "<pre>";
                            var_dump($team_name_ids);
                            die('s');*/
                        }    
                    }
                    // $data->dd();
                    
                $data->orderBy('obf.id' ,'desc')->get();

                return Datatables::of($data)
                    ->addIndexColumn()
                    ->addColumn('action', function ($data) {
                        $return = '<div class="btn-group">';

                        if (auth()->user()->can('cash_receipt-view')) {
                            $return .= '<a href="' . route('cash_receipt.view', ['id' => base64_encode($data->id)]) . '" class="btn btn-default btn-xs">
                                                    <i class="fa fa-eye"></i>
                                                </a> &nbsp;';
                        }

                        if (auth()->user()->can('cash_receipt-delete')) {
                            $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">
                                                        <li><a class="dropdown-item" href="javascript:;" onclick="change_status(this);" data-status="account_accepted" data-id="' . base64_encode($data->id) . '">Account Accepted</a></li>
                                                        <li><a class="dropdown-item" href="javascript:;" onclick="change_status_reject(this);" data-status="account_rejected" data-id="' . base64_encode($data->id) . '">Account Rejected</a></li>
                                                    </ul>';
                        }
                        if (auth()->user()->can('cash_receipt-generate_gate_pass')) {
                            $return .= '<a title="Add Cash" href="' . route('cash_receipt.create', ['id' => base64_encode($data->id),'page' => 'cash_receipt']) . '" class="btn btn-default btn-xs">
                                    <i class="fas fa-file-medical"></i>
                                    </a> &nbsp;';
                        }
                        if (auth()->user()->can('cash_receipt-generate_gate_pass') && $data->status == 'account_accepted') {
                            $return .= '<a title="Generate Cash Receipt" href="' . route('cash_receipt.generate_cash_receipt', ['id' => base64_encode($data->id),'page' => 'cash_receipt']) . '" class="btn btn-default btn-xs">
                                    <i class="fas fa-receipt"></i> 
                                    </a> &nbsp;';
                        }

                        $return .= '</div>';

                        return $return;
                    })

                   /* ->editColumn('name', function ($data) {
                        return $data->customer_name;
                    })
                    ->editColumn('model', function ($data) {
                        return $data->product_name;
                    })*/
                    ->editColumn('status', function ($data) {
                        if ($data->status == 'accepted') {
                            return '<span class="badge badge-pill badge-success">Active</span>';
                        } else if ($data->status == 'pending') {
                            return '<span class="badge badge-pill badge-warning">Pending</span>';
                        } else if ($data->status == 'deleted') {
                            return '<span class="badge badge-pill badge-danger">Deleted</span>';
                        } else if ($data->status == 'account_rejected') {
                            return '<span class="badge badge-pill badge-danger">Account Rejected</span>';
                        } else if ($data->status == 'obf_rejected') {
                            return '<span class="badge badge-pill badge-danger">OBF Rejected</span>';
                        } else if ($data->status == 'rejected') {
                            return '<span class="badge badge-pill badge-danger">Rejected</span>';
                        } else if ($data->status == 'obf_accepted') {
                            return '<span class="badge badge-pill badge-success">Obf Accepted</span>';
                        } else if ($data->status == 'account_accepted') {
                            return '<span class="badge badge-pill badge-success">Account Accepted</span>';
                        }
                    })

                    ->rawColumns(['name', 'action', 'status', 'model'])
                    ->make(true);
            }
            return view('cash_receipt.index');
        }
    /** index */

    /** Create */
        public function create(Request $request)
        {
            $id = base64_decode($request->id);
            $page = $request->page;


            $data = OBF::select('obf.id')
                ->where(['id' => $id])
                ->first();

            $cash_receipt_data = [];
            if(isset($request->cash_receipt_id) && !empty($request->cash_receipt_id)){
                $cash_receipt_id = base64_decode($request->cash_receipt_id);
                $cash_receipt_data = CashReceipt::where(['id' => $cash_receipt_id])->first();   
            }
           /* echo "<pre>";
            print_r($cash_receipt_data);
            die('die');*/
            

                

            if ($data) {
                return view('cash_receipt.create')->with(['data' => $data,'cash_receipt_data' => $cash_receipt_data,'page' => $page]);
            } else {
                return view('cash_receipt.create')->with('error', 'No record found!');
            }
        }
    /** Create */

    /** Insert */
        public function insert(Request $request)
        {
            
            $obf_data = OBF::where('id',$request->id)->first();
            

            if (!empty($obf_data)) {

                // var_dump($obf_data->id);
                $obf_status = '';
                $spcial_case = '';
                $status = ''; 

                $Setting_data = Setting::where('key','MINIMUM_CASH_RECEIPT_BALANCE')->first();

                $MINIMUM_CASH_RECEIPT_BALANCE = 0;
                if(!empty($Setting_data)){
                    if($Setting_data->value != NULL)
                    {
                        $MINIMUM_CASH_RECEIPT_BALANCE = (int) $Setting_data->value;
                    }    
                }
                $Setting_data = Setting::where('key','MAXIMUM_CASH_RECEIPT_BALANCE')->first();

                $MAXIMUM_CASH_RECEIPT_BALANCE = 0;
                if(!empty($Setting_data)){
                    if($Setting_data->value != NULL)
                    {
                        $MAXIMUM_CASH_RECEIPT_BALANCE = (int) $Setting_data->value;
                    }    
                }

                $CashReceipt_sum = (int) CashReceipt::where('obf_id',$request->id)->where('payment_type','cash')->sum('amount');

                if(empty($request->amount) && $request->amount == ''){
                    $request->amount = 0;
                }

                /*var_dump($request->amount);
                die('s');*/

                $total_sum = $CashReceipt_sum + (int) $request->amount;

                if($request->payment_type == 'cash'){
                    if($total_sum > $MAXIMUM_CASH_RECEIPT_BALANCE){
                        return redirect()->back()->with('error', 'cash receipt Maximum balance is '.$MAXIMUM_CASH_RECEIPT_BALANCE)->withInput();
                    }    
                }

                // die('s');
                /*var_dump($CashReceipt_sum);
                var_dump($MINIMUM_CASH_RECEIPT_BALANCE);
                var_dump($MAXIMUM_CASH_RECEIPT_BALANCE);
                die('ss');*/
                

                if ($total_sum < $MINIMUM_CASH_RECEIPT_BALANCE) {
                    $obf_status = 'account_rejected';
                    $spcial_case = 'yes';
                    $status = 'pending';
                }else{
                    $obf_status = 'account_accepted';
                    $spcial_case = 'no';
                    $status = 'accepted';
                }

                if($obf_status == 'account_accepted'){
                    //notification insert
                    $notification_template_master_data = DB::table('notification_template_master')->select('*')->where(['template_name' => 'cashier_recipt_approved','status' => 'active'])->first();
                    if(!empty($notification_template_master_data)){
                        $customer_name = $obf_data->customer_name ?? null;
                        
                        $car_name_get = '';
                        $product_id = 0;
                        if($obf_data->product_id != null && $obf_data->product_id != ''){
                            $product_id = $obf_data->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_approval.view', ['id' => base64_encode($request->id)]),
                            'created_by' => auth()->user()->id,
                        ];
                        DB::table('notifications')->insert($notification_data);
                    }
                    //notification insert
                }


                if($request->cash_receipt_data_id != ''){
                    //update
                    $data = CashReceipt::where('id',$request->cash_receipt_data_id)->first();
                    $data->obf_id = $obf_data->id;
                    $data->amount = $request->amount;
                    $data->payment_type = $request->payment_type;
                    $data->spcial_case = $spcial_case;
                    $data->status = $status;
                    $data->created_at = date('Y-m-d H:i:s');
                    $data->updated_at = date('Y-m-d H:i:s');
                    $data->updated_by = auth()->user()->id;
                    $data->created_by = auth()->user()->id;
                    $data->save();
                }else{
                    //insert
                    $data = new CashReceipt();
                    $data->obf_id = $obf_data->id;
                    $data->amount = $request->amount;
                    $data->payment_type = $request->payment_type;
                    $data->spcial_case = $spcial_case;
                    $data->status = $status;
                    $data->created_at = date('Y-m-d H:i:s');
                    $data->updated_at = date('Y-m-d H:i:s');
                    $data->updated_by = auth()->user()->id;
                    $data->created_by = auth()->user()->id;
                    $data->save();

                    $cash_receipt_id_get = $data->id;

                    //notification code
                    $notification_template_master_data = DB::table('notification_template_master')->select('*')->where(['template_name' => 'recipt_generate','status' => 'active'])->first();

                    // var_dump($notification_template_master_data);


                    if(!empty($notification_template_master_data)){

                        // echo "11";

                        $obf_data_get = DB::table('obf')->select('*')->where(['obf.id' => $obf_data->id])->first();
                        $customer_name = '';
                        $product_id = 0;
                        if(!empty($obf_data_get)){
                            $customer_name = $obf_data_get->customer_name ?? null;    
                            $product_id = $obf_data_get->product_id ?? null;    
                        }

                        $recipt_date = date('d/m/Y');


                         
                        // die('stp[');

                            // die('ss');
                        

                        $car_name_get = '';

                        /*$product_id = 0;
                        if($request->product_id != null && $request->product_id != ''){
                            $product_id = $request->product_id;
                        }*/

                          if($product_id != null){
                           // DB::enableQueryLog();


                        $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();
                          // dd(DB::getQueryLog());

                        /*echo "<pre>";
                        print_r($Cardata_get);
                        die('s');*/

                        
                        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.')';    
                                // $car_name_get .= ' | '$Cardata_get->varient_name;    
                            }
                        }    
                    }
                        // die('stp[--');
                        


                        $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);
                        $data_new = str_replace("[Recipt_no]",$cash_receipt_id_get,$data_new);
                        $data_new = str_replace("[Recipt_date]",$recipt_date,$data_new);

                        $notification_data = [
                            'user_id' => auth()->user()->id,
                            'notification_text' => $data_new,
                            //'notification_link' => route('obf.view', ['id' => base64_encode($data->obf_id)]),
                            'notification_link' => route('cash_receipt.generate_cash_receipt', ['id' => base64_encode($data->obf_id),'page' => 'cash_receipt']),
                            'created_by' => auth()->user()->id,
                        ];

                        // var_dump($notification_data);
                         // DB::enableQueryLog();
                        DB::table('notifications')->insert($notification_data);
                         // dd(DB::getQueryLog());

                    }

                    // die('stp[');

                }




                

                $last_id = $data->id;
                // var_dump($last_id);
                if ($last_id) {
                    $crud_data = [
                        'status' => $obf_status,
                        'updated_at' => date('Y-m-d H:i:s'), 
                        'updated_by' => auth()->user()->id
                    ];
                    $obf_update = OBF::where(['id' => $obf_data->id])->update($crud_data);
                    
                    // die(json_encode(array('status'=>true)));
                    //die('ss');
                    $msg = '';
                    if($request->cash_receipt_data_id != ''){
                         $msg = 'Record Updated successfully';
                    }else{
                         $msg = 'Record inserted successfully';
                    }

                    if($request->page == 'cash_receipt'){
                        return redirect()->route('cash_receipt')->with('success', $msg);
                    }else{
                        return redirect()->route('order')->with('success', $msg);
                    }

                    
                    ///  return view('cash_receipt.index')->with('success', 'Record inserted successfully');
                    
                } else {                   
                     return redirect()->back()->with('error', 'Failed to update record2')->withInput();
                }


            }else {
                 return view('cash_receipt.index')->with('error', 'No Record Found!');
            }
            //die('---ss');

        }




        public function insert_old(Request $request)
        {
            $id = $request->id;
            $data = OBF::select('obf.id')
            ->where(['id' => $id])
            ->first();
            if ($data) {
                if ($request->amount < 25000) {
                    $crud = [
                        'obf_id' => $id,
                        'amount' => $request->amount,
                        'spcial_case' => 'yes',
                        'status' => 'pending',
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                        'updated_by' => auth()->user()->id,
                        'created_by' => auth()->user()->id
                    ];
                    $obf_status = 'account_rejected';
                } else {
                    $crud = [
                        'obf_id' => $id,
                        'amount' => $request->amount,
                        'spcial_case' => 'no',
                        'status' => 'accepted',
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                        'updated_by' => auth()->user()->id,
                        'created_by' => auth()->user()->id
                    ];
                    $obf_status = 'account_accepted';
                }
                // DB::beginTransaction();
                // try {
                    DB::enableQueryLog();
                        $cash_receipt = CashReceipt::where(['obf_id' => $id])->first();
                        if(!empty($cash_receipt)){
                            $last_id = CashReceipt::where(['id' => $cash_receipt->id])->update($crud);
                        }else{
                            $last_id = CashReceipt::insertGetId($crud);
                        }
                        // dd($last_id);
                    if ($last_id) {
                        $crud_data = [
                            'status' => $obf_status,
                            'updated_at' => date('Y-m-d H:i:s'), 
                            'updated_by' => auth()->user()->id
                        ];
                        $obf_update = OBF::where(['id' => $id])->update($crud_data);
                        if ($obf_update) {
                            // dd($obf_update);
                            DB::commit();
                            return redirect()->route('cash_receipt')->with('success', 'Record inserted successfully');
                        } else {
                            dd('ho');
                            DB::rollback();
                            return redirect()->back()->with('error', 'Failed to update record1')->withInput();
                        }
                    } else {
                        dd('by');
                        DB::rollback();
                        return redirect()->back()->with('error', 'Failed to update record2')->withInput();
                    }
                // } catch (\Throwable $th) {
                    dd('exit');
                    DB::rollback();
                    return redirect()->back()->with('error', 'Something went wrong, please try again later')->withInput();
                // }
            } else {
                return view('cash_receipt.index')->with('error', 'No Record Found!');
            }
        }
    /** Insert */

    /** view */
        public function view(Request $request)
        {
            if ($request->ajax()) {
                return true ;
            }
                $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',
                    'branches.name AS branch_name',
                    'branches.id AS branch_id',
                    'obf.company_name',
                    '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.exterior_color',
                    'obf.interior_color',*/
                    '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 fasttag_brand_name',
                    'fasttags.amount AS fasttag_amount',
                    'fasttags.id AS fasttag_id',
                    'obf.trad_in_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.other_documents',
                    'obf.pan_image','obf.adhar_image','obf.licance_image','obf.sub_lead_id'
                    /*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();

                  $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();
                    
                $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')
                                    ->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();

                $lead = Lead::select('id' ,'name')->where(['status' => 'active'])->get();


                if ($data)
                    return view('cash_receipt.view')->with(['data' => $data,'carmodel' => $carmodel,'Cardata' => $Cardata,'lead' => $lead]);
                else
                    return view('cash_receipt')->with('error', 'No record found!');
            
        }
    /** view */

    /** change-status */
        public function change_status(Request $request)
        {
            if ($request->ajax()) {
                if (isset($request->id) && $request->id != null) {
                    $id = base64_decode($request->id);
                } else {
                    return response()->json(['status' => 201, 'message' => 'Id not found!']);
                }


                DB::beginTransaction();
                try {
                    $data = OBF::where(['id' => $id])->first();

                    if (!empty($data)) {

                        if ($request->status == 'account_rejected') {
                            $update = OBF::where(['id' => $id])->update(['status' => $request->status, 'reason' => $request->reason, 'updated_at' => date('Y-m-d H:i:s'), 'updated_by' => auth()->user()->id]);
                            if ($update) {

                                DB::commit();
                                return response()->json(['code' => 200]);
                            } else {
                                DB::rollback();
                                return response()->json(['code' => 201]);
                            }
                        } else if ($request->status == 'account_accepted') {

                            if($request->status == 'account_accepted'){
                                //notification insert
                                $notification_template_master_data = DB::table('notification_template_master')->select('*')->where(['template_name' => 'cashier_recipt_approved','status' => 'active'])->first();
                                if(!empty($notification_template_master_data)){
                                    $customer_name = $data->customer_name ?? null;
                                    
                                    $car_name_get = '';
                                    $product_id = 0;
                                    if($data->product_id != null && $data->product_id != ''){
                                        $product_id = $data->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_approval.view', ['id' => base64_encode($id)]),
                                        'created_by' => auth()->user()->id,
                                    ];
                                    DB::table('notifications')->insert($notification_data);
                                }
                                //notification insert
                            }


                            $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) {
                                DB::commit();
                                return response()->json(['code' => 200]);
                            } else {
                                DB::rollback();
                                return response()->json(['code' => 201]);
                            }
                        } else {
                            $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) {

                                DB::commit();
                                return response()->json(['code' => 200]);
                            } else {
                                DB::rollback();
                                return response()->json(['code' => 201]);
                            }
                        }
                    } else {
                        DB::rollback();
                        return response()->json(['code' => 201]);
                    }
                } catch (\Throwable $th) {
                    DB::rollback();
                    return response()->json(['code' => 201]);
                }
            }
        }
    /** change-status */

    /** change_status_delete */
        public function change_status_delete(Request $request){
            if (!$request->ajax()) { exit('No direct script access allowed'); }
            $id = base64_decode($request->id);
           
            $data = CashReceipt::where(['id' => $id])->first();

            if (!empty($data)) {
                $update = CashReceipt::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_delete */

    /** Generate Cash-receipt */
        public function generate_cash_receipt(Request $request)
        {
            $id = base64_decode($request->id);
            $page = $request->page;


            $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',
                'branches.name AS branch_name',
                'branches.id AS branch_id',
                'obf.company_name',
                'obf.gst',
                'obf.address',
                'obf.registration',
                'obf.email',
                'obf.pan_number',
                'obf.adhar_number',
                'obf.licance_number',
                'obf.contact_number',
                'obf.dob',
                'obf.nominee_name',
                'obf.nominee_reletion',
                'obf.nominee_age',
                'obf.occupation',
                'products.name AS product_name',
                'products.name AS product_name',
                'obf.product_id',
                'products.veriant',
                'products.is_applicable_for_mcp',
                'products.is_applicable_for_mcp',
                /*'obf.exterior_color',
                'obf.interior_color',*/
                '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 fasttag_brand_name',
                'fasttags.amount AS fasttag_amount',
                'fasttags.id AS fasttag_id',
                'obf.trad_in_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',
                'cash_receipt.amount',
                'cash_receipt.id AS cash_receipt_id',
                'cash_receipt.payment_type AS cash_receipt_payment_type',
                'car_model.name AS car_model_name',
                'car_varient.name AS car_varient_name',
                'interiorcolor.name AS interiorcolor_name',
                'exteriorcolor.name AS exteriorcolor_name',
            )
                ->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('accessories', 'obf.accessory_id', 'accessories.id')
                ->leftjoin('extand_warranties', 'obf.extanded_warranty_id', 'extand_warranties.id')
                ->leftjoin('fasttags', 'obf.fasttag_brand_name', 'fasttags.brand_name')
                ->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('cash_receipt', 'cash_receipt.obf_id', 'obf.id')
                ->where(['obf.id' => $id])
                ->where('cash_receipt.status','!=','rejected')
                ->orderBy('obf.id' ,'desc')
                ->get();
                //->first();
         
            if ($data) {
                //$obf = OBF::where(['id' => $data->id])->first();
                $obf = OBF::where(['id' => $id])->first();
                if ($obf) {
                    $crud = [
                        'order_id' => 'Order_ID' . Date('YmdHis'),
                        //'obf_id' => $data->id,
                        'obf_id' => $id,
                        //'branch_id' => $data->branch_id,
                        'branch_id' => $obf->branch_id,
                        'created_by' => auth()->user()->id,
                        'created_at' => date('Y-m-d H:i:s'),
                    ];
                    $curd_update = [
                        'updated_by' => auth()->user()->id,
                        'updated_at' => date('Y-m-d H:i:s')
                    ];
                    $get_order = Order::where(['obf_id' => $id])->first();
                    if(!empty($get_order)){
                        $order = Order::where(['id' => $get_order->id])->update($curd_update);
                    }else{
                        $order = Order::insertGetId($crud);
                    }
                    if ($order) {
                        return view('cash_receipt.cash_receipt')->with(['data' => $data,'page' => $page]);
                    } else {
                        return view('cash_receipt')->with('error', 'Faild to enter in order!');
                    }
                } else {
                    return view('cash_receipt.cash_receipt')->with(['data' => $data,'page' => $page]);
                }
            } else {
                return view('cash_receipt')->with('error', 'No Data Found!');
            }
        }
    /** Generate Cash-receipt */

    public function recipt_pdf(Request $request)
    {
        $obf_id = base64_decode($request->id);
        $page = $request->page;
        $cash_receipt_id = base64_decode($request->cash_receipt_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',
                'branches.name AS branch_name',
                'branches.id AS branch_id',
                'obf.company_name',
                'obf.gst',
                'obf.address',
                'obf.registration',
                'obf.email',
                'obf.pan_number',
                'obf.adhar_number',
                'obf.licance_number',
                'obf.contact_number',
                'obf.dob',
                'obf.nominee_name',
                'obf.nominee_reletion',
                'obf.nominee_age',
                'obf.occupation',
                'products.name AS product_name',
                'products.name AS product_name',
                'obf.product_id',
                'products.veriant',
                'products.is_applicable_for_mcp',
                'products.is_applicable_for_mcp',
                /*'obf.exterior_color',
                'obf.interior_color',*/
                '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 fasttag_brand_name',
                'fasttags.amount AS fasttag_amount',
                'fasttags.id AS fasttag_id',
                'obf.trad_in_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',
                'cash_receipt.amount as cash_receipt_amount',
                'cash_receipt.id AS cash_receipt_id',
                'cash_receipt.payment_type AS cash_receipt_payment_type',
                'cash_receipt.created_at AS cash_receipt_created_at',
                'car_model.name AS car_model_name',
                'car_varient.name AS car_varient_name',
                'interiorcolor.name AS interiorcolor_name',
                'exteriorcolor.name AS exteriorcolor_name',
            )
                ->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('accessories', 'obf.accessory_id', 'accessories.id')
                ->leftjoin('extand_warranties', 'obf.extanded_warranty_id', 'extand_warranties.id')
                ->leftjoin('fasttags', 'obf.fasttag_brand_name', 'fasttags.brand_name')
                ->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('cash_receipt', 'cash_receipt.obf_id', 'obf.id')
                ->where(['obf.id' => $obf_id, 'cash_receipt.id' => $cash_receipt_id])
                ->where('cash_receipt.status','!=','rejected')
                ->orderBy('obf.id' ,'desc')
                ->first();


               
             ini_set('memory_limit', '48M');

            PDF::SetAuthor('Nicola Asuni');
            PDF::SetTitle('RECEIPT');
            PDF::SetSubject('RECEIPT');
            PDF::setImageScale(PDF_IMAGE_SCALE_RATIO);
            PDF::AddPage('L', 'A4');
            /*PDF::Image('assets/img/atul-logo.jpg', 15, 18, 23, 24, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);*/

            PDF::Image(public_path('assets/img/new_kia.jpg'), 15, 11, 46, 19, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);


            PDF::Image(public_path('assets/img/kia_new.jpg'), 180, 14, 100, 16, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);

            /*PDF::Image(public_path('assets/img/ib_logo.jpeg'), 15, 14, 60, 12, 'JPG', '', '', true, 150, '', false, false, 0, false, false, false);*/
            
            $data->customer_name = isset($data->customer_name) ? $data->customer_name : "N/A";
            $data->contact_number = isset($data->contact_number) ? $data->contact_number : "N/A";
            $data->address = isset($data->address) ? $data->address : "N/A";
            $data->cash_receipt_id = isset($data->cash_receipt_id) ? $data->cash_receipt_id : "N/A";
            $data->cash_receipt_payment_type = isset($data->cash_receipt_payment_type) ? $data->cash_receipt_payment_type : "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->finance_name = isset($data->finance_name) ? $data->finance_name : "N/A";
            $data->cash_receipt_amount = isset($data->cash_receipt_amount) ? $data->cash_receipt_amount : "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";
            $amount_word = $this->prisetoword($data->cash_receipt_amount);

            $receipt_title = '';
            $payment_type = '';
            $payment_method = '';
            /*$cheque_no
            $cheque_date 
            $bank_name */

            /*var_dump($data->cash_receipt_payment_type);
            die('stp[');*/
            
            if($data->cash_receipt_payment_type == 'online_transfer'){
                $receipt_title = 'PAYMENT RECEIPT';
                $payment_type = 'bank';
            }else{
                $receipt_title = 'PAYMENT RECEIPT';
                $payment_type = 'cash';
            }
            $payment_method = str_replace('_', ' ', $data->cash_receipt_payment_type);;

             /*<h1 align="center" style="font-size:26px;">ATUL MOTORS JAM LLP</h1>
                        <p align="center">Near Royal Enfield, Jamnagar-Rajkot Highway, Hapa, Jamnagar - 361120, GUJARAT, INDIA.</p>*/
            $tbl = '<div style="border:1px solid black;">   
                               <span><img src="assets(assets/img/111.jpg)"></span>
                               <span><img src="assets(assets/img/222.jpg)"></span><br>                    
                        <table cellspacing="0" cellpadding="1" border="0">
                            <tr>
                                <th align="center" style="border-top:1px solid black;font-size:22px;"><b>NEW CAR SALES RECEIPT</b></th>
                            </tr>
                            <tr>
                                <th align="center" style="border-top:1px solid black;border-bottom:1px solid black;font-size:22px;"><b>'.$receipt_title.'</b></th>
                            </tr>
                            
                        </table>
                        <table cellspacing="0" cellpadding="5" border="0">
                            <tr>
                                <td rowspan="2" style="width:70%;border-right:1px solid black;"><b>Received With Thanks From: </b>'.$data->customer_name.' <br><b>Mobile No. : </b>'.$data->contact_number.' <br> '.$data->address.'.</td>
                                <td ><b>Receipt No. : </b>Receipt-'.$cash_receipt_id.' <br><b>Receipt Date :</b>'.$data->cash_receipt_created_at.'</td>
                            </tr>
                            <tr>
                                <td style="width:30%;border-top:1px solid black;"><b>Registration No. :</b>147452</td>
                            </tr>
                        </table>
                        <table cellspacing="0" cellpadding="5" border="0">
                            <tr>
                                <td style="width:70%;border-right:1px solid black;border-top:1px solid black;border-bottom:1px solid black;"><b>Model: </b>'.$data->car_model_name.' | '.$data->car_varient_name.'<br><b>Color : </b>'.$data->exteriorcolor_name.' | '.$data->interiorcolor_name.'<br><b>KEC / Executive : </b>'.$data->sales_person_name.'</td>
                                <td style="width:30%;border-top:1px solid black;border-bottom:1px solid black;">
                                    <b>Payment Detail</b><br>
                                    Payment Type : '.$payment_type.'<br>
                                    Payment Method : '.$payment_method.' <br>
                                    <b>Hyp/H : </b>'.$data->finance_name.'
                                </td>
                            </tr>
                            <tr>
                                <td style="width:70%;border-right:1px solid black;border-top:1px solid black;border-bottom:1px solid black;"><b>Rupees in words :</b> '.$amount_word.'ONLY</td>
                                <td style="width:30%;border-right:1px solid black;border-top:1px solid black;border-bottom:1px solid black;"><b>Rs. </b>'.$data->cash_receipt_amount.'/- </td>
                            </tr>
                            <tr>
                                <td style="width:70%;"><b>GST No. :</b> 24ABACS4515J1ZH</td>
                                <td style="width:30%;text-align:right;"><b>Savan IB Automotive Private Ltd</b></td>
                            </tr>
                            <tr>
                                <td style="width:70%;">Price Prevailling at the time of delivery will be applicable<br>Rs. 500/- will be charged from customer as Cheque return charges.<br>Rs. 500/- will be charged from the customer against booking cancelling.<br>Consumer/Corporate offer are subject to change without prior notice and will be given/applicable as per the noms.
                                </td>
                                <td style="width:30%;text-align:right; font-size:10px;"><br><br><br><br><br><br>Authorized Signatory</td>
                                
                            </tr>
                            <tr>
                                <td style="width:70%;"><br><br><b>Customer Signature</b></td>
                                <td></td>
                            </tr>
                        </table>
                    </div>';

                    //<br><b>Booking No. :</b>B/NXA/22-23/00038
                    //<br>Created by : abc xyz

            PDF::writeHTML($tbl, true, false, false, false, '');

            PDF::SetFont('dejavusans', '', 10);
            $content = "Office Copy";
            PDF::SetXY(260, 42);
            PDF::Cell(0, 0, $content);
            PDF::Output('receipt.pdf', 'I');
    }

    public function prisetoword($number){
        // $number = 230558;
        $no = floor($number);
        $point = round($number - $no, 2) * 100;
        $hundred = null;
        $digits_1 = strlen($no);
        $i = 0;
        $str = array();
        $words = array('0' => '', '1' => 'ONE', '2' => 'TWO',
        '3' => 'THREE', '4' => 'FOUR', '5' => 'FIVE', '6' => 'SIX',
        '7' => 'SEVEN', '8' => 'EIGHT', '9' => 'NINE',
        '10' => 'TEN', '11' => 'ELEVEN', '12' => 'TWELVE',
        '13' => 'THIRTEEN', '14' => 'FOURTEEN',
        '15' => 'FIFTEEN', '16' => 'SIXTEEN', '17' => 'SEVENTEEN',
        '18' => 'EIGHTEEN', '19' =>'NINETEEN', '20' => 'TWENTY',
        '30' => 'THIRTY', '40' => 'FORTY', '50' => 'FIFTY',
        '60' => 'SIXTY', '70' => 'SEVENTY',
        '80' => 'EIGHTY', '90' => 'NINETY');
        $digits = array('', 'HUNDRED', 'THOUSAND', 'LAKH', 'CRORE');
        while ($i < $digits_1) {
            $divider = ($i == 2) ? 10 : 100;
            $number = floor($no % $divider);
            $no = floor($no / $divider);
            $i += ($divider == 10) ? 1 : 2;
            if ($number) {
                $plural = (($counter = count($str)) && $number > 9) ? 'S' : null;
                $hundred = ($counter == 1 && $str[0]) ? ' ' : null;
                $str [] = ($number < 21) ? $words[$number] .
                    " " . $digits[$counter] . $plural . " " . $hundred
                    :
                    $words[floor($number / 10) * 10]
                    . " " . $words[$number % 10] . " "
                    . $digits[$counter] . $plural . " " . $hundred;
            } else $str[] = null;
        }
        $str = array_reverse($str);
        $word_price = implode('', $str);
       /* $points = ($point) ?
        "." . $words[$point / 10] . " " . 
          $words[$point = $point % 10] : '';*/
        $word_price . "ONLY";

        return $word_price;
    }
    
}
