<?php 

class Model_driver extends CI_Model {	
	
	function __construct()
     {
          parent::__construct();
     }
	 
	 public function getBasicInfo(){ //použité
                $sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, inventura.ID_Veduci, zamestnanec.Meno, zamestnanec.Priezvisko, inventura.Datum_zaciatku, inventura.Datum_konca, inventura.Datum_prihlasovania, inventura.Pocet_zamestnancov
                        FROM inventura
                        LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik 
                        LEFT JOIN zamestnanec ON zamestnanec.ID_ZAM=inventura.ID_Veduci
                        LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I=zakaznik.ID_TYP_I
                        LEFT JOIN rozloha ON rozloha.ID_TYP_R=inventura.ID_TYP_R 
                        WHERE inventura.Datum_prihlasovania >= NOW()
                        ORDER BY inventura.Datum_zaciatku
                        ';
                return $this->db->query($sql)->result();           
        }
        
        public function getMyInventories($id_emp){
                $sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, inventura.ID_Veduci, zamestnanec.Meno, zamestnanec.Priezvisko, inventura.Datum_zaciatku, inventura.Datum_konca, inventura.Datum_prihlasovania, inventura.Pocet_zamestnancov
                        FROM zoznam_prihlasenych 
                        LEFT JOIN inventura ON zoznam_prihlasenych.ID_INV=inventura.ID_INV
                        LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik 
                        LEFT JOIN zamestnanec ON zamestnanec.ID_ZAM=inventura.ID_Veduci
                        LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I=zakaznik.ID_TYP_I
                        LEFT JOIN rozloha ON rozloha.ID_TYP_R=inventura.ID_TYP_R
                        WHERE zoznam_prihlasenych.ID_ZAM='.$id_emp.' AND inventura.Datum_prihlasovania >= NOW()
                        ORDER BY inventura.Datum_zaciatku';
                 return $this->db->query($sql)->result();        
        }
        
        public function getMyDrives($id_emp){
                $sql =  'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, inventura.ID_Veduci, zamestnanec.Meno, zamestnanec.Priezvisko, inventura.Datum_zaciatku, inventura.Datum_konca, inventura.Datum_prihlasovania, inventura.Pocet_zamestnancov
                        FROM rezervacia
                        LEFT JOIN inventura ON rezervacia.ID_INV=inventura.ID_INV
                        LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik 
                        LEFT JOIN zamestnanec ON zamestnanec.ID_ZAM=inventura.ID_Veduci
                        LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I=zakaznik.ID_TYP_I
                        LEFT JOIN rozloha ON rozloha.ID_TYP_R=inventura.ID_TYP_R
                        WHERE rezervacia.ID_SOF='.$id_emp.' AND inventura.Datum_zaciatku >= NOW()';
                return $this->db->query($sql)->result();  
        }
               
        public function getMyInventoriesArchive($id_emp){
                $sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, inventura.ID_Veduci, zamestnanec.Meno, zamestnanec.Priezvisko, inventura.Datum_zaciatku, inventura.Datum_konca, inventura.Datum_prihlasovania, inventura.Pocet_zamestnancov
                        FROM zoznam_prihlasenych 
                        LEFT JOIN inventura ON zoznam_prihlasenych.ID_INV=inventura.ID_INV
                        LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik 
                        LEFT JOIN zamestnanec ON zamestnanec.ID_ZAM=inventura.ID_Veduci
                        LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I=zakaznik.ID_TYP_I
                        LEFT JOIN rozloha ON rozloha.ID_TYP_R=inventura.ID_TYP_R
                        WHERE zoznam_prihlasenych.ID_ZAM='.$id_emp.' AND inventura.Datum_zaciatku <= NOW()
                        ORDER BY inventura.Datum_zaciatku';
                 return $this->db->query($sql)->result();        
        }
        
        public function getMyDrivesArchive($id_emp){
                $sql = 'SELECT inventura.ID_INV, zakaznik.Nazov_spolocnosti, zakaznik.Mesto, inventura.ID_Veduci, zamestnanec.Meno, zamestnanec.Priezvisko, inventura.Datum_zaciatku, inventura.Datum_konca, inventura.Datum_prihlasovania, inventura.Pocet_zamestnancov
                        FROM rezervacia
                        LEFT JOIN inventura ON rezervacia.ID_INV=inventura.ID_INV
                        LEFT JOIN zakaznik ON inventura.ID_Zakaznika=zakaznik.ID_Zakaznik 
                        LEFT JOIN zamestnanec ON zamestnanec.ID_ZAM=inventura.ID_Veduci
                        LEFT JOIN typ_obchodu ON typ_obchodu.ID_TYP_I=zakaznik.ID_TYP_I
                        LEFT JOIN rozloha ON rozloha.ID_TYP_R=inventura.ID_TYP_R
                        WHERE rezervacia.ID_SOF='.$id_emp.' AND inventura.Datum_zaciatku <= NOW()';
                return $this->db->query($sql)->result();         
        }
        
        
        
        
        
        
/// kontroly
        
        public function haveIFreeTimeInventory($id_emp, $start, $end){
                $sql=   'SELECT inventura.ID_INV
                        FROM inventura 
                        RIGHT JOIN zoznam_prihlasenych ON inventura.ID_INV=zoznam_prihlasenych.ID_INV 
                        WHERE (zoznam_prihlasenych.ID_ZAM='.$id_emp.') AND  
                                (
                                (("'.$start.'">=inventura.Datum_zaciatku) AND ("'.$start.'"<=inventura.Datum_konca)) OR
                                (("'.$end.'">=inventura.Datum_zaciatku) AND ("'.$end.'"<=inventura.Datum_konca)) OR
                                (("'.$start.'"<=inventura.Datum_zaciatku) AND ("'.$end.'">=inventura.Datum_konca))
                                )';
                $query = $this->db->query($sql);
                if ($query->num_rows()==0){
                        return TRUE;
                } else {
                        return FALSE;
                }                                 
        }
        
        public function haveIFreeTimeReservation($id_emp, $start, $end){
	       $sql = 'SELECT rezervacia.ID_REZ 
                       FROM rezervacia
                       LEFT JOIN inventura ON rezervacia.ID_INV=inventura.ID_INV
                       WHERE (rezervacia.ID_SOF='.$id_emp.') AND  
                                (
                                (("'.$start.'">=inventura.Datum_zaciatku) AND ("'.$start.'"<=inventura.Datum_konca)) OR
                                (("'.$end.'">=inventura.Datum_zaciatku) AND ("'.$end.'"<=inventura.Datum_konca)) OR
                                (("'.$start.'"<=inventura.Datum_zaciatku) AND ("'.$end.'">=inventura.Datum_konca))
                                )';  
               $query = $this->db->query($sql);
                if ($query->num_rows()==0){
                        return TRUE;
                } else {
                        return FALSE;
                }      
        }
        
        public function isFreeInventory($id_inv, $count_emp){
                $sql = 'SELECT * FROM zoznam_prihlasenych WHERE ID_INV='.$id_inv;
                $query = $this->db->query($sql);
                if ($query->num_rows()<$count_emp){
                        return TRUE;
                } else {
                        return FALSE;
                }     
        }
        
        public function isFreeReservation($id_inv){
                $sql = 'SELECT rezervacia.ID_REZ FROM rezervacia WHERE (rezervacia.ID_INV='.$id_inv.') AND (rezervacia.ID_SOF IS NULL)';
                $query = $this->db->query($sql);
                if ($query->num_rows()==0){
                        return FALSE;
                } else {
                        return TRUE;
                }         
        }
        
        public function amILoggedAsDriver($id_inv, $id_emp){
                $sql = 'SELECT rezervacia.ID_REZ FROM rezervacia WHERE (rezervacia.ID_INV='.$id_inv.') AND (rezervacia.ID_SOF='.$id_emp.')';
                $query = $this->db->query($sql);
                if ($query->num_rows()==0){
                        return FALSE;
                } else {
                        return TRUE;
                }          
        }
        
        public function amILoggedAsEmployee($id_inv, $id_emp){
                $sql = 'SELECT * FROM zoznam_prihlasenych WHERE (zoznam_prihlasenych.ID_INV='.$id_inv.') AND (zoznam_prihlasenych.ID_ZAM='.$id_emp.')';                
                $query = $this->db->query($sql);
                if ($query->num_rows()==0){
                        return FALSE;
                } else {
                        return TRUE;
                } 
        }
 
//prihlasovanie odhlasovanie ako vodic

        public function getFreeReservation($id_inv){
                $sql = 'SELECT rezervacia.ID_REZ FROM rezervacia WHERE rezervacia.ID_INV='.$id_inv.' AND ID_SOF IS NULL ORDER by ID_REZ';
                return $this->db->query($sql)->row()->ID_REZ;
        }
        
        public function LogOnInventoryAsDriver($id_rez,$id_emp){                
                $sql = 'UPDATE rezervacia SET ID_SOF='.$id_emp.' WHERE rezervacia.ID_REZ='.$id_rez;
                $this->db->query($sql);
        }
        
        public function LogOutInventoryAsDriver($id_inv,$id_emp){                
                $sql = 'UPDATE rezervacia SET ID_SOF=NULL WHERE rezervacia.ID_INV='.$id_inv.' AND rezervacia.ID_SOF='.$id_emp;
                $this->db->query($sql);
        }
       
}