<?php 
function returnStringId($id_zam){ //použité
	if (strlen($id_zam)==1) {
			return "0000".$id_zam;
	} elseif (strlen($id_zam)==2) {
			return "000".$id_zam;
	} elseif (strlen($id_zam)==3) {
			return "00".$id_zam;
	} elseif (strlen($id_zam)==4) {
			return "0".$id_zam;
	} else {
			echo $id_zam;
	}
}

function canILogin($id_emp, $TrunkDate, $endDate) { //použité
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee-> canILogin($id_emp, $TrunkDate, $endDate);
} 

function amILoggedElseWhere($id_emp, $id_inv, $startDate, $endDate){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> amILoggedElseWhere($id_emp, $id_inv, $startDate, $endDate);	
}

function isDriver($id_inv,$id_emp){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> isDriver($id_inv,$id_emp);	
}

function getCarInfo($id_car){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getCarInfo($id_car);		
}

function getEmployeeTimeReport($id_rep,$id_emp){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getEmployeeTimeReport($id_rep,$id_emp);	
}

function getReportDateTimeStart($id_rep,$id_lead){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getReportDateTimeStart($id_rep,$id_lead);		
}

function getEmployeeInfo($id_emp){ 
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getEmployeeInfo($id_emp);	
}

function getReservationEmployees($id_rez){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getReservationEmployees($id_rez);	
}

function getReportEmployees($id_inv,$id_lead){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getInventoryEmployees($id_inv,$id_lead);	
}

function getReportEmployeesId($id_rep){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getReportEmployeesId($id_rep);	
}

function getReportDrivers($id_inv){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getInventoryReservations($id_inv);	
}

function getReportInventory($id_inv){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getCustomerInfo($id_inv);	
}

function calculateLeadSalary($date_diff,$salaryPerHoursLead,$driveTime){
	$leadSalary = ((((intval(date('H',strtotime($driveTime)))*3600)+(intval(date('i',strtotime($driveTime)))*60)+(intval(date('s',strtotime($driveTime)))))*2)*($salaryPerHoursLead->Vyplata_veduci_inventury/3600))+(($date_diff->d*86400)+($date_diff->h*3600)+($date_diff->i*60))*($salaryPerHoursLead->Vyplata_veduci_inventury/3600);
	return $leadSalary;

}

function calculateDriverSalary($DriverDateTimeStart,$DriverDateTimeEnd,$inventoryDateTimeEnd,$salaryPerHourDriver,$salaryPerHourEmployee,$driveTime){

	$salaryPerSecondDriver = ($salaryPerHourDriver->Vyplata_sofer)/3600;
	$salaryPerSecondEmployee= ($salaryPerHourEmployee->Vyplata_zamestnanec)/3600;
	$date_diff_work = date_diff($DriverDateTimeStart,$DriverDateTimeEnd);
	$date_diff_pause = date_diff($DriverDateTimeEnd,$inventoryDateTimeEnd);
	
	$salaryDriver = ((((intval(date('H',strtotime($driveTime)))*3600)+(intval(date('i',strtotime($driveTime)))*60)+(intval(date('s',strtotime($driveTime)))))*2)*$salaryPerSecondDriver)+((($date_diff_work->d*86400)+($date_diff_work->h*3600)+($date_diff_work->i*60))*$salaryPerSecondEmployee)+((($date_diff_pause->d*86400)+($date_diff_pause->h*3600)+($date_diff_pause->i*60))*($salaryPerSecondDriver));
	return $salaryDriver;
}

function calculateEmployeeSalary($EmployeeDateTimeStart,$EmployeeDateTimeEnd,$EmployeePause,$salaryPerHourEmployee,$salaryPerKilometer,$inventoryKilometers){
	$date_diff_work = date_diff($EmployeeDateTimeStart,$EmployeeDateTimeEnd);
	return (($date_diff_work->d*86400)+($date_diff_work->h*3600)+($date_diff_work->i*60))*($salaryPerHourEmployee->Vyplata_zamestnanec/3600)+(($inventoryKilometers*$salaryPerKilometer->Vyplata_kilometer)*2);
}
function getLeadEmployeeID($id_inv){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> getLeadEmployeeID($id_inv);	
}

function canIDownloadReport($id_inv){
	$CI =& get_instance();	
	$CI->load->model('model_lead_employee');
	return $CI -> model_lead_employee -> canIDownloadReport($id_inv);	
}

function drawInventory($active,$inventory,$id_emp){
				echo "<div class='vehicle_div' align='left'>"; 			
				echo "<table class='vehicle_table' style='margin-left:35px; padding-top:5px;margin-top:0px; margin-bottom:0px;'>";
					echo "<tr>";
				switch($active){
					case 1:
						if($inventory->ID_Veduci!=NULL OR amILoggedElseWhere($id_emp, $inventory->ID_INV, $inventory->Datum_zaciatku,$inventory->Datum_konca)==TRUE){
							echo "<td width='50px' style='text-align: center'>";
							echo "<img src='".base_url('pictures/red_diod.png')."' height='20%'>";				
						}else{
							echo "<td width='50px' style='text-align: center'>";
							echo "<img src='".base_url('pictures/green_diod.png')."' height='20%'>";	
						}	 
					break;						
					case 2:
						echo "<td width='50px' style='text-align: center'>";
						echo "<img src='".base_url('pictures/green_diod.png')."' height='20%'>";
					break;
						
					case 3:
						echo "<td width='50px'>";																				
					break;						
					case 4:
						if($inventory->ID_Report==NULL){
							echo "<td width='50px'>"; 
							echo "<img src='".base_url('pictures/Exclamation.png')."' height='8%'  style='margin-left:18px;'>";							
						}else{
							echo "<td width='50px'>";															
						}
					break;
					echo "</td>";
				}	
//telo inventury					
					echo "<td width='100px' style='text-align: center; margin-left:0px'>";
						echo returnStringId($inventory->ID_INV);
					echo "</td>";
					
					echo "<td width='160px' style='text-align: center'>";
						echo "<span id='zak".$inventory->ID_INV."'>";
						echo $inventory->Nazov_spolocnosti.", ".$inventory->Mesto;
					echo "</span></td>";
					
					echo "<td width='90px' style='text-align: center'>";
						echo $inventory->Pocet_zamestnancov;
					echo "</td>";
					
					echo "<td width='120px' style='text-align: center'>";
						if ($inventory->ID_Veduci==NULL){
							echo "-neurčené-";
						} else {
							echo $inventory->Meno."&nbsp".$inventory->Priezvisko;	
						}
					echo "</td>";
					
					echo "<td width='160px' style='text-align: center'>";
						echo "<span id='invStart".$inventory->ID_INV."'>";
						echo date('d.m.Y G:i',strtotime($inventory->Datum_zaciatku));
					echo "</span></td>";
					
					echo "<td width='160px' style='text-align: center'>";
						echo date('d.m.Y G:i',strtotime($inventory->Datum_konca));
					echo "</td>";
										
					echo "<td style='font-style: italic; font-size:12px; padding-right:10px'>";
//posledny odstavec, tlacidla, hlasky					
					switch ($active){
						case 1:
							if($inventory->ID_Veduci==$id_emp){
								echo "Na túto inventúru ste už prihlasený.";
							}elseif($inventory->ID_Veduci!=NULL){
								echo "Inventúra už je obsadená.";	
							}elseif(amILoggedElseWhere($id_emp,$inventory->ID_INV, $inventory->Datum_zaciatku,$inventory->Datum_konca)==TRUE){
								echo "Túto dobu ste už prihlásený na inej inventúre.";	
							}else{
								echo '<div class="button-group">';
								echo '<a onclick="showConfirmBoxLogOn('.$inventory->ID_INV.')" class="button">Prihlásiť</a>';
								echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/1/'.$inventory->ID_INV).'" class="button">Detail</a>';								
								echo '</div>';
							}	 
						break;
						case 2: 
								echo '<div class="button-group">';
								echo '<a onclick="showConfirmBoxLogOn('.$inventory->ID_INV.')" class="button">Prihlásiť</a>';
								echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/1/'.$inventory->ID_INV).'" class="button">Detail</a>';								
								echo '</div>';
						break;
						case 3:
								echo '<div class="button-group">'; 
								echo '<a onclick="showConfirmBoxLogOut('.$inventory->ID_INV.')" class="button">&nbsp&nbsp&nbspOdhlásiť&nbsp&nbsp&nbsp</a>';
								echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/1/'.$inventory->ID_INV).'" class="button">Detail</a>';							 
								if(canIDownloadReport($inventory->ID_INV)==FALSE){
									echo '<a href="'.base_url('lead_employee/createExcelReport/'.$inventory->ID_INV).'" class="button">Stiahnúť report</a>';																	
								}
								echo '</div>';
						break;
						case 4: 
							if ($inventory->ID_Report==NULL){	
								echo '<div class="button-group">';			
								echo '<a href="'.base_url('lead_employee/redirectAddReport/1/1/'.$inventory->ID_INV).'" class="button danger">Vložiť report</a>';
								echo '<a href="'.base_url('lead_employee/createExcelReport/'.$inventory->ID_INV).'" class="button danger">Stiahnúť report</a>';
								echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/1/'.$inventory->ID_INV).'" class="button">Detail</a>';
							} else {
								echo '<div class="button-group" style="margin-left:90px">';
								if(date("Y-m",time())==substr($inventory->Datum_konca,0,7)){							
									echo '<a href="'.base_url('lead_employee/redirectEditReport/1/'.$inventory->ID_INV).'" class="button danger">&nbspUpraviť report&nbsp</a>';									
								}else{
									echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/3/'.$inventory->ID_INV).'" class="button">Zobraziť report</a>';
								}
									echo '<a href="'.base_url('lead_employee/redirectLeadEmployeeInventoryDetail/'.$active.'/1/'.$inventory->ID_INV).'" class="button">Detail</a>';
							}
						break;					
					}	
					echo "</td>";
				echo "</tr>";
				echo "</table>";
			echo "</div>";					
			}



?>