<?php
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' => 'preDb_45ab7160e7af49269e17044eaed9e0ba',
				'db_type' => 'mysql',
		),
);

mysql_query("set character set utf8;"); 

$path = dirname(__FILE__).'/bsl/_ma.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'], $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());
}

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
$mysqli->query("truncate ecmtransactions");
$stmt=$mysqli->prepare("INSERT INTO ecmtransactions (id,name,date_entered,date_modified,value,
        parent_id,parent_name,payment_date,type,register_date,record_id,record_type,currency_id,currency_value,settled) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bind_param("sssssssssssssss",$id,$name,$date_entered,$date_modified,$value,$parent_id,$parent_name,$payment_date,$type,$register_date,$record_id,$record_type,$currency_id,$currency_value,$settled);

for($i=$num_recp;$i>=0;$i--){

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

    if($rowp["NR_SYSTEM"]=='')continue;
    $stmt2=$mysqli2->prepare("select id,name from accounts where index_dbf2=?");
    $stmt2->bind_param("s", ch($rowp['NR_SYSTEM']));
    /* execute statement */
    $stmt2->execute();
     
    /* bind result variables */
    $stmt2->bind_result($par,$dostawca_n);
    $parent_id='';
    /* fetch values */
    while ($stmt2->fetch()) {
    
    	if($par!=''){
    		$parent_name=$dostawca_n;
    		$parent_id=$par;
    	}
    	else {
    		$parent_id='';
    	}
    }
    if($parent_id=='')continue;
    $stmt2->close();
    $id=create_guid();
    $name=ch($rowp["OPIS"]);
    $date_entered=date("Y-m-d 00:00:01",strtotime(ch($rowp["DATA"])));
    $date_modified=date("Y-m-d 00:00:01",strtotime(ch($rowp["DATA"])));
    $payment_date=date("Y-m-d",strtotime(ch($rowp["DATA"])));
    $value=ch(str_replace(",",".",$rowp["KWOTA"]));
    $type=1;
    $register_date=date("Y-m-d",strtotime(ch($rowp["DATA_DOK"])));
   if(ch($rowp["WALUTA"])=='' || ch($rowp["WALUTA"])=='PLN'){
        $currency_id='PLN';
    } else if(ch($rowp["KOD_WALUTY"])=='EUR'){
        $currency_id='EUR';
    } else {
        $currency_id='USD';
    }
    $currency_value=str_replace(",",".",ch($rowp['KURS']));
    if($rowp["ROZL"]=='T'){
        $settled=1;
    } else {
        $settled=0;
    }
    $record_id='';
    $record_type='';
		if (!$stmt->execute()) {
		    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
		}
	
}
$stmt->close();
//CREATE QUERY
//echo sizeof($insert).'';
$path = dirname(__FILE__).'/bsl/_wn.dbf';
$dbfp = new dbf_class($path);

$num_recp=$dbfp->dbf_num_rec;

$stmt=$mysqli->prepare("INSERT INTO ecmtransactions (id,name,date_entered,date_modified,value,
        parent_id,parent_name,payment_date,type,register_date,record_id,record_type,currency_id,currency_value,settled) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

$stmt->bind_param("sssssssssssssss",$id,$name,$date_entered,$date_modified,$value,$parent_id,$parent_name,$payment_date,$type,$register_date,$record_id,$record_type,$currency_id,$currency_value,$settled);

for($i=$num_recp;$i>=0;$i--){

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

    if($rowp["NR_SYSTEM"]=='')continue;
    $stmt2=$mysqli2->prepare("select id,name from accounts where index_dbf2=?");
    $stmt2->bind_param("s", ch($rowp['NR_SYSTEM']));
    /* execute statement */
    $stmt2->execute();
     
    /* bind result variables */
    $stmt2->bind_result($par,$dostawca_n);
    $parent_id='';
    /* fetch values */
    while ($stmt2->fetch()) {
    
    	if($par!=''){
    		$parent_name=$dostawca_n;
    		$parent_id=$par;
    	} else {
    		$parent_id='';
    	}
    }
    
    if($parent_id=='')continue;   
    $stmt2->close();
    $id=create_guid();
    $name=ch($rowp["OPIS"]);
    $date_entered=date("Y-m-d 00:00:01",strtotime(ch($rowp["DATA"])));
    $date_modified=date("Y-m-d 00:00:01",strtotime(ch($rowp["DATA"])));
    $payment_date=date("Y-m-d",strtotime(ch($rowp["DATA"])));
    $value=ch(str_replace(",",".",$rowp["KWOTA"]));
    $type=0;
    $register_date=date("Y-m-d",strtotime(ch($rowp["DATA_DOK"])));
    if(ch($rowp["WALUTA"])=='' || ch($rowp["WALUTA"])=='PLN'){
        $currency_id='PLN';
    } else if(ch($rowp["KOD_WALUTY"])=='EUR'){
        $currency_id='EUR';
    } else {
        $currency_id='USD';
    }
    $currency_value=str_replace(",",".",ch($rowp['KURS']));
    if($rowp["ROZL"]=='T'){
        $settled=1;
    } else {
        $settled=0;
    }
    $record_id='';
    $record_type='';
    if($rowp["SYM_DOK"]=='FK' || $rowp["SYM_DOK"]=='KF' ){
    $stmt2=$mysqli2->prepare("select id from ecminvoiceouts where sys_number=?");
    $stmt2->bind_param("s", ch($rowp['NR_SYSTEM1']));
    /* execute statement */
    $stmt2->execute();
     
    /* bind result variables */
    $stmt2->bind_result($par);
     
    /* fetch values */
    while ($stmt2->fetch()) {
    
        if($par!=''){
            $record_type='EcmInvoiceOuts';
            $record_id=$par;
        }
    }
    $stmt2->close();
    }

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

}
$stmt->close();
//CREATE QUERY
//echo sizeof($insert).'';
