<?php
mb_internal_encoding("UTF-8");
include_once("class.dbf.php");
include_once("../../config.php");
include_once("helper.php");
global $sugar_config;
$mysqli = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], $sugar_config['dbconfig']['db_name']);
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());
}
// połączenie do kategorii select
$mysqli2 = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], $sugar_config['dbconfig']['db_name']);
if (!$mysqli2->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli2->error);
} else {
    printf("Current character set: %s\n", $mysqli2->character_set_name());
}
// do kategori insert
$mysqli3 = new mysqli($sugar_config['dbconfig']['db_host_name'], $sugar_config['dbconfig']['db_user_name'], $sugar_config['dbconfig']['db_password'], $sugar_config['dbconfig']['db_name']);
if (!$mysqli3->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli3->error);
} else {
    printf("Current character set: %s\n", $mysqli3->character_set_name());
}
$path = dirname(__FILE__).'/E-m-s/_INDEKSY.DBF';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

$sugar_fields = array ('id','vendor_name','name','code','serial_no','purchase_price','usage_unit_name','usage_unit_id','unit_id','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;
//echo create_guid();
$jednostki =
array (
        1 => 'kg',
        2 => 'szt',
        3 => 'kpl',
        4 => 'mb',
        5 => 'm',
        6 => 't',
        7 => 'm2',
        8 => 'doba',
        9 => 'kmpl',
        '6275f1de-dbd1-7c7f-f722-556daf14780d' => 'l'
);
$vats=array(
        '9b783d21-5548-6653-e1d6-49610eb3f9dd'=>'0',
        '28079566-b825-e38f-9993-4ccc7b781de5'=>'23',
        '66fc9dba-818c-d321-2a55-524d13f8b0c2'=>'8'
        
);

$vats = array(
        "9b783d21-5548-6653-e1d6-49610eb3f9dd"=>0,
        "9c592a4a-2de0-1fa3-e082-494aeceb122c"=>22,
        "bf3a5fec-435f-af22-024a-4958b51863c8"=>7,
        "d7a2073e-44ca-7d52-cc02-5301eedef40f"=>'OO',
       "28079566-b825-e38f-9993-4ccc7b781de5"=>23,
        "49fa02ee-cdc2-9a51-0057-559a9f077708"=>'NP'
);
$oos=array(
        '38.11.55.0'=>1,
        '38.32.33.0'=>1,
        '38.32.29.0'=>1
);
$array=array();
$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,OO) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssss", $id,$nazwa,$indeks,$sys,$status,$unit_id,$vat_id,$vat_name,$vat_value,$pkwiu,$vendor_name,$vendor_id,$oo);
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=array_search($jed, $jednostki);
	$vat=strtolower (str_replace('.','',$rowp['PODATEK']));
	$vat_id=array_search($vat, $vats);
	if(is_numeric($vat)){
	    $vat_name=$vat.'%';
	    $vat_value=$vat.'.00';
	} else {
	    $vat_name=$vat;
	    $vat_value=$vat.'.00';
	}
	$pkwiu=ch($rowp['PKWIU']);
	$oo=$oos[ch($rowp['PKWIU'])];
	
	if($rowp['KOD_DOST']!=''){
	       $stmt2=$mysqli2->prepare("select id,name from accounts where index_dbf=?");
           $stmt2->bind_param("s", ch($rowp['KOD_DOST']));
           /* execute statement */
           $stmt2->execute();
           
           /* bind result variables */
           $stmt2->bind_result($par,$dostawca_n);
           
           /* fetch values */
           while ($stmt2->fetch()) {
        
               if($par!=''){
                  $vendor_name=$dostawca_n;
                  $vendor_id=$par;
               }
        	}
        	$stmt2->close();
	}
	
	if($rowp['PODATEK']=='23')$vat_id='28079566-b825-e38f-9993-4ccc7b781de5';
	if($rowp['PODATEK']=='0')$vat_id='9b783d21-5548-6653-e1d6-49610eb3f9dd';
	if($rowp['NAZWA_J_M']=='KG')$unit='2';
	if($rowp['NAZWA_J_M']=='L')$unit='7';
    if($rowp['NAZWA_J_M']=='SZT')$unit='1';
	if($rowp['NAZWA_J_M']=='M')$unit='6';
   $row = array (
		addslashes(create_guid()), //id
		addslashes($rowp['KOD_DOST']), //suppliercode
		addslashes($nazwa), //name
		addslashes($indeks), //name
		addslashes($rowp['PKWIU']),
		addslashes($rowp['CENA_W_WAL']),
		addslashes($rowp['NAZWA_J_M']),
		addslashes($rowp['NAZWA_J_M']),
		addslashes($unit),
		addslashes($vat_id), //vat_id
		addslashes($rowp['PODATEK'].'%'), //vat_name
		addslashes($rowp['PODATEK'].',00'), //vat_value
		addslashes($rowp['KOD_K_POCH']), //vat_value
		addslashes($rowp['KOD_K_WYS']), //vat_value
		addslashes('active'),
		);
		

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

   // after add kategorie
   
   $stmt2=$mysqli2->prepare("select id from ecmproductcategories where name=?");
   $stmt2->bind_param("s", ch($rowp['GRUPA_KS']));
   /* execute statement */
   $stmt2->execute();
   
   /* bind result variables */
   $stmt2->bind_result($cid);
   
   /* fetch values */
   while ($stmt2->fetch()) {

       if($cid!=''){
           $stmt3=$mysqli->prepare("INSERT INTO ecmproductcategories_bean (id,ecmproductcategory_id,bean_id,bean_name) VALUES (?,?,?,?)");
           $stmt3->bind_param("ssss", $newid,$ccid,$id,$bean_name);
           $newid=create_guid();
           $ccid=$cid;
           $id=$id;
           $bean_name='EcmProducts';
           if (!$stmt3->execute()) {
               echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
           }
           $stmt3->close();
           
       }
   }
   
   /* close statement */
   $stmt2->close();
   /// ceny
   
    if($rowp['CENA_W_WAL']!=''){
        $stmt3=$mysqli->prepare("INSERT INTO ecmprices_ecmproducts (id,ecmprice_id,ecmproduct_id,price) VALUES (?,?,?,?)");
        $stmt3->bind_param("ssss", $newid,$ccid,$id,$price);
        $newid=create_guid();
        $ccid='994bbbe8-905d-ccdb-4cba-5549cb89835c';
        $id=$id;
        $price=$rowp['CENA_W_WAL'];
        if (!$stmt3->execute()) {
            echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        }
        $stmt3->close();
    }
    
    
    //dostawca
}
print_r(array_unique($array));
die();
//print_r( $insert);
//CREATE QUERY
//echo sizeof($insert);

for ($i=0; $i<count($insert); $i++) {
//PRODUCT INSERT
	$query = 'INSERT INTO ekomer.ecmproducts ('.implode(",",$sugar_fields).',date_entered,date_modified,created_by,modified_user_id,assigned_user_id) VALUES (\''.implode("','",$insert[$i]).'\',\''.date("Y-m-d H:i:s").'\',\''.date("Y-m-d H:i:s").'\',\'1\',\'1\',\'1\');'."\n";
	echo $query;
	//mysql_query(ch($query));
	if (mysql_error()) echo 'Database error: '.mysql_error().'
';
} 
//echo $query;
mysql_close($sql);

?>