<?php

namespace App\Http\Controllers\Agencies;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Auth;
use App\SaleStockModel;
use App\SaleStockDetailModel;
use App\ClientModel;
use App\ItemModel;
use App\FundsRegisterModel;
use App\JournalModel;
use App\DepositModel;
use DB;
use DataTables;
use PDF;
use Dompdf\Dompdf;

use Illuminate\Support\Facades\Crypt;
class ReportBookingController extends Controller
{
    public function index(){
        return view('agencies.report-booking.index');
    }
    public function pdf_client_deposite(Request $request,$id){
        // $decrypt_id = Crypt::decrypt($id);
        // $invoice = DB::select("SELECT 
        // nso007_client.id,
        // nso007_client.client_fullname,
        // nso007_client.client_phone1,
        // nso007_client.client_address,
        // nso007_sale_stock.sale_date,
        // nso007_sale_stock.sale_deposit,
        // nso007_sale_stock.sale_receive,
        // nso007_sale_detail_stock.saledetail_price,
        // nso007_item_stock.item_name,
        // nso007_item_stock.item_price,
        // nso007_category_stock.category_name,
        // nso007_project_stock.project_name
        // FROM `nso007_client` 
        // INNER JOIN nso007_sale_stock ON nso007_sale_stock.sale_clientid = nso007_client.id
        // INNER JOIN nso007_sale_detail_stock ON nso007_sale_detail_stock.saledetail_saleid = nso007_sale_stock.id
       	// INNER JOIN nso007_item_stock ON nso007_item_stock.id = nso007_sale_detail_stock.saledetail_itemid
        // INNER JOIN nso007_category_stock ON nso007_category_stock.id = nso007_item_stock.item_categoryid 
        // INNER JOIN nso007_project_stock ON nso007_project_stock.id =nso007_item_stock.item_projectid
        // where  nso007_client.id =  $decrypt_id
        // order by nso007_client.id desc");
        
    
    //     $pdf = PDF::loadView('agencies.report-booking.pdf-client-deposite', compact('invoice'));
       
    //     $file= "_master_pdf_file.pdf";
    //     $pdf->save('_master_pdf_file.pdf');
    //     $headers = array(
    //         // 'Content-Type: application/pdf',
    //         // 'contentType : charset=UTF-8',
    //        'Content-Encoding: UTF-8',
    //    'Content-type: text/ht; charset=UTF-8',
    //         'Content-Disposition:attachment; filename="_master_pdf_file.pdf"',
    //         'Content-Transfer-Encoding:binary',
            
    //         'Content-Length:'.filesize($file),
    //     );
    //     return \Response::download($file,"Client Deposite.pdf", $headers);


    // $html='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    // <style>
    //     html{padding:-30px;}
    // body{ font-family: DejaVu Sans;}
    // </style>
    // </head><body>';
    // $html.='Testខ្មែរ';
    // $html.='</body></html>';
    // if ( isset( $html ) ) {
    //     if ( get_magic_quotes_gpc() )
    //     $html = stripslashes($html);
    //     $old_limit = ini_set("memory_limit", "16M");
    //     $dompdf = new DOMPDF();
    //     $dompdf->load_html($html,'UTF-8');
    //     $dompdf->set_paper('a4', 'portrait');// or landscape
    //     $dompdf->render();
    //     $dompdf->stream("limitless.pdf");
    // exit(0);
    // }


}
   
    public function datatable_get_report_booked(){

        $log_id = Auth::user()->id;
        $clientbooking = DB::select("SELECT 
        nso007_client.id,
        nso007_client.client_fullname,
        nso007_client.client_phone1,
        nso007_client.client_address,
        nso007_sale_stock.sale_date,
        nso007_sale_stock.sale_deposit,
        nso007_sale_stock.sale_receive,
        nso007_sale_stock.sale_discount,
        nso007_sale_stock.sale_amount,
        nso007_sale_stock.sale_purchase,
        nso007_sale_detail_stock.saledetail_price,
        GROUP_CONCAT(nso007_item_stock.item_name) as item_name,
        nso007_item_stock.item_stockstatus,
        nso007_item_stock.item_salestatus,
        nso007_item_stock.item_price,
        nso007_category_stock.category_name,
        nso007_project_stock.project_name
        FROM `nso007_client` 
        INNER JOIN nso007_sale_stock ON nso007_sale_stock.sale_clientid = nso007_client.id
        INNER JOIN nso007_sale_detail_stock ON nso007_sale_detail_stock.saledetail_saleid = nso007_sale_stock.id
       	INNER JOIN nso007_item_stock ON nso007_item_stock.id = nso007_sale_detail_stock.saledetail_itemid
        INNER JOIN nso007_category_stock ON nso007_category_stock.id = nso007_item_stock.item_categoryid 
        INNER JOIN nso007_project_stock ON nso007_project_stock.id =nso007_item_stock.item_projectid
        WHERE  nso007_sale_stock.agency_id = '$log_id'
        -- AND (nso007_item_stock.item_stockstatus = 1 OR nso007_item_stock.item_stockstatus = 2)
        -- AND nso007_item_stock.item_salestatus = 2
        GROUP BY nso007_sale_stock.id
        ORDER BY nso007_client.id desc");
		return  Datatables::of($clientbooking)
        
            ->addColumn('sale_receive', function($clientbooking) {
				return '$'.number_format($clientbooking->sale_receive,2);    
            })
            ->addColumn('sale_price', function($clientbooking) {
				return '$'.number_format(($clientbooking->sale_amount-$clientbooking->sale_discount),2);    
            })
            ->addColumn('sale_date', function($clientbooking) {
                return date('d-m-Y', strtotime($clientbooking->sale_date));
            })
            ->editColumn('status', function ($clientbooking) {
                $status = '';
                if ($clientbooking->sale_deposit == 1) {
                    if ($clientbooking->sale_purchase == 0) {
                        $status = '<strong class="text-info"><i class="fa fa-spinner"></i> Booking</strong>';
                    }elseif ($clientbooking->sale_purchase == 1) {
                        $status = '<strong class="text-success"><i class="fa fa-file-text-o"></i> Contracted</strong>';
                    }
                } elseif ($clientbooking->sale_deposit == 0) {
                    $status = '<strong class="text-danger"><i class="fa fa-ban"></i> Cancelled</strong>';
                }
                return $status;
            })
            ->editColumn('action', function ($clientbooking) {
                return '<a href="'.Route("booking.show",Crypt::encrypt($clientbooking->id)).'" target="_blank"> <i class="fa fa-print"></i> </a>
                
                ';
            })
            ->addIndexColumn()
            ->rawColumns(['action' => 'action', 'status' => 'status'])
            ->make(true);
    }
    /**
     *  Report Contracted Booking
     */ 
    public function report_contracted()
    {
        return view('agencies.report-booking.contracted');
    }
    public function datatable_get_report_contracted(){

        $log_id = Auth::user()->id;
        $clientbooking = ClientModel::with(['get_village','get_commune','get_district','get_province'])
                        ->join('nso007_sale_stock as sale','sale.sale_clientid','nso007_client.id')
                        ->join('nso007_loan as loan','loan.loan_saleid','sale.id')
                        ->where('loan.loan_agentid',Auth::user()->id)
                        ->select('sale.created_at',
                                DB::raw('CONCAT(nso007_client.client_firstname," ",nso007_client.client_lastname) as client_fullname'),
                                'nso007_client.id',
                                'nso007_client.p_village',
                                'nso007_client.p_commune',
                                'nso007_client.p_district',
                                'nso007_client.p_province',
                                'nso007_client.client_gender',
                                'nso007_client.client_phone1'
                                )
                        ->get();
        return  Datatables::of($clientbooking)
        
            ->addColumn('gender', function($clientbooking) {
                if($clientbooking->client_gender == 1){
                    return 'Male';
                }else{
                    return 'Female';
                }
            })
            ->addColumn('date', function($clientbooking) {
                return date('d-m-Y', strtotime($clientbooking->created_at));
            })
            ->editColumn('client_address',function($clientbooking){
                return 'ភូមិ '.$clientbooking->get_village['village_km'].' ឃុំ '.$clientbooking->get_commune['commune_km'].' ស្រុក '.$clientbooking->get_district['district_km'].' ស្រុក '.$clientbooking->get_province['province_km'];
            })
            ->addColumn('details_url', function($customer) {
                return route('datatable.get_booking_detail', $customer->id);
            })
			->addIndexColumn()
            ->rawColumns(['details_url'=>'details_url'])
            ->make(true);
    }

    public function get_detail_client_deposit($id)
    {
        $client = DB::select("SELECT    
            nso007_project_stock.project_name,
            nso007_item_stock.item_name,
            nso007_item_stock.item_size_land,
            nso007_loan.loan_amount,
            nso007_loan.loan_amountos,
            nso007_loan.loan_discount,
            nso007_sale_stock.sale_receive,
            nso007_sale_stock.sale_discount,
            nso007_category_stock.category_name,    
            nso007_sale_stock.id
            FROM nso007_loan
            LEFT JOIN nso007_sale_detail_stock ON nso007_loan.loan_saleid = nso007_sale_detail_stock.saledetail_saleid
            LEFT JOIN nso007_sale_stock ON nso007_sale_detail_stock.saledetail_saleid = nso007_sale_stock.id
            LEFT JOIN nso007_item_stock ON nso007_item_stock.id = nso007_sale_detail_stock.saledetail_itemid
            LEFT JOIN nso007_project_stock ON nso007_project_stock.id = nso007_item_stock.item_projectid
                LEFT JOIN nso007_category_stock ON nso007_category_stock.id = nso007_item_stock.item_categoryid
            WHERE nso007_loan.loan_clientid = $id
            GROUP BY nso007_sale_stock.id,nso007_item_stock.item_name,nso007_item_stock.item_size_land,nso007_loan.loan_amount,nso007_sale_stock.sale_receive,nso007_sale_stock.sale_discount,nso007_project_stock.project_name, nso007_loan.loan_amountos,nso007_category_stock.category_name");
        echo json_encode($client);
    }
}
