<?php

namespace App\Http\Controllers\Contractor;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

use Carbon\Carbon;
use Validator;
use PDF;

use App\Staff;
use App\Model\StaffDetail;
use App\Model\Form;
use App\Model\Subscriber;
use App\Model\Coverage; 
use App\Model\FormStatus;
use App\Model\WorkOrder;
use App\Model\Docket;
Use App\Model\DealerClaim;
use App\Model\Commission;
use App\Model\Company;
use App\Model\Product;

class DocketController extends Controller
{

	public function encryptPassword($action, $string) {
		$output = false;
		$encrypt_method = "AES-256-CBC";
		$secret_key = '28472B4B6250655368566D5970337336';
		$secret_iv =  md5(md5($string)); //'This is my secret iv';
		
		// hash
		$key = hash('sha256', $secret_key);
		
		$iv = substr(hash('sha256', $secret_iv), 0, 16);
		if ( $action == 'encrypt' ) {
			$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
			$output = base64_encode($output);
		} else if( $action == 'decrypt' ) {
			$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
		}
		return $output;
	}

    /*************************************************************************************   Next Step *************************************************************************************/

   

    public function viewContractorDocket()
    {
    	$id = Auth::guard('contractor')->id();
		$user = Staff::with('StaffDetail')->find($id);

		$pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
		$sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
		$rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
		$ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
		$cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());

		$installer = StaffDetail::where('company_id',$user->StaffDetail->company_id)->get(); 

        return view('contractor.view_docket',compact('user','pp','sp','rs','ss','cm'));
    }

    public function viewContractorTeam()
    {
    	$id = Auth::guard('contractor')->id();
		$user = StaffLaravel::with('StaffDetailLaravel')->find($id);

        return view('contractor.view_team',compact('user'));
    }

    public function getContractorWork()
    {

    	$id = Auth::guard('contractor')->id();
		$user = StaffLaravel::with('StaffDetailLaravel')->find($id);
		$curr = Carbon::now()->getTimestamp();

        $work_detail= WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->get();

		$i = 0;
		$nested_data = array();
		if(!empty($work_detail)){
			foreach ($work_detail as $a)
			{	
				$i++; $y = ''; $n1 = '';

				$reg_time = $a->created_at;
				$expiry_date = $reg_time->addDays(3);    
				$expiry_date = $expiry_date->getTimestamp();  

				if($curr < $expiry_date) {
					$n1 = "New/";
				}

				$formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
				$installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
				$contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();

				$inst_name = '';
				if(!empty($installer)){
					$inst_name = $installer->name;
				}

				array_push($nested_data, array(
					'index' => $i.$n1,
					'nature_work' => $a->nature_work,
					'wo'=>$a->wo,
					'customer_name' => $formDetail->SubscribersLaravel->name,
					'customer_phone' => $formDetail->SubscribersLaravel->phone1,
					'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
					'customer_building' => $formDetail->SubscribersLaravel->building_name,
					'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
					'customer_city' => $formDetail->SubscribersLaravel->city,
					'contractor_id' => $contractor->name,
					'installer_id' => $inst_name,
					'dateTimeEnd' => $a->dateTimeEnd,
					'status' => $a->status,
					'action' => $a->id,
				));
			}
		}				
		return \DataTables::of($nested_data)->make(true);	
    }

    public function filterContractorWork($year, $month, $status)
    {

    	$id = Auth::guard('contractor')->id();
		$user = StaffLaravel::with('StaffDetailLaravel')->find($id);
    	$curr = Carbon::now()->getTimestamp();

		if($year == 'null' && $month == 'null' & $status !='null')
		{
			$nested_data = array();
			$work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->where('status', $status)->get();
			$i = 0;
			$nested_data = array();
			if(!empty($work_detail)){
				foreach ($work_detail as $a)
				{	
					$i++; $y = ''; $n1 = '';

					$reg_time = $a->created_at;
					$expiry_date = $reg_time->addDays(3);    
					$expiry_date = $expiry_date->getTimestamp();  

					if($curr < $expiry_date) {
						$n1 = "New/";
					}

					$formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
					$installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
					$contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();

					$inst_name = '';
					if(!empty($installer)){
						$inst_name = $installer->name;
					}

					array_push($nested_data, array(
						'index' => $i.$n1,
						'nature_work' => $a->nature_work,
						'wo'=>$a->wo,
						'customer_name' => $formDetail->SubscribersLaravel->name,
						'customer_phone' => $formDetail->SubscribersLaravel->phone1,
						'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
						'customer_building' => $formDetail->SubscribersLaravel->building_name,
						'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
						'customer_city' => $formDetail->SubscribersLaravel->city,
						'contractor_id' => $contractor->name,
						'installer_id' => $inst_name,
						'dateTimeEnd' => $a->dateTimeEnd,
						'status' => $a->status,
						'action' => $a->id,
					));
				}
			}			
		}

		if($year=='null' && $month == 'null' & $status == 'null')
		{
			$nested_data = array();
			$work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->get();
			$i = 0;
			$nested_data = array();
			if(!empty($work_detail)){
				foreach ($work_detail as $a)
				{	
					$i++; $y = ''; $n1 = '';

					$reg_time = $a->created_at;
					$expiry_date = $reg_time->addDays(3);    
					$expiry_date = $expiry_date->getTimestamp();  

					if($curr < $expiry_date) {
						$n1 = "New/";
					}

					$formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
					$installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
					$contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();

					$inst_name = '';
					if(!empty($installer)){
						$inst_name = $installer->name;
					}

					array_push($nested_data, array(
						'index' => $i.$n1,
						'nature_work' => $a->nature_work,
						'wo'=>$a->wo,
						'customer_name' => $formDetail->SubscribersLaravel->name,
						'customer_phone' => $formDetail->SubscribersLaravel->phone1,
						'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
						'customer_building' => $formDetail->SubscribersLaravel->building_name,
						'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
						'customer_city' => $formDetail->SubscribersLaravel->city,
						'contractor_id' => $contractor->name,
						'installer_id' => $inst_name,
						'dateTimeEnd' => $a->dateTimeEnd,
						'status' => $a->status,
						'action' => $a->id,
					));
				}
			}			
		}

		if($year!='null' && $month!='nulll' && $status !='null')
		{
			$nested_data = array();
			$start_month = $year."-".$month."-01 00:00:00";
			$end_month = $year."-".$month."-32 23:59:59";

			$work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->where('status',$status)->get();
			$i = 0;
			$nested_data = array();
			if(!empty($work_detail)){
				foreach ($work_detail as $a)
				{	
					$i++; $y = ''; $n1 = '';

					$reg_time = $a->created_at;
					$expiry_date = $reg_time->addDays(3);    
					$expiry_date = $expiry_date->getTimestamp();  

					if($curr < $expiry_date) {
						$n1 = "New/";
					}

					$formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
					$installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
					$contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();

					$inst_name = '';
					if(!empty($installer)){
						$inst_name = $installer->name;
					}

					array_push($nested_data, array(
						'index' => $i.$n1,
						'nature_work' => $a->nature_work,
						'wo'=>$a->wo,
						'customer_name' => $formDetail->SubscribersLaravel->name,
						'customer_phone' => $formDetail->SubscribersLaravel->phone1,
						'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
						'customer_building' => $formDetail->SubscribersLaravel->building_name,
						'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
						'customer_city' => $formDetail->SubscribersLaravel->city,
						'contractor_id' => $contractor->name,
						'installer_id' => $inst_name,
						'dateTimeEnd' => $a->dateTimeEnd,
						'status' => $a->status,
						'action' => $a->id,
					));
				}
			}		
		}

		if($year!='null' && $month!='null' && $status =='null')
		{
			$nested_data = array();
			$start_month = $year."-".$month."-01 00:00:00";
			$end_month = $year."-".$month."-32 23:59:59";

			$work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->get();
			$i = 0;
			$nested_data = array();
			if(!empty($work_detail)){
				foreach ($work_detail as $a)
				{	
					$i++; $y = ''; $n1 = '';

					$reg_time = $a->created_at;
					$expiry_date = $reg_time->addDays(3);    
					$expiry_date = $expiry_date->getTimestamp();  

					if($curr < $expiry_date) {
						$n1 = "New/";
					}

					$formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
					$installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
					$contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();

					$inst_name = '';
					if(!empty($installer)){
						$inst_name = $installer->name;
					}

					array_push($nested_data, array(
						'index' => $i.$n1,
						'nature_work' => $a->nature_work,
						'wo'=>$a->wo,
						'customer_name' => $formDetail->SubscribersLaravel->name,
						'customer_phone' => $formDetail->SubscribersLaravel->phone1,
						'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
						'customer_building' => $formDetail->SubscribersLaravel->building_name,
						'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
						'customer_city' => $formDetail->SubscribersLaravel->city,
						'contractor_id' => $contractor->name,
						'installer_id' => $inst_name,
						'dateTimeEnd' => $a->dateTimeEnd,
						'status' => $a->status,
						'action' => $a->id,
					));
				}
			}		
		}

		return \DataTables::of($nested_data)->make(true);		
    }

    public function getContractorDocket($year,$month,$day)
    {

    	$id = Auth::guard('contractor')->id();
		$user = Staff::with('StaffDetail')->find($id);

		if($year == 'null' && $month == 'null' && $day == 'null'){
    		$docket= Docket::with('WorkOrder')->orderBy('created_at', 'desc')->get();
    	}else if($year != 'null' && $month == 'null' && $day == 'null'){

    		$timestamp = $year."-01-01 00:00:00.000Z";
			$masa = strtotime($timestamp);
			$go = Carbon::createFromTimestamp($masa);
			$go2 = Carbon::createFromTimestamp($masa);
			$end_year = $go2->endOfYear();

			$docket = Docket::with('WorkOrder')->orderBy('created_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();

    	}else if($year != 'null' && $month != 'null' && $day == 'null'){

    		$timestamp = $year."-".$month."-01 00:00:00.000Z";
			$masa = strtotime($timestamp);
			$go = Carbon::createFromTimestamp($masa);
			$go2 = Carbon::createFromTimestamp($masa);
			$end_year = $go2->endOfMonth();

			$docket = Docket::with('WorkOrder')->orderBy('created_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
    	}

    	$i = 0; $n1 = ''; $building = '';
    	$curr = Carbon::now()->getTimestamp();
    	$tempD = array();
		$nested_data = array();
    	if(!empty($docket)){
    		foreach($docket as $d){

    			if(!empty($d->WorkOrder->_id)){
    				if($d->WorkOrder->contractor_id == $user->StaffDetail->company_id){
    					$i++; 
		    			$form = Form::with('Subscriber','PackageDetail')->where('_id',$d->WorkOrder->_id)->first();
		    			$company = Company::where('_id', $d->WorkOrder->contractor_id)->first();
		    			$installer = Staff::with('StaffDetail')->withTrashed()->where('_id', $d->installer_id)->first();

		    			if($form->type_application == 'R'){
		    				$building = $form->Subscriber->building_name;
		    			}else if($form->type_application == 'B'){
		    				$building = $form->Subscriber->company_name;
		    			}

		    			$reg_time = $d->created_at;
						$expiry_date = $reg_time->addDays(3);    
						$expiry_date = $expiry_date->getTimestamp(); 

						if($curr < $expiry_date) {
							$n1 = "New/";
						} 

						array_push($tempD, array(
			    			'index' => $n1.$i,
			    			'docket_id' => $d->docket_id,
			    			'work_order_id' => $d->work_order_id,
			    			'nature_work' => $d->nature_work,
			    			'contractor_id' => $company->name,
			    			'installer_name' => $installer->StaffDetail->name,
			    			'customer_id' => $building,
			    			'installer_id' => $installer->StaffDetail->phone,
			    			'end_job' => date('d/m/Y H:i', strtotime($d->end_job)),
			    			'rating' => round((($d->Rating1 + $d->Rating2 + $d->Rating3)/15) * 100)."%",
		                    'action' => $d->docket_id.'/'.$d->work_order_id
			    		));
					}
    			}
    		}
    	}

    	foreach($tempD as $t){
    		array_push($nested_data, array(
    			'index' => $t['index'],
    			'docket_id' => $t['docket_id'],
    			'work_order_id' => $t['work_order_id'],
    			'nature_work' => $t['nature_work'],
    			'contractor_id' => $t['contractor_id'],
    			'installer_name' => $t['installer_name'],
    			'customer_id' => $t['customer_id'],
    			'installer_id' => $t['installer_id'],
    			'end_job' => $t['end_job'],
    			'rating' =>$t['rating'],
                'action' =>$t['action'],
    		));
    	}

		return \DataTables::of($nested_data)->make(true);
    }

    public function filterContractorDocket($year, $month)
    {
        $year = $year;
		$month = $month;
		$nested_data = array();
		$arr = array();
		$i=0;

		if($year=='null' && $month=='null')
		{
			$docket= DocketLaravel::orderBy('created_at', 'desc')->get();

			if(!empty($docket))
			{
				foreach ($docket as $a)
				{				
					$customer = Subscriber::where('_id', $a->customer_id)->first();
					$business = Business::where('_id', $a->customer_id)->first();				

					if(!empty($customer))
					{
						$arr[]=$a;
					}

					if(!empty($business))
					{
						$arr[]=$a;
					}										
				}
			}

			foreach($arr as $b)
			{
				$i++; $n1 = '';

				$tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
				$contractor = $tempContractor->name;

				$tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();

				$tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
				$tempBusiness = Business::where('_id', $b->customer_id)->first();

				if(!empty($tempCustomer))
				{
					$building = $tempCustomer->building;
				}
				else
				{
					$building = $tempBusiness->company_name;
				}

				if(!empty($tempInstaller))
				{
					array_push($nested_data, array(
						'index' => $n1.$i,
						'docket_id' => $b->docket_id,
						'work_order_id' => $b->work_order_id,
						'nature_work' => $b->nature_work,
						'contractor_id' => $contractor,
						'installer_name' => $tempInstaller->name,
						'customer_id' => $building,
						'installer_id' => $tempInstaller->phone,
						'end_job' => $b->end_job,
						'rating' => $b->Rating1
					));
				}
				else
				{
					array_push($nested_data, array(
						'index' => $n1.$i,
						'docket_id' => $a->docket_id,
						'work_order_id' => $a->work_order_id,
						'nature_work' => $a->nature_work,
						'contractor_id' => $contractor,
						'installer_name' => '',
						'customer_id' => $building,
						'installer_id' => '',
						'end_job' => $a->end_job,
						'rating' => $a->Rating1
					));
				}	
			}
		}

		elseif($year!='' && $month=='null')
		{
			$timestamp = $year."-01-01 00:00:00.000Z";
			$masa = strtotime($timestamp);
			$go = Carbon::createFromTimestamp($masa);
			$go2 = Carbon::createFromTimestamp($masa);
			$end_year = $go2->endOfYear();
			// $time = Carbon::createFromTimeString($timestamp);
			// echo $month;

			$docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
			if(!empty($docket))
			{
				foreach ($docket as $a)
				{				
					$customer = Subscriber::where('_id', $a->customer_id)->first();
					$business = Business::where('_id', $a->customer_id)->first();				

					if(!empty($customer))
					{
						$arr[]=$a;
					}

					if(!empty($business))
					{
						$arr[]=$a;
					}										
				}
			}

			foreach($arr as $b)
			{
				$i++; $n1 = '';

				$tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
				$contractor = $tempContractor->name;

				$tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();

				$tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
				$tempBusiness = Business::where('_id', $b->customer_id)->first();

				if(!empty($tempCustomer))
				{
					$building = $tempCustomer->building;
				}
				else
				{
					$building = $tempBusiness->company_name;
				}

				if(!empty($tempInstaller))
				{
					array_push($nested_data, array(
						'index' => $n1.$i,
						'docket_id' => $b->docket_id,
						'work_order_id' => $b->work_order_id,
						'nature_work' => $b->nature_work,
						'contractor_id' => $contractor,
						'installer_name' => $tempInstaller->name,
						'customer_id' => $building,
						'installer_id' => $tempInstaller->phone,
						'end_job' => $b->end_job,
						'rating' => $b->Rating1
					));
				}
				else
				{
					array_push($nested_data, array(
						'index' => $n1.$i,
						'docket_id' => $a->docket_id,
						'work_order_id' => $a->work_order_id,
						'nature_work' => $a->nature_work,
						'contractor_id' => $contractor,
						'installer_name' => '',
						'customer_id' => $building,
						'installer_id' => '',
						'end_job' => $a->end_job,
						'rating' => $a->Rating1
					));
				}	
			}
		}

		elseif($year!='' && $month !='')
		{
			$timestamp = $year."-".$month."-01 00:00:00.000Z";
			$masa = strtotime($timestamp);
			$go = Carbon::createFromTimestamp($masa);
			$go2 = Carbon::createFromTimestamp($masa);
			$end_year = $go2->endOfMonth();
			// $time = Carbon::createFromTimeString($timestamp);

			$docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
			if(!empty($docket))
			{
				foreach ($docket as $a)
				{				
					$customer = Subscriber::where('_id', $a->customer_id)->first();
					$business = Business::where('_id', $a->customer_id)->first();				

					if(!empty($customer))
					{
						$arr[]=$a;
					}

					if(!empty($business))
					{
						$arr[]=$a;
					}										
				}
			}

			foreach($arr as $b)
			{
				$i++; $n1 = '';

				$tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
				$contractor = $tempContractor->name;

				$tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();

				$tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
				$tempBusiness = Business::where('_id', $b->customer_id)->first();

				if(!empty($tempCustomer))
				{
					$building = $tempCustomer->building;
				}
				else
				{
					$building = $tempBusiness->company_name;
				}

				if(!empty($tempInstaller))
				{
					array_push($nested_data, array(
						'index' => $n1.$i,
						'docket_id' => $b->docket_id,
						'work_order_id' => $b->work_order_id,
						'nature_work' => $b->nature_work,
						'contractor_id' => $contractor,
						'installer_name' => $tempInstaller->name,
						'customer_id' => $building,
						'installer_id' => $tempInstaller->phone,
						'end_job' => $b->end_job,
						'rating' => $b->Rating1
					));
				}
				// else
				// {
				// 	array_push($nested_data, array(
				// 		'index' => $n1.$i,
				// 		'docket_id' => $a->docket_id,
				// 		'work_order_id' => $a->work_order_id,
				// 		'nature_work' => $a->nature_work,
				// 		'contractor_id' => $contractor,
				// 		'installer_name' => '',
				// 		'customer_id' => $building,
				// 		'installer_id' => '',
				// 		'end_job' => $a->end_job,
				// 		'rating' => $a->Rating1
				// 	));
				// }	
			}
		}

		return \DataTables::of($nested_data)->make(true);
    }

    public function getContractorTeam()
    {
    	$id = Auth::guard('contractor')->id();
		$user = StaffLaravel::with('StaffDetailLaravel')->find($id);
        $agent = StaffLaravel::with('CompanyLaravel','StaffDetailLaravel')->where('roles_access','Installer')->get();
        
        $i = 0;
        $nested_data = array();
        foreach ($agent as $a)
        {
            $i++; $n1 = '';              
            
            if($a->company_id == $user->StaffDetailLaravel->company_id){
            	array_push($nested_data, array(
            		'index' => $n1.$i,
            		'user_pic' => $a->StaffDetailLaravel->user_pic,
            		'name' => $a->StaffDetailLaravel->name,
            		'ic' => $a->StaffDetailLaravel->ic,
            		'email' => $a->StaffDetailLaravel->email,
            		'phone' => $a->StaffDetailLaravel->phone,
            		'company_id' => $a->CompanyLaravel->name,
            		'position' => $a->StaffDetailLaravel->position,
            		'action' => $a->_id
            	));

            }
        }
        return \DataTables::of($nested_data)->make(true);
    }
    
    public function addContractorTeam()
    {
        return view('contractor.add_contractor');
    }

    public function requestAddContractorTeam(Request $request)
    {
        $validator = Validator::make($request->all(), [
			'password' => 'min:8|confirmed',
		]);

		if ($validator->fails()) {
			return redirect()->back()->withInput()->withErrors($validator);
		}

		$user = StaffLaravel::where('email', $request->get('email'))->first();
        if(empty($user))
        {
			$pass = $this->encryptPassword('encrypt', $request->get('password'));

			$dealerD = new StaffDetailLaravel;		
			$dealerD->name = $request->get('aname');
			$dealerD->ic =  $request->get('ic');
			$dealerD->email = $request->get('email');
			$dealerD->phone = $request->get('phone');
			$dealerD->company_id = $request->get('vendor');
			$dealerD->color = '';
			$dealerD->position = 'Installer';
			$dealerD->password = $pass;
			$dealerD->user_pic = 'assets/avatar/user.png';	

			// Save Login Dealer
			$loginD = new StaffLaravel;
			$loginD->email = $request->get('email');
			$loginD->password = $pass;
			$loginD->roles_access = 'Installer';
			$loginD->company_id = $request->get('vendor');
			$loginD->last_login_at	= '';
			$loginD->last_login_ip	= ''; 

			// $loginData = [
			// 	'email' => $request->get('email'),
			// 	'password' => $pass,
			// 	'roles_access' => 'Sales',
			// 	'last_login_at'	=> '',
			// 	'last_login_ip'	=> '', 
			// ];

            $company = CompanyLaravel::where('_id', $request->get('vendor'))->first();
			$company->stafflaravel()->save($loginD);		
			$loginD->staffdetaillaravel()->save($dealerD);

			return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
		}
		else 
		{
			return redirect()->back()->withInput()->withErrors('Email already exist!');
		}
	}

	public function generateDocketPDF($do)
    {
    	$id = Auth::guard('contractor')->id();
		$user = Staff::with('StaffDetail')->find($id);
        $docket = Docket::with('WorkOrder')->where('docket_id',$do)->first();
        if(!empty($docket)){
            $form = Form::with('Subscriber','PackageDetail')->where('_id',$docket->WorkOrder->_id)->first();

            $address = '';
            if($form->type_application == 'R'){
                if($form->Subscriber->street != ''){
                    $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
                }else {
                    $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
                }

            }else if($form->type_application == 'B'){

                if($form->Subscriber->unit_no != ''){
                    $address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
                }else {
                    $address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
                }

            }

            $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
            $installer = Staff::with('StaffDetail')->where('_id',$docket->installer_id)->withTrashed()->first();

            if(empty($product)){
                $product = 'RMbps';
            }else {
                $product = $product->package_name;
            }

            $dateTime =  Carbon::parse($docket->WorkOrder->dateTimeStart)->toDateTimeString();
            $edateTime =  Carbon::parse($docket->end_job)->toDateTimeString();

            $pdf = PDF::loadView('pdf.docket-pdf',compact('docket','form','address','product','installer','dateTime','edateTime'));
            $pdf->setPaper('A4', 'potrait');
            return $pdf->stream();
        }
    }
}