<?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";

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

if (isset($send))
	{
	$headers="From: ".$email_admin;
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();
	
	$subject="Your Invoice is Ready";
	
	$query5="select ";
	$query5.="email, ";
	$query5.="first_name ";
	$query5.="from ";
	$query5.="user ";
	$query5.="where uid='".addslashes(trim($uid))."'";

	$rs5=mysql_fetch_row(mysql_query($query5));

	$client_email=stripslashes(trim($rs5[0]));
	$first_name=stripslashes(trim($rs5[1]));
	
	$message=("Greetings ".$first_name.",\nPlease visit the URL to view and pay this invoice.\n\n".$http_web."/invoice_hosting_payable.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $send))."\n\nThanks,\nWeb Support Team\n\nThis email was generated on: ".date("m/d/Y h:i:s a"));

	mail($client_email, $subject, $message, $headers);

	$invoice_sent=true;
	}

include $server_admin_inc."/header.php";

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='0' border='0'>
				<tr>
					<td align='left' valign='top'>
					<table width='100%' cellpadding='0' cellspacing='0' border='0'>
						<tr>
							<td width='40%' align='left' valign='middle'><img src='".$http_images."/menu_arrow.gif'><b>Paid Billable Service Invoices</b></td>
							<td width='45%' 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 Paid Billable Service Invoices Per Client</option>
		");
	$row0=mysql_query("select uid, first_name, last_name, zip_code, status from user order by first_name asc");
	while ($rs0=mysql_fetch_row($row0))
		{
		$ct=mysql_fetch_row(mysql_query("select count(*) from invoice where uid='".addslashes(trim($rs0[0]))."' and status='1' and invoice_type='1'"));
		if ($ct[0]>0&&$rs0[4]!=69)
			{
			echo"<option value='".$http_admin."/invoice_paid.php?sid=".$sid."&search=".$rs0[0]."&all=99'".((isset($search))?"".(($search==$rs0[0])?" selected":"")."":"").">".$rs0[1]." ".$rs0[2]." - ".$rs0[3]."</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 align='left' valign='top'><hr color='#F0EFED'></td>
				</tr>
	");
if (isset($invoice_sent))
	{
	echo("
				<tr>
					<td align='left' valign='top'><img src='".$http_images."/error_arrow.gif'><font color='#990000'><b>The invoice was sent successfully!</b></font></td>
				</tr>
				<tr>
					<td 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='1' ";
$nav_query.="and ";
$nav_query.="invoice_type='1'";
if (isset($search))
	{
	$nav_query.="and ";
	$nav_query.="uid='".addslashes(trim($search))."'";
	}
$on_page="#990000";
echo generate_invoice_nav($r2s, $nav_query, $on_page, $pg, $sid, $all, "invoice_paid", 0, $http_admin);

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."/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'>Resend 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='100%' border='0' cellspacing='1' cellpadding='2'>
				<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>Client 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 Terms</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.="uid, ";			// 6
			$query.="invoice_terms, ";	// 7
			$query.="total_due_today, ";	// 8
			$query.="charge_tax ";			// 9
			$query.="from invoice ";
			$query.="where status='1' ";
			$query.="and ";
			$query.="invoice_type='1' ";
			if (isset($search))
				{
				$query.="and ";
				$query.="uid='".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]));
				$uid=stripslashes(trim($rs[6]));
				$invoice_terms=stripslashes(trim($rs[7]));
				$total_due_today=stripslashes(trim($rs[8]));
				$charge_tax=stripslashes(trim($rs[9]));
				
				$total_due_today=$currency.sprintf("%01.2f", ($total_due_today+($total_due_today*$charge_tax)))." ".$currency_type;

				$invoice_terms_displayed=translate_invoice_terms($invoice_terms);

				$rs1=mysql_fetch_row(mysql_query("select first_name, last_name, email from user where uid='".$uid."' limit 0, 1"));

				$client_name="<a href='mailto:".stripslashes(trim($rs1[2]))."'>";
				$client_name.=stripslashes(trim($rs1[0]))." ".stripslashes(trim($rs1[1]))."</a>";
		
				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."/invoice_create.php?sid=".$sid."&iid=".$iid."&p=1&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_service.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_pending.php?sid=".$sid."&uid=".$uid."&send=".$iid."&pg=".$pg."'><img alt='Click to resend 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;".$client_name."</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;".$invoice_terms_displayed."</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."'>&nbsp;".$total_due_today."</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 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("
				</form>
				</td>
			</tr>
		</table>
		");

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