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

if (isset($submit))
	{
	$xbuffer="";
	foreach ($oid as $value) { $xbuffer.=create_individual_invoice_history($value, $below_public); }
	}

include $server_admin_inc."/header.php";

if (!isset($pg)) { $pg=0; }
echo("
	<form action='".$PHP_SELF."' method='POST'>
	<input type='hidden' name='sid' value='".$sid."'>
	<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='100%' cellpadding='4' cellspacing='1' border='0'>
				<tr>
					<tdalign='left' valign='top'><img src='".$http_images."/menu_arrow.gif'> <b>Create Past Invoices</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[<a href='".$http_admin."/invoice.php?sid=".$sid."'>back</a>]</td>
				</tr>
				<tr>
					<td align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
			</table>
		");
if (isset($xbuffer)) 
	{
	echo("
			<table width='100%' cellpadding='4' cellspacing='1' border='0'>
				<tr>
					<td align='left' valign='top'><a href='".$http_admin."/past_invoice_creation.php?sid=".$sid."'>Click to continue</a></td>
				</tr>
				<tr>
					<td>".((trim($xbuffer)=="")?"Nothing was submitted.":"<textarea cols='110' rows='20' ".$input_style.">".$xbuffer."</textarea>")."</td>
				</tr>
				<tr>
					<td align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
			</table>
				</td>
			</tr>
		</table>
		</form>
		");

	include $server_admin_inc."/footer.php";
	mysql_close($dblink);
	die();
	}

$buffer=("
			<table width='100%' cellpadding='4' cellspacing='1' border='0'>
				<tr>
					<td width='15%' align='center' valign='middle' bgcolor='#E5E7E9'><b>Include</b></td>
					<td width='60%' align='left' valign='top' bgcolor='#E5E7E9'>&nbsp;<b>Domain Name</b></td>
					<td width='25%' align='left' valign='top' bgcolor='#E5E7E9'>&nbsp;<b>Account Created</b></td>
				</tr>
		");

$r2s=10;

$nav_query=0;
$query="select ";
$query.="oid ";
$query.="from ";
$query.="hosting_order ";
$query.="where ";
$query.="status='1' ";
$query.="order by ";
$query.="domain_name asc ";

$row=mysql_query($query);
while ($rs=mysql_fetch_row($row))
	{
	$query_ct="select ";
	$query_ct.="count(*) ";
	$query_ct.="from ";
	$query_ct.="invoice ";
	$query_ct.="where ";
	$query_ct.="oid='".addslashes(trim($rs[0]))."' ";
	$query_ct.="and ";
	$query_ct.="invoice_type='0'";
	$ct=mysql_fetch_row(mysql_query($query_ct));

	if ($ct[0]<1) { $nav_query+=1; }
	}

$query="select ";
$query.="oid, ";
$query.="domain_name, ";
$query.="ogcreate ";
$query.="from ";
$query.="hosting_order ";
$query.="where ";
$query.="status='1' ";
$query.="order by ";
$query.="domain_name asc ";

if (!isset($all)) { $query.="limit ".addslashes(trim($pg)).", ".addslashes(trim($r2s)).""; }

$y=1;
$x=0;
$row=mysql_query($query);
while ($rs=mysql_fetch_row($row))
	{
	$query_ct="select ";
	$query_ct.="count(*) ";
	$query_ct.="from ";
	$query_ct.="invoice ";
	$query_ct.="where ";
	$query_ct.="oid='".addslashes(trim($rs[0]))."' ";
	$query_ct.="and ";
	$query_ct.="invoice_type='0'";

	$ct=mysql_fetch_row(mysql_query($query_ct));

	if ($ct[0]<1)
		{
		if (is_valid_date($rs[2])==0) { $ogcreate="<font color='#990000'>Invalid Date</font>"; $disable=true; }
		else { $ogcreate=date("m/d/Y", $rs[2]); }

		if ($y==1) {$bgcolor="#F2F4F6"; $y=0;} else {$bgcolor="#ECEEF0"; $y=1;}
		$buffer.=("
				<tr>
					<td width='15%' align='center' valign='middle' bgcolor='".$bgcolor."'><input type='checkbox' name='oid[]' value='".$rs[0]."' id='oid".$rs[0]."' ".((isset($disable))?" disabled":"")." checked></td>
					<td width='60%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;<label for='oid".$rs[0]."'>".stripslashes(trim($rs[1]))."</label></td>
					<td width='25%' align='left' valign='top' bgcolor='".$bgcolor."'>&nbsp;".$ogcreate."</td>
				</tr>
			");
		if (isset($disable)) { unset($disable); }
		$x+=1;
		}
	}
$buffer.=("
		
				<tr>
					<td colspan='1' align='center' valign='middle'></td>
					<td colspan='2' align='left' valign='top'><input ".$button_style." type='submit' name='submit' value='Create Invoices'></td>
				</tr>
	</table>
	");

if ($x==0)
	{
	echo("
			<table width='100%' cellpadding='4' cellspacing='1' border='0'>
				<tr>
					<td><font color='#990000'>No qualifying accounts found, all have invoices.</font></td>
				</tr>
			</table>
		");
	}
else 
	{ 
	$on_page="#990000";
	echo generate_invoice_nav($r2s, $nav_query, $on_page, $pg, $sid, $all, "past_invoice_creation", 0, $http_admin);

	echo $buffer; 
	}

echo("
			</td>
		</tr>
	</table>
	</form>
	");

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