<?PHP
include "../inc/var.php";
include "../inc/connect.php";

$query.="select ";
$query.="bid, ";
$query.="customer_first_name, ";
$query.="customer_last_name, ";
$query.="customer_address, ";
$query.="customer_city, ";
$query.="customer_state, ";
$query.="customer_zip, ";
$query.="customer_country, ";
$query.="customer_phone, ";
$query.="customer_fax, ";
$query.="customer_email, ";
$query.="uid, ";
$query.="card_info, ";
$query.="oid ";
$query.="from ";
$query.="authnet_batch ";
$query.="order by bid asc";

$row=mysql_query($query);
while ($rs=mysql_fetch_row($row))
	{
	$bid=stripslashes(trim($rs[0]));
	$customer_first_name=stripslashes(trim($rs[1]));
	$customer_last_name=stripslashes(trim($rs[2]));
	$customer_address=stripslashes(trim($rs[3]));
	$customer_city=stripslashes(trim($rs[4]));
	$customer_state=stripslashes(trim($rs[5]));
	$customer_zip=stripslashes(trim($rs[6]));
	$customer_country=stripslashes(trim($rs[7]));
	$customer_phone=stripslashes(trim($rs[8]));
	$customer_fax=stripslashes(trim($rs[9]));
	$customer_email=stripslashes(trim($rs[10]));
	$uid=stripslashes(trim($rs[11]));
	$card_info=stripslashes(trim($rs[12]));
	
	if (trim($card_info)=="") { continue; }

	$oid=stripslashes(trim($rs[13]));

	$queryCT="select ";
	$queryCT.="bid, ";
	$queryCT.="count(*) ";
	$queryCT.="from ";
	$queryCT.="authnet_master_cc ";
	$queryCT.="where ";
	$queryCT.="card_info='".addslashes(trim($card_info))."' ";
	$queryCT.="group by bid";

	$ct=@mysql_fetch_row(@mysql_query($queryCT));

	if ($ct[1]<1)
		{
		# make sure we have a valid uid or bail
		if ($uid==0&&$oid==0) { continue; }
		else if ($uid==0)
			{
			$queryGETuid="select ";
			$queryGETuid.="uid ";
			$queryGETuid.="from ";
			$queryGETuid.="hosting_order ";
			$queryGETuid.="where ";
			$queryGETuid.="oid='".addslashes(trim($oid))."'";

			$rsGETuid=mysql_fetch_row(mysql_query($queryGETuid));
			$uid=stripslashes(trim($rsGETuid[0]));
			}
		
		# reset the master
		$query2="update ";
		$query2.="authnet_master_cc ";
		$query2.="set ";
		$query2.="master_cc='0' ";
		$query2.="where ";
		$query2.="uid='".addslashes(trim($uid))."' ";

		mysql_query($query2);

		# create the cc record in the database
		$query="insert into ";
		$query.="authnet_master_cc ";
		$query.="set ";
		$query.="customer_first_name='".addslashes(trim($customer_first_name))."', ";
		$query.="customer_last_name='".addslashes(trim($customer_last_name))."', ";
		$query.="customer_address='".addslashes(trim($customer_address))."', ";
		$query.="customer_city='".addslashes(trim($customer_city))."', ";
		$query.="customer_state='".addslashes(trim($customer_state))."', ";
		$query.="customer_zip='".addslashes(trim($customer_zip))."', ";
		$query.="customer_country='".addslashes(trim($customer_country))."', ";
		$query.="customer_phone='".addslashes(trim($customer_phone))."', ";
		$query.="customer_fax='".addslashes(trim($customer_fax))."', ";
		$query.="customer_email='".addslashes(trim($customer_email))."', ";
		$query.="uid='".addslashes(trim($uid))."', ";
		$query.="card_info='".addslashes(trim($card_info))."', ";
		$query.="master_cc='1', ";
		$query.="ogcreate='".time()."'";

		mysql_query($query);

		$ccid=mysql_insert_id();

		# update the batch witht he ccid
		$query="update ";
		$query.="authnet_batch ";
		$query.="set ";
		$query.="ccid='".addslashes(trim($ccid))."' ";
		$query.="where ";
		$query.="bid='".addslashes(trim($bid))."'";

		mysql_query($query);
		}
	
	
	unset($bid);
	unset($oid);
	unset($customer_first_name);
	unset($customer_last_name);
	unset($customer_address);
	unset($customer_city);
	unset($customer_state);
	unset($customer_zip);
	unset($customer_country);
	unset($customer_phone);
	unset($customer_fax);
	unset($customer_email);
	unset($uid);
	unset($card_info);
	}

mysql_close($dblink);
?>