<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lead_employee extends CI_Controller {		
	
	
	
	public function getInventories($active){
		$this->load->model('model_lead_employee');
		$data['active'] = $active;
		switch($active){
			case 1:
				$data['inventories'] = $this->model_lead_employee->getNoticedInventories($this->session->userdata['id_zamestnanca']);
			break;
			case 2:
				$data['inventories'] = $this->model_lead_employee->getFreeInventories($this->session->userdata['id_zamestnanca']);
			break;
			case 3:
				$data['inventories'] = $this->model_lead_employee->getMyInventories($this->session->userdata['id_zamestnanca']);				
			break;
			case 4:
				$this->load->library('Pagination');
				$pageLimit=10;
				$totalRows=$this->model_lead_employee->getCountMyArchive($this->session->userdata['id_zamestnanca']);
				$configuration=$this->getConfigurationForPagination($pageLimit, base_url('lead_employee/getInventories/4'), $totalRows, 4);	
				$data['inventories'] = $this->model_lead_employee->getMyArchive($this->session->userdata['id_zamestnanca'],$pageLimit,$this->uri->segment(4));
				$this->pagination->initialize($configuration);
			break;
		}		
		
				
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_inventories', $data);
		$this->load->view('include/footer');
	}	
	
	
	public function getAllEmployees(){
		$this->load->library('Pagination');
		$this->load->model('model_lead_employee');
		$total_rows = $this->model_lead_employee->getCountAllEmployees();
		
		$configuration = array();
		$configuration['base_url'] = base_url() . 'lead_employee/getAllEmployees/';		
		$configuration['per_page'] = 10;
		
		$configuration['total_rows'] = $total_rows;
		$fetch = $this->model_lead_employee->fetchAllEmployees($configuration['per_page'],$this->uri->segment(3));
		$data['Employees'] = $fetch;
		$data['Active'] = 1;

		$configuration['full_tag_open'] = '<ul class="pagination">';
  		$configuration['full_tag_close'] = '</ul></div><!--pagination-->';
		$configuration['first_link'] = '&laquo;';
		$configuration['first_tag_open'] = '<li class="prev page">';
		$configuration['first_tag_close'] = '</li>';
		$configuration['last_link'] = '&raquo;';
		$configuration['last_tag_open'] = '<li class="next page">';
		$configuration['last_tag_close'] = '</li>';
		$configuration['next_link'] = '&rarr;';
		$configuration['next_tag_open'] = '<li class="next page">';
		$configuration['next_tag_close'] = '</li>';
		$configuration['prev_link'] = '&larr;';
		$configuration['prev_tag_open'] = '<li class="prev page">';
		$configuration['prev_tag_close'] = '</li>';
		$configuration['cur_tag_open'] = '<li><a href="" class="active">';
		$configuration['cur_tag_close'] = '</a></li>';
		$configuration['num_tag_open'] = '<li class="page">';
		$configuration['num_tag_close'] = '</li>';		
		
		$this->pagination->initialize($configuration);
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_employees_show', $data);
		$this->load->view('include/footer');
	}
	
	public function getLeadEmployees(){
		$this->load->library('Pagination');
		$this->load->model('model_lead_employee');
		$total_rows = $this->model_lead_employee->getCountAllLeadEmployees();
		
		$configuration = array();
		$configuration['base_url'] = base_url() . 'lead_employee/getLeadEmployees/';		
		$configuration['per_page'] = 10;
		
		$configuration['total_rows'] = $total_rows;
		$fetch = $this->model_lead_employee->fetchLeadEmployees($configuration['per_page'],$this->uri->segment(3));
		$data['Employees'] = $fetch;
		$data['Active'] = 2;

		$configuration['full_tag_open'] = '<ul class="pagination">';
  		$configuration['full_tag_close'] = '</ul></div><!--pagination-->';
		$configuration['first_link'] = '&laquo;';
		$configuration['first_tag_open'] = '<li class="prev page">';
		$configuration['first_tag_close'] = '</li>';
		$configuration['last_link'] = '&raquo;';
		$configuration['last_tag_open'] = '<li class="next page">';
		$configuration['last_tag_close'] = '</li>';
		$configuration['next_link'] = '&rarr;';
		$configuration['next_tag_open'] = '<li class="next page">';
		$configuration['next_tag_close'] = '</li>';
		$configuration['prev_link'] = '&larr;';
		$configuration['prev_tag_open'] = '<li class="prev page">';
		$configuration['prev_tag_close'] = '</li>';
		$configuration['cur_tag_open'] = '<li><a href="" class="active">';
		$configuration['cur_tag_close'] = '</a></li>';
		$configuration['num_tag_open'] = '<li class="page">';
		$configuration['num_tag_close'] = '</li>';		
		
		$this->pagination->initialize($configuration);
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_employees_show', $data);
		$this->load->view('include/footer');	
	}	
	
	public function getDrivers(){
		$this->load->library('Pagination');
		$this->load->model('model_lead_employee');
		$total_rows = $this->model_lead_employee->getCountAllDrivers();
		
		$configuration = array();
		$configuration['base_url'] = base_url() . 'lead_employee/getDrivers/';		
		$configuration['per_page'] = 10;
		
		$configuration['total_rows'] = $total_rows;
		$fetch = $this->model_lead_employee->fetchDrivers($configuration['per_page'],$this->uri->segment(3));
		$data['Employees'] = $fetch;
		$data['Active'] = 3;

		$configuration['full_tag_open'] = '<ul class="pagination">';
  		$configuration['full_tag_close'] = '</ul></div><!--pagination-->';
		$configuration['first_link'] = '&laquo;';
		$configuration['first_tag_open'] = '<li class="prev page">';
		$configuration['first_tag_close'] = '</li>';
		$configuration['last_link'] = '&raquo;';
		$configuration['last_tag_open'] = '<li class="next page">';
		$configuration['last_tag_close'] = '</li>';
		$configuration['next_link'] = '&rarr;';
		$configuration['next_tag_open'] = '<li class="next page">';
		$configuration['next_tag_close'] = '</li>';
		$configuration['prev_link'] = '&larr;';
		$configuration['prev_tag_open'] = '<li class="prev page">';
		$configuration['prev_tag_close'] = '</li>';
		$configuration['cur_tag_open'] = '<li><a href="" class="active">';
		$configuration['cur_tag_close'] = '</a></li>';
		$configuration['num_tag_open'] = '<li class="page">';
		$configuration['num_tag_close'] = '</li>';		
		
		$this->pagination->initialize($configuration);
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_employees_show', $data);
		$this->load->view('include/footer');
	}	
	
	public function getBasicEmployees(){
		$this->load->library('Pagination');
		$this->load->model('model_lead_employee');
		$total_rows = $this->model_lead_employee->getCountAllBasicEmployees();
		
		$configuration = array();
		$configuration['base_url'] = base_url() . 'lead_employee/getBasicEmployees/';		
		$configuration['per_page'] = 10;
		
		$configuration['total_rows'] = $total_rows;
		$fetch = $this->model_lead_employee->fetchEmployees($configuration['per_page'],$this->uri->segment(3));
		$data['Employees'] = $fetch;
		$data['Active'] = 4;

		$configuration['full_tag_open'] = '<ul class="pagination">';
  		$configuration['full_tag_close'] = '</ul></div><!--pagination-->';
		$configuration['first_link'] = '&laquo;';
		$configuration['first_tag_open'] = '<li class="prev page">';
		$configuration['first_tag_close'] = '</li>';
		$configuration['last_link'] = '&raquo;';
		$configuration['last_tag_open'] = '<li class="next page">';
		$configuration['last_tag_close'] = '</li>';
		$configuration['next_link'] = '&rarr;';
		$configuration['next_tag_open'] = '<li class="next page">';
		$configuration['next_tag_close'] = '</li>';
		$configuration['prev_link'] = '&larr;';
		$configuration['prev_tag_open'] = '<li class="prev page">';
		$configuration['prev_tag_close'] = '</li>';
		$configuration['cur_tag_open'] = '<li><a href="" class="active">';
		$configuration['cur_tag_close'] = '</a></li>';
		$configuration['num_tag_open'] = '<li class="page">';
		$configuration['num_tag_close'] = '</li>';		
		
		$this->pagination->initialize($configuration);
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_employees_show', $data);
		$this->load->view('include/footer');	
	}	
	
	public function searchAllEmployees($active){
		$this->load->model('model_lead_employee');
		$Employee = $this->input->post('lead_employee_search');
		
		if(isset($Employee) AND !empty($Employee)){
			$data['Employees'] = $this->model_lead_employee->searchEmployee($Employee, $active);
			$data['Active'] = $active;
			$this->load->view('include/header');
			$this->load->view('lead_employee/lead_employee_employees_show', $data);
			$this->load->view('include/footer');
		} else {
			switch($active){
				case 1:redirect(base_url('lead_employee/getAllEmployees'));break;
				case 2: redirect(base_url('lead_employee/getLeadEmployees'));break;
				case 3: redirect(base_url('lead_employee/getDrivers'));break;
				case 4: redirect(base_url('lead_employee/getBasicEmployees'));break;
			}
			
		}
	}
	
	public function redirectLeadEmployeeInventoryDetail($modeInv, $modeDet, $id_inv){		
		$this->load->model('model_lead_employee');
		
		$data['modeDet']=$modeDet;
		$data['modeInv']=$modeInv;
		$data['id_inv']=$id_inv;		
		
		switch ($modeDet) {
    		case 1:
        		$data['inventoryInfo'] = $this->model_lead_employee->getGeneralInventoryInfo($id_inv); 
				
				$this->load->library('googlemaps');
				$config = array();
				$config['center'] = $data['inventoryInfo']->GPS;
				$config['map_height'] = 374;
				$config['map_width'] = 600;
				
				$marker = array();
				$marker['position'] = $data['inventoryInfo']->GPS;
	
				$this->googlemaps->add_marker($marker);
				$this->googlemaps->initialize($config);
				$data['map'] = $this->googlemaps->create_map();
				$this->load->view('include/header2', $data);
			break;
    		case 2:
       			$data['inventoryInfo'] = $this->model_lead_employee->getOnlyInventoryReservations($id_inv);
				$this->load->view('include/header');
       		break;
			case 3:
       			$data['inventoryInfo'] = $this->model_lead_employee->getInventoryReport($id_inv);
				$this->load->view('include/header');
       		break;
		}	
		
		
		$this->load->view('lead_employee/lead_employee_inventory_detail', $data);
		$this->load->view('include/footer');		
	}
	
	
	
	
	public function joinLeadEmployeeInventory($active,$id_inv){
		$this->load->model('model_lead_employee');
		$this->model_lead_employee->joinLeadEmployeeInventory($id_inv, $this->session->userdata('id_zamestnanca'));	
		redirect(base_url('lead_employee/getInventories/'.$active));
		
	}
	
	public function disjoinLeadEmployeeInventory($active,$id_inv){
		$this->load->model('model_lead_employee');
		$this->model_lead_employee->disjoinLeadEmployeeInventory($id_inv, $this->session->userdata('id_zamestnanca'));	
		redirect(base_url('lead_employee/getInventories/'.$active));	
	}
	
	public function createExcelReport($id_inv){
		
		$this->load->model('model_lead_employee'); 
		$inventoryInfo = $this->model_lead_employee->getCustomerInfo($id_inv);
		$reservations = $this->model_lead_employee->getInventoryReservations($id_inv);
		$employees = $this->model_lead_employee->getInventoryEmployees($id_inv,$inventoryInfo->ID_Veduci);
		
		if ($inventoryInfo!=FALSE AND $reservations!=FALSE){
			$this->load->library('Excel');
			
			$style = array(
				'alignment'	=> array(
					'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
					'vertical' =>  PHPExcel_Style_Alignment::VERTICAL_CENTER	
				)
			);
			
			$styleBorder1 = array(
				'alignment'	=> array(
					'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
					'vertical' =>  PHPExcel_Style_Alignment::VERTICAL_CENTER	
				),
				'borders' => array(
    				'allborders' => array(
      					'style' => PHPExcel_Style_Border::BORDER_THIN
    				)
  				)
			);
			
			$styleBorder2 = array(
				'alignment'	=> array(
					'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
					'vertical' =>  PHPExcel_Style_Alignment::VERTICAL_CENTER	
				),				
  				'borders' => array(
    				'outline' => array(
      					'style' => PHPExcel_Style_Border::BORDER_MEDIUM
    				)
  				)
			);
			
						
			$ExcelFile = new PHPExcel();			
			$ExcelFile->getProperties()->setTitle($id_inv.'_'.$inventoryInfo->Mesto)->setDescription("none");
			$ExcelFile->getActiveSheet()->getDefaultStyle()->applyFromArray($style);
			$ExcelFile->getActiveSheet()->getStyle('A1:O200')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
			$ExcelFile->getActiveSheet()->getProtection()->setPassword('123456789');
			$ExcelFile->getActiveSheet()->getProtection()->setSheet(true);
			
			$ExcelFile->getActiveSheet()->mergeCells('A1:B1');
			$ExcelFile->getActiveSheet()->mergeCells('C1:D1');
			$ExcelFile->getActiveSheet()->mergeCells('A2:B2');
			$ExcelFile->getActiveSheet()->mergeCells('C2:D2');			
			$ExcelFile->getActiveSheet()->setCellValue('A1', 'ID inventúry:');
			$ExcelFile->getActiveSheet()->setCellValue('A2', 'Zákazník:');
			$ExcelFile->getActiveSheet()->setCellValue('C1', $inventoryInfo->ID_INV);		
			$ExcelFile->getActiveSheet()->setCellValue('C2', $inventoryInfo->Nazov_spolocnosti);
			$ExcelFile->getActiveSheet()->getStyle('A1:B2')->getFont()->setBold(true);
			
			$ExcelFile->getActiveSheet()->mergeCells('A3:B3');
			$ExcelFile->getActiveSheet()->mergeCells('C3:D3');
			$ExcelFile->getActiveSheet()->mergeCells('A4:B4');
			$ExcelFile->getActiveSheet()->mergeCells('C4:D4');
			$ExcelFile->getActiveSheet()->mergeCells('A5:B5');
			$ExcelFile->getActiveSheet()->mergeCells('C5:D5');
			$ExcelFile->getActiveSheet()->mergeCells('A6:B6');
			$ExcelFile->getActiveSheet()->mergeCells('C6:D6');
			$ExcelFile->getActiveSheet()->mergeCells('A7:B7');
			$ExcelFile->getActiveSheet()->mergeCells('C7:D7');
			$ExcelFile->getActiveSheet()->setCellValue('A3', 'Dátum:');
			$ExcelFile->getActiveSheet()->setCellValue('A4', 'Počet zam.:');
			$ExcelFile->getActiveSheet()->setCellValue('A5', 'Počet vodičov:');
			$ExcelFile->getActiveSheet()->setCellValue('A6', 'Počet km.:');
			$ExcelFile->getActiveSheet()->setCellValue('A7', 'Čas cesty:');
			$ExcelFile->getActiveSheet()->setCellValue('C3', date('d.m.Y',strtotime($inventoryInfo->Datum_zaciatku)));
			$ExcelFile->getActiveSheet()->setCellValue('C4', $inventoryInfo->Pocet_zamestnancov);
			$ExcelFile->getActiveSheet()->setCellValue('C5', count($reservations));
			$ExcelFile->getActiveSheet()->setCellValue('C6', $inventoryInfo->Vzdialenost);
			$ExcelFile->getActiveSheet()->setCellValue('C7', date('G:i',strtotime($inventoryInfo->Cas_cesty)));			
			$ExcelFile->getActiveSheet()->getStyle('A3:B7')->getFont()->setBold(true);
			$ExcelFile->getActiveSheet()->getStyle('A1:D7')->applyFromArray($styleBorder1);
			$ExcelFile->getActiveSheet()->getStyle('A1:D7')->applyFromArray($styleBorder2);
			
			$ExcelFile->getActiveSheet()->mergeCells('G1:H1');
			$ExcelFile->getActiveSheet()->mergeCells('G2:H2');
			$ExcelFile->getActiveSheet()->mergeCells('G3:H3');
			$ExcelFile->getActiveSheet()->mergeCells('G4:H4');
			$ExcelFile->getActiveSheet()->mergeCells('I1:J1');
			$ExcelFile->getActiveSheet()->mergeCells('I2:J2');
			$ExcelFile->getActiveSheet()->mergeCells('I3:J3');
			$ExcelFile->getActiveSheet()->mergeCells('I4:J4');
			$ExcelFile->getActiveSheet()->setCellValue('G1', 'Mesto:');
			$ExcelFile->getActiveSheet()->setCellValue('G2', 'Ulica:');
			$ExcelFile->getActiveSheet()->setCellValue('G3', 'Číslo:');
			$ExcelFile->getActiveSheet()->setCellValue('G4', 'PSČ:');
			$ExcelFile->getActiveSheet()->setCellValue('I1', $inventoryInfo->Mesto);
			$ExcelFile->getActiveSheet()->setCellValue('I2', $inventoryInfo->Ulica);
			$ExcelFile->getActiveSheet()->setCellValue('I3', $inventoryInfo->Cislo);
			$ExcelFile->getActiveSheet()->setCellValue('I4', $inventoryInfo->PSC);
			$ExcelFile->getActiveSheet()->getStyle('G1:H4')->getFont()->setBold(true);
			$ExcelFile->getActiveSheet()->getStyle('G1:J4')->applyFromArray($styleBorder1);
			$ExcelFile->getActiveSheet()->getStyle('G1:J4')->applyFromArray($styleBorder2);
			
			$ExcelFile->getActiveSheet()->mergeCells('G6:H6');
			$ExcelFile->getActiveSheet()->mergeCells('I6:J6');
			$ExcelFile->getActiveSheet()->mergeCells('G7:H7');
			$ExcelFile->getActiveSheet()->mergeCells('I7:J7');
			$ExcelFile->getActiveSheet()->setCellValue('G6', 'Začiatok:');
			$ExcelFile->getActiveSheet()->setCellValue('G7', 'Koniec:');
			$ExcelFile->getActiveSheet()->getStyle('I6:J7')->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
			
			$dateStart=strtotime($inventoryInfo->Datum_zaciatku);
			$hoursDrive=intval(date('G',strtotime($inventoryInfo->Cas_cesty)));
			$minutesDrive=intval(date('i',strtotime($inventoryInfo->Cas_cesty)));
			$timeDrive=($hoursDrive*3600)+($minutesDrive*60);
			$startDate=$dateStart+$timeDrive;
			
			$dateEnd=strtotime($inventoryInfo->Datum_konca);
			$endDate=$dateEnd-$timeDrive;
			
			$ExcelFile->getActiveSheet()->setCellValue('I6', date('d.m.Y G:i',$startDate));
			$ExcelFile->getActiveSheet()->setCellValue('I7', date('d.m.Y G:i',$endDate));			
			$ExcelFile->getActiveSheet()->getStyle('G6:H7')->getFont()->setBold(true);
			$ExcelFile->getActiveSheet()->getStyle('G6:J7')->applyFromArray($styleBorder1);
			$ExcelFile->getActiveSheet()->getStyle('G6:J7')->applyFromArray($styleBorder2);
//Vypis zamestnancov			
			$number = 9;
			$count = 1;
			
			$ExcelFile->getActiveSheet()->setCellValue('A'.$number, "Por. č.");
			$ExcelFile->getActiveSheet()->setCellValue('B'.$number, "ID Zam.");
			$ExcelFile->getActiveSheet()->mergeCells('C'.$number.':D'.$number);
			$ExcelFile->getActiveSheet()->setCellValue('C'.$number, "Priezvisko");
			$ExcelFile->getActiveSheet()->mergeCells('E'.$number.':F'.$number);
			$ExcelFile->getActiveSheet()->setCellValue('E'.$number, "Meno");
			$ExcelFile->getActiveSheet()->mergeCells('G'.$number.':H'.$number);
			$ExcelFile->getActiveSheet()->setCellValue('G'.$number, "Nástup");
			$ExcelFile->getActiveSheet()->mergeCells('I'.$number.':J'.$number);
			$ExcelFile->getActiveSheet()->setCellValue('I'.$number, "Koniec");
			$ExcelFile->getActiveSheet()->mergeCells('K'.$number.':L'.$number);	
			$ExcelFile->getActiveSheet()->setCellValue('K'.$number, "Prestávka");			
			$ExcelFile->getActiveSheet()->getStyle('A'.$number.':L'.$number)->getFont()->setBold(true);
			$ExcelFile->getActiveSheet()->getStyle('A'.$number.':L'.$number)->applyFromArray($styleBorder1);
			$ExcelFile->getActiveSheet()->getStyle('A'.$number.':L'.$number)->applyFromArray($styleBorder2);			
			$number++;
			
			foreach($reservations as $reservation){
				if($reservation->ID_ZAM!=0){
					$ExcelFile->getActiveSheet()->getStyle('G'.$number.':L'.$number)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
					$ExcelFile->getActiveSheet()->setCellValue('A'.$number, 'S');
					$ExcelFile->getActiveSheet()->setCellValue('B'.$number, $reservation->ID_ZAM);
					$ExcelFile->getActiveSheet()->mergeCells('C'.$number.':D'.$number);
					$ExcelFile->getActiveSheet()->setCellValue('C'.$number, $reservation->Priezvisko);
					$ExcelFile->getActiveSheet()->mergeCells('E'.$number.':F'.$number);
					$ExcelFile->getActiveSheet()->setCellValue('E'.$number, $reservation->Meno);
					$ExcelFile->getActiveSheet()->mergeCells('G'.$number.':H'.$number);
					$ExcelFile->getActiveSheet()->mergeCells('I'.$number.':J'.$number);
					$ExcelFile->getActiveSheet()->mergeCells('K'.$number.':L'.$number);
					$ExcelFile->getActiveSheet()->getStyle('A'.$number.':L'.$number)->applyFromArray($styleBorder1);
					$number++;		
				}
				
				//unprotection
				
				
			}
			
			
			foreach ($employees as $employee){
				$ExcelFile->getActiveSheet()->getStyle('G'.$number.':L'.$number)->getProtection()->setLocked(PHPExcel_Style_Protection::PROTECTION_UNPROTECTED);
				$ExcelFile->getActiveSheet()->setCellValue('A'.$number, $count);
				$ExcelFile->getActiveSheet()->setCellValue('B'.$number, $employee->ID_ZAM);
				$ExcelFile->getActiveSheet()->mergeCells('C'.$number.':D'.$number);
				$ExcelFile->getActiveSheet()->setCellValue('C'.$number, $employee->Priezvisko);
				$ExcelFile->getActiveSheet()->mergeCells('E'.$number.':F'.$number);
				$ExcelFile->getActiveSheet()->setCellValue('E'.$number, $employee->Meno);
				$ExcelFile->getActiveSheet()->mergeCells('G'.$number.':H'.$number);
				$ExcelFile->getActiveSheet()->mergeCells('I'.$number.':J'.$number);
				$ExcelFile->getActiveSheet()->mergeCells('K'.$number.':L'.$number);
				$ExcelFile->getActiveSheet()->getStyle('A'.$number.':L'.$number)->applyFromArray($styleBorder1);
				$number++; $count++;	
			}
			
			$objWriter = PHPExcel_IOFactory::createWriter($ExcelFile, 'Excel5');
        
        	header('Content-Type: application/vnd.ms-excel');
        	header('Content-Disposition: attachment;filename="'.$id_inv.'_'.$inventoryInfo->Mesto.'.xls"');
        	header('Cache-Control: max-age=0');
 
        	$objWriter->save('php://output');
					
		} else {
			echo "daco je spatne";
		}
	}
	
	public function saveEditedReport(){
		$this->load->model('model_lead_employee');
		$this->load->helper('date');
		$this->load->helper('lead_employee_helper');
		
		$id_inv=$this->input->post('id_inv_hidden');
		$id_rep=$this -> model_lead_employee -> getReportId($id_inv);
		$inventoryInfo = $this->model_lead_employee-> getInventoryInfo($id_inv);
		
		$countEmployees = count($this -> model_lead_employee -> getInventoryEmployees($id_inv,$inventoryInfo->ID_Veduci));
		
		$drivers = $this -> model_lead_employee -> getInventoryReservations($id_inv);
		
		if($drivers==NULL OR $drivers==Array()){
			$countDrivers=0;	
		}else{
			$countDrivers=0;
			foreach($drivers as $driver){
				if($driver->ID_SOF!=0){
					$countDrivers=$countDrivers+1;		
				}
			}		
		}
		
		
		$salaryPerHoursLead = $this -> model_lead_employee -> getSalaryPerHourLead();
		$salaryPerHourDriver = $this -> model_lead_employee -> getSalaryPerHourDriver();
		$salaryPerHourEmployee = $this -> model_lead_employee -> getSalaryPerHourEmployee();
		$salaryPerKilometer = $this -> model_lead_employee -> getSalaryPerKilometer();
//veduci		
		$this->model_lead_employee->updateReportInfo($id_rep,date('Y-m-d G:i:s',now()),$this->input->post('notice'));
		$invStart = date_create($this->input->post('reportDatumZaciatok0')." ".$this->input->post('reportHodinyZaciatok0').":".$this->input->post('reportMinutyZaciatok0'));
		$invEnd = date_create($this->input->post('reportDatumKoniec0')." ".$this->input->post('reportHodinyKoniec0').":".$this->input->post('reportMinutyKoniec0')); 
		$date_diff = date_diff($invStart,$invEnd);
		$salary = calculateLeadSalary($date_diff,$salaryPerHoursLead,$inventoryInfo->Cas_cesty);
		$id_lead_employee = $inventoryInfo->ID_Veduci;
		$pause=NULL;
		$this->model_lead_employee->updateReportEmployeeInfo($id_rep,$id_lead_employee,$invStart,$invEnd,$pause,round($salary,2));
//zamestnanci
		for($i=1; $i<($countDrivers+$countEmployees+1); $i++){
			$id_emp=$this->input->post('id_emp_hidden'.$i);			
			if ($this->model_lead_employee->isDriver($id_inv,$id_emp)==TRUE){
				$DriverDateTimeStart = date_create($this->input->post('reportDatumZaciatok'.$i)." ".$this->input->post('reportHodinyZaciatok'.$i).":".$this->input->post('reportMinutyZaciatok'.$i));                    
				$DriverDateTimeEnd = date_create($this->input->post('reportDatumKoniec'.$i)." ".$this->input->post('reportHodinyKoniec'.$i).":".$this->input->post('reportMinutyKoniec'.$i));				
				$salary = calculateDriverSalary($DriverDateTimeStart,$DriverDateTimeEnd,$invEnd,$salaryPerHourDriver,$salaryPerHourEmployee,$inventoryInfo->Cas_cesty);	
				$pause=NULL;
				$this->model_lead_employee->updateReportEmployeeInfo($id_rep,$id_emp,$DriverDateTimeStart,$DriverDateTimeEnd,$pause,round($salary,2));				
			} else {
				$EmployeeDateTimeStart = date_create($this->input->post('reportDatumZaciatok'.$i)." ".$this->input->post('reportHodinyZaciatok'.$i).":".$this->input->post('reportMinutyZaciatok'.$i));                    
				$EmployeeDateTimeEnd = date_create($this->input->post('reportDatumKoniec'.$i)." ".$this->input->post('reportHodinyKoniec'.$i).":".$this->input->post('reportMinutyKoniec'.$i));				
				$EmployeePause = $this->input->post('reportHodinyPauza'.$i).":".$this->input->post('reportMinutyPauza'.$i);
				$salary = calculateEmployeeSalary($EmployeeDateTimeStart,$EmployeeDateTimeEnd,$EmployeePause,$salaryPerHourEmployee,$salaryPerKilometer,$inventoryInfo->Vzdialenost);
				if($salary<3){
					$salary=0;
				}
				$this->model_lead_employee->updateReportEmployeeInfo($id_rep,$id_emp,$EmployeeDateTimeStart,$EmployeeDateTimeEnd,$EmployeePause,round($salary,2));
			}
		}
		
		redirect(base_url('lead_employee/redirectLeadEmployeeInventoryDetail/4/3/'.$id_inv));
		
	} 
	
	public function createNewReport(){
		$this->load->model('model_lead_employee');
		$this->load->helper('date');
		$this->load->helper('lead_employee_helper');
//vseobecne data		
		$id_inv=$this->input->post('id_inv_hidden');
		
		$inventoryInfo = $this->model_lead_employee-> getInventoryInfo($id_inv);
		$countEmployees = count($this -> model_lead_employee -> getInventoryEmployees($id_inv,$inventoryInfo->ID_Veduci));
		$countDrivers = count($this -> model_lead_employee -> getInventoryReservations($id_inv));
		
		$salaryPerHoursLead = $this -> model_lead_employee -> getSalaryPerHourLead();
		$salaryPerHourDriver = $this -> model_lead_employee -> getSalaryPerHourDriver();
		$salaryPerHourEmployee = $this -> model_lead_employee -> getSalaryPerHourEmployee();
		$salaryPerKilometer = $this -> model_lead_employee -> getSalaryPerKilometer();	
		
//vytvorenie reportu, update inventura - vlozenie reportku k inventure		

		$notice = $this->input->post('notice');
		$id_rep = $this->model_lead_employee->createReport($id_inv,date('Y-m-d G:i:s',now()),$notice);
		
//vypocet dat pre veduceho inventury a vlozenie
		
		$invStart = date_create($this->input->post('reportDatumZaciatok0')." ".$this->input->post('reportHodinyZaciatok0').":".$this->input->post('reportMinutyZaciatok0'));
		$invEnd = date_create($this->input->post('reportDatumKoniec0')." ".$this->input->post('reportHodinyKoniec0').":".$this->input->post('reportMinutyKoniec0')); 
		$date_diff = date_diff($invStart,$invEnd);
		$salary = calculateLeadSalary($date_diff,$salaryPerHoursLead,$inventoryInfo->Cas_cesty);
		$id_lead_employee = getLeadEmployeeID($id_inv);
		$this->model_lead_employee->insertIntoListLeadEmployee($id_rep,$id_lead_employee,$invStart,$invEnd,round($salary,2));
			
//vytvorenie zaznamov zamestnancov		
		for($i=1; $i<($countDrivers+$countEmployees+1); $i++){
			$id_emp=$this->input->post('id_emp_hidden'.$i);
			
			if ($this->model_lead_employee->isDriver($id_inv,$id_emp)==TRUE){
				$DriverDateTimeStart = date_create($this->input->post('reportDatumZaciatok'.$i)." ".$this->input->post('reportHodinyZaciatok'.$i).":".$this->input->post('reportMinutyZaciatok'.$i));                    
				$DriverDateTimeEnd = date_create($this->input->post('reportDatumKoniec'.$i)." ".$this->input->post('reportHodinyKoniec'.$i).":".$this->input->post('reportMinutyKoniec'.$i));				
				$salary = calculateDriverSalary($DriverDateTimeStart,$DriverDateTimeEnd,$invEnd,$salaryPerHourDriver,$salaryPerHourEmployee,$inventoryInfo->Cas_cesty);	
				$this->model_lead_employee->insertIntoListDriver($id_rep,$id_emp,$DriverDateTimeStart,$DriverDateTimeEnd,round($salary,2));				
			} else {
				$EmployeeDateTimeStart = date_create($this->input->post('reportDatumZaciatok'.$i)." ".$this->input->post('reportHodinyZaciatok'.$i).":".$this->input->post('reportMinutyZaciatok'.$i));                    
				$EmployeeDateTimeEnd = date_create($this->input->post('reportDatumKoniec'.$i)." ".$this->input->post('reportHodinyKoniec'.$i).":".$this->input->post('reportMinutyKoniec'.$i));				
				$EmployeePause = $this->input->post('reportHodinyPauza'.$i).":".$this->input->post('reportMinutyPauza'.$i);
				$salary = calculateEmployeeSalary($EmployeeDateTimeStart,$EmployeeDateTimeEnd,$EmployeePause,$salaryPerHourEmployee,$salaryPerKilometer,$inventoryInfo->Vzdialenost);
				if($id_emp!=0){
				if($salary<3){
					$salary=0;
				}
				$this->model_lead_employee->insertIntoListEmployee($id_rep,$id_emp,$EmployeeDateTimeStart,$EmployeeDateTimeEnd,$EmployeePause,round($salary,2));					
				}
			}
		}
		
		redirect(base_url('lead_employee/getInventories/4'));
	}
	
public function createNewReportAuto($id_inv){
	
	function checkDocumentDate($invDate,$date){
		if (strlen($date)>16 OR strlen($date)<15){
			return substr($invDate,0,10)." 00:00";
		}else{
			return $date;
		}
	}
	
	function checkPasuseTime($time){
		if (strlen($time)<>5){
			return "00:00";
		} else {
			return $time;
		}
	}
	
	$this->load->library('Excel');
	
	set_include_path(get_include_path() . PATH_SEPARATOR . './Classes/');
	
	$fileType = 'Excel5';
	$fileName = $_FILES['fileUpload']['tmp_name'];
	$Reader = PHPExcel_IOFactory::createReader('Excel5');
	$Excel = $Reader->load($_FILES['fileUpload']['tmp_name']);
	
	if($Excel->getActiveSheet()->getCell('C1')->getValue()!=$id_inv){
		$data['id_inv']=$id_inv;
		$data['mode']=1;
		$data['error']='Obsah súboru neodpovedá danej inventúre! Vyberte prosím správny súbor.';			
	} else {		
		$this->load->model('model_lead_employee');
		$inventoryInfo=$this->model_lead_employee-> getInventoryInfo($id_inv);
		$countEmployees = count($this -> model_lead_employee -> getInventoryEmployees($id_inv,$inventoryInfo->ID_Veduci));
		
		$drivers = $this -> model_lead_employee -> getInventoryReservations($id_inv);
		
		if($drivers==NULL OR $drivers==Array()){
			$countDrivers=0;	
		}else{
			$countDrivers=0;
			foreach($drivers as $driver){
				if($driver->ID_SOF!=0){
					$countDrivers=$countDrivers+1;		
				}
			}		
		}
		$data['inventory_start']=$Excel->getActiveSheet()->getCell('I6')->getValue();
		$data['inventory_end']=$Excel->getActiveSheet()->getCell('I7')->getValue();
		$employees=array();
		for($i=10; $i<($countDrivers+$countEmployees+10); $i++){
			if($Excel->getActiveSheet()->getCell('G'.$i)->getValue()=="storno" OR $Excel->getActiveSheet()->getCell('I'.$i)->getValue()=="storno" OR $Excel->getActiveSheet()->getCell('K'.$i)->getValue()=="storno"){
			$x = array(
				 'EmpNumber' => $Excel->getActiveSheet()->getCell('A'.$i)->getValue(),
				 'EmpID' => $Excel->getActiveSheet()->getCell('B'.$i)->getValue(),
				 'EmpLastName' => $Excel->getActiveSheet()->getCell('C'.$i)->getValue(),
				 'EmpFirstName' => $Excel->getActiveSheet()->getCell('E'.$i)->getValue(),
				 'EmpStart' => $Excel->getActiveSheet()->getCell('I6')->getValue(),
				 'EmpEnd' => $Excel->getActiveSheet()->getCell('I6')->getValue(),
				 'EmpPause' => checkPasuseTime($Excel->getActiveSheet()->getCell('K'.$i)->getValue()) 		
			);	
			}else{
			$x = array(
				 'EmpNumber' => $Excel->getActiveSheet()->getCell('A'.$i)->getValue(),
				 'EmpID' => $Excel->getActiveSheet()->getCell('B'.$i)->getValue(),
				 'EmpLastName' => $Excel->getActiveSheet()->getCell('C'.$i)->getValue(),
				 'EmpFirstName' => $Excel->getActiveSheet()->getCell('E'.$i)->getValue(),
				 'EmpStart' => checkDocumentDate($data['inventory_start'],$Excel->getActiveSheet()->getCell('G'.$i)->getValue()),
				 'EmpEnd' => checkDocumentDate($data['inventory_end'],$Excel->getActiveSheet()->getCell('I'.$i)->getValue()),
				 'EmpPause' => checkPasuseTime($Excel->getActiveSheet()->getCell('K'.$i)->getValue()) 		
			);	
			}
			
		array_push($employees, $x);					
		}
			$data['id_inv']=$id_inv;
			$data['mode']=2;
			$data['employees']=$employees;
			$data['auto']=TRUE;		
		}
		$this->redirectFormReport($data);	
	}
	
	public function redirectFormReport($data){
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_add_report', $data);
		$this->load->view('include/footer');
	}
	
	
		
		

	public function redirectAddReport($modeInv, $mode,$id_inv){
		$data['modeInv']=$modeInv;
		$data['id_inv']=$id_inv;
		$data['mode']=$mode;
		$data['error']='';
		$data['auto']=FALSE;
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_add_report', $data);
		$this->load->view('include/footer');	
	}
	
	public function redirectEditReport($modeInv, $id_inv){
		$this->load->model('model_lead_employee');
		$data['inventoryInfo'] = $this->model_lead_employee->getGeneralInventoryInfo($id_inv);
		$data['leadEmployee'] = $this->model_lead_employee->getEmployeeInfo($data['inventoryInfo']->ID_Veduci);
		$data['reportInfo'] = $this->model_lead_employee->getInventoryReport($id_inv);
		
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_edit_report', $data);
		$this->load->view('include/footer');	
	}
	
	public function redirectDetailEmployee($modeInv, $modeDet, $id_emp){
		$this->load->model('model_employee');
		$data['modeInv']=$modeInv;
		$data['id_emp']=$id_emp;
		$data['modeDet']=$modeDet;
		$type_emp = $this->model_employee->getEmployeeType($id_emp);
		$data['type_emp']=$type_emp;
		$limit=5;
		switch($modeDet){
			case 1: 
				$data['employee']=$this->model_employee->getGeneralEmployeeInfo($id_emp);
			break; 
			case 2: 
				switch($type_emp){
					case 1: 
						$data['inventories'] = FALSE;
					break;
					case 2:
						$data['inventories'] = $this->model_employee->getLastInventoriesLeadEmployee($id_emp, $limit);
					break;
					case 3:
						$data['inventories'] = $this->model_employee->getLastInventoriesDriver($id_emp, $limit);
					break;
					case 4:
						$data['inventories'] = $this->model_employee->getLastInventoriesEmployee($id_emp, $limit);
					break;
				}
			break;
		}
		$this->load->view('include/header');
		$this->load->view('lead_employee/lead_employee_employee_detail',$data);
		$this->load->view('include/footer');	
	}
	
	public function getConfigurationForPagination($pageLimit, $base_url, $totalRows, $uriSegment){
		$configuration=array();
		$configuration['per_page'] = $pageLimit;
		$configuration['base_url'] = $base_url;	
		$configuration['total_rows'] = $totalRows;
		$configuration['uri_segment'] = intval($uriSegment);
		
		$configuration['full_tag_open'] = '<ul class="pagination">';
  		$configuration['full_tag_close'] = '</ul></div><!--pagination-->';
		$configuration['first_link'] = '&laquo;';
		$configuration['first_tag_open'] = '<li class="prev page">';
		$configuration['first_tag_close'] = '</li>';
		$configuration['last_link'] = '&raquo;';
		$configuration['last_tag_open'] = '<li class="next page">';
		$configuration['last_tag_close'] = '</li>';
		$configuration['next_link'] = '&rarr;';
		$configuration['next_tag_open'] = '<li class="next page">';
		$configuration['next_tag_close'] = '</li>';
		$configuration['prev_link'] = '&larr;';
		$configuration['prev_tag_open'] = '<li class="prev page">';
		$configuration['prev_tag_close'] = '</li>';
		$configuration['cur_tag_open'] = '<li><a href="" class="active">';
		$configuration['cur_tag_close'] = '</a></li>';
		$configuration['num_tag_open'] = '<li class="page">';
		$configuration['num_tag_close'] = '</li>';
		
		return $configuration;
	}
	
}
		
	
	
	
		
		
	
		
		



	


