<?php
set_time_limit(9999999999);
$sql=mysql_connect("localhost","root","");
mysql_select_db("saascrm");
mysql_query("set names utf8");
mysql_query("truncate table ecmstockdocouts");
mysql_query("truncate table ecmstockdocoutitems");


function addWz($row,$i,$stock_id){
	$r=mysql_fetch_array(mysql_query("select id from accounts where name like '%".$row['PLATNIK']."%' and deleted='0'"));
	$parent_id=$r['id'];
	$rd=$row['DATA'];
	$a1=explode(" ",$row['ADRES1']);
	$r=mysql_fetch_array(mysql_query("select id from ecmstockdocouts where name like 'WZ ".$row['KOR_DO_WZ']."' and deleted='0'"));
	$correct_id=$r['id'];
	if($correct_id)$pre="WZKOR";
	else $pre="WZ";
	$id=create_guid();
	$arr=array(
		"id"=>$id,
		"name"=>$pre." ".$row['NR_WZ'],
		"date_entered"=>date("Y-m-d H:i:s"),
		"date_modified"=>date("Y-m-d H:i:s"),
		"document_no"=>$pre." ".$row['NR_WZ'],
		"number"=>($i+1),
		"modified_user_id"=>1,
		"assigned_user_id"=>1,
		"created_by"=>1,
		"parent_type"=>"Accounts",
		"parent_name"=>ch(str_replace("'","",$row['PLATNIK'])),
		"parent_id"=>$parent_id,
		"status"=>"accepted",
		"register_date"=>$rd[0].$rd[1].$rd[2].$rd[3]."-".$rd[4].$rd[5]."-".$rd[6].$rd[7],
		"validtill_date"=>$rd[0].$rd[1].$rd[2].$rd[3]."-".$rd[4].$rd[5]."-".$rd[6].$rd[7],
		"parent_address_street"=>ch(str_replace("'","",$row['ADRES2'])),
		"parent_address_city"=>ch($r1,$r2,$a1[1]),
		"parent_address_postalcode"=>$a1[0],
		"subtotal"=>($row['WARTOSC']-$row['PODATEK']),
		"total"=>$row['WARTOSC'],
		"discount"=>0,
		"ecmlanguage"=>"pl_pl",
		"template_id"=>"1d178dc6-0904-3fa3-f51b-4a4b4698a173",
		"template_name"=>"E5 Polska Sp. z o. o.",
		"stock_id"=>$stock_id,
		"currency_id"=>"PLN",
		"correct_id"=>$correct_id,
	);
	foreach($arr as $k=>$v){
		$in[]=$k."='".$v."'";
	}
	mysql_query("insert into ecmstockdocouts set ".implode(",",$in));
	return $id;
}

function addProductLine($id,$i,$r){
	$vats=array(
		0=>"9b783d21-5548-6653-e1d6-49610eb3f9dd",
		22=>"9c592a4a-2de0-1fa3-e082-494aeceb122c",
		7=>"bf3a5fec-435f-af22-024a-4958b51863c8",
	);
	$rr=mysql_fetch_array(mysql_query("select id from ecmproducts where code like '".$r['INDEKS']."' and deleted='0'"));
	$pid=$rr['id'];
	if($pid==""){
		$pid=addProduct($r['INDEKS'],$r['NAZWA']);
	}
	$iid=create_guid();	
	
	$arr=array(
		"id"=>$iid,
		"date_entered"=>date("Y-m-d H:i:s"),
		"date_modified"=>date("Y-m-d H:i:s"),
		"deleted"=>0,
		"modified_user_id"=>1,
		"assigned_user_id"=>1,
		"created_by"=>1,
		"ecmstockdocout_id"=>$id,
		"ecmproduct_id"=>$pid,
		"position"=>$i,
		"code"=>ch(str_replace("'","",$r['INDEKS'])),
		"name"=>ch(str_replace("'","",$r['NAZWA'])),
		"quantity"=>$r['ILOSC'],
		"price"=>$r['CENA_SPRZ'],
		"discount"=>$r['RABAT'],
		"total"=>$r['WARTOSC'],
		"dd_unit_name"=>$r['NAZWA_J_M'],
		"dd_unit_id"=>1,
		"ecmvat_id"=>$vats[$r['VAT']],
		"ecmvat_value"=>$r['VAT'],
		"ecmvat_name"=>$r['VAT']."%",
		"currency_id"=>"PLN",
		"currency_name"=>"PLN",
	);
	foreach($arr as $k=>$v){
		$in[]=$k."='".$v."'";
	}
	mysql_query("insert into ecmstockdocoutitems set ".implode(",",$in));
	if(mysql_error()){
		echo mysql_error()."\n".implode(",",$in);
	}
	return $iid;
}

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

$w=mysql_query("select id,name,dir from ecmstocks where deleted='0'");
while($r=mysql_fetch_array($w)){	
	$dbfp = new dbf_class('klk/'.$r['dir'].'/_wz_list.dbf');
	$num_recp=$dbfp->dbf_num_rec;
	//$num_recp=10000;
	$unset_arr=array("INDEKS","NAZWA","CENA_SPRZ","ILOSC","RABAT","WARTOSC","NAZWA_J_M","VAT","NR_SYSTEM");
	for($i=0;$i<$num_recp;$i++){
		echo "products ".$i."/".$num_recp."\n";
		$rowp=$dbfp->getRowAssoc($i);
		foreach($rowp as $k=>$v){
			if(!in_array($k,$unset_arr))unset($rowp[$k]);
		}
		$products[$rowp['NR_SYSTEM']][]=$rowp;
	}

	$dbf = new dbf_class('klk/'.$r['dir'].'/_wz.dbf');
	$num_rec=$dbf->dbf_num_rec;
	//$num_rec=1000;
	$unset_arr=array("NR_WZ","PLATNIK","PODATEK","WARTOSC","ADRES2","ANULOWANA","NR_SYSTEM","DATA","KOR_DO_WZ");
	for($i=0;$i<$num_rec;$i++){
		echo "docs ".$i."/".$num_rec."\n";
		$docs[$i]=$dbf->getRowAssoc($i);
		foreach($docs[$i] as $k=>$v){
			if(!in_array($k,$unset_arr))unset($docs[$i][$k]);
		}
	}
	
	for($i=0;$i<$num_rec;$i++){
		echo $i."/".$num_rec."\n";
		if($docs[$i]['ANULOWANA']!="F")continue;
		$id=addWz($docs[$i],$i,$r['id']);
		$j=1;
		foreach($products[$docs[$i]['NR_SYSTEM']] as $prod){
			addProductLine($id,$j,$prod);
			$j++;
		}
	}
}
mysql_close($sql);
?>