<?php 

class Model_manager extends CI_Model {	
	
	function __construct()
     {
          parent::__construct();
     } 
	 public function getAllInventories(){
		 $sql= 'SELECT * FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				ORDER BY inventura.Datum_zaciatku';
		 $query = $this->db->query($sql); 
         return $query->result();
	 }
	  
	 public function getAllActiveInventories(){
		 $sql= 'SELECT * FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE (inventura.Datum_zaciatku>NOW()) AND (inventura.status=1)
				ORDER BY inventura.Datum_zaciatku DESC';
		 $query = $this->db->query($sql); 
         return $query->result();
	 }
	 
	 public function getAllUnactiveInventories(){
		$sql= 'SELECT * FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE (inventura.Datum_zaciatku>NOW()) AND (inventura.status=0)
				ORDER BY inventura.Datum_zaciatku';
		 $query = $this->db->query($sql); 
         return $query->result(); 
	 }
	 
	 public function getAllArchiveInventories(){
		$sql= 'SELECT * FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE inventura.Datum_zaciatku<NOW()
				ORDER BY inventura.Datum_zaciatku ';
		 $query = $this->db->query($sql); 
         return $query->result(); 
	 }
	 public function getAllCustomers(){
		 $sql= 'SELECT ID_Zakaznik, Nazov_spolocnosti, Mesto, Ulica FROM zakaznik ORDER BY Nazov_spolocnosti' ;		 		
		 $query = $this->db->query($sql); 
         return $query->result();
	 }
	 
	 public function getCountCustomers(){
		 return $this->db->count_all('zakaznik');
	 }
	 
	 public function search($limit, $offset) {
		 $q = $this->db->select('ID_Zakaznik, Nazov_spolocnosti, Mesto, Ulica, Cislo')
		 				->from('zakaznik')
						->limit($limit, $offset);
		 $ret['rows'] = $q->get()->result();
		 
		 $q = $this->db->select('COUNT(*) as count', FALSE)
		 			   ->from('zakaznik');
		 $tmp = $q->get()->result();
		 $ret['num_rows'] = $tmp[0]->count;
		 
		 return $ret;
		 
	 }
	 	 
	 public function fetchCustomers($limit,$offset){
		 $this->db->limit($limit,$offset);
		 $query = $this->db->get('zakaznik');
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 
	 public function searchCustomer($Company){
		 $Company=strtoupper($Company);
		$sql = 'SELECT * 
				FROM zakaznik 
				WHERE UCASE(ID_Zakaznik) LIKE "%'.$Company.'%" OR UCASE(Nazov_spolocnosti) LIKE "%'. $Company .'%" OR UCASE(Mesto) LIKE "%'. $Company .'%" OR UCASE(Ulica) LIKE "%'. $Company .'%"';
		$query = $this->db->query($sql);		 
		 if($query->num_rows()>0){
			 return $query->result();
		 } else {
			 return $query->result();
		 }
	 }
	 
	 public function searchEmployee($searchStr, $mode){
		if ($mode==0){
			$sql = 'SELECT * 
					FROM zamestnanec 
					WHERE (Meno LIKE "%'. $searchStr .'%" OR Priezvisko LIKE "%'. $searchStr .'%" OR ID_ZAM LIKE "%'. $searchStr .'%" OR Mesto_zamestnanca LIKE "%'. $searchStr .'%" OR Ulica_zamestnanca LIKE "%'. $searchStr .'%" OR Email LIKE "%'. $searchStr .'%")';			
		} else {
			$sql = 'SELECT * 
					FROM zamestnanec 
					WHERE (Meno LIKE "%'. $searchStr .'%" OR Priezvisko LIKE "%'. $searchStr .'%" OR ID_ZAM LIKE "%'. $searchStr .'%" OR Mesto_zamestnanca LIKE "%'. $searchStr .'%" OR Ulica_zamestnanca LIKE "%'. $searchStr .'%" OR Email LIKE "%'. $searchStr .'%") AND ID_TYP_Z='. $mode;			
		}
		
		$query = $this->db->query($sql);		 
		 if($query->num_rows()>0){
			 return $query->result();	
		 } else { 
			 return $query->result();
		 }	 
	 }
	 
	 public function getCustomerInfo($id_cus){
		$sql = 'SELECT *
		 		FROM zakaznik 
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I 
				WHERE ID_Zakaznik=' . $id_cus;
		$query = $this->db->query($sql);
		return $query->row();		
	 } 
	
	public function CreateCustomer($nazov,$druh,$mesto,$ulica,$cislo,$psc,$suradnice){
		if($suradnice==NULL){
			$sql = 'INSERT INTO `zakaznik` (`ID_TYP_I`, `Nazov_spolocnosti`, `Mesto`, `Ulica`, `Cislo`, `PSC`, `GPS`) 
			    VALUES ("' . $druh . '","' . $nazov . '","' . $mesto . '","' . $ulica . '","' . $cislo . '","' . $psc . '", "00.000000,00.000000")';
		}else{
			$sql = 'INSERT INTO `zakaznik` (`ID_TYP_I`, `Nazov_spolocnosti`, `Mesto`, `Ulica`, `Cislo`, `PSC`, `GPS`) 
			    VALUES ("' . $druh . '","' . $nazov . '","' . $mesto . '","' . $ulica . '","' . $cislo . '","' . $psc . '","' . $suradnice . '")';	
		}			
		$this->db->query($sql);
		return $this->db->insert_id();
	}
    
	public function getNewCustomerInfo($nazov,$druh,$mesto,$ulica,$cislo,$psc){
		$sql = 'SELECT * FROM zakaznik
				WHERE (ID_TYP_I = ' . $druh .') AND (Nazov_spolocnosti = "' . $nazov . '" ) AND (Mesto = "'  . $mesto . '") AND (Ulica = "' . $ulica . '") AND (PSC="' . $psc . '")';	
		$query = $this->db->query($sql);
		return $query->row();
	}
	   
	
	public function CreateInventory($id_cus,$rozloha,$vzdialenost,$datumZaciatku,$datumKonca,$datumPrihlasovania,$casCesty){
		$sql = 'INSERT INTO `inventura` (`ID_Zakaznika`, `ID_TYP_R`, `Vzdialenost`, `Cas_cesty`, `Datum_prihlasovania`, `Datum_zaciatku`, `Datum_konca`)
				VALUES ("' . $id_cus . '","' . $rozloha . '","' . $vzdialenost . '","' . $casCesty . '","' . $datumPrihlasovania . '","' . $datumZaciatku . '","' . $datumKonca . '")';	
		$this->db->query($sql);
	}
	public function getInventoryInfo($id_cus,$rozloha,$datumZaciatku,$datumKonca,$datumPrihlasovania){
		$sql = 'SELECT * FROM inventura
				LEFT JOIN zakaznik ON inventura.ID_Zakaznika = zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I = zakaznik.ID_TYP_I
				WHERE inventura.ID_Zakaznika=' . $id_cus . ' AND inventura.ID_TYP_R=' . $rozloha . ' AND Datum_zaciatku="' . $datumZaciatku . '" AND Datum_konca="' . $datumKonca . '" AND Datum_prihlasovania="' .$datumPrihlasovania .'"';
		$query = $this->db->query($sql);
		return $query->row();
	}
	
	public function getCountAllEmployees(){
		$sql='SELECT COUNT(ID_ZAM) AS pocet FROM zamestnanec WHERE ID_ZAM!=0';
			$query = $this->db->query($sql);
			return $query->row()->pocet;			
	}
	public function getCountManagers(){
		$sql='SELECT COUNT(ID_ZAM) AS pocet FROM zamestnanec WHERE ID_TYP_Z=1';
			$query = $this->db->query($sql);
			return $query->row()->pocet;			
	}
	
	public function getCountLeadEmployees(){
		$sql='SELECT COUNT(ID_ZAM) AS pocet FROM zamestnanec WHERE ID_TYP_Z=2';
			$query = $this->db->query($sql);
			return $query->row()->pocet;			
	}
	public function getCountDrivers(){
		$sql='SELECT COUNT(ID_ZAM) AS pocet FROM zamestnanec WHERE ID_TYP_Z=3';
			$query = $this->db->query($sql);
			return $query->row()->pocet;			
	}
	public function getCountEmployees(){
		$sql='SELECT COUNT(ID_ZAM) AS pocet FROM zamestnanec WHERE ID_TYP_Z=4';
			$query = $this->db->query($sql);
			return $query->row()->pocet;			
	}
	
	
	public function fetchAllEmployees($limit,$offset){
		 $this->db->limit($limit,$offset);
		 $this->db->where('ID_ZAM >', 0);
		 $query = $this->db->get('zamestnanec');
		
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 
	 public function fetchManagers($limit,$offset){
		$query = $this->db->get_where('zamestnanec', 'ID_TYP_Z = 1' ,$limit,$offset);
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 public function fetchLeadEmployees($limit,$offset){
		$query = $this->db->get_where('zamestnanec', 'ID_TYP_Z = 2' ,$limit,$offset);
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 public function fetchDrivers($limit,$offset){
		$query = $this->db->get_where('zamestnanec', 'ID_TYP_Z = 3' ,$limit,$offset);
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 public function fetchEmployees($limit,$offset){
		 $query = $this->db->get_where('zamestnanec', 'ID_TYP_Z = 4' ,$limit,$offset);
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }
	 }
	 
	 public function createEmployee($FirstName,$LastName,$Email,$BirthDate,$Type, $prijaty){
		$sql='INSERT INTO `zamestnanec` (`ID_TYP_Z`, `Rodne_cislo`, `Meno`, `Priezvisko`, `Email`, `Prijaty`) 
			    VALUES ("' . $Type . '","' . $BirthDate . '","' . $FirstName . '","' . $LastName . '","' . $Email . '","' . $prijaty . '")';	 
	 	$this->db->query($sql);
	 }
	 
	 public function existsNewCustomer($companyName,$companyCity,$companyStreet){
		 $sql = 'SELECT ID_Zakaznik FROM zakaznik
		 		 WHERE UCASE(Nazov_spolocnosti)="'.$companyName.'" AND UCASE(Mesto)="'.$companyCity.'" AND UCASE(Ulica)="'.$companyStreet.'"';
		 $query = $this->db->query($sql);
		 if($query->num_rows()==0){
			 return FALSE;
		 }else{
			 return $query->row()->ID_Zakaznik;
		 }
	 }
	 
	 public function getCustomerDetail($id_cus){
		$sql='SELECT * FROM zakaznik 
			  LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
			  WHERE ID_Zakaznik='.$id_cus;
		$query = $this->db->query($sql); 
		return $query->row();
	 }
	 
	 public function saveCustomer($id_cus,$Name,$Type,$City,$Street,$Number,$PostCode,$coordinates){
		$sql = 'UPDATE zakaznik 
				SET ID_TYP_I="' . $Type . '", 
				    Nazov_spolocnosti="' . $Name . '",
					Mesto="' . $City . '",
					Ulica="' . $Street . '",
					Cislo="' . $Number . '",
					PSC="' . $PostCode . '",
					GPS="' . $coordinates . '"					
				WHERE ID_Zakaznik=' . $id_cus;
		$query = $this->db->query($sql);			 
	 }

	 public function getReservations(){
		 $sql = 'SELECT inventura.Datum_zaciatku, inventura.Datum_konca, rezervacia.ID_VOZ 
		 		 FROM rezervacia
				 LEFT JOIN inventura ON rezervacia.ID_INV=inventura.ID_INV
				 WHERE inventura.Datum_zaciatku>=NOW()';
		 return $this->db->query($sql)->result();		 			 
	 } 
	 
	 public function getCustomersInfo(){
		$sql = 'SELECT zakaznik.Nazov_spolocnosti, zakaznik.Mesto, zakaznik.Ulica, zakaznik.Cislo 
		 		FROM zakaznik';
		 return $this->db->query($sql)->result();	 
	 }
	 
	 public function saveNewInventoryNewCustomer($companyName, $companyCity, $companyStreet, $companyStreetNumer, $postCode, $companyType, $range, $inventoryType, $driveTime, $dateTimeJoin, $dateTimeStart, $dateTimeEnd, $vehicles){
		
		 $address = urlencode($companyStreet . ' ' . $companyStreetNumer . ' ' . $companyCity);
			$mapLink = "http://maps.google.com/maps/api/geocode/json?sensor=false&address=" . $address;
			$URL = curl_init();			
				curl_setopt($URL, CURLOPT_URL, $mapLink);
				curl_setopt($URL, CURLOPT_HEADER, 0);
				curl_setopt($URL, CURLOPT_RETURNTRANSFER, 1);
				
				$aData = curl_exec($URL);				
				curl_close($URL);
				$data = json_decode ( $aData );
				
		    if ($data=='ZERO_RESULTS'){
				$coordinates=null;
				 $sql = "INSERT INTO zakaznik (ID_TYP_I, Nazov_spolocnosti, Mesto, Ulica, Cislo, PSC, GPS) 
		 		 VALUES (".$companyType.",'".$companyName."','".$companyCity."','".$companyStreet."','".$companyStreetNumer."','".$postCode."','00.000000,00.000000')";		 
			} else {
				$coordinates = $data->results[0]->geometry->location->lat . ',' . $data->results[0]->geometry->location->lng;
				 $sql = "INSERT INTO zakaznik (ID_TYP_I, Nazov_spolocnosti, Mesto, Ulica, Cislo, PSC, GPS) 
		 		 VALUES (".$companyType.",'".$companyName."','".$companyCity."','".$companyStreet."','".$companyStreetNumer."','".$postCode."','".$coordinates."')";		 					
			}
				
		$this->db->query($sql);
		 $customerId = $this->db->insert_id();
		 
		 $sql = "INSERT INTO inventura (ID_TYP_R, ID_Zakaznika, Vzdialenost, Pocet_zamestnancov, Cas_cesty, Datum_prihlasovania, Datum_zaciatku, Datum_konca, Status)
		 		 VALUES (".$inventoryType.",".$customerId.",".$range.",0,'".$driveTime."','".$dateTimeJoin."','".$dateTimeStart."','".$dateTimeEnd."',1)";
		 $this->db->query($sql);
		 $inventoryId = $this->db->insert_id();
		 
		 $reservationForLeadEmployee=TRUE;
		 $countEmployees = 0;
		 
		 foreach($vehicles as $vehicle){
			 
			 $sqlCount = 'SELECT Pocet_miest FROM vozidlo WHERE ID_VOZ='.$vehicle;
			 $vehicleSlots = $this->db->query($sqlCount)->row()->Pocet_miest;
			 $countEmployees=$countEmployees+($vehicleSlots-1);	//minus sedadla vodica
			 
			 if($vehicleSlots>9){
				 $sqlRez1 = 'INSERT INTO rezervacia (ID_INV, ID_VOZ, ID_SOF)
			 		 	 	 VALUES ('.$inventoryId.','.$vehicle.',0)';
			 }else{
				 $sqlRez1 = 'INSERT INTO rezervacia (ID_INV, ID_VOZ)
			 		 	 	 VALUES ('.$inventoryId.','.$vehicle.')';
			 }
							  					  
			 $this->db->query($sqlRez1);
			 
			 if ($reservationForLeadEmployee==TRUE){
				$reservationId = $this->db->insert_id();
				$sqlEmp = 'INSERT INTO cesty (ID_REZ) VALUES ('.$reservationId.')';
				$this->db->query($sqlEmp);
				$reservationForLeadEmployee=FALSE;			 	 
			 }
			 
		 }
		 $countEmployees=($countEmployees-1);	//minus rezervacia pre veduceho
		 $sqlUpdateCount = 'UPDATE inventura SET Pocet_zamestnancov="' . $countEmployees . '" WHERE inventura.ID_INV='.$inventoryId;
		 $this->db->query($sqlUpdateCount);
	 }
	 
	 public function saveNewInventoryOldCustomer($id_cus, $range, $inventoryType, $driveTime, $dateTimeJoin, $dateTimeStart, $dateTimeEnd, $vehicles){
		 $sql = "INSERT INTO inventura (ID_TYP_R, ID_Zakaznika, Vzdialenost, Pocet_zamestnancov, Cas_cesty, Datum_prihlasovania, Datum_zaciatku, Datum_konca, Status)
		 		 VALUES (".$inventoryType.",".$id_cus.",".$range.",0,'".$driveTime."','".$dateTimeJoin."','".$dateTimeStart."','".$dateTimeEnd."',1)";
		 $this->db->query($sql);
		 $inventoryId = $this->db->insert_id();
		 
		 $reservationForLeadEmployee=TRUE;
		 $countEmployees = 0;
		 
		 foreach($vehicles as $vehicle){
			 
			 $sqlCount = 'SELECT Pocet_miest FROM vozidlo WHERE ID_VOZ='.$vehicle;
			 $vehicleSlots = $this->db->query($sqlCount)->row()->Pocet_miest;
			 $countEmployees=$countEmployees+($vehicleSlots-1);	//minus sedadla vodica
			 
			 if($vehicleSlots>9){
				 $sqlRez = 'INSERT INTO rezervacia (ID_INV, ID_VOZ, ID_SOF)
			 		 VALUES ('.$inventoryId.','.$vehicle.', 0)';
			 }else{
				 $sqlRez = 'INSERT INTO rezervacia (ID_INV, ID_VOZ)
			 		 VALUES ('.$inventoryId.','.$vehicle.')';
			 }
			 				  					  
			 $this->db->query($sqlRez);
			 
			 if ($reservationForLeadEmployee==TRUE){
				$reservationId = $this->db->insert_id();
				$sqlEmp = 'INSERT INTO cesty (ID_REZ) VALUES ('.$reservationId.')';
				$this->db->query($sqlEmp);
				$reservationForLeadEmployee=FALSE;			 	 
			 }
			 
		 }
		 $countEmployees=($countEmployees-1);	//minus rezervacia pre veduceho
		 $sqlUpdateCount = 'UPDATE inventura SET Pocet_zamestnancov="' . $countEmployees . '" WHERE inventura.ID_INV='.$inventoryId;
		 $this->db->query($sqlUpdateCount);	 
	 }
	 
	 public function getCountCustomerInventories($id_cus){
		 $sql = 'SELECT COUNT(ID_INV) AS pocet FROM inventura WHERE ID_Zakaznika='.$id_cus;
		 return $this->db->query($sql)->row()->pocet;	 
	 }
	 
	 public function getLastInventory($id_cus){
		 $sql = 'SELECT inventura.Datum_zaciatku FROM inventura WHERE ID_Zakaznika='.$id_cus.' ORDER BY inventura.Datum_zaciatku DESC';
	 	 return $this->db->query($sql)->row();
	 }
	 
	 public function getOldSettings(){
		 $sql = 'SELECT * FROM nastavenia ORDER BY Datum_nastavenia DESC';
		 return $this->db->query($sql)->row();	 
	 }
	 
	 public function saveNewSettings($date, $salaryLead, $salaryDriver, $salaryEmployee, $salaryKm){
		 $sql = 'INSERT INTO nastavenia (Datum_nastavenia, Vyplata_veduci_inventury, Vyplata_sofer, Vyplata_zamestnanec, Vyplata_kilometer)
		 		 VALUES ("'.$date.'",'.$salaryLead.','.$salaryDriver.','.$salaryEmployee.','.$salaryKm.')';
		 $this->db->query($sql);
	 }
	 
	 public function getCountPosts(){
		$sql='SELECT COUNT(ID_HLA) AS pocet FROM hlasenie';
		$query = $this->db->query($sql);
		return $query->row()->pocet;	 
	 }
	 
	 public function fetchPosts($limit,$offset){
		 if ($offset==''){
			$offset=0;
		} 		
		 $sql = 'SELECT hlasenie.ID_HLA, hlasenie.Predmet, hlasenie.Hlasenie, zamestnanec.Meno, zamestnanec.Priezvisko, hlasenie.Status, hlasenie.Datum_podania FROM hlasenie 
		         LEFT JOIN zamestnanec ON hlasenie.ID_ZAM=zamestnanec.ID_ZAM
				 ORDER BY hlasenie.Status DESC, hlasenie.Datum_podania DESC
				 LIMIT '. $limit .' OFFSET '. $offset; 
		 $query = $this->db->query($sql);
		 
		 if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }	
	 }	
	 
	 public function isThereUnreadPost(){
		 $sql = 'SELECT ID_HLA FROM hlasenie WHERE status=1';
		 $count = $this->db->query($sql);	
		 if($count->num_rows()>0){
			 return TRUE;
		 }else{
			 return FALSE;
		 }
	 }
	 
	 public function getPostDetail($id_post){
		 $sql = 'SELECT * FROM hlasenie WHERE ID_HLA='.$id_post;
		 return $this->db->query($sql)->row();
	 }
	 
	 public function setPostAsDone($id_post, $date , $id_emp){
		$sql = 'UPDATE hlasenie SET Status=0 , Datum_vyriesenia="'.$date.'", Vyriesil='.$id_emp.' WHERE ID_HLA='.$id_post;
		$this->db->query($sql);		 
	 }
	 
		
	 public function penaltyEmployee($id_emp,$id_rep){
		 $sql = 'UPDATE report_zoznam SET Vyplatene='.(-20).' WHERE ID_REP='.$id_rep.' AND ID_ZAM='.$id_emp;
		 $this->db->query($sql);	
	 }
	 
	 public function unPenaltyEmployee($id_emp,$id_rep){
		 $sql = 'UPDATE report_zoznam SET Vyplatene='.(0).' WHERE ID_REP='.$id_rep.' AND ID_ZAM='.$id_emp;
		 $this->db->query($sql);	 
	 } 	 
	 
	 public function fetchActiveInventories($pageLimit, $offset){
		$sql = 'SELECT * FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE (inventura.Datum_zaciatku>NOW()) AND (inventura.status=1)
				ORDER BY inventura.Datum_zaciatku 
				LIMIT '.$limit.' OFFSET '.$offset;
		
	 }
	 
	 public function getCountArchiveInventories(){
		 $sql='SELECT COUNT(ID_INV) AS Pocet FROM inventura WHERE inventura.Datum_zaciatku<NOW()';
		 return $this->db->query($sql)->row()->Pocet;
	 }
	 
	 public function fetchAllArchiveInventories($pageLimit, $offset){			 		
		if ($offset==''){
			$offset=0;
		} 	
		$sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, rozloha.ID_TYP_R, inventura.Datum_zaciatku, zamestnanec.Meno, zamestnanec.Priezvisko FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE inventura.Datum_zaciatku<NOW()
				ORDER BY inventura.Datum_zaciatku DESC 
				LIMIT '. $pageLimit .' OFFSET '. $offset;
		$query=$this->db->query($sql);
		if($query->num_rows()>0){
			 foreach($query->result() as $row) {
				 $data[] = $row;
			 }
			 return $data;
		 } else {
			 return false;	
		 }	
	 }
	 
	 public function searchArchiveInventories($searchStr){
		$sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, rozloha.ID_TYP_R, inventura.Datum_zaciatku, zamestnanec.Meno, zamestnanec.Priezvisko FROM inventura 
		 		LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik
				LEFT JOIN typ_obchodu ON zakaznik.ID_TYP_I=typ_obchodu.ID_TYP_I
				LEFT JOIN zamestnanec ON inventura.ID_Veduci=zamestnanec.ID_ZAM
				LEFT JOIN rozloha ON inventura.ID_TYP_R=rozloha.ID_TYP_R
				WHERE inventura.Datum_zaciatku<NOW() AND ((UCASE(zakaznik.Nazov_spolocnosti) LIKE "%'. $searchStr .'%") OR (UCASE(inventura.ID_INV) LIKE "%'. $searchStr .'%") OR (UCASE(zakaznik.Mesto) LIKE "%'. $searchStr .'%"))
				ORDER BY inventura.Datum_zaciatku';
		$query = $this->db->query($sql);		 
		 if($query->num_rows()>0){
			 return $query->result();
		 } else {
			 return $query->result();
		 }
	 
	 }

}