<?php
ini_set('display_errors',1);
function countDigits($str) {
    $noDigits=0;
    for ($i=0;$i<strlen($str);$i++) {
        if (is_numeric($str{$i})) $noDigits++;
    }
    return $noDigits;
}

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_7561b7965a2f9cebf2cbca60a9a07064',
				'db_type' => 'mysql',
		),
		);

$path = dirname(__FILE__).'/przemyslowka/glowny/_ODBIOR.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_7561b7965a2f9cebf2cbca60a9a07064');
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();
}

$sugar_fields = array ('id','index_dbf2','name','register_address_postalcode','register_address_street','to_vatid','phone_office','phone_fax','description','email','index_dbf','ownership','currency_id','invoice_type','account_type');

$insert = array();
$count =0; 
//for($i=0;$i<$num_recp;$i++){ 
$j=0;


$mysqli->query("truncate accounts");
$mysqli->query("delete from dom_lists where dom_name='account_type_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);
$mysqli->query("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES ('234e','pl_pl','account_type_dom','c37746a5-0f62-5173-9020-574edf44b054','Odbiorca')");
$mysqli->query("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES ('234e4','pl_pl','account_type_dom','8d34359a-7d53-eb64-c607-57611fb2b087','Dostawca')");
for($i=$num_recp;$i>=0;$i--){
	$rowp=$dbfp->getRowAssoc($i);
	

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

$stmt=$mysqli->prepare("INSERT INTO accounts (id,name,register_address_street,register_address_city,register_address_state,register_address_postalcode,register_address_country,to_vatid
		,index_dbf,account_type,index_dbf2,ks_account,description,phone_office,phone_fax,currency_id,invoice_type,allow_prevent,allow_debt_collection) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssssssssss", $id,$nazwa,$ulica,$miasto,$woj,$poczta,$panstwo,$nip,$indeks,$typ,$indeks2,$ks_account,$description,$tel,$fax,$wal,$pdf,$sms_allow,$sms_allow2);

$a=1;
// uzupełnianie listy profili


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

    if($rowp['NAZWA']!=''){
        $nazwa= $rowp['NAZWA']; 
        if($rowp['NAZWA1']!=''){
            $nazwa.= ' '.$rowp['NAZWA1'];
        }
    } else {
        continue;
    }

    $indeks=ch($rowp['INDEKS']);
    $nazwa=ch($nazwa);
    $indeks2=ch($rowp['NR_EWIDENC']);
    $id=create_guid();
    $adres=explode(" ",ch($rowp['ADRES1']));
    $poczta=$adres[0];
    $ks_account=ch($rowp['KONTO']);
    $ulica=ch($rowp['ADRES2']);
    $miasto=$adres[1];
    $nip=ch($rowp['NR_IDENT']);
    $tel=ch($rowp['TEL']);
    $fax=ch($rowp['FAX']);
    $opis=ch($rowp['OPIS1']).' '.ch($rowp['OPIS2']).' '.ch($rowp['OPIS3']).' '.ch($rowp['OPIS4']).' '.ch($rowp['OPIS5']).' '.ch($rowp['ADRES1']).' '.ch($rowp['ADRES2']).' '.ch($rowp['UWAGI']); 
    $emal=ch($rowp['EMAIL']);
    $dyr=ch($rowp['DYR_WLRK']);
    $wal=ch('PLN');
    $pdf=ch('K');
    $typ='c37746a5-0f62-5173-9020-574edf44b054';
    $sms_allow=0;
    $sms_allow2=0;
    if(ch($rowp['SMS1'])!='' || ch($rowp['SMS2'])!='' || ch($rowp['SMS3'])!=''){
    	$sms_allow=1;
    	$sms_allow2=1;
    } 

    if(ch($rowp['BRAK_VAT'])!='F'){
        $vat_payer='1';
    } else {
        $vat_payer='0';
    }
    $typ2='ab54eee0-5782-a292-bcaf-54d11a78b153';

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

if(ch($rowp['SMS1'])!=''){
	$id2 = create_guid();
$t = array(
		$id2,
		$id,
		'0',
		date("Y-m-d H:i:s"),
		'0',
		addslashes(ch($rowp['SMS1'])),
		'0',
		date("Y-m-d H:i:s"),
		'0',
);
$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
$mysqli->query($q);
}
if(ch($rowp['SMS2'])!=''){
	$id2 = create_guid();
	$t = array(
			$id2,
			$id,
			'0',
			date("Y-m-d H:i:s"),
			'0',
			addslashes(ch($rowp['SMS2'])),
			'0',
			date("Y-m-d H:i:s"),
			'0',
	);
	$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
	$mysqli->query($q);
}
if(ch($rowp['SMS3'])!=''){
	$id2 = create_guid();
	$t = array(
			$id2,
			$id,
			'0',
			date("Y-m-d H:i:s"),
			'0',
			addslashes(ch($rowp['SMS3'])),
			'0',
			date("Y-m-d H:i:s"),
			'0',
	);
	$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
	$mysqli->query($q);
}

$a++;

}
// dostawcy

$path = dirname(__FILE__).'/przemyslowka/glowny/_DOSTAW.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_7561b7965a2f9cebf2cbca60a9a07064');
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();
}

$sugar_fields = array ('id','index_dbf2','name','register_address_postalcode','register_address_street','to_vatid','phone_office','phone_fax','description','email','index_dbf','ownership','currency_id','invoice_type','account_type');

$insert = array();
$count =0;
//for($i=0;$i<$num_recp;$i++){
$j=0;




$stmt=$mysqli->prepare("INSERT INTO dom_lists (id,lang,dom_name,`key`,`value`) VALUES (?,?,?,?,?)");
$stmt->bind_param("sssss", $id,$lang,$dom_name,$key,$value);

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


	if(ch($rowp['PROFIL'])=='')continue;

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

}

$stmt=$mysqli->prepare("INSERT INTO accounts (id,name,register_address_street,register_address_city,register_address_state,register_address_postalcode,register_address_country,to_vatid
		,index_dbf,account_type,index_dbf2,ks_account,description,phone_office,phone_fax,currency_id,invoice_type,allow_prevent,allow_debt_collection) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$stmt->bind_param("sssssssssssssssssss", $id,$nazwa,$ulica,$miasto,$woj,$poczta,$panstwo,$nip,$indeks,$typ,$indeks2,$ks_account,$description,$tel,$fax,$wal,$pdf,$sms_allow,$sms_allow2);

$a=1;
// uzupełnianie listy profili


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

	if($rowp['NAZWA']!=''){
		$nazwa= $rowp['NAZWA'];
		if($rowp['NAZWA1']!=''){
			$nazwa.= ' '.$rowp['NAZWA1'];
		}
	} else {
		continue;
	}

	$indeks=ch($rowp['INDEKS']);
	$nazwa=ch($nazwa);
	$indeks2=ch($rowp['NR_EWIDENC']);
	$id=create_guid();
	$adres=explode(" ",ch($rowp['ADRES1']));
	$poczta=$adres[0];
	$ks_account=ch($rowp['KONTO']);
	$ulica=ch($rowp['ADRES2']);
	$miasto=$adres[1];
	$nip=ch($rowp['NR_IDENT']);
	$tel=ch($rowp['TEL']);
	$fax=ch($rowp['FAX']);
	$opis=ch($rowp['OPIS1']).' '.ch($rowp['OPIS2']).' '.ch($rowp['OPIS3']).' '.ch($rowp['OPIS4']).' '.ch($rowp['OPIS5']).' '.ch($rowp['ADRES1']).' '.ch($rowp['ADRES2']).' '.ch($rowp['UWAGI']);
	$emal=ch($rowp['EMAIL']);
	$dyr=ch($rowp['DYR_WLRK']);
	$wal=ch('PLN');
	$pdf=ch('K');
	$typ='8d34359a-7d53-eb64-c607-57611fb2b087';
	$sms_allow=0;
	$sms_allow2=0;
	if(ch($rowp['SMS1'])!='' || ch($rowp['SMS2'])!='' || ch($rowp['SMS3'])!=''){
		$sms_allow=1;
		$sms_allow2=1;
	}

	if(ch($rowp['BRAK_VAT'])!='F'){
		$vat_payer='1';
	} else {
		$vat_payer='0';
	}
	$typ2='ab54eee0-5782-a292-bcaf-54d11a78b153';

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

	if(ch($rowp['SMS1'])!=''){
		$id2 = create_guid();
		$t = array(
				$id2,
				$id,
				'0',
				date("Y-m-d H:i:s"),
				'0',
				addslashes(ch($rowp['SMS1'])),
				'0',
				date("Y-m-d H:i:s"),
				'0',
		);
		$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
		$mysqli->query($q);
	}
	if(ch($rowp['SMS2'])!=''){
		$id2 = create_guid();
		$t = array(
				$id2,
				$id,
				'0',
				date("Y-m-d H:i:s"),
				'0',
				addslashes(ch($rowp['SMS2'])),
				'0',
				date("Y-m-d H:i:s"),
				'0',
		);
		$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
		$mysqli->query($q);
	}
	if(ch($rowp['SMS3'])!=''){
		$id2 = create_guid();
		$t = array(
				$id2,
				$id,
				'0',
				date("Y-m-d H:i:s"),
				'0',
				addslashes(ch($rowp['SMS3'])),
				'0',
				date("Y-m-d H:i:s"),
				'0',
		);
		$q = "INSERT INTO accounts_telephones VALUES ('".implode("','", $t)."')";
		$mysqli->query($q);
	}

	$a++;

}





?>