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

class Employee extends CI_Controller {		
		
	public function getInventories($mode) { 
		$this->load->model('model_inventory');
		$data['mode']=$mode; 
			switch($mode){
				case 1: 
					$data['inventories'] = $this->model_inventory->getBasicInfo();
				break;
				case 2: 
					$data['inventories'] = $this->model_inventory->getBasicInfo();
				break;
				case 3: 
					$data['inventories'] = $this->model_inventory->getMyInventories($this->session->userdata['id_zamestnanca']);
				break;
				case 4: 
					$data['inventories'] = $this->model_inventory->getMyArchive($this->session->userdata['id_zamestnanca']);
				break;
			}	
		$this->load->view('include/header');
		$this->load->view('employee/employee_inventory', $data);
		$this->load->view('include/footer');
	}
	
	public function LogOnInventory2($mode,$id_inv) { 
		$this->load->model('model_inventory');
		$this->model_inventory->LogOnInventory2($id_inv, $this->session->userdata['id_zamestnanca']);
		$Reservations = $this->model_inventory->getInvetoryReservations($id_inv);
		$Status=FALSE;
			 
		foreach ($Reservations as $Reservation) {
			$Count = $this->model_inventory->getCountOfEmployeesOnInv($Reservation->ID_REZ);
			$Seats = $this->model_inventory->getNumberOfSeats($Reservation->ID_REZ);
			if ((($Count->Pocet)<($Seats->Pocet_miest-1)) AND ($Status==FALSE)) {
				$this->model_inventory->LogOnReservation($Reservation->ID_REZ,$this->session->userdata['id_zamestnanca']);
				$Status=TRUE;				
				redirect(base_url('employee/getInventories/'.$mode));
			}
		}
	}
	
	
	public function LogOutInventory($id_inv) { 
		$this->load->model('model_inventory');
		$this->model_inventory->LogOutInventory($id_inv, $this->session->userdata['id_zamestnanca'],$this->model_inventory->getReservationInfo($id_inv, $this->session->userdata['id_zamestnanca']));
		redirect(base_url().'employee/getInventories/3');
	}
		
	public function DetailOfInventory($modeInv, $modeDet, $id_inv) {
		$this->load->model('model_lead_employee');
			$data['modeInv']=$modeInv; 
			$data['modeDet']=$modeDet;
			$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;
			}			
		$this->load->view('employee/employee_inventory_detail', $data);
		$this->load->view('include/footer');
	}
	
	public function redirectPayChecks($year){
		$this->load->model('model_employee');
		
		$data['year']=$year;
		$data['startDate']=$this->model_employee->whenIStarted($this->session->userdata['id_zamestnanca']);	
		if($this->model_employee->whenIEnded($this->session->userdata['id_zamestnanca'])==NULL){
			$data['endDate']=date("Y-m-d");	
		}else{
			$data['endDate']-$this->model_employee->whenIEnded($this->session->userdata['id_zamestnanca']);		
		}
		$this->load->view('include/header');
		$this->load->view('employee/employee_pay_checks', $data);
		$this->load->view('include/footer');
	}
	
	
	
	public function createPayCheckPdf($mode,$year,$month){
		function getLongId($id){
		if (strlen($id)==1) {
			return "0000".$id;
		} elseif (strlen($id)==2) {
			return "000".$id;
		} elseif (strlen($id)==3) {
			return "00".$id;
		} elseif (strlen($id)==4) {
			return "0".$id;
		} else {
			return $id;
		}	
	}
		
		
		$this->load->library('m_pdf');
		$this->load->model('model_employee');
		$this->load->helper('graph_helper');
		$this->load->helper('payCheck_helper');
		if(strlen($month)==1){
			$month="0".$month;
		}
		
		$mena='€';
		$id_emp=getLongId($this->session->userdata['id_zamestnanca']);
		$odpracHod=getCountHoursWorked($this->session->userdata['id_zamestnanca'],$year,$month);
		$pocetAbse=getCountPenaltyEmp($this->session->userdata['id_zamestnanca'],$year,$month);
		$mzdaZakla=getOnlySalaryGrossPayCheck($this->session->userdata['id_zamestnanca'],$year,$month);
		$stronoHod=getTotalPenaltyPayCheck($this->session->userdata['id_zamestnanca'],$year,$month);
		$invetories=getAllMyInventoriesPerMonth($this->session->userdata['id_zamestnanca'],$year,$month);
		$stornos=getAllMyInventoriesPenaltiesPerMonth($this->session->userdata['id_zamestnanca'],$year,$month);		
		$inventoriesData="<br>";
		$stornoData="<br>";
		$mzdaCompe=$mzdaZakla+$stronoHod;
		if($mzdaCompe<0){
			$mzdaCompe=0;
		}
		
		foreach($invetories as $inventory){
			$inventoriesData=$inventoriesData.getLongId($inventory->ID_INV)." | ".date('d.m.Y', strtotime($inventory->Datum_zaciatku))." | ".$inventory->Nazov_spolocnosti." - ".$inventory->Mesto." Vyplatené: ".$inventory->Vyplatene." ".$mena."<br>";
		}
		
		foreach($stornos as $storno){
			$stornoData=$stornoData."STORNO | ".date('d.m.Y', strtotime($storno->Koniec))." | Strhnuté zo mzdy: ".$storno->Vyplatene."<br>";
		}
		
		switch($month){
			case 1: $dateWord = "Január ".$year; break;
			case 2: $dateWord = "Február ".$year; break;
			case 3: $dateWord = "Marec ".$year; break;
			case 4: $dateWord = "Apríl ".$year; break;
			case 5: $dateWord = "Máj ".$year; break;
			case 6: $dateWord = "Jún ".$year; break;
			case 7: $dateWord = "Júl ".$year; break;
			case 8: $dateWord = "August ".$year; break;
			case 9: $dateWord = "September ".$year; break;
			case 10: $dateWord = "Október ".$year; break;
			case 11: $dateWord = "November ".$year; break;
			case 12: $dateWord = "December ".$year; break;
		}
		$id_emp=getLongId($this->session->userdata['id_zamestnanca']);
		
		
		
		$employee = $this->model_employee->getInfoForPayCheck($this->session->userdata['id_zamestnanca']);				
		
		$mpdf = new mPDF();
		
		
		
		$mpdf->useFixedNormalLineHeight = false;
		$mpdf->useFixedTextBaseline = false;
		$this->adjustFontDescLineheight = 1.14;

		$mpdf->WriteHTML('
		<table style="border: 1px solid black; font-size:11px">
			<tr>
				<td style="width:10px;">						
				</td>
				<td style="width:180px; vertical-align: top;">
					<div><br>Výplatná páska </div>							
				</td>
				<td style="width:200px; vertical-align: top;">
					<div><br>Názov spoločnosti</div>							
				</td>
				<td style="width:330;text-align:right; vertical-align: top;">
					<div><br>'.$dateWord.'</div>							
				</td>
				<td style="width:10px;">					
				</td>
			</tr>
			<tr>
				<td style="width:10px;">						
				</td>
				<td style="width:180px">
					<div>'.$year.$month.$id_emp.'</div>							
				</td>
				<td style="width:200px">
					<div><b>('.getLongId($id_emp).') '.$employee->Meno.' '.$employee->Priezvisko.'</b></div>							
				</td>
				<td style="width:330;text-align:right;">
					<div>('.$employee->Kod_poistovne.') '.$employee->Nazov_poistovne.'</div>							
				</td>
				<td style="width:10px;">					
				</td>
			</tr>	
			<tr>
				<td>
					<br>
				</td>
			</tr>		
		</table>
		<table style="border: 1px solid black; border-top:0px; font-size:11px">
			<tr>
				<td style="width:10px">
				</td>
				<td style="width:100px; vertical-align: top;">
						<br>
						Odpracované<br>
						Dovolenka<br>
						Sviatky<br>
						Nadčas<br>
						Nočné<br>
						Poh.na prac.<br>
						Pod. doma<br>
						Nemoc<br>	
						<br><br>									
				</td>
				<td style="width:70px;text-align:right; vertical-align: top;">
						<br>
						'.$odpracHod.',00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						<br><br>										
				</td>
				<td style="width:10px">
				</td>
				<td style="width:100px; vertical-align: top;">
						<br>
						Prerážky<br>
						Plat.voľno<br>
						Neplat. voľno<br>
						Náhr. voľno<br>
						Absencia<br>
						Vyňatie<br>
						Iné<br><br>
																									
				</td>
				<td style="width:70px;text-align:right; vertical-align: top;">
						<br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						'.$pocetAbse.',00 <br>
						0,00 <br>
						0,00 <br>
							
														
				</td>
				<td style="width:10px; border-right:1px solid black">
				</td>
				
				<td style="width:10px">
				</td>
				<td style="width:100px;">
						Základná mzda<br>
						Mzda za nadčas<br>
						Nadčasový prípl.<br>
						Ostatné príplatky<br>
						Náhrady<br>
						Prémie a odmeny<br>
						Jednoráz. odmeny<br>
						Ostatné odmeny<br>
																
				</td>
				<td style="width:70px;text-align:right; vertical-align: top;">
						<br>
						'.$mzdaZakla.',00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>										
				</td>
				<td style="width:10px">
				</td>
				<td style="width:100px; vertical-align: top;">
						<br>
						Odmena štatutára<br>
						Súbežný pr. pom<br>
						Práca v zahraničí<br>
						Zo soc. fondu<br>
						Nat príjem zo SF<br>
						Naturálie<br>
						Fiktívna mzda<br>
						Storno<br>
																											
				</td>
				<td style="width:70px;text-align:right; vertical-align: top;">
						<br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						0,00 <br>
						'.$stronoHod.',00 <br>																
				</td>
				<td style="width:10px;">
				</td>
			</tr>
		</table>
		<table style="border: 1px solid black; border-top:0px; font-size:11px">	
			<tr>
				<td style="width:10px;">
				</td>
				<td style="width:350px; vertical-align: top;">
					'.$inventoriesData.$stornoData.'
				</td>
				<td style="width:10px;">
				</td>
				<td style="width:10px; border-right:1px solid black">
				</td>
				<td style="width:10px;">
				</td>
				<td style="width:160px; vertical-align:top;">
					<br>
					Hrubá mzda<br>
					<br>
					Starobné poistenie<br>
					Invalidné poistenie<br>
					Sociálne poistenie spolu<br>
					Zdravotné poistenie<br>
					<br>
					Iné príjmy<br>
					Daň zo mzdy<br>
					Čistá mzda<br>
					<br>
					K výplate v hotovosti<br>
					K výplate bankovým prevodom na účet <b>'.$employee->Cislo_uctu.'/'.$employee->Kod_banky.'</b><br>
				</td>
				<td style="width:200px; vertical-align: top;  text-align:right">
					<br>
					'.($mzdaCompe).' '.$mena.'
					<br><br>
					'.round((($mzdaCompe)*0.04),2).' '.$mena.'<br>
					'.round((($mzdaCompe)*0.03),2).' '.$mena.'<br>
					'.round((($mzdaCompe)*0.07),2).' '.$mena.'<br>
					'.round((($mzdaCompe)*0.04),2).' '.$mena.'<br>
					<br>
					0 '.$mena.'<br>
					'.round((($mzdaCompe)*0.1691),2).' '.$mena.'<br>
					'.(($mzdaCompe)-round((($mzdaCompe)*0.07),2)-round((($mzdaCompe)*0.04),2)-round((($mzdaCompe)*0.1691),2)).' '.$mena.'<br>
					<br>
					0 '.$mena.'<br>
					<br>
					<b>'.(($mzdaCompe)-round((($mzdaCompe)*0.07),2)-round((($mzdaCompe)*0.04),2)-round((($mzdaCompe)*0.1691),2)).' '.$mena.'</b><br>
					<br>
					<br>
				</td>
				<td style="width:10px;">
				</td>
			</tr>				
		</table>
		<table style="border: 1px solid black; border-top:0px; font-size:11px; width:800px; text-align:center">	
			<tr>
				<td>
					
						V prípade akých koľvek nezhôd a pripomienok kontaktujte prosím manažéra spoločnosti. Kontaktné údaje najdete v informačnom systéme.
					<br>
				</td>
			</tr>
		</table>
		');
		$mpdf->Output();
	
		exit;


		/*$pdfFilePath = "output_pdf_name.pdf";
		$html=$this->load->view('employee/employee');
		$this->load->library('m_pdf');
		$this->m_pdf->pdf->WriteHTML($html);
		$this->m_pdf->pdf->Output($pdfFilePath, "D");*/
	}
	
	
}		
		
	
	
	
		
		
	
		
		



	


