<?php
class Summeryreport extends Admin_Controller {

	function __construct() {

		parent::__construct(TRUE);
		$this->load->model('summeryreport_model','',TRUE);
                $this->load->helper('uri');
                $this->load->helper('mcb_date');
                $this->load->library("pagination");
                $this->load->helper('url');

	}

	function index()
        {
                $this->load->view('summeryreport_view');

	}

        function loaddata($cmd='false')
	{
               $arrData= array();
               $user = $this->session->userdata('user_id');

               if($cmd=="export")
               {
               	$device = $this->input->get('device');
               	$sdate = $this->input->get('sdate');
               	$edate = $this->input->get('edate');
               }
               else
               {
               	$device = $this->input->post('device');
               	$sdate = $this->input->post('sdate');
               	$edate = $this->input->post('edate');
               }

               //Get all rides report
               //$num_rows = $this->summeryreport_model->get_all_rides_report_count($sdate,$edate,$device);
               $all_data = $this->summeryreport_model->get_all_rides_report($sdate,$edate,$device);

               $assetDetail = $this->summeryreport_model->assetDetail($device);
               $driver_name = $assetDetail[0]["driver_name"];



              // echo "<pre>"; print_r($all_data);die();
                        $fitr .="<tr style='background-color:#DDE7FF; align='left'>";
                        //$fitr.="<th colspan='10' style='font-size:12px'  align='left'>Trajets : </th>";
                        $fitr .="</tr>";
                        $fitr .="<tr style='background-color:#DDE7FF;font-size:Small;' align='left'>";
			//$fitr.="<th style='font-size:11px' width='3%'>Id</th>";
            $fitr.="<th style='font-size:11px' width='7%'>Chauffeur</th>";
			$fitr.="<th style='font-size:11px' width='15%'>Départ Heure</th>";
			$fitr.="<th style='font-size:11px' width='18%'>Adresse Départ</th>";
			$fitr.="<th style='font-size:11px' width='15%'>Arrivée Heure</th>";
			$fitr.="<th style='font-size:11px' width='18%'>Adresse Arrivée</th>";
                        $fitr.="<th style='font-size:11px'>Durée du trajet</th>";
                       // $fitr.="<th style='font-size:11px'>Temps d'arrêt</th>";
                        $fitr.="<th style='font-size:11px'>Vitesse Max</th>";
                        $fitr.="<th style='font-size:11px'>".$this->lang->line('Avg. Speed')."</th>";
                        $fitr.="<th style='font-size:11px'>Distance parcourue</th>";
			$fitr .="</tr>";
			$id=1;
			/*if($_SERVER["REMOTE_ADDR"] == "41.250.34.230")
			{
				echo "<pre>";
				print_r($all_data);
				echo "</pre>";
				die();
			}*/
			$modif = array("Ã©","Ã","Ã´","Ã¢","Ã®","Ã¨","Ãª");
			$amodif = array("&eacute;","&agrave;","&ocirc;","&acirc;","&icirc;","&egrave;","&ecirc;");
			for ($iLoop = 0; $iLoop < count($all_data); $iLoop++){
                                       $off_time=$all_data[$iLoop]['ignition_off'];
				        $RECORD .="<tr align='left' style='background-color:#F5F5F5;font-size:Small;'>";
					//$RECORD.="<td>".$id."</td>";
				        $RECORD.="<td>".$driver_name."</td>";

				        $all_data[$iLoop]['ignition_on_address'] = mb_convert_encoding( $all_data[$iLoop]['ignition_on_address'],'ISO-8859-15','utf-8');
				        $all_data[$iLoop]['ignition_off_address']= mb_convert_encoding( $all_data[$iLoop]['ignition_off_address'],'ISO-8859-15','utf-8');

					$RECORD.="<td>".date('d-m-Y h:i:s',strtotime($all_data[$iLoop]['ignition_on']))."</td>";
                                        $RECORD.="<td>".$all_data[$iLoop]['ignition_on_address']."</td>";
                                        if($off_time){
                                        $RECORD.="<td>".date('d-m-Y h:i:s',strtotime($all_data[$iLoop]['ignition_off']))."</td>";
                                        }else{
                                        $RECORD.="<td>".""."</td>";
                                        }
					$RECORD.="<td>".$all_data[$iLoop]['ignition_off_address']."</td>";
                                        if($off_time){
                                        $RECORD.="<td>".$all_data[$iLoop]['duration']." min</td>";
                                        }else{
                                        $RECORD.="<td>".""."</td>";
                                        }
                                       // $RECORD.="<td>".""."</td>";


			 						if(!empty($all_data[$iLoop]['max_speed']))
                                        {
                                        		$RECORD.="<td>".$all_data[$iLoop]['max_speed']." km/h</td>";
                                        }
                                        else
                                        {
                                        		$RECORD.="<td>".""."</td>";
                                        }
                                        $round=round($all_data[$iLoop]['avg_speed'],2);
                                        if($round){
                                        	$RECORD.="<td>".$round." km/h</td>";
                                        }else{
                                        	$RECORD.="<td>".""."</td>";
                                        }
                                       /* if($off_time){
                                        $RECORD.="<td>".$all_data[$iLoop]['distance']." km</td>";
                                        }else{
                                        $RECORD.="<td>".""."</td>";
                                        }*/
                                        $RECORD.="<td>".$all_data[$iLoop]['distance']." km</td>";
					$RECORD .="</tr>";
                                        $id++;

			}

                //end
                //Get distance
                $distance_data = $this->summeryreport_model->get_distance($sdate,$edate,$device);
                $countdistance_data=count($distance_data);
                if($countdistance_data){

                foreach($distance_data as $dis_row){
                    $arrData['distance']=$dis_row->distance;
                   // $responce->rows[$i]=$arrData['distance'];
                }
               }
                // Get Asset details
                $asset_data = $this->summeryreport_model->get_asset_summery($device);
                $countasset_data=count($asset_data);
                if($countasset_data){
                foreach($asset_data as $asset_row){
                    $arrData['id']=$asset_row->id;
                    $arrData['assets_name']=$asset_row->assets_name;
                    $arrData['device_desc']=$asset_row->device_desc;
                    $arrData['asset_model']=$asset_row->asset_model;
                    $arrData['asset_brand']=$asset_row->asset_brand;
                    $arrData['fuel_type']=$asset_row->fuel_type;
                    $arrData['asset_year']=$asset_row->asset_year;
                    $arrData['asset_sieges']=$asset_row->asset_sieges;
                    $arrData['fuel_consumption']=$asset_row->fuel_consumption;
                    $arrData['carbon_emission']=$asset_row->carbon_emission;
                    $arrData['gasoline_costs']=$asset_row->gasoline_costs;
                 }
                 }

                 //Get max and avg speed
                 $speed_data = $this->summeryreport_model->get_max_avg_speed($sdate,$edate,$device);
                 $countspeed_data=count($speed_data);
                 if($countspeed_data){
                 foreach($speed_data as $speed_row){
                    $arrData['maxspeed']=$speed_row->maxspeed;
                    $arrData['avgspeed']=$speed_row->avgspeed;
                 }
                 }

                 //Get departure and arrival address
                 $addr_data1 = $this->summeryreport_model->get_departure_address($sdate,$edate,$device);
                 $countaddr_data1=count($addr_data1);
                if($countaddr_data1){
                 foreach($addr_data1 as $addr_row1){
                    $arrData['departure_addr']=$addr_row1->address;
                 }
                 }

                 $addr_data2 = $this->summeryreport_model->get_arrival_address($sdate,$edate,$device);
                 $countaddr_data2=count($addr_data2);
                 if($countaddr_data2){
                 foreach($addr_data2 as $addr_row2){
                    $arrData['arrival_addr']=$addr_row2->address;
                 }
                 }
                 //get driving time
                $time_data = $this->summeryreport_model->driving_time($sdate,$edate,$device);
                 $counttime_data=count($time_data);
                if($counttime_data){
                foreach($time_data as $time_row){
                $totalduration=$time_row->duration;
               // echo $totalduration;
                if($totalduration){
                    $strtotime=str_replace(".000000","",$totalduration);
                    $arrData['driving_time']=$strtotime;
                   // echo $strtotime;
                }
                }
                }
                //get total stop time
                $time_data = $this->summeryreport_model->time_off($sdate,$edate,$device);


                $counttime_data=count($time_data);
                if($counttime_data){
                foreach($time_data as $time_row){
                $totalduration=$time_row->totalduration;
               // echo $totalduration;
                if($totalduration){
                    $strtotime=str_replace(".000000","",$totalduration);
                    $arrData['stop_time']=$strtotime;
                   // echo $strtotime;
                }
                }
                }
                // get number of rides
                $ride_data = $this->summeryreport_model->get_rides($sdate,$edate,$device);
                $countride_data=count($ride_data);
                if($countride_data){
                foreach($ride_data as $ride_row){
                   $arrData['num_of_ride']=$ride_row->num_of_ride;
                }
                }
                else{
                    $arrData['num_of_ride']="No Data Found";
                }

                $result .="<a href='#' id='exportDataBtn'>Exporter</a>";
                $result .="<table border='0' width='100%' id='tbl_rides_report' class='tbl_rides_report'>";
                $result .= $fitr;
                $result .= $RECORD ;
                $result .= "</table>";
                $result .= "<table border='1' width='100%' id='tbl_summery' class='summery'>";
                $result .= "<tr class='header_blue_smoke'><td colspan='4' align='left' class='size'><img style='margin-right:6px' src='../assets/images/prev.png'></img>".$this->lang->line('Summary')." : </td></tr>";
                $result .= "<tr><td valign='top' class='blue_smoke' width='20%'>".$this->lang->line('Asset Name')."</td><td class='white_smoke' width='25%'>".$arrData['assets_name']."</td><td valign='top' class='blue_smoke' width='20%'>".$this->lang->line('Vehicle Number')."</td><td width='25%' align='left' class='white_smoke'>".$arrData['device_desc']."</td></tr>";
                //$result .= "<tr><td valign='top' class='blue_smoke'>".$this->lang->line('Brand')."</td><td class='white_smoke' align='left' >".$arrData['asset_brand']."</td><td valign='top' class='blue_smoke'>".$this->lang->line('Model')."</td><td class='white_smoke' align='left'>".$arrData['asset_model']."</td></tr>";
		//$result .= "<tr><td valign='top' class='blue_smoke'>".$this->lang->line('Year')."</td><td class='white_smoke' align='left'>".$arrData['asset_year']."</td><td valign='top' class='blue_smoke'>".$this->lang->line('Sieges')."</td><td class='white_smoke' align='left'>".$arrData['asset_sieges']."</td></tr>";
		//$result .= "<tr><td valign='top' class='blue_smoke'>".$this->lang->line('Fuel Type')."</td><td class='white_smoke' align='left'>".$arrData['fuel_type']."</td><td valign='top' class='blue_smoke'>".$this->lang->line('Fuel Consumption')."</td><td class='white_smoke' align='left'>".$arrData['fuel_consumption']."</td></tr>";
		//$result .= "<tr><td valign='top' class='blue_smoke'>".$this->lang->line('Gasoline Cost')."</td><td class='white_smoke' align='left'>".$arrData['gasoline_costs']."</td><td valign='top' class='blue_smoke'><img style='margin-right:5px;' src='../assets/images/Emis-co2.png'></img>".$this->lang->line('CO_Emission')."</td><td class='white_smoke' align='left'>".$arrData['carbon_emission']."</td></tr>";

                $result .= "<tr class=''><td colspan='4'>"." "."</td></tr>";
                $result .= "<tr class=''><td colspan='4'>"." "."</td></tr>";
                $result .= "<tr><td width='20%' valign='top' class='blue_smoke'>Distance Totale</td><td width='25%' class='white_smoke' align='left'>".$arrData['distance']." km</td><td width='20%' valign='top' class='blue_smoke'>Nombre de trajets</td><td width='25%' class='white_smoke' align='left'>".$arrData['num_of_ride']."</td></tr>";
		$result .= "<tr><td valign='top' class='blue_smoke'>  Vitesse Max de la journée (KM/H)</td><td class='white_smoke' align='left'>".$arrData['maxspeed']." km/h</td><td valign='top' class='blue_smoke'>Vitesse moyenne (KM/H)</td><td class='white_smoke' align='left'>".round($arrData['avgspeed'],2)." km/h</td></tr>";
		$result .= "<tr><td valign='top' class='blue_smoke'> Temps de conduite journalier</td><td class='white_smoke' align='left'>".$arrData['driving_time']."</td><td valign='top' class='blue_smoke'>Temps d'arrêt journalier</td><td class='white_smoke' align='left'>".$arrData['stop_time']."</td></tr>";

                $result .= "<tr class=''><td colspan='4'>"." "."</td></tr>";
                $result .= "<tr class=''><td colspan='4'>"." "."</td></tr>";
                $result .= "<tr><td valign='top' width='20%' class='blue_smoke'>Adresse Départ</td><td class='white_smoke' width='25%' align='left'>".$arrData['departure_addr']."</td><td valign='top' width='20%' class='blue_smoke'>Adresse Arrivée</td><td class='white_smoke' width='25%' align='left'>".$arrData['arrival_addr']."</td></tr>";


                $result .="<script>";
                $result .="jQuery('#exportDataBtn').click(function() {";
				$result .="var sdate = $('#summery_report_sdate').val();";
				$result .="var edate = $('#summery_report_edate').val();";
                $result .="var device = $('#summery_reports_device').val();";
                $result .="var qrystr ='/export?sdate='+sdate+'&edate='+edate+'&device='+device;";
				$result .="document.location ='".base_url()."index.php/reports/summeryreport/loaddata'+qrystr;";
				$result .="});</script>";

                if($cmd=="export")
                {

                	header("Content-Type: application/vnd.ms-excel");
                	header("Content-Disposition: attachment; filename=rapport_resume". date("s").".xls");
                	$EXCEL = "";
                	$fitrexp="";

                	//session date & time format
                	$date_format = $this->session->userdata('date_format');
                	$time_format = $this->session->userdata('time_format');
                	$date="";
                	//$device="";
                	$fitrexp .="<tr>";
                	$fitrexp.="<th>Chauffeur</th>";
                	$fitrexp.="<th>Départ Heure</th>";
                	$fitrexp.="<th>Adresse Départ</th>";
                	$fitrexp.="<th>Arrivée Heure</th>";
                	$fitrexp.="<th>Adresse Arrivée</th>";
                	$fitrexp.="<th>Durée du trajet</th>";
                	$fitrexp.="<th>Vitesse Max/th>";
                	$fitrexp.="<th>Vitesse moyenne</th>";
                	$fitrexp.="<th>Distance parcourue</th>";

                	$fitrexp .="</tr>";




                	foreach($all_data as $data)
                	{

                		$data['ignition_on_address'] = mb_convert_encoding( $data['ignition_on_address'],'ISO-8859-15','utf-8');
                		$data['ignition_off_address']= mb_convert_encoding( $data['ignition_off_address'],'ISO-8859-15','utf-8');
                		$EXCEL.="<tr align='center'>";
                		$EXCEL.="<td>".$driver_name."</td>";
                		$EXCEL.="<td>&nbsp;".date('d-m-Y h:i:s',strtotime($data['ignition_on']))."</td>";
                		$EXCEL.="<td>&nbsp;".$data['ignition_on_address']."</td>";
                		$EXCEL.="<td>".date('d-m-Y h:i:s',strtotime($data['ignition_off']))."</td>";
                		$EXCEL.="<td>".$data['ignition_off_address']."</td>";
                		$EXCEL.="<td>".$data['duration']." min</td>";
                		$EXCEL.="<td>".$data['max_speed']." km/h</td>";

                		$round=round($data['avg_speed'],2);
                		$EXCEL.="<td>".$round."  km/h</td>";
                		$EXCEL.="<td>".$data['distance']." km</td>";
                		$EXCEL .="</tr>";
                		$date=date("$date_format $time_format", strtotime($stop_time));
                		$device_name = $arrData['device_desc'];
                	}

                	$sdt = date("$date_format $time_format", strtotime($sdate));
                	$edt = date("$date_format $time_format", strtotime($edate));
                	if($this->session->userdata('id')==1)
                		$count=3;
                	else
                		$count=2;

                	if($device == '')
                		$device = "ALL";
                	else
                		$device = $device_name;


                	echo "<table border='1'>";
                	echo "<tr><th colspan='9'>Résumé</th></tr>";
                	echo "<tr><th colspan='4'>Du</th><th colspan='5'>Au</th></tr>";
                	echo "<tr><th colspan='4'>&nbsp;$sdt</th><th colspan='5'>&nbsp;$edt</th></tr>";
                	echo $fitrexp;
                	echo $EXCEL;
                	echo "</table>";
                	die();
                }


                //$this->output->set_output(json_encode($responce));
                $this->output->set_output($result);
         }



}
?>