<?PHP
include "../inc/var.php";
include "../inc/connect.php";
include $server_inc."/client_functions.php";
include $server_inc."/login_functions.php";
include $server_inc."/invoice_functions.php";

if (isset($submit))
	{
	$query0="update ";
	$query0.="invoice ";
	$query0.="set ";
	$query0.="status='".addslashes(trim($status))."', ";
	
	if (isset($created)) { $query0.="created='".create_timestamp($created)."', "; } 

	if ($status==1&&!isset($date_paid)) { $query0.="date_paid='".time()."', "; }
	else if ($status==1&&isset($date_paid)) { $query0.="date_paid='".create_timestamp($date_paid)."', "; } 
	else { $query0.="date_paid='', "; }

	if (isset($due_date)) { $query0.="due_date='".create_timestamp($due_date)."', "; } 

	$query0.="txn_id='".addslashes(trim($txn_id))."', ";
	$query0.="payment_method='".addslashes(trim($payment_method))."', ";
	if (trim($total_due_today)!="") { $query0.="total_due_today='".addslashes(trim($total_due_today))."', "; }
	if (trim($total_due_reoccur)!="") { $query0.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', "; }
	$query0.="extras='".addslashes(trim($extras))."' ";
	$query0.="where ";
	$query0.="iid='".addslashes(trim($iid))."'";
	
	mysql_query($query0);
	
	/*
	if (isset($change_hosting_order_date))
		{
		$query1="update ";
		$query1.="hosting_order ";
		$query1.="set ";
		$query1.="next_due_date='".create_timestamp($due_date)."' ";
		$query1.="where ";
		$query1.="oid='".addslashes(trim($oid))."'";

		mysql_query($query1);
		}
	*/
	}

include $server_admin_inc."/header.php";

echo("
	<table width='725' cellpadding='0' cellspacing='0' border='0'>
		<tr>
			<td width='1%' align='left' valign='top'><img src='".$http_images."/space.gif' width='15' height='375'></td>
			<td width='99%' align='left' valign='top'>

			<form action='".$PHP_SELF."' method='POST'>
			<input type='hidden' name='sid' value='".trim($sid)."'>
			<input type='hidden' name='oid' value='".$oid."'>
			<input type='hidden' name='uid' value='".$uid."'>
			<input type='hidden' name='iid' value='".$iid."'>
			<input type='hidden' name='p' value='".$p."'>
			".((isset($pg))?"<input type='hidden' name='pg' value='".$pg."'>":"")."
			<table width='725' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/menu_arrow.gif'><b>Web Hosting Invoice</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/invoice_".(($p==1)?"hosting_paid":"".(($p==2)?"hosting_past_due":"".(($p==0)?"hosting_unpaid":"history")."")."").".php?sid=".$sid."&oid=".$oid."&uid=".$uid."".((isset($pg))?"&pg=".$pg."":"")."'><font color='#990000'>Back</font></a>]</td>
				</tr>
				<tr>
					<td align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
			</table>
			<table width='725' cellpadding='6' cellspacing='0' border='0'>
	");

$query0="select ";
$query0.="invoice.due_date, ";				// 0
$query0.="invoice.payment_method, ";		// 1
$query0.="invoice.invoice_details, ";		// 2
$query0.="invoice.status, ";				// 3
$query0.="invoice.created, ";				// 4
$query0.="invoice.date_paid, ";				// 5
$query0.="invoice.invoice_number, ";		// 6
$query0.="invoice.total_due_today, ";		// 7
$query0.="invoice.total_due_reoccur, ";		// 8
$query0.="invoice.extras, ";				// 9
$query0.="invoice.master, ";				// 10
$query0.="invoice.checkout_id, ";			// 11
$query0.="invoice.invoice_type, ";			// 12
$query0.="hosting_order.domain_name, ";		// 13
$query0.="invoice.txn_id ";					// 14
$query0.="from ";
$query0.="invoice, hosting_order ";
$query0.="where ";
$query0.="invoice.iid='".addslashes(trim($iid))."' ";
$query0.="and ";
$query0.="invoice.oid=hosting_order.oid ";
$query0.="order by invoice.oid asc";

$rs0=mysql_fetch_row(mysql_query($query0));

$due_date=stripslashes(trim($rs0[0]));
$payment_method=stripslashes(trim($rs0[1]));
$invoice_details=stripslashes(trim($rs0[2])); # scrap
$invoice_status=stripslashes(trim($rs0[3]));
$created=stripslashes(trim($rs0[4]));
$date_paid=stripslashes(trim($rs0[5]));
$invoice_number=stripslashes(trim($rs0[6]));
$total_due_today=stripslashes(trim($rs0[7]));
$total_due_reoccur=stripslashes(trim($rs0[8]));
$extras=stripslashes(trim($rs0[9]));
$master=stripslashes(trim($rs0[10]));
$checkout_id=stripslashes(trim($rs0[11]));
$invoice_type=stripslashes(trim($rs0[12]));
$domain_name=stripslashes(trim($rs0[13]));
$txn_id=stripslashes(trim($rs0[14]));

echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice #: </td>
					<td width='80%' align='left' valign='middle'><font color='#990000'><b>#".$invoice_number."".(($master==1)?"&nbsp;&nbsp;[Current Invoice]":"")."</b></font></td>
				</tr>
				<tr>
					<td width='20%' align='left' valign='middle'>Domain: </td>
					<td width='80%' align='left' valign='middle'><font color='#990000'>".$domain_name."</font></td>
				</tr>
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice Created: </td>
					<td width='80%' align='left' valign='middle'><input size='15' maxlength='25' ".$input_style." type='text' name='created' value='".date("m/d/Y", $created)."'> <font color='#990000'>ex. mm/dd/YYYY</font></td>
				</tr>
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice Status: </td>
					<td width='80%' align='left' valign='middle'><select name='status' ".$input_style.">
	");

$state=invoice_status_select($invoice_status);
$count=(substr_count($state, "|")-1);
$array=explode("|", $state);
for ($i=0; $i<=$count; $i++)
	{
	list($id, $state)=explode("@", $array[$i]);
	if ($id==$invoice_status&&$id==1) { $paid=true; }
	echo "<option value='".$id."'".(($id==$invoice_status)?" selected":"").">".$state."</option>";
	}

echo("
					</select></td>
				</tr>
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice ".((isset($paid))?"Paid":"Due")." Date: </td>
					<td width='80%' align='left' valign='middle'>".((isset($paid))?"<input size='15' maxlength='25' ".$input_style." type='text' name='date_paid' value='".((trim($date_paid)!="")?"".date("m/d/Y", $date_paid)."":"")."'>":"<input size='15' maxlength='25' ".$input_style." type='text' name='due_date' value='".((trim($due_date)!="")?"".date("m/d/Y", $due_date)."":"")."'>")." <font color='#990000'>ex. mm/dd/YYYY</font></td>
				</tr>
		");
if (isset($paid))
	{
	echo("
				<tr>
					<td width='25%' align='left' valign='top'>&nbsp;Transaction ID:</td>
					<td width='75%' align='left' valign='top'><input ".$input_style." size='14' maxlength='255' type='text' name='txn_id' value='".((trim($txn_id)=="")?"None":"".$txn_id."")."'></td>
				</tr>
		");
	}
echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Payment Method: </td>
					<td width='80%' align='left' valign='middle'>".((isset($paid))?"<input type='hidden' name='payment_method' value='".$payment_method."'>":"")."<select name='payment_method' ".$input_style."".((isset($paid))?" disabled":"").">
	");
$query1="select ";
$query1.="pid, ";
$query1.="name ";
$query1.="from ";
$query1.="payment_process ";
$query1.="where ";
$query1.="status='1'";

$row1=mysql_query($query1);
while ($rs1=mysql_fetch_row($row1))
	{
	echo "<option value='".$rs1[0]."'".(($payment_method==$rs1[0])?" selected":"").">".stripslashes(trim($rs1[1]))."</option>";
	}

echo("
					</select>".(($payment_method==4&&$invoice_type==1)?"&nbsp;&nbsp;&nbsp;".((trim($checkout_id)=="")?"<font color='#990000'><b>Enter 2Checkout Plan ID:</b></font>":"Enter 2Checkout Plan ID:")."&nbsp;<input size='8' maxlength='10' ".$input_style." type='text' name='checkout_id' value='".$checkout_id."'>":"")."</td>
				</tr>
	");
if ($total_due_today>0)
	{
	echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice Amount:</td>
					<td width='80%' align='left' valign='middle'>".$currency."<input size='8' maxlength='10' ".$input_style." type='text' name='total_due_today' value='".sprintf("%01.2f", $total_due_today)."'> ".$currency_type."</td>
				</tr>
		");
	}
else
	{
	echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice Amount:</td>
					<td width='80%' align='left' valign='middle'>".$currency."<input size='8' maxlength='10' ".$input_style." type='text' name='total_due_reoccur' value='".sprintf("%01.2f", $total_due_reoccur)."'> ".$currency_type."</td>
				</tr>
		");
	}
/*
echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Amount One Time:</td>
					<td width='80%' align='left' valign='middle'>".$currency."<input size='8' maxlength='10' ".$input_style." type='text' name='total_due_today' value='".$total_due_today."'> ".$currency_type."&nbsp;&nbsp;<font color='#990000' size='1'>0 Amounts will not show up on invoice.</font></td>
				</tr>
	");
echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Amount Recurring:</td>
					<td width='80%' align='left' valign='middle'>".$currency."<input size='8' maxlength='10' ".$input_style." type='text' name='total_due_reoccur' value='".$total_due_reoccur."'> ".$currency_type."</td>
				</tr>
	");
echo("
				<tr>
					<td width='20%' align='left' valign='middle'>Invoice Amount:</td>
					<td width='80%' align='left' valign='middle'>
		");

if ($total_due_today==0) { echo $currency.sprintf("%01.2f", $total_due_reoccur)." ".$currency_type; }
else { echo $currency.sprintf("%01.2f", $total_due_today)." ".$currency_type; }

echo("
					</td>
				</tr>
	");
*/
echo("
				<tr>
					<td width='20%' align='left' valign='top'>Account Addons Ordered:</td>
					<td width='80%' align='left' valign='top'><textarea name='extras' ".$input_style." rows='6' cols='75'>".$extras."</textarea></td>
				</tr>
				<tr>
					<td colspan='2' align='left' valign='top'><img src='".$http_images."/space.gif' width='1' height='12'></td>
				</tr>
				<tr>
					<td width='20%' align='left' valign='top'></td>
					<td width='80%' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Save Changes Now'></td>
				</tr>
			</table>
			</form>

			</td>
		</tr>
	</table>
	");

include $server_admin_inc."/footer.php";
mysql_close($dblink);
?>