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


$query0="select ";
$query0.="logo_path, ";
$query0.="company_contact, ";
$query0.="payable_to ";
$query0.="from ";
$query0.="invoice_config ";
$query0.="limit 0, 1";

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

$logo_path=stripslashes(trim($rs0[0]));
$company_contact=stripslashes(trim($rs0[1]));
$payable_to=stripslashes(trim($rs0[2]));

$query1="select ";
$query1.="company_contact, ";	// 0
$query1.="created, ";			// 1
$query1.="invoice_number, ";	// 2
$query1.="due_date, ";			// 3
$query1.="invoice_terms, ";		// 4
$query1.="payable_to, ";		// 5
$query1.="uid, ";				// 6
$query1.="payment_method, ";	// 7
$query1.="invoice_details, ";	// 8
$query1.="additional_information, ";	// 9
$query1.="status ";				// 10
$query1.="from ";
$query1.="invoice ";
$query1.="where ";
$query1.="iid='".addslashes(trim($iid))."'";

$rs1=mysql_fetch_row(mysql_query($query1));

$company_contact=stripslashes(trim($rs1[0]));
$created=stripslashes(trim($rs1[1]));
$invoice_number=stripslashes(trim($rs1[2]));
$due_date=stripslashes(trim($rs1[3]));
$invoice_terms=stripslashes(trim($rs1[4]));
$payable_to=stripslashes(trim($rs1[5]));
$uid=stripslashes(trim($rs1[6]));
$payment_method=stripslashes(trim($rs1[7]));
$invoice_details=stripslashes(trim($rs1[8]));
$additional_information=stripslashes(trim($rs1[9]));
$status=stripslashes(trim($rs1[10]));

$query2="select ";
$query2.="first_name, ";		// 0
$query2.="last_name,  ";		// 1
$query2.="street_address_1,  ";	// 2
$query2.="city,  ";				// 3
$query2.="state,  ";			// 4
$query2.="zip_code,  ";			// 5
$query2.="country,  ";			// 6
$query2.="phone,  ";			// 7
$query2.="fax,  ";				// 8
$query2.="email,  ";			// 9
$query2.="street_address_2 ";	// 10
$query2.="from ";
$query2.="user ";
$query2.="where ";
$query2.="uid='".addslashes(trim($uid))."'";

$rs2=mysql_fetch_row(mysql_query($query2));

$first_name=stripslashes(trim($rs2[0]));
$last_name=stripslashes(trim($rs2[1]));
$address_1=stripslashes(trim($rs2[2]));
$city=stripslashes(trim($rs2[3]));
$state=stripslashes(trim($rs2[4]));
$zip_code=stripslashes(trim($rs2[5]));
$country=stripslashes(trim($rs2[6]));
$phone=stripslashes(trim($rs2[7]));
$fax=stripslashes(trim($rs2[8]));
$email=stripslashes(trim($rs2[9]));
$address_2=stripslashes(trim($rs2[10]));

$query3="select ";
$query3.="name ";		// 0
$query3.="from ";
$query3.="payment_process ";
$query3.="where ";
$query3.="pid='".addslashes(trim($payment_method))."'";

$rs3=mysql_fetch_row(mysql_query($query3));

$payment_method_name=stripslashes(trim($rs3[0]));

echo("
	<html>
	<head>
	<title>Invoice #".$invoice_number." for ".$first_name." ".$last_name."</title>
	<style>
	 td{ font-family:tahoma,Tahoma; font-size:8pt; } 
	</style>
	<style>
		 body, td, center, p {font-family:tahoma, arial, helvetica; font-size: 11px; color: #000000} div {font-family:tahoma, arial, helvetica; font-size: 11px} A:link { text-decoration: underline: none; none; color:#000000;} A:visited { text-decoration: underline: none; none; color:#000000;} A:hover { text-decoration: underline; font-weight: none;color:#990000;} .linkTable { PADDING-LEFT: 5px } 
	</style>
	</head>
	<body>
	<table width='85%' cellpadding='8' cellspacing='0' border='0' align='center'>
		<tr>
			<td align='left' valign='top'><img src='".$logo_path."'></td>
		</tr>
	</table>
	<table width='85%' cellpadding='4' cellspacing='0' border='1' align='center'>
		<tr>
			<td width='50%' align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'><b>Remit payment to:</b></td>
				</tr>
				<tr>
					<td align='left' valign='top'>&nbsp;</td>
				</tr>
				<tr>
					<td align='left' valign='top'>".nl2br($company_contact)."</td>
				</tr>
			</table>
			</td>
			<td width='50%' align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td align='left' valign='top'><b>Invoice Date:</b></td>
					<td align='left' valign='top'>".date("m/d/Y", $created)."</td>
				</tr>
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'><b>Invoice Number:</b></td>
					<td align='left' valign='top' bgcolor='#F7F3F7'>".$invoice_number."</td>
				</tr>
				<tr>
					<td align='left' valign='top'><b>Due Date:</b></td>
					<td align='left' valign='top'>".date("m/d/Y", $due_date)."</td>
				</tr>
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'><b>Terms:</b></td>
					<td align='left' valign='top' bgcolor='#F7F3F7'>".$invoice_terms."</td>
				</tr>
				<tr>
					<td align='left' valign='top'><b>Payable to:</b></td>
					<td align='left' valign='top'>".$payable_to."</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<table width='85%' cellpadding='8' cellspacing='0' border='0' align='center'>
		<tr>
			<td align='left' valign='top'><br></td>
		</tr>
	</table>
	<table width='85%' cellpadding='4' cellspacing='0' border='1' align='center'>
		<tr>
			<td width='50%' align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'><b>Invoiced to:</b></td>
				</tr>
				<tr>
					<td align='left' valign='top'>&nbsp;</td>
				</tr>
				<tr>
					<td align='left' valign='top'><font color='#990000'>".$first_name." ".$last_name."</font></td>
				</tr>
				<tr>
					<td align='left' valign='top'>".$address_1."</td>
				</tr>
		");
if (trim($address_2)!="")
	{
	echo("
			<tr>
				<td align='left' valign='top'>".$address_1."</td>
			</tr>
		");
	}
echo("
				<tr>
					<td align='left' valign='top'>".$city.", ".$state." ".$zip_code." ".$country."</td>
				</tr>
				<tr>
					<td align='left' valign='top'>Phone: ".$phone."</td>
				</tr>
		");
if (trim($fax)!="")
	{
	echo("
			<tr>
				<td align='left' valign='top'>Fax: ".$fax."</td>
			</tr>
		");
	}

if ($payment_method=="1") { $pay_image="paypal.gif"; }
else if ($payment_method=="3") { $pay_image="paysystems.gif"; }
else if ($payment_method=="6") { $pay_image="mailin.gif"; }
else if ($payment_method=="4") { $pay_image="2checkout.gif"; }
else if ($payment_method=="2") { $pay_image="worldpay.gif"; }
else if ($payment_method=="8") { $pay_image="authorize.gif"; }

echo("
				<tr>
					<td align='left' valign='top'>".$email."</td>
				</tr>
			</table>
			</td>
			<td width='50%' align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='1' border='0'>
				<tr>
					<td align='left' valign='top'><b>Click the icon below to pay this invoice:</b></td>
				</tr>
				<tr>
					<td align='left' valign='top'>&nbsp;</td>
				</tr>
				<tr>
					<td align='center' valign='middle'><br><br><a href='".$http_web."/pay_invoice.php?iid=".$iid."'><img src='".$http_images."/$pay_image' border='0'></a></td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	<table width='85%' cellpadding='8' cellspacing='0' border='0' align='center'>
		<tr>
			<td align='left' valign='top'><br></td>
		</tr>
	</table>
	<table width='85%' cellpadding='8' cellspacing='0' border='1' align='center'>
		<tr>
			<td colspan='5' align='left' valign='top' bgcolor='#F7F3F7'>&nbsp;<b>Invoice Details</b></td>
		</tr>
		<tr>
			<td align='left' valign='top'>&nbsp;<b>Service</b></td>
			<td align='left' valign='top'>&nbsp;<b>Qty.</b></td>
			<td align='left' valign='top'>&nbsp;<b>Rate</b></td>
			<td align='left' valign='top'>&nbsp;<b>Subtotal</b></td>
		</tr>
	");
$count=(substr_count($invoice_details, "|")-1);
$array=explode("|", $invoice_details);
for ($i=0; $i<=$count; $i++)
	{
	list($stored_srid, $stored_qty)=explode("@", $array[$i]);
	
	$query4="select ";
	$query4.="service_name, ";				// 0
	$query4.="service_description, ";		// 1
	$query4.="service_show_description, ";	// 2
	$query4.="setup_cost, ";				// 3
	$query4.="setup_activate, ";			// 4
	$query4.="service_cost, ";				// 5
	$query4.="service_state, ";				// 6
	$query4.="service_activate, ";			// 7
	$query4.="service_term ";				// 8
	$query4.="from ";
	$query4.="invoice_service ";
	$query4.="where ";
	$query4.="srid='".addslashes(trim($stored_srid))."'";

	$rs4=mysql_fetch_row(mysql_query($query4));
	
	$service_name=stripslashes(trim($rs4[0]));
	$service_description=stripslashes(trim($rs4[1]));
	$service_show_description=stripslashes(trim($rs4[2]));
	$setup_cost=stripslashes(trim($rs4[3]));
	$setup_activate=stripslashes(trim($rs4[4]));
	$service_cost=stripslashes(trim($rs4[5]));
	$service_state=stripslashes(trim($rs4[6]));
	$service_activate=stripslashes(trim($rs4[7]));
	$service_term=stripslashes(trim($rs4[8]));

	echo("
		<tr>
			<td align='left' valign='top'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='top'>&nbsp;".$service_name."</td>
				</tr>
		");
	if ($service_show_description==1)
		{
		echo("
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'>&nbsp;".$service_description."</td>
				</tr>
			");
		}
	echo("
			</table>
			</td>
			<td align='left' valign='top'>&nbsp;".$stored_qty."</td>
			<td align='left' valign='top'>&nbsp;
		");
	if ($service_activate==1)
		{
		if ($service_state==1) {$display="One Time Fee";}
		else if ($service_state==2) 
			{
			if ($service_term==1)
				{
				$display="every 1 Month";
				}
			else
				{
				$display="every ".$service_term." Months";
				}
			}
		echo $currency."".sprintf("%01.2f", $service_cost)."&nbsp;".$currency_type." / ".$display;
		if ($setup_activate==1)
			{
			echo "<br>&nbsp;+&nbsp;".$currency."".sprintf("%01.2f",$setup_cost)."&nbsp;".$currency_type."&nbsp;Setup Fee";
			}
		}
	else
		{
		if ($setup_activate==1)
			{
			echo $currency."".sprintf("%01.2f",$setup_cost)."&nbsp;".$currency_type."&nbsp;Setup Fee";
			}
		else
			{
			echo "None";
			$setup_cost=0;
			}
		}
	
	echo("
			</td>
			<td align='left' valign='top'>&nbsp;
		");
	$sub_total=(($stored_qty*$service_cost)+$setup_cost);
	$total+=$sub_total;
	echo $currency.sprintf("%01.2f", $sub_total)."&nbsp;".$currency_type;
	echo("
			</td>
		</tr>
		");
	
	unset($stored_srid);
	unset($stored_qty);
	}

echo("
		<tr>
			<td colspan='4'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td width='79%' align='left' valign='top'>
					<table width='100%' cellpadding='2' cellspacing='0' border='0'>
						<tr>
							<td align='left' valign='top'><b>Additional Information:</b></td>
						</tr>
						<tr>
							<td align='left' valign='top' bgcolor='#F7F3F7'>".$additional_information."</td>
						</tr>
					</table>
					</td>
					<td width='1%' align='right' valign='top'><img src='".$http_images."/space.gif' width='15' height='1'></td>
					<td width='20%' align='right' valign='top'>
					<table width='100%' cellpadding='4' cellspacing='0' border='0'>
						<tr>
							<td align='right' valign='top'><b>Subtotal:</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;".$currency."".$total."&nbsp;".$currency_type."</td>
						</tr>
						<tr>
							<td align='right' valign='top'><b>Payments:</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;".(($status==0)?"".$currency."0.00&nbsp;".$currency_type."":"")."</td>
						</tr>
						<tr>
							<td align='right' valign='top'><b>Total:</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;<font color='#990000'>".(($status==0)?"".$currency."".$total."&nbsp;".$currency_type."":"".$currency."0.00&nbsp;".$currency_type."")."</font></td>
						</tr>
						<tr>
							<td align='right' valign='top'><img src='".$http_images."/space.gif' width='150' height='1'></td>
							<td align='left' valign='top'><img src='".$http_images."/space.gif' width='125' height='1'></td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	</body>
	</html>
	");
mysql_close($dblink);
?>