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

class Settings extends CI_Controller {		
	
public function createPayCheckForMonthPDF($mode, $type, $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');
		$this->load->model('model_settings');
		
		$employees = $this->model_settings->getAllActualEmployees();
		
		
		$mpdf = new mPDF();
		
		$mpdf->useFixedNormalLineHeight = false;
		$mpdf->useFixedTextBaseline = false;
		$this->adjustFontDescLineheight = 1.14;
		
		$pageCounter=0;
		
		foreach ($employees as $employee){					
		$pageCounter++;
		
		if(strlen($month)==1){
			$month="0".$month;
		}
		
		$mena='€';
		$id_emp=getLongId($employee->ID_ZAM);
		$odpracHod=getCountHoursWorked($employee->ID_ZAM,$year,$month);
		$pocetAbse=getCountPenaltyEmp($employee->ID_ZAM,$year,$month);
		$mzdaZakla=getOnlySalaryGrossPayCheck($employee->ID_ZAM,$year,$month);
		$stronoHod=getTotalPenaltyPayCheck($employee->ID_ZAM,$year,$month);
		$invetories=getAllMyInventoriesPerMonth($employee->ID_ZAM,$year,$month);
		$stornos=getAllMyInventoriesPenaltiesPerMonth($employee->ID_ZAM,$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($employee->ID_ZAM);
		
		
		
		$employee = $this->model_employee->getInfoForPayCheck($employee->ID_ZAM);				
		
		

		$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>
		<br><br><br><br><br>
		');	

		}
		$mpdf->Output();
	
		exit;
}	
		
		
public function createSummaryPerMonthPDF($mode, $type, $year, $month){
	$this->load->library('m_pdf');
	$this->load->model('model_settings');
	
	if(strlen($month)==1){
		$month="0".$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;
		}	
	}
	
	function returnTypeInv($num){
		if($num<4){
			return "Predajňa";
		}else{
			return "Sklad";
		}
	}
	
	
		$mena='€';
		$mpdf = new mPDF();
		
		$mpdf->useFixedNormalLineHeight = false;
		$mpdf->useFixedTextBaseline = false;
		$this->adjustFontDescLineheight = 1.14;
		
	
	
	$inventories = $this->model_settings->getSummaryReportInventories($year,$month);

	foreach($inventories as $inventory){
		$inventoryInfo = $this->model_settings->getInventoryInfo($inventory->ID_INV);
		$vehiclesCount = $this->model_settings->getCountVehicles($inventory->ID_INV);
		$vehicles = $this->model_settings->getInventorzVehicles($inventory->ID_INV);
		$driversInter = $this->model_settings->getDriversInter($inventory->ID_INV);	
		$driversExter = $this->model_settings->getDriversExter($inventory->ID_INV);
		$leadEmployee = $this->model_settings->getInvrentoryLead($inventory->ID_INV);
		$employees = $this->model_settings->getEmployees($inventory->ID_INV);
		$reportTimes = $this->model_settings->getReportTimes($inventoryInfo->ID_Veduci,$inventory->ID_Report);
		
		$vehiclesHTML = "";
		foreach($vehicles as $vehicle){
			$vehiclesHTML=$vehiclesHTML."".$vehicle->SPZ." | ".$vehicle->Nazov." (";
			if(intval($vehicle->Pocet_miest)>9){
				$vehiclesHTML=$vehiclesHTML."externé vozidlo";	
			}else{
				$vehiclesHTML=$vehiclesHTML."interné vozidlo";	
			}
			$vehiclesHTML=$vehiclesHTML.")<br>";
		}
		
		$employeesHTML='';
		$salary=$this->model_settings->getSalaryEmployee($leadEmployee->ID_ZAM, $inventory->ID_Report);
		$employeesHTML=$employeesHTML."<tr><td width='35px'>VEI</td><td width='50px'>".getLongId($leadEmployee->ID_ZAM)."</td><td>".$leadEmployee->Priezvisko." ".$leadEmployee->Meno."</td><td style='padding-left:20px; width:70px'>".$salary." €</td></tr>";
		if(count($driversInter)!=0){
			foreach($driversInter as $driver){
				$salary=$this->model_settings->getSalaryEmployee($driver->ID_ZAM, $inventory->ID_Report);
				$employeesHTML=$employeesHTML."<tr><td width='35px'>SOF</td><td width='50px'>".getLongId($driver->ID_ZAM)."</td><td>".$driver->Priezvisko." ".$driver->Meno."</td><td style='padding-left:20px'>".$salary." €</td></tr>";	
			}
		}
		
		foreach($employees as $employee){
			if($employee->ID_ZAM!=$leadEmployee->ID_ZAM){
			$salary=$this->model_settings->getSalaryEmployee($employee->ID_ZAM, $inventory->ID_Report);
			$employeesHTML=$employeesHTML."<tr><td width='35px'>ZAM</td><td width='50px'>".getLongId($employee->ID_ZAM)."</td><td>".$employee->Priezvisko." ".$employee->Meno."</td><td style='padding-left:20px'>".$salary." €</td>";					
			if($salary==0){
				$employeesHTML=$employeesHTML."<td style='color:grey'>Poznámka: Absencia</td></tr>";	
			}else{
				$employeesHTML=$employeesHTML."</tr>";
			}
			}

		}
		
		
		$mpdf->WriteHTML('
						<table style="font-size:12px;font-family: Georgia; color:grey; width:100%; text-align:right; border: 0px solid black">
							<tr>
								<td width="500px" style="text-align:left">
									'.$inventoryInfo->Nazov_spolocnosti.' - '.$inventoryInfo->Mesto.'
								</td>
								<td>
									'.substr($inventoryInfo->Datum_zaciatku,0,4).''.substr($inventoryInfo->Datum_zaciatku,5,2).''.substr($inventoryInfo->Datum_zaciatku,8,2).''.getLongId($inventoryInfo->ID_INV).'
								</td>								
							</tr>
							<tr>
						</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia">
							<tr>
								<td width="230px">
									<b>Identifikačné číslo inventúry:
								</td>
								<td>
									'.getLongId($inventoryInfo->ID_INV).'
								</td>
							</tr>
							<tr>
								<td>
									<b>Typ inventúry:
								</td>
								<td>
									'.returnTypeInv($inventoryInfo->ID_TYP_R).'
								</td>
							</tr>						
							<tr>
								<td>
									<b>Typ spoločnosti:
								</td>
								<td>
									'.$inventoryInfo->Nazov_typ_obchodu.'
								</td>
							</tr>
							<tr>
								<td>
									<b>Názov spoločnosti:
								</td>
								<td>
									'.$inventoryInfo->Nazov_spolocnosti.'
								</td>
							</tr>
							<tr>
								<td>
									<b>Adresa spoločnosti:
								</td>
								<td>
									'.$inventoryInfo->Ulica.' '.$inventoryInfo->Cislo.',
								</td>
							</tr>
							<tr>
								<td>
									
								</td>
								<td>
									'.$inventoryInfo->PSC.', '.$inventoryInfo->Mesto.'
								</td>
							</tr>
						</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia;">
							<tr>
								<td style="width:230px">
									<b>Zodpovedný vedúci:
								</td>								
								<td>
									'.$leadEmployee->Priezvisko.' '.$leadEmployee->Meno.' ('.getLongId($leadEmployee->ID_ZAM).')
								</td>								
							</tr>
							<tr>
								<td style="width:230px">
								
								</td>								
								<td>
									'.$leadEmployee->Email.'	
								</td>								
							</tr>
							<tr>
								<td style="width:230px">
								
								</td>								
								<td>
									00421 '.substr($leadEmployee->Tel_cislo_zamestnanca,0,3).' '.substr($leadEmployee->Tel_cislo_zamestnanca,3,3).' '.substr($leadEmployee->Tel_cislo_zamestnanca,6,3).'	
								</td>								
							</tr>
						</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia;">
							<tr>
								<td style="width:230px">
									<b>Začiatok:
								</td>								
								<td>
									'.date("d.m.Y H:i",strtotime($reportTimes->Nastup)).'
								</td>								
							</tr>
							<tr>
								<td>
									<b>Koniec:
								</td>								
								<td>
									'.date("d.m.Y H:i",strtotime($reportTimes->Koniec)).'
								</td>								
							</tr>
							<tr>
								<td>
									<b>Čas jednosmernej cesty:
								</td>								
								<td>
									'.$inventoryInfo->Cas_cesty.'
								</td>								
							</tr>
							<tr>
								<td>
									<b>Vzdialenosť jednosmernej cesty:
								</td>								
								<td>
									'.$inventoryInfo->Vzdialenost.' Km
								</td>								
							</tr>	
							</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia;">						
							<tr>
								<td style="width:230px">
									<b>Počet interných vozidiel:
								</td>								
								<td width="150px">
									'.$vehiclesCount.'
								</td>						
							<tr>
								<td>
									<b>Počet interných vodičov:
								</td>								
								<td>
									'.count($driversInter).'
								</td>
							</tr>
							<tr>
								<td>
									<b>Počet externých vozidiel:
								</td>								
								<td>
									'.count($driversExter).'
								</td>									
							</tr>
							<tr>
							<td style="width:200px">
									<b>Počet externých vodičov:
								</td>
								<td>
									'.count($driversExter).'
								</td>								
							</tr>
							<tr>
								<td>
									<b>Počet zamestnancov:
								</td>								
								<td>
									'.(count($employees)-1).'
								</td>								
							</tr>
						</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia;">						
							<tr>
								<td style="width:230px">
									<b>Vozidlá:
								</td>								
								<td width="400px">
									'.$vehiclesHTML.'
								</td>						
							<tr>
						</table>
						<div style="border-bottom:1px solid #B0B0B0; width:100%; margin-top:5px; margin-bottom:5px"></div>
						<table style="font-size:12px;font-family: Georgia;">						
							<tr>
								<td>
									<b>Inventúru absolvovali:
								</td>								
														
							<tr>
						</table>
						<table style="font-size:12px;font-family: Georgia;">
							<tr>
								<td>
									'.$employeesHTML.'
								</td>								
													
							<tr>
						</table>
		');
		$mpdf->AddPage();
	}
	$mpdf->Output();
	Exit;
}

		
}		


		
	
	
	
		
		
	
		
		



	


