<?php
#include("../../includes/session.php");
include("../../includes/db.php");
// die("test dipendenti");
#session(1);


function cleanDB($str){
	return trim(addslashes($str));	
}
function parseAnagr($str){
	$str = cleanDB($str);
	$space_pos = strpos($str,"   ");
	$str = $space_pos > 0 ? substr($str,0,$space_pos) : $str;
	return $str;
}
function data($date){
	$date = trim($date);
	
	//formato, da aaaammgg a gg/mm/aaaa	
	if(strlen($date) == 8){
		$gg = substr($date,6,2);
		$mm = substr($date,4,2);
		$aaaa = substr($date,0,4);
		$data = $aaaa.'-'.$mm.'-'.$gg;
	}else{
		$data = '0000-00-00';
	}
	return $data;
}



/*
CEAZI00P.txt - imprese
CEDIP00F.txt - dipendenti
cedi100f.TXT - taglie
*/

$sql = "UPDATE `lavoratori` SET lav_abb = 0";
mysql_query($sql);

//dipendenti
$fpath = "../../../txtimport/CEDIP00F.txt";
if(is_file($fpath)){
	
	$handle = fopen($fpath, "r");
	if ($handle) {
		while(!feof($handle)){
			
			$line = fgets($handle,4096);
            echo $line; echo "<br>";




            $lav_cod = intval(substr($line, 1, 5));
			$lav_anagrafica = parseAnagr(substr($line, 23, 35));
			$lav_codfisc = cleanDB(substr($line, 142, 16));
			$imp_cod = intval(substr($line, 243, 6));
            
            if($lav_cod > 0){
                
                $sql = "UPDATE `lavoratori` SET lav_abb = 1 WHERE lav_cod = " . $lav_cod;
                mysql_query($sql);
                
                echo "lav_cod: " . $lav_cod; echo "<br>";
                echo "lav_anagrafica: " . $lav_anagrafica; echo "<br>";
                echo "lav_codfisc: " . $lav_codfisc; echo "<br>";
                echo "imp_cod: " . $imp_cod; echo "<br>";
            }
            echo "<br><br>";						
			
		}
		
		fclose($handle);
		
	}
}

?>