<?php
include_once("class.dbf.php");
include_once("/var/www/crm/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']);


//get actual id's
$ids = array();
$res = mysql_query("SELECT id, index_dbf FROM ecmpricebooks");
while ($row = mysql_fetch_array($res))
	$ids[$row['index_dbf']] = $row['id'];
	
//var_dump($ids);

mysql_query("truncate ecmpricebooks");
mysql_query("truncate ecmpricebooks_ecmproducts");
mysql_query("set character set utf8;"); 

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

$num_recp=$dbfp->dbf_num_rec;

$pricebooks = array();

for($i=0;$i<$num_recp;$i++){
	$rowp=$dbfp->getRowAssoc($i);	
	
	if (($rowp['KOD_CENYQ']=='')) continue;
	
	if (!isset($pricebooks[$rowp['KOD_CENYQ']]))
		$pricebooks[$rowp['KOD_CENYQ']]= array();

	$pricebooks[$rowp['KOD_CENYQ']]['products'][$rowp['NR_SYSTEMQ']] = $rowp['CENAENYQ'];
}


//get pricebooks names 
$path = dirname(__FILE__).'/_kursy.dbf';
$dbfp = new dbf_class($path);
$num_recp=$dbfp->dbf_num_rec;

for($i=0;$i<$num_recp;$i++){
	$rowp=$dbfp->getRowAssoc($i);	
	
	foreach ($pricebooks as $k=>$v)
		if (intval($k)*(-1)==intval($rowp['KOD'])) {
			$pricebooks[$k]['name']=$rowp['NAZWA'];
			$pricebooks[$k]['currency_value']=$rowp['KURS'];
			$pricebooks[$k]['date_modified']=$rowp['DATA_AKT'];
			$pricebooks[$k]['exchange_rate_name']=$rowp['SYMBOL'];
			$pricebooks[$k]['active']=$rowp['UZYTY'];
		}
}

$path = dirname(__FILE__).'/_naz_cen.dbf';
$dbfp = new dbf_class($path);
$num_recp=$dbfp->dbf_num_rec;

for($i=0;$i<$num_recp;$i++){
	$rowp=$dbfp->getRowAssoc($i);	
	
	foreach ($pricebooks as $k=>$v)
		if (intval($k)==intval($rowp['KOD'])) {
			$pricebooks[$k]['name']=$rowp['NAZWA'];
			$pricebooks[$k]['currency_value']=$rowp['KURS'];
			$pricebooks[$k]['date_modified']=$rowp['DATA_AKT'];
			$pricebooks[$k]['exchange_rate_name']=$rowp['SYMBOL'];
			$pricebooks[$k]['active']=$rowp['UZYTY'];
		}
}

//foreach ($pricebooks as $k=>$v)
	
	//if ((!isset($v['active'])) || ($v['active']!='1')) unset($pricebooks[$k]);

		
//insert to db
//get ecmproducts id's
global $ecmproducts;

$ecmproducts = array();
$res = mysql_query("SELECT product_id, nr_system FROM ecmstockstates");
while ($row = mysql_fetch_array($res))
	$ecmproducts[trim($row['nr_system'])] = $row['product_id'];
	
$braki = array();
$i=0;
foreach ($pricebooks as $k=>$v) {
	$i++;
	echo $i.'
';

	if (isset($ids[$k]))
		$ecmpricebook_id = $ids[$k];
	else
		$ecmpricebook_id = create_guid();

	$date = $v['date_modified'];
	
	$fields = array ('id', 'name', 'date_entered', 'date_modified', 'modified_user_id', 'assigned_user_id', 'created_by', 'deleted', 'active', 'exchange_rate_name','currency_value', 'index_dbf');
	$row = array (
		addslashes($ecmpricebook_id),
		addslashes($v['name']),
		addslashes($date),
		addslashes($date),
		addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"),
		addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"),
		addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"),
		addslashes("0"),
		addslashes($v['active']),
		addslashes($v['exchange_rate_name']),
		addslashes($v['currency_value']),
		addslashes($k),
	);
	
	$query = "INSERT INTO ecmpricebooks (".implode(",", $fields).") VALUES ('".implode("','", $row)."')";

	mysql_query(ch($query));
	if (mysql_error()) echo 'Database error: '.mysql_error().'
';
	
	//insert products
	foreach ($v['products'] as $nr_system => $price) {
		
		//$ecmproduct_id = $ecmproducts[trim($nr_system)];

		if (isset($ecmproducts[trim($nr_system)])) 
			$ecmproduct_id=$ecmproducts[trim($nr_system)];
		else {
		$braki[trim($nr_system)]=true;
		continue;
		} 
		
		$fields = array('id','ecmpricebook_id','ecmproduct_id','deleted','price','created_by','date_entered','date_modified','modified_user_id');
		
		$row = array(
			addslashes(create_guid()),
			addslashes($ecmpricebook_id),
			addslashes($ecmproduct_id),
			addslashes("0"),
			addslashes($price),
			addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"),
			addslashes($date),
			addslashes($date),
			addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"),
		);
		
		$query = "INSERT INTO ecmpricebooks_ecmproducts (".implode(",", $fields).") VALUES ('".implode("','", $row)."')";
		
		mysql_query(ch($query));
		if (mysql_error()) echo 'Database error: '.mysql_error().'
';
	}
}
mysql_close($sql);
?>