<?
	$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";
	
	if(!$_SESSION['__id']){
		exit;
	}
	
	
	if($_POST){
		if(strstr($_SERVER['HTTP_REFERER'], $g_path_sl.'res/inventura_upload')){
			$l_file = $_FILES['imgInvFile']['tmp_name'];
			$l_size = $_FILES['imgInvFile']['size'];
			$l_name = $_FILES['imgInvFile']['name'];
			
			if($l_file && $l_name){
				$l_dest_name = "";
				
				$la_name = explode(".", $l_name);
				for($i = 0; $i < sizeof($la_name) - 1; $i++){
					$l_dest_name .= $la_name[$i];
				}
				$l_dest_name .= "-".u_datum(0, 1).u_sat().u_minuta().u_sekunda().rand(10000000000, 99999999999).".".$la_name[sizeof($la_name) - 1];
				
				$l_person_id = '';
				if(!$l_person_id){
					$l_person_id = $_SESSION['__id'];
				}
				
				$l_dest = '../res/files/'.mk_f_name($l_person_id).'/'.$l_dest_name;
				
				/*
				print $_GET['id']." -- <br>";
				print $l_person_id." -- <br>";
				print $l_dest." -- <br>";
				exit;
				*/
				
				$is_ok = move_uploaded_file($l_file, $l_dest);
				
				header('location:inventura_upload.php?upload=done&rbr='.$_GET['rbr'].'&uploaded='.substr($l_dest, 3));
			}else{
				header('location:inventura_upload.php?upload=error');
			}
			
			exit;
		}else{
			print "error 9874";
			exit;
		}
	}



	if($_GET['upload'] == 'done'){
?>
		<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
		<html>
		<head>
			<link rel='stylesheet' type='text/css' href="css/file_upload.css">
			<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-inventura-upload.js"></script>
		</head>
		<body>
			Upload dokumenta je uspješan.
<?
			if($_GET['rbr'] == 1){
				
				
				$handle = @fopen($g_path.'../'.$_GET['uploaded'], 'r');
				
				$contents = '';
				if($handle){
					while (!feof($handle)) {
						$contents .= fread($handle, 8192);
					}
					fclose($handle);
				}
				
				
				$la_data = array();
				
				$la_tmp = explode("\n", $contents);
				for($i = 0; $i < sizeof($la_tmp); $i++){
					$la_tmp[$i] = trim($la_tmp[$i]);
					
					
					if($i == 0){
						$l_tab_cnt = substr_count($la_tmp[$i], "\t");
						$l_tab_tz = substr_count($la_tmp[$i], ";");
						
						if($l_tab_cnt > $l_tab_tz){
							$l_delimiter = "\t";
						}else{
							$l_delimiter = ";";
						}
					}
					
					
					$la2_tmp = explode($l_delimiter, $la_tmp[$i]);
					
					for($j = 0; $j < sizeof($la2_tmp); $j++){
						$la2_tmp[$j] = trim($la2_tmp[$j]);
					}
					$la_data[$i] = array($la2_tmp[0], $la2_tmp[3]);
				}
?>
				<a href="#" onclick="new_inventura_close(<?=$_GET['rbr']?>, '<?=$_GET['uploaded']?>'); return false;">Potvrdi unos inventure i zatvori prozor</a>.
				<ul style="display:none" id="all-codes">
<?
					$l_no_code = '';
					for($i = 0; $i < sizeof($la_data); $i++){
						if($la_data[$i][0]){
							$l_has_code = get_item('select id from articles where id = '.$la_data[$i][0].' limit 1 ');
							if($l_has_code){
								print '<li class="my-code" id="code-'.$la_data[$i][0].'">'.$la_data[$i][1].'</li>'."\n";
							}else{
								$l_no_code .= ', '.$la_data[$i][0];
							}
						}
					}
					if(strlen($l_no_code) > 0){
						$l_no_code = substr($l_no_code, 1);
					}
?>
				</ul>
				<div id="no-codes">
<?
					if($l_no_code){
?>
						<p style="color:red; ">Neke stavke datoteke inventure ne nalaze se u bazi artikala. To su stavke sa sljedećim šiframa: <b><?print $l_no_code;?></b>.</p>
						<p>Bez obzira što postoje stavke s nepostojećim šiframa, sve ostale stavke koje imaju svoju šifru možete neometano učitati klikom na gornji link "Potvrdi unos inventure i zatvori prozor".</p>
<?
					}
?>
					<p style="color:red; ">PAŽNJA!!! Prozor programa mora biti aktivan cijelo vrijeme dok se vrši unos. Ukoliko aktivirate neki drugi prozor, proces unosa će stati i dotad upisani podaci će se izbrisati te će biti potrebno ponovo pokrenuti unos</b>.</p>
					<br>
					<br>
					<br>
				</div>
<?
			}
?>
			<br>
			<br>
		</body>
		</html>
<?
	}else{
		if($_GET['upload'] == 'error'){
?>
			<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
			<html>
			<head>
				<link rel='stylesheet' type='text/css' href="css/file_upload.css">
				<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-inventura-upload.js"></script>
			</head>
			<body>
				Upload nije uspio.
				<a href="#" onclick="new_inventura_close(0, ''); return false;">Zatvori prozor</a>
				<br>
				<br>
			</body>
			</html>
<?
		}else{
			ob_start();
?>
				<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
				<html>
				<head>
					<link rel='stylesheet' type='text/css' href="css/file_upload.css">
					<script type="text/javascript" src="<?=$g_path_sl?>res/js/f-inventura-upload.js"></script>
				</head>
				<body>
					<form name='fuinForma' method='post' action='inventura_upload.php?rbr=1' enctype='multipart/form-data'>
						<input type="hidden" name="MAX_FILE_SIZE" value="4194304">
						Odaberite datoteku inventure koju želite dodati<br>
						<br>
						<input type='file' name='imgInvFile' value=''><br>
						<br>
						<center><button title="Upload dokumenta" onclick="document.fuinForma.submit()">Upload</button></center>
						<br><br>
						<a href="#" onclick="new_inventura_close(0, ''); return false;">Zatvori prozor</a>
					</form>
				</body>
				</html>
<?
			$output = ob_get_contents();
			ob_end_clean();
			print  $output;
		}
	}
?>