<?
print $_REQUEST['return_module'];
if($_REQUEST['return_module']=="Contacts")
{
	$field_id="contact_id";
	$table="contacts";
}
if($_REQUEST['return_module']=="Accounts")
{
	$field_id="account_id";
	$table="accounts";
}
function getValue($field,$items)
{
	global $field_id,$table;
	foreach($items as $item)$wh[]="id='".$item."'";
	$where=implode(" or ",$wh);
	
	$query=mysql_query("select ".$field." from ".$table." where (".$where.") and deleted='0' order by date_modified desc");
	while($row=mysql_fetch_array($query))
	{
		if($row[$field])return $row[$field];
		else continue;
	}
}
function setRelations($relation,$items,$merged_item)
{
	global $field_id,$table;
	foreach($items as $item)
	{
		mysql_query("update ".$relation." set ".$field_id."='".$merged_item."' where ".$field_id."='".$item."'");
		if($table=="contacts")
		{
			$r=mysql_fetch_array(mysql_query("select first_name,last_name from contacts where id='".$merged_item."'"));
			$name=$r['first_name']." ".$r['last_name'];
			mysql_query("update tasks set parent_id='".$merged_item."',parent_name='".$name."' where parent_id='".$item."'");
		}
		if($table=="accounts")
		{
			$r=mysql_fetch_array(mysql_query("select name from accounts where id='".$merged_item."'"));
			$name=$r['name'];
			mysql_query("update ecmcontracts set contractant_id='".$merged_item."',contractant_name='".$name."' where contractant_id='".$item."'");
			mysql_query("update ecmcontracts set group_id='".$merged_item."',group_name='".$name."' where group_id='".$item."'");
			mysql_query("update ecmcorrespondenceins set account_from='".$merged_item."',account_name_from='".$name."' where account_from='".$item."'");
			mysql_query("update ecmcorrespondenceins set account_to='".$merged_item."',account_name_to='".$name."' where account_to='".$item."'");
			mysql_query("update ecmcorrespondenceouts set account_from='".$merged_item."',account_from_name='".$name."' where account_from='".$item."'");
			mysql_query("update ecmcorrespondenceouts set account_to='".$merged_item."',account_to_name='".$name."' where account_to='".$item."'");
			mysql_query("update ecminvoiceins set from_id='".$merged_item."',from_name='".$name."' where from_id='".$item."'");
			mysql_query("update ecminvoiceins set to_id='".$merged_item."',to_name='".$name."' where to_id='".$item."'");
			mysql_query("update ecminvoiceouts set from_id='".$merged_item."',from_name='".$name."' where from_id='".$item."'");
			mysql_query("update ecminvoiceouts set to_id='".$merged_item."',to_name='".$name."' where to_id='".$item."'");
			
			mysql_query("update meetings set account_id='".$merged_item."',account_name='".$name."' where account_id='".$item."'");
			mysql_query("update calls set account_id='".$merged_item."',account_name='".$name."' where account_id='".$item."'");
			mysql_query("update tasks set account_id='".$merged_item."',account_name='".$name."' where account_id='".$item."'");
		}
	}
}

foreach($_REQUEST['check'] as $check)
{
	if($check!=$_REQUEST['first'])$va[]=$check;
}
if(!$_REQUEST['first'])$_REQUEST['first']=$va[0];
foreach($va as $v)
{
	if($v!=$_REQUEST['first'])$items[]=$v;
}

$merged_item=$_REQUEST['first'];

//print "first: ".$merged_item;
if($_REQUEST['return_module']=="Contacts")
{
	$fields=array("salutation","first_name","last_name","lead_source","title","department","reports_to_id","birthdate","do_not_call","phone_home","phone_mobile","phone_work","phone_other","phone_fax","email1","email2","assistant","assistant_phone","email_opt_out","primary_address_street","primary_address_city","primary_address_state","primary_address_postalcode","primary_address_country","alt_address_street","alt_address_city","alt_address_state","alt_address_postalcode","alt_address_country","description","portal_name","portal_active","portal_app","invalid_email");
	$relations=array("calls_contacts","ecmdocuments_contacts","emails_contacts","meetings_contacts","projects_contacts","contacts_users");
}
if($_REQUEST['return_module']=="Accounts")
{
	$fields=array("parent_id","account_type","industry","annual_revenue","phone_fax","billing_address_street","billing_address_city","billing_address_state","billing_address_postalcode","billing_address_country","description","rating","phone_office","phone_alternate","email1","email2","website","ownership","employees","sic_code","ticker_symbol","shipping_address_street","shipping_address_city","shipping_address_state","shipping_address_postalcode","shipping_address_country","bank","bank_no");
	$relations=array('ecmdocuments_accounts','accounts_contacts','accounts_opportunities');
}
foreach($fields as $field)
{
	$r=mysql_fetch_array(mysql_query("select ".$field." from ".$table." where id='".$merged_item."'"));
	if(!$r[$field])$fi[]=$field."='".getValue($field,$items)."'";
}
$set=implode(",",$fi);

mysql_query("update ".$table." set ".$set." where id='".$merged_item."'");
foreach($relations as $relation)setRelations($relation,$items,$merged_item);

foreach($items as $item)
{
	mysql_query("update ".$table." set modified_user_id='".$_SESSION['authenticated_user_id']."',date_modified='".date("Y-m-d H:i:s")."',deleted='1' where id='".$item."'");
}

if($_REQUEST['backtounified']==1)header("Location: ".$_SERVER['HTTP_REFERER']);
else header("Location: index.php?module=".$_REQUEST['return_module']."&action=index#title");
?>
