<?PHP
include "inc/var.php";
include "inc/connect.php";
include $server_inc."/pagefind.php";
include $server_inc."/languages/".$local_lang.".php";
include $server_inc."/languages/".$local_lang."/".file_name().".php";
include $server_inc."/whm_functions.php";
include $server_inc."/invoice_functions.php";

# Decode ID
# ---------------------------------------------------------------------------------
if (!isset($c)) { die("invalid invoice"); }
else { $iid=urldecode(d("576cb7f68040520768bf51c75f7f4c84", $c)); }
# ---------------------------------------------------------------------------------
# END: Decode ID

# Pull out company variable
# ---------------------------------------------------------------------------------
$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]));
# ---------------------------------------------------------------------------------
# END: Pull out company variable

# Pull out invoice details
# ---------------------------------------------------------------------------------
$query1="select ";
$query1.="iid, ";				// 0
$query1.="payment_method, ";	// 1
$query1.="status, ";			// 2
$query1.="created, ";			// 3
$query1.="date_paid, ";			// 4
$query1.="invoice_number, ";	// 5
$query1.="total_due_today, ";	// 6
$query1.="uid, ";				// 7
$query1.="invoice_details, ";	// 8
$query1.="invoice_terms, ";		// 9
$query1.="additional_information, ";// 10
$query1.="charge_tax, ";		// 11
$query1.="tax_type ";			// 12
$query1.="from ";
$query1.="invoice ";
$query1.="where ";
$query1.="iid='".addslashes(trim($iid))."' ";

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

$iid=stripslashes(trim($rs1[0]));
$payment_method=stripslashes(trim($rs1[1]));
$status=stripslashes(trim($rs1[2]));
$created=stripslashes(trim($rs1[3]));
$date_paid=stripslashes(trim($rs1[4]));
$invoice_number=stripslashes(trim($rs1[5]));
$total_due_today=stripslashes(trim($rs1[6]));
$uid=stripslashes(trim($rs1[7]));
$invoice_details=stripslashes(trim($rs1[8]));
$invoice_terms=stripslashes(trim($rs1[9]));
$additional_information=stripslashes(trim($rs1[10]));
$charge_tax=stripslashes(trim($rs1[11]));
$tax_type=stripslashes(trim($rs1[12]));
# $sub_total=sprintf("%01.2f", $total_due_today);
# ---------------------------------------------------------------------------------
# END: Pull out invoice details

# Client details
# ---------------------------------------------------------------------------------
$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]));
# ---------------------------------------------------------------------------------
# END: Client details

# Processor name
# ---------------------------------------------------------------------------------
$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]));
# ---------------------------------------------------------------------------------
# END: Processor name

echo("
	<html>
	<head>
	<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>
	");
if (trim($logo_path)!="")
		{
		echo("
			<table width='85%' cellpadding='8' cellspacing='0' border='0' align='center'>
				<tr>
					<td align='left' valign='middle'><img src='".$logo_path."'></td>
				</tr>
			</table>
			");
		}
echo("
	<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>".$text_remitpayment.":</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>".$text_invoicedate.":</b></td>
					<td align='left' valign='top'>".date("m/d/Y", $created)."</td>
				</tr>
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'><b>".$text_invoicenumber.":</b></td>
					<td align='left' valign='top' bgcolor='#F7F3F7'>".$invoice_number."</td>
				</tr>
				<tr>
					<td align='left' valign='top'><b>".$text_invoicedue.":</b></td>
					<td align='left' valign='top'>
		"); 
	$m=date("m", $created);
	$d=date("d", $created);
	$y=date("Y", $created);
	echo date("m/d/Y", mktime(0, 0, 0, $m, ($d+$invoice_terms), $y))." ";
	echo "[ ".str_replace(" Invoice", "s", translate_invoice_terms($invoice_terms))." ]";
	echo("
					</td>
				</tr>
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'><b>".$text_terms.":</b></td>
					<td align='left' valign='top' bgcolor='#F7F3F7'>".$text_autobilling."</td>
				</tr>
				<tr>
					<td align='left' valign='top'><b>".$text_payableto.":</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>".$text_invoicedto.":</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'>".$text_phone.": ".$phone."</td>
				</tr>
		");
if (trim($fax)!="")
	{
	echo("
			<tr>
				<td align='left' valign='top'>".$text_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'><font color='#COCOCO'><b>".$text_thisinvoice.(($status==1)?" ".$text_was." ":" ".$text_willbe." ").$text_paidusing.":</b></font></td>
				</tr>
				<tr>
					<td align='left' valign='top'>&nbsp;</td>
				</tr>
				<tr>
					<td align='center' valign='middle'><br><br><img src='".$http_images."/$pay_image' border='0' alt='".$text_altpaymentmethod.".'></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>".$text_invoicedetails."</b></td>
		</tr>
		<tr>
			<td width='10%' align='left' valign='top'>&nbsp;<b>".$text_quantity."</b></td>
			<td width='60%' align='left' valign='top'>&nbsp;<b>".$text_service."</b></td>
			<td width='15%' align='left' valign='top'>&nbsp;<b>".$text_cost."</b></td>
			<td width='15%' align='left' valign='top'>&nbsp;<b>".$text_subtotal."</b></td>
		</tr>
	");

$sub_total=0;
$ct=(substr_count($invoice_details, "|")-1);
$arr=explode("|", $invoice_details);
for ($i=0; $i<=$ct; $i++)
	{
	list($srid, $qty, $service_name, $service_description, $service_show_description, $service_cost)=explode("@", $arr[$i]);
	
	echo("
		<tr>
			<td align='center' valign='middle'>".$qty."</td>
			<td align='left' valign='middle'>
			<table width='100%' cellpadding='2' cellspacing='0' border='0'>
				<tr>
					<td align='left' valign='middle'>".$service_name."</td>
				</tr>
		");
	if ($service_show_description==1)
		{
		echo("
				<tr>
					<td align='left' valign='top' bgcolor='#F7F3F7'>".base64_decode($service_description)."</td>
				</tr>
			");
		}
	echo("
			</table>
			</td>
			<td align='left' valign='middle'>&nbsp;".$currency."".sprintf("%01.2f", $service_cost)." ".$currency_type."</td>
			<td align='left' valign='middle'>&nbsp;".$currency."".sprintf("%01.2f", ($service_cost*$qty))." ".$currency_type."</td>
		</tr>
		");
	$sub_total+=($service_cost*$qty);
	unset($srid);
	unset($qty);
	unset($service_name);
	unset($service_description);
	unset($service_show_description);
	unset($service_cost);
	}

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='4' cellspacing='0' border='0'>
						<tr>
							<td align='left' valign='top'><b>".$text_additionalinformation.":</b></td>
						</tr>
						<tr>
							<td align='left' valign='top' bgcolor='#F7F3F7'>".((trim($additional_information)=="")?$text_none:"".nl2br($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'>
		");
	if ($charge_tax>0)
		{
		$tax=$sub_total*$charge_tax;
		echo("
						<tr>
							<td align='right' valign='top'><b>".$text_total." ".$tax_type." ".$text_amount.":</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;<b>".$currency."".sprintf("%01.2f", $tax)."&nbsp;".$tax_type."&nbsp[".($charge_tax*100)."%]</b></td>
						</tr>
			");
		}
	echo("
						<tr>
							<td align='right' valign='top'><b>".$text_invoiceamount.":</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;<b>".$currency."".sprintf("%01.2f", ($sub_total+$tax))."&nbsp;".$currency_type."</b></td>
						</tr>
						<tr>
							<td align='right' valign='top'><b>".$text_invoicestatus.":</b></td>
							<td align='left' valign='top'>&nbsp;&nbsp;".invoice_status($status)."</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='100' height='1'></td>
						</tr>
					</table>
					</td>
				</tr>
			</table>
			</td>
		</tr>
	</table>
	</body>
	</html>
	");
mysql_close($dblink);
?>