<?php
mb_internal_encoding("UTF-8");
include_once("class.dbf.php");
include_once("../config.php");
include_once("helper.php");
global $sugar_config;
$sql=mysql_connect($sugar_config['dbconfig']['db_host_name'],$sugar_config['dbconfig']['db_user_name'],$sugar_config['dbconfig']['db_password']);
mysql_select_db($sugar_config['dbconfig']['db_name']);

mysql_query("set character set utf8;"); 

$path = dirname(__FILE__).'/st/_INDEKSY.DBF';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

$sugar_fields = array ('id','vendor_name','name','code','purchase_price','usage_unit_name','vat_id','vat_name','vat_value','country_of_origin','kod_k_wys','status');

$insert = array();
$count =0;
//for($i=0;$i<$num_recp;$i++){
$j=0;
$aa=0;
for($i=$num_recp;$i>0;$i--){
	
	$rowp=$dbfp->getRowAssoc($i);
	//$r = mysql_fetch_array(mysql_query("SELECT index_dod FROM ecmproducts WHERE index_dod='".$rowp['INDEKS']."'"));
	
	//if (trim($rowp['INDEKS'])==$r['index_dod']) continue; 
	//tax variables (only 23%)
	
	//exists?
	
	//print_r($rowp);
	//ECHO $rowp['NAZWA'];
//BEGIN ROW ARRAY
$indeks=null;
	$nazwa=ch($rowp['GRUPA_KS']);
	$indeks=ch($rowp['INDEKS']);

		$a=0;
		$zap=mysql_query("select code,id from ekomer.ecmproducts");

		while($a=mysql_fetch_array($zap)){
			if($indeks==$a['code']){
				$zap2=mysql_query("select name,id from ekomer.ecmproductcategories");
				while($a2=mysql_fetch_array($zap2)){
				if($nazwa==$a2['name']){
				//mysql_query("set character set utf8;",$sql); 
				echo "UPDATE `ekomer`.`ecmproducts`
SET
`product_category_id` = '".$a2['id']."',
`product_category_name` = '".$a2['name']."'
WHERE `id` = '".$a['id']."';
\n";
echo "INSERT INTO `ekomer`.`ecmproductcategories_bean`
(`id`,
`ecmproductcategory_id`,
`bean_id`,
`bean_name`,
`date_entered`,
`date_modified`,
`created_by`,
`modified_user_id`,
`deleted`)
VALUES
('".create_guid()."',
'".$a2['id']."',
'".$a['id']."',
'EcmProducts',
'".date("Y-m-d H:i:s")."',
'".date("Y-m-d H:i:s")."',
'1',
'1',
'0');\n";
//echo mysql_errno($sql) . ": " . mysql_error($sql) . "\n";
				} }
			}
		}
	
//END ROW ARRAY	

}
//echo $aa;
//print_r( $insert);
//CREATE QUERY
//echo sizeof($insert);
$licznik=0;
for ($i=0; $i<count($insert); $i++) {
//PRODUCT INSERT
	
} 
//echo $query;
mysql_close($sql);
?>