<?php
error_reporting(E_ERROR);
header('Content-type: text/html; charset=utf-8');
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']);

mysql_query("truncate ecmproduct_replacement_groups");
mysql_query("set character set utf8;"); 

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

$num_recp=$dbfp->dbf_num_rec;

$zamienniki = array();

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

	$res = mysql_query("SELECT id FROM ecmproducts WHERE index_dod='".$rowp['INDEKS1']."'");
	
	$row = mysql_fetch_assoc($res);
	
	if ((isset($row['id'])) && ($row['id']!='')) {
		//if (!isset($zamienniki[$rowp['GRUPA_ZAM']])) $zamienniki[$rowp['GRUPA_ZAM']] = array();
		$zamienniki[$rowp['GRUPA_ZAM']][] = $row['id'];
	}
}

foreach ($zamienniki as $k=>$v) {
	$gid = create_guid();
	foreach ($v as $id) {
		$row = array (
			addslashes(create_guid()), //id
			addslashes(date("Y-m-d H:i:s")), //date_entered
			addslashes("cf3a00c2-807d-8e12-592e-4fe88f59cbf6"), //created_by
			addslashes("0"), //deleted
			addslashes($gid), //group_id
			addslashes($id), //ecmproduct_id
			addslashes($k), //group_index_dbf
		);
		$query = "INSERT INTO ecmproduct_replacement_groups VALUES ('".implode("','", $row)."')";
		mysql_query(ch($query));
	}
}

?>