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

if (isset($send))
	{
	invoice_resend($send, $http_web, $email_admin, $uid, 1);

	$invoice_sent=true;
	}

if (isset($delete))	{ foreach ($delete as $line) { mysql_query("delete from invoice where iid='".addslashes(trim($line))."'"); } }

echo(" 
<form method='POST' action='".$PHP_SELF."' method='POST' onsubmit='return verify_delete(this)'>
<input type='hidden' name='sid' value='".$sid."'>
".((isset($pg))?"<input type='hidden' name='pg' value='".$pg."'>":"")."
	<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'>
			<table width='725' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td colspan='5' align='left' valign='top'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td width='25%' align='left' valign='middle'><img src='".$http_images."/menu_arrow.gif'><b>Unpaid Invoices</b></td>
							<td width='75%' align='left' valign='middle'><select name='search' ".$input_style." onChange=\"window.open(this.options[this.selectedIndex].value, '_top')\">
				<option value='#'".((isset($search))?"".((trim($search)=="0")?" selected":"")."":" selected").">View Unpaid Hosting Order Invoices for a Domain</option>
		");
	$row0=mysql_query("select oid, domain_name from hosting_order order by domain_name asc");
	while ($rs0=mysql_fetch_row($row0))
		{
		$ct=mysql_fetch_row(mysql_query("select count(*) from invoice where oid='".addslashes(trim($rs0[0]))."' and status='0' and invoice_type='0'"));
		if ($ct[0]>0)
			{
			echo"<option value='".$http_admin."/invoice_hosting_unpaid.php?sid=".$sid."&search=".$rs0[0]."&all=99'".((isset($search))?"".(($search==$rs0[0])?" selected":"")."":"").">".stripslashes(trim($rs0[1]))."</option>\n";
			}
		}
	echo("
			</select></td>
							<td width='15%' align='right' valign='middle'>[<a href='".$http_admin."/invoice.php?sid=".$sid."'><font color='#990000'>Back</font></a>]</td>
						</tr>
					</table>
					</td>
				</tr>
				<tr>
					<td colspan='5' align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
	");
if (isset($invoice_sent)==true)
	{
	echo("
				<tr>
					<td colspan='5' align='left' valign='top'><font color='#990000'><b>Your invoice was resent successfully!</b></font></td>
				</tr>
				<tr>
					<td colspan='5' align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
		");
	}
echo "</table>";

if (!isset($pg)) { $pg=0; }
$r2s=25;
$nav_query="select count(*)";
$nav_query.="from invoice ";
$nav_query.="where status='0' ";
$nav_query.="and ";
$nav_query.="invoice_type='0'";
if (isset($search))
	{
	$nav_query.="and ";
	$nav_query.="oid='".addslashes(trim($search))."'";
	}
$on_page="#990000";
echo generate_invoice_nav($r2s, $nav_query, $on_page, $pg, $sid, $all, "invoice_hosting_unpaid", 0, $http_admin);


# hosting objects invoiced
echo("
<table width='100%' cellpadding='4' cellspacing='0' border='1' bordercolor='#F0EFED'>
	<tr>
		<td align='center' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='middle'><img src='".$http_images."/icon_view.gif' alt='Hosting Account Information'>&nbsp;&nbsp;</td>
				<td width='40%' align='left' valign='middle'>Hosting Account Information</td>
			</tr>
		</table>
		</td>
		<td align='center' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='middle'><img src='".$http_images."/manage_invoice.gif' alt='Manage Invoice'>&nbsp;&nbsp;</td>
				<td width='99%' align='left' valign='middle'>Manage Invoice</td>
			</tr>
		</table>
		</td>
		<td align='center' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='middle'><img src='".$http_images."/view_formatted.gif' alt='View Formatted Invoice'>&nbsp;&nbsp;</td>
				<td width='99%' align='left' valign='middle'>View Formatted Invoice</td>
			</tr>
		</table>
		</td>
		<td align='center' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='middle'><img src='".$http_images."/resend_invoice.gif' alt='Resend Invoice'>&nbsp;&nbsp;</td>
				<td width='99%' align='left' valign='middle'>Send Invoice</td>
			</tr>
		</table>
		</td>
		<td align='center' valign='middle'>
		<table width='100%' cellpadding='0' cellspacing='0' border='0'>
			<tr>
				<td width='1%' align='left' valign='middle'><img src='".$http_images."/resend_invoice_.gif' alt='Invoice is Paid'>&nbsp;&nbsp;</td>
				<td width='99%' align='left' valign='middle'>Invoice is Paid</td>
			</tr>
		</table>
		</td>
	</tr>
</table>
	");
?>
<script language="JavaScript">
<!--
function verify_delete(theForm)
	{
	if(confirm("Sure you want to delete these invoices?")) { return true; }
	else { return false }
	}
//-->
</script> 
<?PHP
$buffer=("
			<table width='725' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td width='5%' align='center' valign='middle' bgcolor='#E5E7E9'><input type='image' alt='Click here to delete the invoices checked below.' src='".$http_images."/x.gif' onmouseover='this.src=\"".$http_images."/x_.gif\"' onmouseout='this.src=\"".$http_images."/x.gif\"'></td>
					<td width='10%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Actions</b></td>
					<td width='10%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Invoice</b></td>
					<td width='25%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Domain Name</b></td>
					<td width='15%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Invoice Created</b></td>
					<td width='15%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Invoice Due</b></td>
					<td width='10%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Status</b></td>
					<td width='10%' align='left' valign='middle' bgcolor='#E5E7E9'>&nbsp;<b>Amount</b></td>
				</tr>
				");

			$x=1;
			$y=1;
			$query="select ";
			$query.="iid, ";			// 0
			$query.="invoice_number, ";	// 1
			$query.="created, ";		// 2
			$query.="due_date, ";		// 3
			$query.="status, ";			// 4
			$query.="date_paid, ";		// 5
			$query.="oid, ";			// 6
			$query.="total_due_today, ";		// 7
			$query.="total_due_reoccur ";		// 8
			$query.="from invoice ";
			$query.="where status='0' ";
			$query.="and ";
			$query.="invoice_type='0' ";
			if (isset($search))
				{
				$query.="and ";
				$query.="oid='".addslashes(trim($search))."'";
				}
			$query.="order by invoice_number desc ";
			if (!isset($all)) { $query.="limit ".addslashes(trim($pg)).", ".addslashes(trim($r2s)).""; }

			$row=mysql_query($query);
			while ($rs=mysql_fetch_row($row))
				{
				$iid=stripslashes(trim($rs[0]));
				$invoice_number=stripslashes(trim($rs[1]));
				$created=stripslashes(trim($rs[2]));
				$due_date=stripslashes(trim($rs[3]));
				$invoice_status=stripslashes(trim($rs[4]));
				$date_paid=stripslashes(trim($rs[5]));
				$oid=stripslashes(trim($rs[6]));
				$total_due_today=stripslashes(trim($rs[7]));
				$total_due_reoccur=stripslashes(trim($rs[8]));
				
				if ($total_due_today==0) 
					{ 
					$total_invoice_amount=$currency.sprintf("%01.2f", $total_due_reoccur)." ".$currency_type; 
					}
				else { $total_invoice_amount=$currency.sprintf("%01.2f", $total_due_today)." ".$currency_type; }
				# $total_invoice_amount=$currency.sprintf("%01.2f", $total_due_today+$total_due_reoccur)." ".$currency_type;

				$rs1=mysql_fetch_row(mysql_query("select domain_name, uid from hosting_order where oid='".$oid."' limit 0, 1"));

				$domain_name=stripslashes(trim($rs1[0]));
				$uid=stripslashes(trim($rs1[1]));
		
				if ($y==1) { $bgcolor="#F2F4F6"; $y=0; } else { $bgcolor="#ECEEF0"; $y=1; }

				$buffer.=("
					<tr>
						<td width='5%' align='center' valign='middle' bgcolor='#E5E7E9'><input type='checkbox' name='delete[]' value='".$iid."'></td>
						<td width='10%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;<a href='".$http_admin."/client_view.php?sid=".$sid."&oid=".$oid."&ix=5&p=0&pg=".$pg."'><img alt='Click to view this domains hosting account information.' src='".$http_images."/icon_view.gif' border='0'></a>&nbsp;&nbsp;<a href='".$http_admin."/invoice_hosting.php?sid=".$sid."&iid=".$iid."&oid=".$oid."&uid=".$uid."&p=0&pg=".$pg."'><img alt='Click to manage this invoice.' src='".$http_images."/manage_invoice.gif' border='0'></a>&nbsp;&nbsp;<a href='".$http_web."/invoice_hosting.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $iid))."' target='_blank'><img alt='Click to view a printable invoice.' src='".$http_images."/view_formatted.gif' border='0'></a>&nbsp;".(($invoice_status!=1)?"&nbsp;<a href='".$http_admin."/invoice_hosting_unpaid.php?sid=".$sid."&uid=".$uid."&oid=".$oid."&send=".$iid."&pg=".$pg."'><img alt='Click to send a payable invoice.' src='".$http_images."/resend_invoice.gif' border='0'></a>":"&nbsp;<img alt='This invoice has been paid.' src='".$http_images."/resend_invoice_.gif' border='0'>")."&nbsp;</td>
						<td width='10%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;#".$invoice_number."</td>
						<td width='25%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;<a href='http://".$domain_name."' target='_blank'>http://".$domain_name."</a></td>
						<td width='15%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;".date("m/d/Y", $created)."</td>
						<td width='15%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;
					");
					
				if ($invoice_status==0)
					{
					if ($rs[3]<time())
						{
						$buffer.="<A HREF='".$http_admin."/invoice_hosting_history_details.php?sid=".$sid."&iid=".$iid."&oid=".$oid."&uid=".$uid."&pg=".$pg."' class='cleanlink3' onmouseover=\"return overlib('This invoice is now past due, you should consider changing the status by clicking here.', CAPTION, 'Past Due - Status Change?');\" onmouseout='nd();'><font color='#990000'>".date("m/d/Y", $due_date)."</font></a>";
						}
					else
						{
						$buffer.="<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('This invoice is unpaid.', CAPTION, 'Invoice Unpaid');\" onmouseout='nd();'>".date("m/d/Y", $due_date)."</a>";
						}
					}
				else if ($invoice_status==1)
					{
					$buffer.="<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('This invoice was paid on:<br>".date("m/d/Y", $date_paid).".', CAPTION, 'Paid Invoice');\" onmouseout='nd();'>".date("m/d/Y", $due_date)."</a>";
					}
				else if ($invoice_status==2)
					{
					$buffer.="<A HREF='javascript:void(0)' class='cleanlink3' onmouseover=\"return overlib('This invoice is past due', CAPTION, 'Past Due');\" onmouseout='nd();'><font color='#990000'>".date("m/d/Y", $due_date)."</font>";
					}
				
				$buffer.=("
						</td>
						<td width='10%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;".invoice_status($invoice_status)."</td>
						<td width='10%' align='left' valign='top' bgcolor='".$bgcolor."'>".$total_invoice_amount."</td>
					</tr>
					");
				$x++;
				}

	if ($x==1)
		{
		echo("
			<table width='725' cellpadding='2' cellspacing='1' border='0'>
					<tr>
						<td><img src='".$http_images."/space.gif' width='1' height='4'></td>
					</tr>
					<tr>
						<td><font color='#990000'>No hosting invoices found in the database for this client.</font></td>
					</tr>
				</table>
			");
		}
	else 
		{ 
		echo $buffer;
		echo("
					<tr>
						<td colspan='8'><img src='".$http_images."/space.gif' width='1' height='16'></td>
					</tr>
				</table>
			"); 
		}

	echo("
					<tr>
						<td colspan='8'><img src='".$http_images."/space.gif' width='1' height='16'></td>
					</tr>
				</table>
				</form>
				</td>
			</tr>
		</table>
		");

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