<?php
set_time_limit(9999999999);

include_once("class.dbf.php");
include_once("helper.php");

$sql=mysql_connect("localhost","root","");
mysql_select_db("saascrm");
mysql_query("set names utf8");

$dbf = new dbf_class("klk/_fk.dbf");
$num_rec=$dbf->dbf_num_rec;

$dbfp = new dbf_class("klk/_fk_list.dbf");
$num_recp=$dbfp->dbf_num_rec;
$n=0;
$k=0;
for($i=420000;$i<$num_recp;$i++){
	$rowt = $dbfp->getRowAssoc($i);
	if($rowt['DATA']>20081231){
		$rowp[$k]=$rowt;
		$k++;
	}
	echo $i."/".$num_recp."\n";
}
$k=0;
for($i=35000;$i<$num_rec;$i++){
	$rowt = $dbf->getRowAssoc($i);
	if($rowt['DATA']>20081231){
		$row[$k]=$rowt;
		$k++;
	}
}
$k=0;
echo count($row)." ";
echo count($rowp)."\n";
for($i=0;$i<count($row);$i++){
	$nn=$n;
	for($j=$nn;$j<$nn+200;$j++){
		if($row[$i]['NR_SYSTEM']==$rowp[$j]['NR_SYSTEM']){
			/*$d[$k]['name']=$rowp[$j]['NAZWA'];
			$d[$k]['code']=$rowp[$j]['INDEKS'];
			$d[$k]['qty']=$rowp[$j]['ILOSC'];
			$d[$k]['date']=$row[$i]['DATA'];
			$d[$k]['price']=$rowp[$j]['CENA_SPRZ'];
			$d[$k]['value']=$rowp[$j]['ILOSC']*$rowp[$j]['CENA_SPRZ'];
			$d[$k]['client']=$row[$i]['PLATNIK'];
			
			*/
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['name']=$rowp[$j]['NAZWA'];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['code']=$rowp[$j]['INDEKS'];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['qty']+=$rowp[$j]['ILOSC'];
			$qty[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]+=$rowp[$j]['ILOSC'];
			$aq[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]+=$rowp[$j]['ILOSC']*$rowp[$j]['CENA_SPRZ'];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['date']=$row[$i]['DATA'];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['price']=$aq[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]/$qty[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['value']=$aq[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']];
			$d1[$row[$i]['PLATNIK']."_".$rowp[$j]['INDEKS']]['client']=$row[$i]['PLATNIK'];
			
			$n++;
			$k++;
		}
	}
	echo $i."/".count($row)." p\n";
}
unset($row);
unset($rowp);
mysql_close($sql);

$dc=count($d);
$h=";";
$csv="";

$r1=array("ť","—","¨","˝","¤","ŕ");
$r2=array("Ł","Ś","Ę","Ż","Ą","Ó");
/*for($i=0;$i<$dc;$i++){
	$csv.=str_replace($r1,$r2,$d[$i]['name']).$h;
	$csv.=$d[$i]['code'].$h;
	$csv.=$d[$i]['qty'].$h;
	$csv.=$d[$i]['price'].$h;
	$csv.=$d[$i]['value'].$h;
	$csv.=str_replace($r1,$r2,$d[$i]['client']).$h;
	$csv.=$d[$i]['date'].$h."\n";
	echo "xls ".$i."/".$dc."\n";
}*/
foreach($d1 as $dd){
	$csv.=str_replace($r1,$r2,$dd['name']).$h;
	$csv.=$dd['code'].$h;
	$csv.=$dd['qty'].$h;
	$csv.=$dd['price'].$h;
	$csv.=$dd['value'].$h;
	$csv.=str_replace($r1,$r2,$dd['client']).$h."\n";
	//$csv.=$dd['date'].$h."\n";
}

chmod("d:/www/saas1/cache/upload",0777);
$microtime=str_replace(".","",str_replace(" ","",microtime()));
$name="d:/www/saas1/cache/upload/Products".$microtime.".csv";

$file = $name;
$fp = fopen($file, "w");
flock($fp, 2);
fwrite($fp, $csv);
flock($fp, 3);
fclose($fp);
chmod($name,0777);
?>