<?PHP
function create_individual_invoice_history($oid, $below_public)
	{
	$buffer="Starting current run: [".date("m/d/Y h:i:s")."]\n\n";

	mysql_query("delete from invoice where invoice_type='0' and oid='".addslashes(trim($oid))."'");
	$buffer.="Deleted all hosting order invoices...\n";

	mysql_query("update hosting_order set next_due_date='' where invoice_type='0' and oid='".addslashes(trim($oid))."'");
	$buffer.="Reset hosting order next due date timestamps...\n\n";

	$query0="select ";
	$query0.="hosting_order.next_due_date, ";		// 0
	$query0.="hosting_order.domain_name, ";			// 1
	$query0.="hosting_order.payment_method, ";		// 2
	$query0.="hosting_order.payment_term, ";		// 3
	$query0.="hosting_order.total_due_reoccur, ";	// 4
	$query0.="user.first_name, ";					// 5
	$query0.="user.email, ";						// 6
	$query0.="hosting_order.oid, ";					// 7
	$query0.="hosting_order.total_due_today, ";		// 8
	$query0.="hosting_order.total_due_reoccur, ";	// 9
	$query0.="hosting_order.addon_choices, ";		// 10
	$query0.="hosting_order.pid, ";					// 11
	$query0.="hosting_order.ogcreate ";				// 12
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.status='1' ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by oid asc ";
	
	$rs0=mysql_fetch_row(mysql_query($query0));

	$ogcreate_hosting=stripslashes(trim($rs0[12]));
	$next_due_date=stripslashes(trim($rs0[0]));
	# if (trim($next_due_date)=="") { $next_due_date=$ogcreate_hosting; }
	$next_due_date=$ogcreate_hosting;
	$domain_name=stripslashes(trim($rs0[1]));
	$payment_method=stripslashes(trim($rs0[2]));
	$payment_term=stripslashes(trim(strtolower(str_replace("-", "_", $rs0[3]))));
	$total_due_reoccur=stripslashes(trim($rs0[4]));
	$first_name=stripslashes(trim($rs0[5]));
	$email=stripslashes(trim($rs0[6]));
	$oid=stripslashes(trim($rs0[7]));
	$total_due_today=stripslashes(trim($rs0[8]));
	$total_due_reoccur=stripslashes(trim($rs0[9]));
	$addon_choices=stripslashes(trim($rs0[10]));
	$processor_id=stripslashes(trim($rs0[11]));

	$buffer.=$first_name." ".$last_name." -> [".$domain_name."]\n";

	# -------------------------------------------------------------------------

	$query7="select ";
	$query7.="monthly_pid, ";			// 11
	$query7.="quarterly_pid, ";		// 12
	$query7.="semi_annual_pid, ";		// 13
	$query7.="annual_pid ";			// 14
	$query7.="from ";
	$query7.="plan_specs ";
	$query7.="where pid='".addslashes($processor_id)."'";

	$rs7=mysql_fetch_row(mysql_query($query7));

	$monthly_pid=stripslashes(trim($rs7[0]));
	$quarterly_pid=stripslashes(trim($rs7[1]));
	$semi_annual_pid=stripslashes(trim($rs7[2]));
	$annual_pid=stripslashes(trim($rs7[3]));

	# -------------------------------------------------------------------------

	if ($payment_term=="monthly") { $payment_term=1; $checkout_id=$monthly_pid; }
	else if ($payment_term=="quarterly") { $payment_term=3; $checkout_id=$quarterly_pid; }
	else if ($payment_term=="semi_annual") { $payment_term=6; $checkout_id=$semi_annual_pid; }
	else if ($payment_term=="annual") { $payment_term=12; $checkout_id=$annual_pid; }

	$buffer.="building hosting account variables...\n";

	# -------------------------------------------------------------------------

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

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

	$payment_processor_name=stripslashes(trim($rs2[0]));

	# -------------------------------------------------------------------------


	while ($next_due_date<time())
		{
		# shadow from previous invoice or create new master invoice?		
		$query2="select ";
		$query2.="iid, ";
		$query2.="invoice_number ";
		$query2.="from ";
		$query2.="invoice ";
		$query2.="where ";
		$query2.="invoice_type='0' ";
		$query2.="and ";
		$query2.="oid='".addslashes(trim($oid))."' ";
		$query2.="and ";
		$query2.="master='1'";
		
		$rs2=mysql_fetch_row(mysql_query($query2));

		$iid=stripslashes(trim($rs2[0]));
		if (trim($iid)=="") 
			{ 
			$buffer.="No prior invoices found...\n";
			unset($iid); 
			}
		else { $buffer.="Using invoice #".$rs2[0]." as a base for this invoice...\n"; }
		
		$invoice_number=grab_invoice_number();
		
		# -------------------------------------------------------------------------
		
		# integrity check
		# if (date("m/d/Y", $next_due_date)=="12/31/1969")
		if (is_valid_date($next_due_date)==0)
			{
			$headers="From: ".$email_admin;
			$headers.="\nReply-To: ".$email_admin;
			$headers.="\nX-Mailer: PHP/".phpversion();
			
			$subject="Invalid Next Due Date: ".$domain_name;
					
			$message=("Greetings ".$first_name.",\nPlease log into your AutoPilot and correct this date.\n\nThanks,\nWeb Support Team\n\nThis email was generated on: ".date("m/d/Y h:i:s a"));

			mail($email_admin, $subject, $message, $headers);
			$buffer.="Aborting for this client, the next due date is not correct == ".date("m/d/Y", $next_due_date)."\n";
			$buffer.="-------------------------------------------------------------------------\n\n";
			
			break;
			}
		else
			{
			if (isset($iid))
				{
				# email notice
				$m=date("m", $next_due_date);
				$d=date("d", $next_due_date);
				$y=date("Y", $next_due_date);
				$notice_days=sprintf("%01.0f", $invoice_notice_period/84600);
				$modified_due_date=mktime(0, 0, 0, $m, ($d-$notice_days), $y);
				}
			else
				{
				$modified_due_date=$ogcreate_hosting;
				$next_due_date=$ogcreate_hosting;
				}
# $g+=1;
	# 	if ($g==3) { $modified_due_date=100000000000000000000000; }
			# ---------------------------------------------------------------------------
			# echo date("m-d-Y", $modified_due_date)."<----Modified due date"; die;
			if ($modified_due_date>time()) { $modified_due_date=time; }
			if ($modified_due_date<=time())
				{
				if (isset($iid))
					{
					# pull data from last invoice		
					$query4="select ";	
					$query4.="total_due_reoccur, ";	// 0
					$query4.="extras, ";			// 1
					$query4.="total_due_today ";	// 2
					$query4.="from ";
					$query4.="invoice ";
					$query4.="where ";
					$query4.="invoice_type='0' ";
					$query4.="and ";
					$query4.="oid='".addslashes(trim($oid))."'";
					$query4.="and ";
					$query4.="master='1'";
					
					$rs4=mysql_fetch_row(mysql_query($query4));

					$total_due_reoccur=stripslashes(trim($rs4[0]));
					$extras=stripslashes($rs4[1]);
					$total_due_today=stripslashes(trim($rs4[2]));

					$extra_buffer="";
					$xct=(substr_count($extras, "\n")-1);
					$xarr=explode("\n", $extras);
					for ($i=0; $i<=$xct; $i++)
						{
						if (ereg("one time", $xarr[$i])!=true&&trim($xarr[$i])!="") { $extra_buffer.=$xarr[$i]."\n"; }
						}
					
					# reset the master
					$query5="update ";
					$query5.="invoice ";
					$query5.="set ";
					$query5.="master='0' ";
					$query5.="where ";
					$query5.="invoice_type='0' ";
					$query5.="and ";
					$query5.="oid='".addslashes(trim($oid))."'";
					$query5.="and ";
					$query5.="master='1'";

					mysql_query($query5);

					# create a new master
					$query6="insert ";
					$query6.="into ";
					$query6.="invoice ";
					$query6.="set ";
					$query6.="oid='".addslashes(trim($oid))."', ";
					$query6.="due_date='".addslashes(trim($next_due_date))."', ";
					$query6.="created='".addslashes(trim($next_due_date))."', ";
					$query6.="invoice_number='".addslashes(trim($invoice_number))."', ";
					if (trim($extra_buffer)!="") { $query6.="extras='".addslashes($extra_buffer)."', "; }
					$query6.="payment_method='".addslashes(trim($payment_method))."', ";
					$query6.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";
					$query6.="invoice_type='0', ";
					$query6.="master='1'";
					
					mysql_query($query6);
					$buffer.="created invoice #".$invoice_number." for ".date("m/d/Y",$next_due_date)."...\n";
					}
				else
					{
					$query3="insert ";
					$query3.="into ";
					$query3.="invoice ";
					$query3.="set ";
					$query3.="oid='".addslashes(trim($oid))."', ";
					
					# $query3.="due_date='".addslashes(trim($next_due_date))."', ";
					$query3.="due_date='".addslashes(trim($next_due_date))."', "; # changed 10/13/2003

					# $query3.="created='".time()."', ";
					$query3.="created='".addslashes(trim($next_due_date))."', "; # changed 10/13/2003

					$query3.="invoice_number='".addslashes(trim($invoice_number))."', ";
					if ($total_due_today>0)
						{
						# $total_due_today=$total_due_today-$total_due_reoccur;
						$query3.="total_due_today='".addslashes(trim($total_due_today))."', ";
						}
					$query3.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";
					$query3.="payment_method='".addslashes(trim($payment_method))."', ";
					
					$extras=build_hosting_extras($addon_choices);
					if ($extras!==0) { $query3.="extras='".addslashes(trim($extras))."', "; }

					$query3.="invoice_type='0', ";
					$query3.="master='1'";
					
					mysql_query($query3);
					$buffer.="inital invoice created successfully....\n";
					}
				

				# reset next due date
				# if ($next_due_date<time())
				if ($modified_due_date<=time())
					{
					
					$next_due_date=create_next_due($next_due_date, $payment_term);
					
					$query1="update ";
					$query1.="hosting_order ";
					$query1.="set ";
					$query1.="next_due_date='".addslashes(trim($next_due_date))."' ";
					$query1.="where ";
					$query1.="oid='".addslashes(trim($oid))."'";
					
					mysql_query($query1);
					$buffer.="updated the hosting order next due date....Completed!\n";
					$buffer.="-------------------------------------------------------------------------\n\n";
					}
				}
			}
		}

	$rand=date("mdYhisa");
	$buffer.="A log of this process was created here:\n".$below_public."/create_past_invoice[".$rand."].log \n\n";
	$buffer.="Ending current run: [".date("m/d/Y h:i:s")."]\n\n";

	$read=fopen($below_public."/create_past_invoice[".$rand."].log", "w");
	$result=fwrite($read, $buffer);
	fclose($read);

	chmod($below_public."/create_past_invoice[".$rand."].log", 0777);

	return $buffer;
	}

# db navigation functions for invoice
function generate_invoice_nav($r2s, $nav_query, $on_page, $pg, $sid, $all, $c, $oid, $http_admin)
	{
	if (ereg("#990000", $on_page)!=true&&$on_page!=0) { $var=$on_page; }

	if (ereg("select", $nav_query)==true)
		{
		$ctnav=mysql_fetch_row(mysql_query($nav_query));
		$total_pages=$ctnav[0];
		}
	else { $total_pages=$nav_query; }

	$moveF=$pg+$r2s;
	$moveB=$pg-$r2s;

	if ($total_pages<=0||$total_pages<=$r2s) { $nav=""; }
	else if ($all==99) { $nav="<a href='".$http_admin."/".$c.".php?pg=".$pg."&sid=".$sid."".((trim($oid)!="")?"&oid=".$oid."":"")."'><b>&lt&lt Go back<b></a>"; }
	else
		{
		$nav="<table width='100%' cellpadding='2' cellspacing='0' border='0'>\n<tr>\n<td align='left' valign='middle'>";

		$pg_ct=ceil($total_pages/$r2s);
		
		$start="<a href='".$PHP_SELF."?pg=0&sid=".$sid."".((trim($oid)!="")?"&oid=".$oid."":"")."'>&lt&lt</a>&nbsp;&nbsp;";			
		
		if ($pg==0) { $nav.=""; } else { $nav.=$start; }
		
		$x=1;
		$br=1;
		for ($i=0; $i<=($pg_ct-1); $i++)
			{
			if ($i==0)
				{
				$nav.="[<a href='".$PHP_SELF."?pg=0&sid=".$sid."".((trim($oid)!="")?"&oid=".$oid."":"")."'>".(($pg==0)?"<font color='#C60C08'><b>".$x."</b></font>":"".$x."")."</a>]&nbsp;";
				$x++;
				}
			else 
				{
				$nav.="[<a href='".$PHP_SELF."?pg=".($i*$r2s)."&sid=".$sid."".((trim($oid)!="")?"&oid=".$oid."":"")."'>".((($pg/2)==(($i*$r2s)/2))?"<font color='#C60C08'><b>".$x."</b></font>":"".$x."")."</a>]&nbsp;";
				$x++;
				}
			if ($br==25&&$br<$total_pages) { $nav.="<br>"; $br=1; } else { $br++; }
			}
		if ($pg_ct==$i) { $xp=(($i*$r2s)-$r2s); }
		else  { $xp=$i*$r2s; }
		$end="&nbsp;&nbsp;<a href='".$PHP_SELF."?pg=".$xp."&sid=".$sid."".((trim($oid)!="")?"&oid=".$oid."":"")."'>&gt&gt</a>";
		
		if ($pg!=(($i-1)*$r2s)) { $nav.=$end; }

		$nav.=("
					</td>
					<td align='right' valign='middle'>
			");
		if (isset($var)) {$nav.="[<a href='".$http_admin."/license.php?sid=".$sid."&pg=0".((trim($oid)!="")?"&oid=".$oid."":"")."'>Go Back</a>]&nbsp;";}
		$nav.=("[<a href='".$PHP_SELF."?all=99&sid=".$sid."&pg=".$pg."".((trim($oid)!="")?"&oid=".$oid."":"")."'>Show all</a>]</td>
				</tr>
			</table>
			");
		}
	return $nav;
	}

function build_hosting_extras($addon_choices)
	{
	GLOBAL $reg_period; 

	$addon_list="";
	$coupon_list="";
	$ct=(substr_count($addon_choices, "|")-1);
	$array=explode("|", $addon_choices);

	for ($i=0; $i<=$ct; $i++)
		{
		$query0="select ";
		$query0.="addon_name, ";				// 0
		$query0.="setup_cost, ";				// 1
		$query0.="setup_activate, ";			// 2
		$query0.="addon_cost, ";				// 3
		$query0.="addon_state, ";				// 4
		$query0.="addon_activate, ";			// 5
		$query0.="coupon, ";					// 6
		$query0.="coupon_terms, ";				// 7
		$query0.="coupon_code, ";				// 8
		$query0.="coupon_discount_type, ";		// 9
		$query0.="coupon_discount_percent, ";	// 10
		$query0.="coupon_discount_whole ";		// 11
		$query0.="from addon_specs ";
		$query0.="where aid='".addslashes(trim($array[$i]))."'";

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

		$addon_name=stripslashes(trim($rs0[0])); 
		$setup_cost=stripslashes(trim($rs0[1])); 
		$setup_activate=stripslashes(trim($rs0[2]));
		$addon_cost=stripslashes(trim($rs0[3]));
		$addon_state=stripslashes(trim($rs0[4]));
		$addon_activate=stripslashes(trim($rs0[5]));
		$coupon=stripslashes(trim($rs0[6]));
		$coupon_terms=stripslashes(trim($rs0[7]));
		$coupon_code=stripslashes(trim($rs0[8]));
		$coupon_discount_type=stripslashes(trim($rs0[9]));
		$coupon_discount_percent=stripslashes(trim($rs0[10]));
		$coupon_discount_whole=stripslashes(trim($rs0[11]));
		
		if ($coupon==1)
			{
			$coupon_list.="Coupon Code: ".$coupon_code." ";
			
			# 1. figure out one time or monthly
			if ($coupon_terms==1) # one time
				{
				if ($coupon_discount_type==1) # %
					{
					# - %
					$coupon_list.="one time discount of ".$coupon_discount_percent."% off hosting fees\n";
					}
				else if ($coupon_discount_type==2) # whole dollar amount
					{
					# - WHOLE DOLLAR
					$coupon_list.="one time discount of ".($currency.sprintf("%01.2f", $coupon_discount_whole)." ".$currency_type)." off hosting fees\n";
					}
				}
			else if ($coupon_terms==2&&strcmp("2checkout", strtolower($payment_type_name))!=0) # monthly
				{
				if ($coupon_discount_type==1) # %
					{
					# - %
					$coupon_list.="recurring discount of ".$coupon_discount_percent."%\n";
					}
				else if ($coupon_discount_type==2) # whole dollar amount
					{ 
					# - WHOLE DOLLAR 
					$coupon_list.="recurring discount of ".$currency.sprintf("%01.2f", $coupon_discount_whole)." ".$currency_type."\n";
					}
				}
			}

	if (trim($addon_name)!=""&&$coupon!=1)
			{
			$addon_list.=$addon_name." ";
			if ($addon_activate==1)
				{
				$addon_list.=$currency.sprintf("%01.2f", $addon_cost)." ".$currency_type." ";

				if ($addon_state==1) { $addon_list.="one time fee\n"; }
				else if ($addon_state==2) { $addon_list.="monthly recurring\n"; }
				else if ($addon_state==3) { $addon_list.="annually recurring\n"; }
				}
			else { $addon_list.="No Cost \n"; }

			if ($set_activate==1) 
				{ 
				$addon_list=trim($addon_list);
				$addon_list.="+ ".$currency.sprintf("%01.2f", $setup_cost)." ".$currency_type." setup fee\n"; 
				}
			}

		unset($addon_name);
		unset($setup_cost);
		unset($setup_activate);
		unset($addon_cost);
		unset($addon_state);
		unset($addon_activate);
		unset($coupon);
		unset($coupon_terms);
		unset($coupon_code);
		unset($coupon_discount_type);
		unset($coupon_discount_percent);
		unset($coupon_discount_whole);
		}
	
	$extras="";
	if (trim($addon_list)!="") { $extras.=$addon_list."\n"; }
	if (trim($coupon_list)!="") { $extras.=$coupon_list; }

	$extras.="\n".$reg_period;
	
	if (trim($extras)=="") { return 0; }
	else { return $extras; }
	}

function update_invoice_internal($oid)
	{
	$query0="select ";
	$query0.="hosting_order.next_due_date, ";		// 0
	$query0.="hosting_order.domain_name, ";			// 1
	$query0.="hosting_order.payment_method, ";		// 2
	$query0.="hosting_order.payment_term, ";		// 3
	$query0.="hosting_order.total_due_reoccur, ";	// 4
	$query0.="user.first_name, ";					// 5
	$query0.="user.email, ";						// 6
	$query0.="hosting_order.total_due_today, ";		// 7
	$query0.="hosting_order.total_due_reoccur, ";	// 8
	$query0.="hosting_order.addon_choices, ";		// 9
	$query0.="hosting_order.pid ";					// 10
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

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

	$next_due_date=stripslashes(trim($rs0[0]));
	$domain_name=stripslashes(trim($rs0[1]));
	$payment_method=stripslashes(trim($rs0[2]));
	$payment_term=stripslashes(trim(strtolower(str_replace("-", "_", $rs0[3]))));
	$total_due_reoccur=stripslashes(trim($rs0[4]));
	$first_name=stripslashes(trim($rs0[5]));
	$email=stripslashes(trim($rs0[6]));
	$total_due_today=stripslashes(trim($rs0[7]));
	$total_due_reoccur=stripslashes(trim($rs0[8]));
	$addon_choices=stripslashes(trim($rs0[9]));
	$processor_id=stripslashes(trim($rs0[10]));

	# ----------------------------------------------------------------------------

	$query7="select ";
	$query7.="monthly_pid, ";		// 0
	$query7.="quarterly_pid, ";		// 1
	$query7.="semi_annual_pid, ";	// 2
	$query7.="annual_pid ";			// 3
	$query7.="from ";
	$query7.="plan_specs ";
	$query7.="where pid='".addslashes($processor_id)."'";

	$rs7=mysql_fetch_row(mysql_query($query7));

	$monthly_pid=stripslashes(trim($rs7[0]));
	$quarterly_pid=stripslashes(trim($rs7[1]));
	$semi_annual_pid=stripslashes(trim($rs7[2]));
	$annual_pid=stripslashes(trim($rs7[3]));

	# ----------------------------------------------------------------------------

	if ($payment_term=="monthly") { $payment_term=1; $checkout_id=$monthly_pid; }
	else if ($payment_term=="quarterly") { $payment_term=3; $checkout_id=$quarterly_pid; }
	else if ($payment_term=="semi_annual") { $payment_term=6; $checkout_id=$semi_annual_pid; }
	else if ($payment_term=="annual") { $payment_term=12; $checkout_id=$annual_pid; }

	# ----------------------------------------------------------------------------

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

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

	$payment_processor_name=stripslashes(trim($rs2[0]));

	# ----------------------------------------------------------------------------

	$query3="select ";
	$query3.="count(*) ";
	$query3.="from ";
	$query3.="invoice ";
	$query3.="where ";
	$query3.="oid='".addslashes(trim($oid))."' ";
	$query3.="and ";
	$query3.="master='1'";

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

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

	# ----------------------------------------------------------------------------

	$query4="select ";
	$query4.="count(*) ";
	$query4.="from ";
	$query4.="invoice ";
	$query4.="where ";
	$query4.="oid='".addslashes(trim($oid))."' ";

	$rs4=mysql_fetch_row(mysql_query($query4));

	$existing_count=stripslashes(trim($rs4[0]));

	# ----------------------------------------------------------------------------
				
	# create new master
	$query3="update ";
	$query3.="invoice ";
	$query3.="set ";

	
	$query4="select ";
	$query4.="count(*) ";
	$query4.="from ";
	$query4.="invoice ";
	$query4.="where ";
	$query4.="oid='".addslashes(trim($oid))."' ";
	
	$rs4=mysql_fetch_row(mysql_query($query4));

	$is_inital_invoice=$rs4[0];
	
	if ($is_inital_invoice>1) { $query3.="due_date='".addslashes(trim($next_due_date))."', "; }
	
	$query3.="created='".time()."', ";

	if ($existing_count<=1) { $go=true; }
	if ($existing_master_count<1) { $go=true; }
	if (isset($go))
		{
		#if ($total_due_today>$total_due_reoccur) { $total_due_today=$total_due_today-$total_due_reoccur; }
		#else if ($total_due_today<$total_due_reoccur) { $total_due_today=$total_due_reoccur-$total_due_today; }
		$query3.="total_due_today='".addslashes(trim($total_due_today))."', ";
		}

	$query3.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";

	$extras=build_hosting_extras($addon_choices);
	if ($extras!==0) { $query3.="extras='".addslashes(trim($extras))."', "; }

	$query3.="payment_method='".addslashes(trim($payment_method))."' ";
	$query3.="where invoice_type='0' and oid='".addslashes(trim($oid))."' and master='1'";


	mysql_query($query3);
	}


function create_new_invoice_shell($oid)
	{
	$query0="select ";
	$query0.="hosting_order.domain_name, ";			// 0
	$query0.="user.first_name, ";					// 1
	$query0.="user.email, ";						// 2
	$query0.="hosting_order.next_due_date ";		// 3
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

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

	$domain_name=stripslashes(trim($rs0[0]));
	$first_name=stripslashes(trim($rs0[1]));
	$email=stripslashes(trim($rs0[2]));
	$next_due_date=stripslashes(trim($rs0[3]));
	$invoice_number=grab_invoice_number();

	# create new master
	$query1="insert ";
	$query1.="into ";
	$query1.="invoice ";
	$query1.="set ";
	$query1.="oid='".addslashes(trim($oid))."', ";
	$query1.="created='".time()."', ";
	$query1.="invoice_number='".addslashes(trim($invoice_number))."', ";
	$query1.="due_date='".addslashes(trim($next_due_date))."', ";
	$query1.="invoice_type='0', ";
	$query1.="master='1'";

	mysql_query($query1);
	}

function create_new_invoice($oid, $status)
	{
	$query0="select ";
	$query0.="hosting_order.next_due_date, ";		// 0
	$query0.="hosting_order.domain_name, ";			// 1
	$query0.="hosting_order.payment_method, ";		// 2
	$query0.="hosting_order.payment_term, ";		// 3
	$query0.="hosting_order.total_due_reoccur, ";	// 4
	$query0.="user.first_name, ";					// 5
	$query0.="user.email, ";						// 6
	$query0.="hosting_order.total_due_today, ";		// 7
	$query0.="hosting_order.total_due_reoccur, ";	// 8
	$query0.="hosting_order.addon_choices, ";		// 9
	$query0.="hosting_order.pid, ";					// 10
	$query0.="hosting_order.txn_id, ";				// 11
	$query0.="hosting_order.domain_registration, ";	// 12
	$query0.="hosting_order.tld_id, ";				// 13
	$query0.="hosting_order.status ";				// 14
	$query0.="from ";
	$query0.="hosting_order, user ";
	$query0.="where ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="hosting_order.oid='".addslashes(trim($oid))."' ";
	$query0.="order by hosting_order.oid asc ";

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

	$next_due_date=stripslashes(trim($rs0[0]));
	$domain_name=stripslashes(trim($rs0[1]));
	$payment_method=stripslashes(trim($rs0[2]));
	$payment_term=stripslashes(trim(strtolower(str_replace("-", "_", $rs0[3]))));
	# $total_due_reoccur=stripslashes(trim($rs0[4]));
	$first_name=stripslashes(trim($rs0[5]));
	$email=stripslashes(trim($rs0[6]));
	$total_due_today=stripslashes(trim($rs0[7]));
	$total_due_reoccur=stripslashes(trim($rs0[8]));
	$addon_choices=stripslashes(trim($rs0[9]));
	$processor_id=stripslashes(trim($rs0[10]));
	$txn_id=stripslashes(trim($rs0[11]));
	$domain_registration=stripslashes(trim($rs0[12]));
	$tld_id=stripslashes(trim($rs0[13]));
	$status=stripslashes(trim($rs0[14]));

	# ----------------------------------------------------------------------------
	if ($domain_registration==1)
		{
		$query="select ";
		$query.="reg_period, ";
		$query.="tld ";
		$query.="from ";
		$query.="tld_chart ";
		$query.="where ";
		$query.="tld_id='".addslashes(trim($tld_id))."'";

		$rs=mysql_fetch_row(mysql_query($query));

		$reg_period=$rs[0];
		$tld=$rs[1];

		if ($reg_period==1) { $reg_period="Domain registration for 1 year. [.".$tld."]\n";	}
		else if ($reg_period==2) { $reg_period="Domain registration for 2 years. [.".$tld."]\n";	}
		else if ($reg_period==3) { $reg_period="Domain registration for 5 years. [.".$tld."]\n";	}
		else if ($reg_period==4) { $reg_period="Domain registration for 10 years. [.".$tld."]\n";	}
		}

	# ----------------------------------------------------------------------------

	$query7="select ";
	$query7.="monthly_pid, ";		// 0
	$query7.="quarterly_pid, ";		// 1
	$query7.="semi_annual_pid, ";	// 2
	$query7.="annual_pid ";			// 3
	$query7.="from ";
	$query7.="plan_specs ";
	$query7.="where pid='".addslashes($processor_id)."'";

	$rs7=mysql_fetch_row(mysql_query($query7));

	$monthly_pid=stripslashes(trim($rs7[0]));
	$quarterly_pid=stripslashes(trim($rs7[1]));
	$semi_annual_pid=stripslashes(trim($rs7[2]));
	$annual_pid=stripslashes(trim($rs7[3]));

	# ----------------------------------------------------------------------------

	if ($payment_term=="monthly") { $payment_term=1; $checkout_id=$monthly_pid; }
	else if ($payment_term=="quarterly") { $payment_term=3; $checkout_id=$quarterly_pid; }
	else if ($payment_term=="semi_annual") { $payment_term=6; $checkout_id=$semi_annual_pid; }
	else if ($payment_term=="annual") { $payment_term=12; $checkout_id=$annual_pid; }

	# ----------------------------------------------------------------------------

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

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

	$payment_processor_name=stripslashes(trim($rs2[0]));

	# ----------------------------------------------------------------------------
				
	# create new master
	$query3="insert into ";
	$query3.="invoice ";
	$query3.="set ";
	$query3.="created='".time()."', ";
	# if ($total_due_today>$total_due_reoccur) { $total_due_today=$total_due_today-$total_due_reoccur; }
	# else if ($total_due_today<$total_due_reoccur) { $total_due_today=$total_due_reoccur-$total_due_today; }
	$query3.="total_due_today='".addslashes(trim($total_due_today))."', ";
	$query3.="total_due_reoccur='".addslashes(trim($total_due_reoccur))."', ";
	$query3.="due_date='".addslashes(trim($next_due_date))."', ";
	$query3.="txn_id='".addslashes(trim($txn_id))."', ";
	$extras=build_hosting_extras($addon_choices);
	if ($domain_registration==1)
		{
		# $query3.="extras='".addslashes(trim($reg_period))."', ";
		$extras.=$reg_period;
		}
	if ($extras!==0) { $query3.="extras='".addslashes(trim($extras))."', "; }

	$query3.="payment_method='".addslashes(trim($payment_method))."', ";
	$invoice_number=grab_invoice_number();
	$query3.="invoice_number='".$invoice_number."', ";
	$query3.="oid='".addslashes(trim($oid))."', ";
	$query3.="master='1', ";
	if ($status==1) { $query3.="date_paid='".time()."', "; }
	$query3.="status='".$status."'";
	
	# echo $query3; die;
	$q=mysql_query($query3);
	# if (!$q) { echo mysql_error(); } else { echo "was good"; } die;
	return $invoice_number;
	}

function invoice_due_in_x_days($email_admin, $first_name, $domain_name, $total_due_reoccur, $payment_processor_name, $client_email, $days_until_due, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='39' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$days_until_due=$days_until_due." day".(($days_until_due==1)?"":"s")."";

	$subject=str_replace("{{days_until_due}}", $days_until_due, $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{domain_name}}", $domain_name, $subject);
	$subject=str_replace("{{total_recuur}}", $total_due_reoccur, $subject);
	$subject=str_replace("{{payment_processor}}", $payment_processor_name, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{days_until_due}}", $days_until_due, $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{domain_name}}", $domain_name, $message);
	$message=str_replace("{{total_recuur}}", $total_due_reoccur, $message);
	$message=str_replace("{{payment_processor}}", $payment_processor_name, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

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

	if ($which==1)
		{
		$message.="\n\n-----------------------------------------------------\nThis is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}
	}

function daily_invoice_summary_processor_details($pid)
	{
	GLOBAL  $currency, $currency_type;

	$query0="select ";
	$query0.="invoice.invoice_number, ";	// 0
	$query0.="invoice.total_due_today, ";	// 1
	$query0.="invoice.total_due_reoccur, ";	// 2
	$query0.="invoice.due_date, ";			// 3
	$query0.="invoice.status, ";			// 4
	$query0.="user.first_name, ";			// 5
	$query0.="user.last_name, ";			// 6
	$query0.="hosting_order.domain_name, ";	// 7
	$query0.="payment_process.name ";		// 8
	$query0.="from ";
	$query0.="invoice, user, hosting_order, payment_process ";
	$query0.="where ";
	$query0.="invoice.payment_method='".addslashes(trim($pid))."' ";
	$query0.="and ";
	$query0.="invoice.invoice_type='0' ";
	$query0.="and ";
	$query0.="invoice.oid=hosting_order.oid ";
	$query0.="and ";
	$query0.="hosting_order.uid=user.uid ";
	$query0.="and ";
	$query0.="invoice.payment_method=payment_process.pid ";
	$query0.="order by ";
	$query0.="invoice.payment_method asc ";
	
	$process_buffer="";
	$row0=mysql_query($query0);
	while ($rs0=mysql_fetch_row($row0))
		{
		$invoice_number=stripslashes(trim($rs0[0]));
		$total_due_today=stripslashes(trim($rs0[1]));
		$total_due_reoccur=stripslashes(trim($rs0[2]));
		$due_date=stripslashes(trim($rs0[3]));
		$status=stripslashes(trim($rs0[4]));
		$first_name=stripslashes(trim($rs0[5]));
		$last_name=stripslashes(trim($rs0[6]));
		$domain_name=stripslashes(trim($rs0[7]));
		$payment_processor=stripslashes(trim($rs0[8]));
		
		// fix by brandee on 1-24-3004	
		if ($status!=1)
			{
			if ($status==2||$due_date<time())
				{	
				$process_buffer.="#".$invoice_number." | ";
				$process_buffer.=$first_name." ".$last_name." | ";
				$process_buffer.=$domain_name." | ";

				if ($total_due_today==0) { $process_buffer.=$currency.sprintf("%01.2f", $total_due_reoccur).$currency_type." | "; }
				else { $process_buffer.=$currency.sprintf("%01.2f", $total_due_today).$currency_type." | "; }
				
				$process_buffer.=date("m/d/Y", $due_date)." | ";
				$process_buffer.=invoice_status($status)." |\n";
				}
			}
			
		unset($invoice_number);
		unset($total_due_today);
		unset($total_due_reoccur);
		unset($due_date);
		unset($status);
		unset($first_name);
		unset($last_name);
		unset($domain_name);
		unset($payment_processor);
		}
	
	if (trim($process_buffer)!="") { return $process_buffer."\n"; } 
	}

# $which == 1 return content
# $which == 0 mail content
function daily_invoice_summary($email_admin, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='41' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------
	
	$xmail_in_summary=daily_invoice_summary_processor_details(6);
	if ($xmail_in_summary!="")
		{
		$buffer="Mail In Payment\n";
		$buffer.="=================\n";
		$buffer.=$xmail_in_summary;
		$mail_in_summary=$buffer;

		unset($buffer);
		}

	$xpaypal_summary=daily_invoice_summary_processor_details(1);
	if ($xpaypal_summary!="")
		{
		$buffer="PayPal\n";
		$buffer.="=================\n";
		$buffer.=$xpaypal_summary;
		$paypal_summary=$buffer;

		unset($buffer);
		}

	$xpaysystems_summary=daily_invoice_summary_processor_details(3);
	if ($xpaysystems_summary!="")
		{
		$buffer="Paysystems\n";
		$buffer.="=================\n";
		$buffer.=$xpaysystems_summary;
		$paysystems_summary=$buffer;

		unset($buffer);
		}
	
	$xauthnet_summary=daily_invoice_summary_processor_details(8);
	if ($xauthnet_summary!="")
		{
		$buffer="Authorize.net\n";
		$buffer.="=================\n";
		$buffer.=$xauthnet_summary;
		$authnet_summary=$buffer;

		unset($buffer);
		}

	$xcheckout_summary=daily_invoice_summary_processor_details(4);
	if ($xcheckout_summary!="")
		{
		$buffer="2Checkout\n";
		$buffer.="=================\n";
		$buffer.=$xcheckout_summary;
		$checkout_summary=$buffer;

		unset($buffer);
		}

	$xworldpay_summary=daily_invoice_summary_processor_details(2);
	if ($xworldpay_summary!="")
		{
		$buffer="WorldPay\n";
		$buffer.="=================\n";
		$buffer.=$xworldpay_summary;
		$worldpay_summary=$buffer;

		unset($buffer);
		}

	# {{generate_date}}
	# {{mail_in_summary}}
	# {{paypal_summary}}
	# {{paysystems_summary}}
	# {{authorize.net_summary}}
	# {{2checkout}}
	# {{worldpay_summary}}


	$subject=str_replace("{{mail_in_summary}}", $mail_in_summary, $subject);
	$subject=str_replace("{{paypal_summary}}", $paypal_summary, $subject);
	$subject=str_replace("{{paysystems_summary}}", $paysystems_summary, $subject);
	$subject=str_replace("{{authorize.net_summary}}", $authnet_summary, $subject);
	$subject=str_replace("{{2checkout}}", $checkout_summary, $subject);
	$subject=str_replace("{{worldpay_summary}}", $worldpay_summary, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
	$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
	$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
	$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
	$message=str_replace("{{2checkout}}", $checkout_summary, $message);
	$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	if ($which===1) 
		{ 
		$message="*** Past Due Invoices as of {{generate_date}} Seperated by Payment Gateway ***\n\n";
		$message.="{{mail_in_summary}}{{paypal_summary}}{{paysystems_summary}}";	$message.="{{authorize.net_summary}}{{2checkout}}{{worldpay_summary}}";
		$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
		$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
		$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
		$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
		$message=str_replace("{{2checkout}}", $checkout_summary, $message);
		$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
		$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

		return nl2br($message); 
		}
	else 
		{ 
		$subject=str_replace("{{mail_in_summary}}", $mail_in_summary, $subject);
		$subject=str_replace("{{paypal_summary}}", $paypal_summary, $subject);
		$subject=str_replace("{{paysystems_summary}}", $paysystems_summary, $subject);
		$subject=str_replace("{{authorize.net_summary}}", $authnet_summary, $subject);
		$subject=str_replace("{{2checkout}}", $checkout_summary, $subject);
		$subject=str_replace("{{worldpay_summary}}", $worldpay_summary, $subject);
		$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
		
		$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
		$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
		$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
		$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
		$message=str_replace("{{2checkout}}", $checkout_summary, $message);
		$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
		$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

		mail($email_admin, $subject, $message, $headers); 
		}
	}

function daily_invoice_summary_processor_details_service($pid)
	{
	GLOBAL  $currency, $currency_type;

	$query0="select ";
	$query0.="invoice.invoice_number, ";	// 0
	$query0.="invoice.total_due_today, ";	// 1
	$query0.="invoice.charge_tax, ";		// 2
	$query0.="invoice.invoice_terms, ";		// 3
	$query0.="invoice.status, ";			// 4
	$query0.="user.first_name, ";			// 5
	$query0.="user.last_name, ";			// 6
	$query0.="payment_process.name ";		// 7
	$query0.="from ";
	$query0.="invoice, user, payment_process ";
	$query0.="where ";
	$query0.="invoice.payment_method='".addslashes(trim($pid))."' ";
	$query0.="and ";
	$query0.="invoice.invoice_type='1' ";
	$query0.="and ";
	$query0.="user.uid=invoice.uid ";
	$query0.="and ";
	$query0.="invoice.payment_method=payment_process.pid ";
	$query0.="order by ";
	$query0.="invoice.payment_method asc ";
	
	$process_buffer="";
	$row0=mysql_query($query0);
	while ($rs0=mysql_fetch_row($row0))
		{
		$invoice_number=stripslashes(trim($rs0[0]));
		$total_due_today=stripslashes(trim($rs0[1]));
		$charge_tax=stripslashes(trim($rs0[2]));
		$invoice_terms=translate_invoice_terms(stripslashes(trim($rs0[3])));
		$status=stripslashes(trim($rs0[4]));
		$first_name=stripslashes(trim($rs0[5]));
		$last_name=stripslashes(trim($rs0[6]));
		$payment_processor=stripslashes(trim($rs0[7]));
		
		// fix by brandee on 1-24-3004		
		if ($status!=1)
			{
			if ($status==2||$due_date<time())
				{	
				$process_buffer.="#".$invoice_number." | ";
				$process_buffer.=$first_name." ".$last_name." | ";
				$process_buffer.=$currency.sprintf("%01.2f", ($total_due_today+($total_due_today*$charge_tax))).$currency_type." | "; 
				$process_buffer.=$invoice_terms." | ";
				$process_buffer.=invoice_status($status)." |\n";
				}
			}

		unset($invoice_number);
		unset($total_due_today);
		unset($charge_tax);
		unset($due_date);
		unset($status);
		unset($first_name);
		unset($last_name);
		unset($payment_processor);
		}
	
	if (trim($process_buffer)!="") { return $process_buffer."\n"; } 
	}

# $which == 1 return content
# $which == 0 mail content
function daily_invoice_summary_service($email_admin, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='42' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------
	
	$xmail_in_summary=daily_invoice_summary_processor_details_service(6);
	if ($xmail_in_summary!="")
		{
		$buffer="Mail In Payment\n";
		$buffer.="=================\n";
		$buffer.=$xmail_in_summary;
		$mail_in_summary=$buffer;

		unset($buffer);
		}

	$xpaypal_summary=daily_invoice_summary_processor_details_service(1);
	if ($xpaypal_summary!="")
		{
		$buffer="PayPal\n";
		$buffer.="=================\n";
		$buffer.=$xpaypal_summary;
		$paypal_summary=$buffer;

		unset($buffer);
		}

	$xpaysystems_summary=daily_invoice_summary_processor_details_service(3);
	if ($xpaysystems_summary!="")
		{
		$buffer="Paysystems\n";
		$buffer.="=================\n";
		$buffer.=$xpaysystems_summary;
		$paysystems_summary=$buffer;

		unset($buffer);
		}
	
	$xauthnet_summary=daily_invoice_summary_processor_details_service(8);
	if ($xauthnet_summary!="")
		{
		$buffer="Authorize.net\n";
		$buffer.="=================\n";
		$buffer.=$xauthnet_summary;
		$authnet_summary=$buffer;

		unset($buffer);
		}

	$xcheckout_summary=daily_invoice_summary_processor_details_service(4);
	if ($xcheckout_summary!="")
		{
		$buffer="2Checkout\n";
		$buffer.="=================\n";
		$buffer.=$xcheckout_summary;
		$checkout_summary=$buffer;

		unset($buffer);
		}

	$xworldpay_summary=daily_invoice_summary_processor_details_service(2);
	if ($xworldpay_summary!="")
		{
		$buffer="WorldPay\n";
		$buffer.="=================\n";
		$buffer.=$xworldpay_summary;
		$worldpay_summary=$buffer;

		unset($buffer);
		}

	# {{generate_date}}
	# {{mail_in_summary}}
	# {{paypal_summary}}
	# {{paysystems_summary}}
	# {{authorize.net_summary}}
	# {{2checkout}}
	# {{worldpay_summary}}


	$subject=str_replace("{{mail_in_summary}}", $mail_in_summary, $subject);
	$subject=str_replace("{{paypal_summary}}", $paypal_summary, $subject);
	$subject=str_replace("{{paysystems_summary}}", $paysystems_summary, $subject);
	$subject=str_replace("{{authorize.net_summary}}", $authnet_summary, $subject);
	$subject=str_replace("{{2checkout}}", $checkout_summary, $subject);
	$subject=str_replace("{{worldpay_summary}}", $worldpay_summary, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
	$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
	$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
	$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
	$message=str_replace("{{2checkout}}", $checkout_summary, $message);
	$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	if ($which===1) 
		{ 
		$message="*** UnPaid and Past Due Billable Service Invoices as of {{generate_date}} Seperated by Payment Gateway ***\n\n";
		$message.="{{mail_in_summary}}{{paypal_summary}}{{paysystems_summary}}";	$message.="{{authorize.net_summary}}{{2checkout}}{{worldpay_summary}}";
		$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
		$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
		$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
		$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
		$message=str_replace("{{2checkout}}", $checkout_summary, $message);
		$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
		$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

		return nl2br($message); 
		}
	else 
		{ 
		$subject=str_replace("{{mail_in_summary}}", $mail_in_summary, $subject);
		$subject=str_replace("{{paypal_summary}}", $paypal_summary, $subject);
		$subject=str_replace("{{paysystems_summary}}", $paysystems_summary, $subject);
		$subject=str_replace("{{authorize.net_summary}}", $authnet_summary, $subject);
		$subject=str_replace("{{2checkout}}", $checkout_summary, $subject);
		$subject=str_replace("{{worldpay_summary}}", $worldpay_summary, $subject);
		$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
		
		$message=str_replace("{{mail_in_summary}}", $mail_in_summary, $message);
		$message=str_replace("{{paypal_summary}}", $paypal_summary, $message);
		$message=str_replace("{{paysystems_summary}}", $paysystems_summary, $message);
		$message=str_replace("{{authorize.net_summary}}", $authnet_summary, $message);
		$message=str_replace("{{2checkout}}", $checkout_summary, $message);
		$message=str_replace("{{worldpay_summary}}", $worldpay_summary, $message);
		$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

		mail($email_admin, $subject, $message, $headers); 
		}
	}

function invoice_resend($iid, $http_web, $email_admin, $id, $which)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='38' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$query1="select ";
	$query1.="invoice_type "; // 0
	$query1.="from ";
	$query1.="invoice ";
	$query1.="where ";
	$query1.="iid='".addslashes(trim($iid))."'";

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

	$invoice_type=stripslashes(trim($rs1[0])); 
	
	$query2="select ";
	$query2.="first_name, ";
	$query2.="last_name, ";
	$query2.="email ";
	$query2.="from ";
	$query2.="user ";
	$query2.="where ";
	$query2.="uid='".addslashes(trim($id))."' ";

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

	$first_name=stripslashes(trim($rs2[0]));
	$last_name=stripslashes(trim($rs2[1]));
	$client_email=stripslashes(trim($rs2[2]));

	if ($invoice_type==0)
		{	
		$pay_link=$http_web."/invoice_hosting_payable.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $iid));
		}
	else
		{	
		$pay_link=$http_web."/invoice_service_payable.php?c=".urlencode(e("576cb7f68040520768bf51c75f7f4c84", $iid));
		}
	
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{pay_link}}", $pay_link, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{pay_link}}", $pay_link, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

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

	if ($which==1)
		{
		$message.="\n\n-----------------------------------------------------\nThis is an exact copy of what was sent to the client.";
		mail($email_admin, $subject, $message, $headers);
		}
	}
function invoice_paid_successfully($iid, $currency, $currency_type, $email_admin)
	{
	$query0="select ";
	$query0.="subject, ";
	$query0.="message, ";
	$query0.="default_email ";
	$query0.="from ";
	$query0.="email_templates ";
	$query0.="where ";
	$query0.="emid='37' ";
	$query0.="limit 0, 1";

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

	$subject=stripslashes(trim($rs0[0]));
	$message=stripslashes(trim($rs0[1]));
	$default_email=stripslashes(trim($rs0[2]));
	
	# -------------------------------------------

	$query1="select ";
	$query1.="uid, ";				// 0
	$query1.="oid, ";				// 1
	$query1.="date_paid, ";			// 2
	$query1.="invoice_number, ";	// 3	
	$query1.="invoice_type, ";		// 4
	$query1.="total_due_today, ";	// 5
	$query1.="total_due_reoccur, ";	// 6
	$query1.="charge_tax ";			// 7
	$query1.="from ";
	$query1.="invoice ";
	$query1.="where ";
	$query1.="iid='".addslashes(trim($iid))."'";

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

	$uid=stripslashes(trim($rs1[0]));
	$oid=stripslashes(trim($rs1[1]));
	$date_paid=stripslashes(trim($rs1[2])); 
	$invoice_number=stripslashes(trim($rs1[3])); 
	$invoice_type=stripslashes(trim($rs1[4])); 
	$total_due_today=stripslashes(trim($rs1[5])); 
	$total_due_reoccur=stripslashes(trim($rs1[6])); 
	$charge_tax=stripslashes(trim($rs1[7])); 

	if ($invoice_type==0)
		{
		if ($total_due_today>0) { $total_invoice_amount=$total_due_today; }
		else { $total_invoice_amount=$total_due_reoccur; }

		$query2="select ";
		$query2.="user.first_name, ";
		$query2.="user.last_name ";
		$query2.="from ";
		$query2.="user, hosting_order ";
		$query2.="where hosting_order.uid=user.uid ";
		$query2.="and ";
		$query2.="hosting_order.oid='".addslashes(trim($oid))."' ";
		$query2.="order by ";
		$query2.="hosting_order.oid asc";

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

		$first_name=stripslashes(trim($rs2[0]));
		$last_name=stripslashes(trim($rs2[1]));
		}
	else
		{
		if ($charge_tax>0)
			{
			$tax_amount=$total_due_today*$charge_tax;
			$total_due_today+=$tax_amount;
			}
		
		$total_invoice_amount=$total_due_today;
		
		$query2="select ";
		$query2.="first_name, ";
		$query2.="last_name ";
		$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]));
		}
	
	$total_invoice_amount=$currency.sprintf("%01.2f", $total_invoice_amount)." ".$currency_type;
	
	$subject=str_replace("{{invoice_number}}", $invoice_number, $subject);
	$subject=str_replace("{{date_paid}}", date("m/d/Y h:i:s a", $date_paid), $subject);
	$subject=str_replace("{{first_name}}", $first_name, $subject);
	$subject=str_replace("{{last_name}}", $last_name, $subject);
	$subject=str_replace("{{total_invoice_amount}}", $total_invoice_amount, $subject);
	$subject=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $subject);
	
	$message=str_replace("{{invoice_number}}", $invoice_number, $message);
	$message=str_replace("{{date_paid}}", date("m/d/Y h:i:s a", $date_paid), $message);
	$message=str_replace("{{first_name}}", $first_name, $message);
	$message=str_replace("{{last_name}}", $last_name, $message);
	$message=str_replace("{{total_invoice_amount}}", $total_invoice_amount, $message);
	$message=str_replace("{{generate_date}}", date("m/d/Y h:i:s a"), $message);

	
	$headers="From: ".(($default_email!="")?"".$default_email."":"".$email_admin."")."";
	$headers.="\nReply-To: ".$email_admin;
	$headers.="\nX-Mailer: PHP/".phpversion();

	mail($email_admin, $subject, $message, $headers);
	}

function translate_invoice_terms($invoice_terms)
	{
	if ($invoice_terms==0) { return "Upon Receipt"; }
	else if ($invoice_terms==30) { return "30 Day Invoice"; }
	else if ($invoice_terms==60) { return "60 Day Invoice"; }
	else if ($invoice_terms==90) { return "90 Day Invoice"; }
	else if ($invoice_terms==120) { return "120 Day Invoice"; }
	else { return "error!"; }
	}


function grab_invoice_number()
	{
	# grab invoice number
	$query0="select ";
	$query0.="invoice_number ";
	$query0.="from ";
	$query0.="invoice ";
	$query0.="order by iid desc ";
	$query0.="limit 0, 1";

	$rs0=mysql_fetch_row(mysql_query($query0));
	
	if (trim($rs0[0])=="")
		{
		$query1="select ";
		$query1.="invoice_number ";
		$query1.="from ";
		$query1.="invoice_config ";
		$query1.="limit 0, 1";

		$rs1=mysql_fetch_row(mysql_query($query1));
		
		return $rs1[0];
		}
	else { return $rs0[0]+1; }
	}


function invoice_status($invoice_status)
	{
	if ($invoice_status==0) { return "Unpaid"; }
	else if ($invoice_status==1) { return "Paid"; }
	else if ($invoice_status==2) { return "Past Due"; }
	else { return "Read Error!"; }
	}


function invoice_status_select($invoice_status)
	{
	$output="0@Unpaid|";
	$output.="1@Paid|";
	return $output.="2@Past Due|";
	}

function create_timestamp($formatted_date)
	{
	list($m, $d, $y)=explode("/", $formatted_date);
	return mktime(0, 0, 0, $m, $d, $y);
	}

function create_invoice_session($iid)
	{
	$sid=e("576cb7f68040520768bf51c75f7f4c84", $iid);
	
	$query5="insert ";
	$query5.="into ";
	$query5.="invoice_session ";
	$query5.="set ";
	$query5.="sid='".addslashes(trim($sid))."', ";
	$query5.="created='".time()."'";

	mysql_query($query5);

	return $sid; 
	}

function convert_payment_term_to_number($payment_term)
	{
	if ($payment_term=="monthly") { return 1; }
	else if ($payment_term=="quarterly") { return 3; }
	else if ($payment_term=="semi_annual") { return 6; }
	else if ($payment_term=="annual") { return 12; }
	}

function is_valid_date($next_due_date)
	{
	$invalid=mktime(0, 0, 0, 1, 1, 1970);

	if ($next_due_date>$invalid) { return 1; }
	else if ($next_due_date==-1) { return 1; }
	else if (trim($next_due_date)=="") { return 1; }
	else { return 0; }
	}
?>