<?php
ini_set('display_errors',0);
ini_set('memory_limit', '-1');

include_once("class.dbf.php");

include_once("helper.php");

$sugar_config = array(
		'dbconfig' =>
		array(
				'db_host_name' => 'localhost',
				'db_user_name' => 'root',
				'db_password' => '%g7!@fG',
				'db_name' => 'sugarcrm',
				'db_type' => 'mysql',
		),
		);

$path = dirname(__FILE__).'/bsl/_indeksy.dbf';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

$mysqli = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], 'preDb_45ab7160e7af49269e17044eaed9e0ba');
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}
$mysqli2 = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], 'preDb_45ab7160e7af49269e17044eaed9e0ba');
if (!$mysqli->set_charset("utf8")) {
	printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
	printf("Current character set: %s\n", $mysqli->character_set_name());
}
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$insert = array();
$count =0; 
//for($i=0;$i<$num_recp;$i++){ 
$j=0;
// import jednostek miar
$stmt=$mysqli->query('delete from dom_lists where dom_name="ecmproducts_unit_dom"');
$stmt=$mysqli->query('delete from dom_lists where dom_name="ecmproducts_unit_dom_precision"');
$stmt=$mysqli->query('delete from dom_lists where dom_name="ecmproducts_group_ks_dom"');

$stmt=$mysqli->prepare("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss", $id,$lang,$dom_name,$key,$value);
$stmt2=$mysqli->prepare("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES (?,?,?,?,?)");
$stmt3=$mysqli->prepare("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES (?,?,?,?,?)");
$stmt4=$mysqli->query("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES ('123a','pl_pl','ecmproducts_group_ks_dom','1112','INNE')");
$stmt2->bind_param("sssss", $id,$lang,$dom_name,$key,$value);
$stmt3->bind_param("sssss", $id,$lang,$dom_name,$key,$value);
for($i=$num_recp;$i>=0;$i--){
	$rowp=$dbfp->getRowAssoc($i);




	$value=ch($rowp['NAZWA_J_M']);
	$key=md5(ch($rowp['NAZWA_J_M']));
	$id=md5(ch($rowp['NAZWA_J_M']).'asd');
	$lang='pl_pl';
	$dom_name='ecmproducts_unit_dom';
	if($value!=''){
		if (!$stmt->execute()) {
		//	echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		}
	}
	
	
	$value=2;
	$key=md5(ch($rowp['NAZWA_J_M']));
	$id=md5(ch($rowp['NAZWA_J_M']).'asdd');
	$lang='pl_pl';
	$dom_name='ecmproducts_unit_dom_precision';
	if($key!=''){
		if (!$stmt2->execute()) {
			//echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		}
	}
	

	
	$key=md5(ch($rowp['GRUPA_KS']));
	$id=md5(ch($rowp['GRUPA_KS']).'asdd');
	$lang='pl_pl';
	$dom_name='ecmproducts_group_ks_dom';
	$value=ch($rowp['GRUPA_KS']);
	if($value!=''){
	if (!$stmt2->execute()) {
	//	echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
	}
	}
	$a++;

} 
// stawki vat
 
 
$stmt=$mysqli->query('truncate ecmvats');
$stmt2=$mysqli->prepare("INSERT INTO ecmvats (id,name,deleted,value) values (?,?,?,?)");
$stmt2->bind_param("ssss", $id,$name,$deleted,$value);
for($i=$num_recp;$i>=0;$i--){
	$rowp=$dbfp->getRowAssoc($i);



	if(ch($rowp['PODATEK'])=='')continue;
	$name=ch($rowp['PODATEK']).'%';
	$deleted=0;
	$id=md5(ch($rowp['PODATEK']).'asd');
	$value=strtolower (str_replace('.','',$rowp['PODATEK']));
	if (!$stmt2->execute()) {
		//echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
	}
}
// kategorie


$array=array();
$stmt=$mysqli->query('truncate ecmproducts');
$stmt=$mysqli->prepare("INSERT INTO ecmproducts (id,name,code,sys_ident,status,unit_id,vat_id,vat_name,vat_value,pkwiu,vendor_name,vendor_id,ks_group) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssss", $id,$nazwa,$indeks,$sys,$status,$unit_id,$vat_id,$vat_name,$vat_value,$pkwiu,$vendor_name,$vendor_id,$ks_group);
for($i=$num_recp;$i>=0;$i--){

	$rowp=$dbfp->getRowAssoc($i);

	$nazwa=ch($rowp['NAZWA']);
	$array[]=ch($rowp['GRUPA_KS']);
	if($nazwa!=''){
	  
	} else {
		continue;
	}
	$id=create_guid();
	$sys=ch($rowp['NR_SYSTEM']);
	$indeks=ch($rowp['INDEKS']);
	$status='active';
	$jed=strtolower (str_replace('.','',$rowp['NAZWA_J_M']));

	$unit_id=md5(ch($rowp['NAZWA_J_M']));
	$vat_id=md5(ch($rowp['PODATEK']).'asd');
	$vat_name=ch($rowp['PODATEK']).'%';
	$pkwiu=ch($rowp['PKWIU']);
	$ks_group=md5(ch($rowp['GRUPA_KS']).'');
	if(ch($rowp['GRUPA_KS'])==''){
		$ks_group='1112';
	}


	if (!$stmt->execute()) {
	//	echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
	}

	

	 



	//dostawca
}
