<?php

include "checklog.php";

$GoOn = 0;
$testopuls = "OK";

if (isset($HTTP_POST_VARS['delimitatore'])) {
	if (isset($HTTP_POST_VARS['decimali'])) {
		if (isset($HTTP_POST_VARS['modo'])) {
			if (is_uploaded_file($HTTP_POST_FILES['file']['tmp_name'])) {
				$GoOn = 1;
			}
		}
	}
}

if (!$GoOn) {
	$Out = "<b>Impossibile leggere il file</b>";
} else {

	$oDelim = $HTTP_POST_VARS['delimitatore'];
	$oDec = $HTTP_POST_VARS['decimali'];
	$oModo = $HTTP_POST_VARS['modo'];
	
	$FileName = $HTTP_POST_FILES['file']['tmp_name'];
	
	switch ($oDelim) {
		case 0:
			$dc = ";";
			break;
		case 1:
			$dc = "\t";
			break;
		case 2:
			$dc = ",";
			break;
	}

	switch ($oDec) {
		case 0:
			$dd = ",";
			$td = ".";
			break;
		case 1:
			$dd = ".";
			$td = ",";
			break;
	}
	
	$nr = 0;
	$na = 0;
	$file = fopen($FileName, "rb");
	if ($file) {
		@set_time_limit(28800);
		$csv = fread($file, filesize($FileName));
		fclose($file);
		$csv = trim($csv);
		$pieces = explode("\n", $csv);
		if ($oModo == "0" || $oModo == "2") {
			$sql = "DELETE FROM giacenze";
			$result = mysql_query($sql);
		}
		$sql = "LOCK TABLES giacenze WRITE";
		$result = mysql_query($sql);
		foreach ($pieces as $piece) {
			$riga = trim($piece);
			$ok = 1;
			if ($riga != "") {
				$nr = $nr +1;
				if (substr($riga,0,1) == "\"") {
					$riga2 = substr($riga, 1);
					$riga2 = str_replace("\"\"","++++++++++",$riga2);
					$pos = strpos($riga2,"\"".$dc);
					if (is_integer($pos)) {
						$codice = substr($riga2, 0, $pos);
						$codice = str_replace("++++++++++","\"",$codice);
						$giacenza = trim(substr($riga2, $pos + 2));
						$pos = strpos($giacenza,$dc);
						if (is_integer($pos)) {
							$giacenza = substr($giacenza, 0, $pos);
						}
						$giacenza = trim(str_replace("\"","",$giacenza));
					} else {
						$ok = 0;
					}
				} else {
					$pos = strpos($riga,$dc);
					if (is_integer($pos)) {
						$codice = substr($riga, 0, $pos);
						$giacenza = trim(substr($riga, $pos + 1));
						$pos = strpos($riga,$dc);
						if (is_integer($pos)) {
							$giacenza = substr($giacenza, 0, $pos);
						}
						$giacenza = trim(str_replace("\"","",$giacenza));
		    		} else {
						$ok = 0;
					}
				}
				$codice = MakeQuery(trim($codice));
				$giacenza = str_replace($td,"",$giacenza);
				$giacenza = str_replace($dd,".",$giacenza);
				if ($giacenza == "") { $ok=0; }
				if (!is_numeric($giacenza)) { $ok=0; }
				if ($ok) {
					if ($oModo == "1") {
						$sql = "REPLACE INTO giacenze (codice, giacenza) VALUES ('$codice','$giacenza')";
					} else {
						$sql = "INSERT INTO giacenze (codice, giacenza) VALUES ('$codice','$giacenza')";
					}
					$result = mysql_query($sql);
					$na = $na +1;
	
				}
			}
			$sql = "UNLOCK TABLES";
			$result = mysql_query($sql);
			if ($oModo == "2") {
				$testopuls = "Continua (azzera giacenze non definite)";
			}
			$Out = "<b>Aggiornamento completato.</b><BR>Righe totali (non vuote): <b>$nr</b> Codici aggiornati: <b>$na</b>";
		}
	} else {
		$Out = "<b>Impossibile leggere il file</b>";
	}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<base target="_self">
<link rel="stylesheet" href="backoffice.css" type="text/css">
</head>
<body topmargin="0" leftmargin="4" marginwidth="4" marginheight="0" class="pagina">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr> 
    <td width="50%"><span class="titolo">Gestione Giacenze - </span><span class="sottotitolo">Importazione</span></td>
    <td width="25%"><span class="sottotitolo"><a href="bogiac1.php" target="_self">&gt;&gt;Articoli</a></span></td>
    <td width="25%"><span class="sottotitolo"><a href="bogiac2.php" target="_self">&gt;&gt;Varianti</a></span></td>
    <td width="25%"><span class="sottotitolo"><a href="bogiac4.php" target="_self">&gt;&gt;Esportazione</a></span></td>
  </tr>
</table>
<p>&nbsp;</p>
<?php
	if ($oModo != "2") {
?>
<form action="bogiac3.php" method="GET">
<?php
	} else {
?>
<form action="bogiac3reset.php" method="POST">
<input type="hidden" name="reset" value="reset">
<?php
	}
?>
  <table border="0" cellpadding="2" class="form">
    <tr> 
      <td nowrap> 
        <?=$Out?>
      </td>
      <td align="left" valign="middle" nowrap>&nbsp;&nbsp;&nbsp;</td>
      <td align="left" valign="middle" width="100%"> 
        <input type="submit" name="Submit" value="<?php echo $testopuls; ?>" class="pulsante">
      </td>
    </tr>
  </table>
</form>
</body>
</html>