<?
	header("Content-Type: text/html; charset=utf-8");
	
	$g_path = '../../my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	
	include $g_path."u_headers.php";
	
	
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
	ini_set('display_errors', '1');
	
	function pdf_my_write($pdf, $x, $y, $size, $text){
		$pdf->SetFont('ArialCE', '', $size);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->SetXY($x, $y);
		$pdf->Write(0, $text);
	}
	
	function _convert($str){
		//return iconv('utf-8', 'iso-8859-2//TRANSLIT', utf8_decode($str));
		return iconv('utf-8', 'iso-8859-2//TRANSLIT', $str);
	} 
	
	$l_person_id = $_SESSION['__id'];
	
	define("FPDF_FONTPATH", "font/");
	require_once('../../my/class/fpdf.php');
	require_once('../../my/class/fpdi.php');
	
	$pdf =& new FPDI('P', 'mm', 'A4');
	
	$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
	$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
	
	$pdf->AddPage();
	$pdf->SetFont('ArialCE', '', 9);
	
	$l_t1 = 'U skladu s odredbom članka 6. Pravilnika o kriterijima za određivanje namjene pojedinog dijela luke otvorene za javni promet županijskog i lokalnog značaja, način plaćanja veza, uvjete korištenja, te određivanja maksimalne visine naknade i raspodjele prihoda';
	$pdf->SetXY(20, 13);
	$pdf->MultiCell(170, 4, _convert($l_t1), 0, "L");
	
	$l_t2 = 'LUČKA UPRAVA POREČ - AUTORITÀ PORTUALE DI PARENZO, Obala M. Tita 6, OIB 71925651469, koju zastupa ravnatelj Željko Čorak /dalje u tekstu: Lučka uprava/';
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t2), 0, "L");
	
	$x = get_data('select * from evidencija_plovila where guid = "'.$_GET['id'].'" ', 0);
	
	$y = get_data('select * from poslovni_partner where guid = "'.$x['vlasnik_guid'].'" ', 0);
	$l_t3 = $y['PunoIme'].' (OIB '.$y['MB'].'), iz '.$y['Mjesto'].', ulica '.$y['Adresa1'].' /dalje u tekstu korisnik/, s druge strane, zaključili su, sljedeći:';
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t3), 0, "L");
	
	$pdf->SetFont('ArialCE', 'B', 14);
	$pdf->SetXY(20, $pdf->GetY() + 5);
	$pdf->MultiCell(170, 4, _convert('UGOVOR'), 0, "C");
	
	$pdf->SetFont('ArialCE', 'B', 12);
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($x['Ugovor'].'/'.$x['Godina']), 0, "C");
	$pdf->SetFont('ArialCE', '', 9);
	
	$l_t4 = 'o stalnom vezu u komunalnom dijelu luke Lučke uprave Poreč';
	$pdf->SetXY(20, $pdf->GetY() + 4);
	$pdf->MultiCell(170, 4, _convert($l_t4), 0, "C");
	
	$pdf->SetFont('ArialCE', 'B', 10);
	$pdf->SetXY(20, $pdf->GetY() + 8);
	$pdf->MultiCell(170, 4, _convert('Predmet ugovora'), 0, "L");
	
	
	
	$pdf->SetFont('ArialCE', 'B', 10);
	$pdf->SetXY(20, $pdf->GetY() + 6);
	$pdf->MultiCell(170, 4, _convert('Članak 1'), 0, "C");
	
	$l_t11 = 'Ovim ugovorom njegove ugovorne stranke uređuju međusobne odnose vezane za upotrebu obale u Lučkoj upravi Poreč, u svrhu stalnog veza. /dalje u tekstu: Luka/.'.
		"\n".
		"\n".
		'Mjesto i broj privremenog veza, te oznaka plovila (broda/ brodice)';
	$pdf->SetFont('ArialCE', '', 9);
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t11), 0, "L");
	
	
	$pdf->SetFont('ArialCE', 'B', 10);
	$pdf->SetXY(20, $pdf->GetY() + 6);
	$pdf->MultiCell(170, 4, _convert('Članak 2'), 0, "C");
	
	$l_t12 = 'Lučka uprava daje, a korisnik prima na korištenje vez s mjestom priveza na obilježenom vezu u luci Červar, komunalni, kč-50, u komunalnom dijelu Luke /dalje u tekstu: stalni vez/';
	$pdf->SetFont('ArialCE', '', 9);
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t12), 0, "L");
	
	
	$pdf->SetFont('ArialCE', 'B', 10);
	$pdf->SetXY(20, $pdf->GetY() + 6);
	$pdf->MultiCell(170, 4, _convert('Članak 3'), 0, "C");
	
	$y = get_data('select Naziv from tip_plovila where guid = "'.$x['tip_plovila_guid'].'" ', 0);
	$l_t13 = 'Korisnik će stalni vez koristiti isključivo za privez plovila u njegovom vlasništvu /dalje u tekstu: plovilo/, slijedećih identifikacijskih oznaka:'.
		"\n".
		'- registarska oznaka / ime broda: '.$x['RegistarskaOznaka'].'/'.$x['ImeBroda'].
		"\n".
		'- tip: '.$y['Naziv'].
		"\n".
		'- dužina: '.$x['Duzina'];
	$pdf->SetFont('ArialCE', '', 9);
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t13), 0, "L");
	
	
	$pdf->SetFont('ArialCE', 'B', 10);
	$pdf->SetXY(20, $pdf->GetY() + 6);
	$pdf->MultiCell(170, 4, _convert('Članak 4'), 0, "C");
	
	$l_t14 = 'U slučaju promjene prava vlasništva na plovilu, novi vlasnik plovila ne stječe pravo na korištenje veza iz članka 2. ovog ugovora, već se ovaj ugovor raskida u skladu s njegovim raskidnim klauzulama.'.
		"\n".
		"\n".
		'U slučaju da korisnik, prije dana upisa u upisnik brodova ili brodica promjene prava vlasništva na plovilu, obavijesti Lučku upravu i zatraži njezin pristanak na prijenos ugovora na novog vlasnika, Lučka uprava će dati svoj pristanak na ugovor o prijenosu ovog ugovora na novog vlasnika, pod uvjetom da novi vlasnik ispunjava kriterije propisane Pravilnikom o kriterijima za određivanje namjene pojedinog dijela luke otvorene za javni promet županijskog i lokalnog značaja, način plaćanja veza, uvjete korištenja, te određivanja maksimalne visine naknade i raspodjele prihoda, te uvjete i kriterije prvenstva pri sklapanju ugovora o stalnom vezu propisan općim aktima Lučke uprave, donesenim na temelju tog Pravilnika.';
	$pdf->SetFont('ArialCE', '', 9);
	$pdf->SetXY(20, $pdf->GetY() + 2);
	$pdf->MultiCell(170, 4, _convert($l_t14), 0, "L");
	
	$y = get_data('select max(guid) as max from evidencija_plovila_datoteka where evidencija_plovila_guid = "'.$x['guid'].'" ', 0);
	$la_tmp = explode('-', $y['max']);
	$l_num = (1 + intval($la_tmp[sizeof($la_tmp) - 1]));
	
	$l_dest = $g_path_res.'files/'.mk_f_name($l_person_id).'/Ugovor-'.$_GET['id'].'-'.$l_num.'.pdf';
	
	$pdf->Output($l_dest, 'F');
	
	
	$la_data[0] = 'ok';
	$la_data[1] = $_GET['c'];
	$la_data[2] = 'Ugovor '.$x['Ugovor'].'/'.$x['Godina'];
	$la_data[3] = 'res/files/'.mk_f_name($l_person_id).'/Ugovor-'.$_GET['id'].'-'.$l_num.'.pdf';
	//$la_data[4] = $l_num;
	//$la_data[1] = mk_f_name($l_person_id).'/Ugovor-'.$_GET['id'].'-'.$l_num.'.pdf';
	
	$la_json = json_encode($la_data);
	print $la_json;
	
	exit;









				
	
	
	
	if($_GET['id'] && $_GET['id'] != '-1'){
		$l_person_id = get_item('select rbr_users_outer_db from zaposlenik where guid = "'.$_GET['id'].'" ');
	}
	if(!$l_person_id){
		$l_person_id = $_SESSION['__id'];
	}
	
	
	$x = get_data('select * from izlazni_racuni where guid = "'.$_GET['rbr'].'" ', 0);
	
	for($i = 1; $i <= $pgNumber; $i++){
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage($i);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 210);
		
		$la_tmp_fiskal = explode('/', $x['Broj']);
		$l_broj = intval($la_tmp_fiskal[1]).'/'.$la_tmp_fiskal[2].'/'.$la_tmp_fiskal[3];
	
		pdf_my_write($pdf, 20, 45, 14, _convert('Račun: '.$l_broj));
		pdf_my_write($pdf, 110, 45, 10, _convert('Datum računa: '.u_datum($x['Datum'], 1).'-'.u_mjesec($x['Datum'], 1).'-'.u_godina($x['Datum'])));
		pdf_my_write($pdf, 110, 50, 10, _convert('Datum dospijeća: '.u_datum($x['Dospijece'], 1).'-'.u_mjesec($x['Dospijece'], 1).'-'.u_godina($x['Dospijece'])));
		pdf_my_write($pdf, 180, 47, 18, 'R1');
		pdf_my_write($pdf, 110, 58, 10, _convert('Vrijeme izdavanja: '.u_datum($x['Datum'], 1).'-'.u_mjesec($x['Datum'], 1).'-'.u_godina($x['Datum']).' '.u_sat($x['Datum']).':'.u_minuta($x['Datum']).'h'));
		pdf_my_write($pdf, 110, 63, 10, _convert('Mjesto izdavanja: '.$x['mjesto_izdavanja']));
		
		$y = get_data('select MB, PunoIme, Adresa1, Posta, Mjesto, Zemlja from poslovni_partner where guid = "'.$x['kupac_guid'].'" ', 0);
		pdf_my_write($pdf, 20, 53, 10, _convert($y['PunoIme']));
		pdf_my_write($pdf, 20, 57, 10, _convert('OIB: '.$y['MB']));
		pdf_my_write($pdf, 20, 61, 10, _convert($y['Adresa1']));
		pdf_my_write($pdf, 20, 65, 10, _convert($y['Posta'].' '.$y['Mjesto'].', '.$y['Zemlja']));
		
		
		//$la_width = array(10, 35, 45, 10, 35, 45, 10, 35, 45, 10, 35, 45, 10, 35, 45, 10, 35, 45);
		//$la_border = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		//$la_end = array(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1);
		$la_width_header = array(10, 5, 15, 52, 11, 14, 20, 10, 14, 27, 2);
		$la_border_header = array(0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0);
		//$la_tekst_header = array('', '#', 'Oznaka', 'Jezik', 'Medij', 'Jedinica mjere', 'Količina', 'Jedinična cijena (kn)', 'Popust (%)', 'PDV (%)', 'Iznos (kn)');
		$la_tekst_header = array('', '#', 'Šifra', 'Naziv', 'JM', 'Količina', 'Cijena', 'Dana', 'Rabat (%)', 'Iznos', '');
		$la_end_header = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1);
		$la_multi = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		
		
		$pdf->SetFont("ArialCE", "B", 7);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->SetXY(10, 80);
		
		$l_left = 10;
		for($i = 0; $i < 11; $i++){
			$l_left += $la_width_header[$i];
			if($la_multi[$i] == 1){
				$yb = $pdf->GetY();
				$pdf->MultiCell($la_width_header[$i], 2.8, _convert($la_tekst_header[$i]), $la_border_header[$i], "C");
				$ya = $pdf->GetY();
				$yH = $ya - $yb;
				$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			}else{
				$pdf->Cell($la_width_header[$i], 5.6, _convert($la_tekst_header[$i]), $la_border_header[$i], $la_end_header[$i], "C");
			}
		}
		
		//$pdf->Ln(1);
		
		$l_valuta = get_item('select ValutaID from tecajna_lista where guid = "'.$x['tecajna_lista_guid'].'" ');
		$la_valuta = array('', '', '', '', '', '', ' '.$l_valuta, '', '', ' '.$l_valuta);
		
		$la_name = array('', '', 'Sifra', 'Naziv', 'jedinica_mjere_guid', 'Kolicina', 'Cijena', 'Dana', 'Rabat', 'Ukupno');
		
		$pdf->SetFont("ArialCE", "", 7);
		$y = get_data('select * from izlazni_racuni_stavke where izlazni_racuni_guid = "'.$x['guid'].'" ', 1);
		$l_sum = 0;
		for($i = 0; $i < sizeof($y['guid']); $i++){
			$yb = $pdf->GetY();
			$l_left = 10;
			for($j = 0; $j < 11; $j++){
				$l_left += $la_width_header[$j];
				$yb = $pdf->GetY();
				
				switch($j){
					case 0:
						$pdf->MultiCell($la_width_header[$j], 5, _convert(''), $la_border_header[$j], "C");
						break;
						
					case 1:
						$pdf->MultiCell($la_width_header[$j], 5, _convert($i + 1), $la_border_header[$j], "C");
						break;
						
					case 4:
						$y[$la_name[$j]][$i] = get_item('select Naziv from jedinica_mjere where guid = "'.$y[$la_name[$j]][$i].'" ');
						$pdf->MultiCell($la_width_header[$j], 5, _convert($y[$la_name[$j]][$i]), $la_border_header[$j], "C");
						break;
						
					case 9:
						$l_sum += $y[$la_name[$j]][$i];
						$pdf->MultiCell($la_width_header[$j], 5, _convert($y[$la_name[$j]][$i].$la_valuta[$j]), $la_border_header[$j], "C");
						break;
					
					default:
						$pdf->MultiCell($la_width_header[$j], 5, _convert($y[$la_name[$j]][$i].$la_valuta[$j]), $la_border_header[$j], "C");
						
				}
				
				$ya = $pdf->GetY();
				$yH = $ya - $yb;
				$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			}
			//$ya = $pdf->GetY();
			//$yH = $ya - $yb;
			//$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			$pdf->Ln(5);
		}
		
		
		$pdf->SetFont("ArialCE", "B", 7);
		$la_tekst_bottom = array('', '', '', '', '', '', '', 'Ukupno', $l_sum.' '.$l_valuta, '');
		$la_width_bottom = array(10, 5, 15, 52, 11, 14, 20, 24, 27, 2);
		$la_border_bottom = array(0, 0, 0, 0, 0, 0, 0, 1, 1, 0);
		
		$l_left = 10;
		for($i = 0; $i < 10; $i++){
			$l_left += $la_width_bottom[$i];
			$yb = $pdf->GetY();
			$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_tekst_bottom[$i]), $la_border_bottom[$i], "C");
			$ya = $pdf->GetY();
			$yH = $ya - $yb;
			$pdf->SetXY($l_left, $pdf->GetY() - $yH);
		}
		
		$pdf->Ln(5);
		

		if($x['Rabat2'] > 0){
			$pdf->SetFont("ArialCE", "B", 7);
			$la_tekst_bottom = array('', '', '', '', '', '', '', 'Rabat '.$x['Rabat2'].' %', strval(-1 * $l_sum * $x['Rabat2'] / 100).' '.$l_valuta, '');
			$la_width_bottom = array(10, 5, 15, 52, 11, 14, 20, 24, 27, 2);
			$la_border_bottom = array(0, 0, 0, 0, 0, 0, 0, 1, 1, 0);
			
			$l_left = 10;
			for($i = 0; $i < 10; $i++){
				$l_left += $la_width_bottom[$i];
				$yb = $pdf->GetY();
				$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_tekst_bottom[$i]), $la_border_bottom[$i], "C");
				$ya = $pdf->GetY();
				$yH = $ya - $yb;
				$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			}
			
			$pdf->Ln(5);
			
			
			
			$pdf->SetFont("ArialCE", "B", 7);
			$la_tekst_bottom = array('', '', '', '', '', '', '', 'Ukupno s rabatom', strval($l_sum * (1 - $x['Rabat2'] / 100)).' '.$l_valuta, '');
			$la_width_bottom = array(10, 5, 15, 52, 11, 14, 20, 24, 27, 2);
			$la_border_bottom = array(0, 0, 0, 0, 0, 0, 0, 1, 1, 0);
			
			$l_left = 10;
			for($i = 0; $i < 10; $i++){
				$l_left += $la_width_bottom[$i];
				$yb = $pdf->GetY();
				$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_tekst_bottom[$i]), $la_border_bottom[$i], "C");
				$ya = $pdf->GetY();
				$yH = $ya - $yb;
				$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			}
			
			$pdf->Ln(5);
		}
		


		if($x['PDV'] > 0){
			$pdf->SetFont("ArialCE", "B", 7);
			$la_tekst_bottom = array('', '', '', '', '', '', '', 'PDV (25%)', $x['PDVIznos'].' '.$l_valuta, '');
			
			$l_left = 10;
			for($i = 0; $i < 10; $i++){
				$l_left += $la_width_bottom[$i];
				$yb = $pdf->GetY();
				$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_tekst_bottom[$i]), $la_border_bottom[$i], "C");
				$ya = $pdf->GetY();
				$yH = $ya - $yb;
				$pdf->SetXY($l_left, $pdf->GetY() - $yH);
			}
			
			$pdf->Ln(5);
		}
		
		
		
		$pdf->SetFont("ArialCE", "B", 7);
		$la_tekst_bottom = array('', '', '', '', '', '', '', 'Sveukupno', strval($l_sum * (1 - $x['Rabat2'] / 100) + $x['PDVIznos']).' '.$l_valuta, '');
		
		$l_left = 10;
		for($i = 0; $i < 11; $i++){
			$l_left += $la_width_bottom[$i];
			$yb = $pdf->GetY();
			$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_tekst_bottom[$i]), $la_border_bottom[$i], "C");
			$ya = $pdf->GetY();
			$yH = $ya - $yb;
			$pdf->SetXY($l_left, $pdf->GetY() - $yH);
		}
		
		//$pdf->Ln(5);
		
		pdf_my_write($pdf, 20, $pdf->GetY() + 10, 8, 'Datum isporuke: '.u_datum($x['Isporuka'], 1).'-'.u_mjesec($x['Isporuka'], 1).'-'.u_godina($x['Isporuka']));
		$pdf->Ln(5);
		pdf_my_write($pdf, 20, $pdf->GetY(), 8, 'Poziv na broj: '.$x['PozivNaBroj']);
		
		$y = get_data('select BrojRacuna, Naziv, IBAN from nosioc_racuna where guid = "'.$x['nosioc_racuna_guid'].'" ', 0);
		
		//print 'select BrojRacuna, Naziv, IBAN from nosioc_racuna where guid = "'.$x['nosioc_racuna_guid'].'" \n';
		
		pdf_my_write($pdf, 20, $pdf->GetY() + 10, 8, _convert('Broj računa banke: '.$y['BrojRacuna']));
		$pdf->Ln(5);
		pdf_my_write($pdf, 20, $pdf->GetY(), 8, $y['Naziv']);
		$pdf->Ln(5);
		pdf_my_write($pdf, 20, $pdf->GetY(), 8, 'IBAN: '.$y['IBAN']);
		$pdf->Ln(5);
		
		
		//pdf_my_write($pdf, 20, $pdf->GetY(), 8, 'Napomena: '.$x['Napomena']);
		
		if($x['Napomena']){
			$pdf->Ln(3);
			$pdf->SetFont('ArialCE', '', 8);
			$pdf->SetXY(20, $pdf->GetY());
			$pdf->MultiCell(170, 3, _convert('Napomena: '."\r\n".$x['Napomena']), 0, "L");
			
			$pdf->Ln(3);
		}
		
		$y = get_data('select DisplayName from zaposlenik where rbr_users_outer_db = "'.$l_person_id.'" ', 0);
		
		$y2 = get_data('select DisplayName from zaposlenik where guid = "'.$x['operater_guid'].'" ', 0);
		
		
		pdf_my_write($pdf, 20, $pdf->GetY() + 10, 8, _convert('Račun sastavio: '.$y['DisplayName']));
		pdf_my_write($pdf, 105, $pdf->GetY(), 8, _convert('Račun odobrio: '.$y2['DisplayName']));
		$pdf->Ln(5);
	}
	
	
	
	$l_dest = $g_path_res.'files/'.mk_f_name($l_person_id).'/Invoice-'.$_GET['rbr'].'.pdf';
	
	$pdf->Output($l_dest, 'F');
	
	
	$la_data[0] = 'ok';
	$la_data[1] = mk_f_name($l_person_id).'/Invoice-'.$_GET['rbr'].'.pdf';
	
	$la_tmp_fiskal = explode('/', $x['Broj']);
	$la_data[2] = intval($la_tmp_fiskal[1]).'/'.$la_tmp_fiskal[2].'/'.$la_tmp_fiskal[3];
	
	
	$la_json = json_encode($la_data);
	print $la_json;
	
	exit;
?>
