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

echo("
	<!doctype html public \"-//W3C//DTD HTML 4.0 Transitional//EN\">
	<html>
	<head>
	<title> Create Invoice History </title>
	</head>

	<body>
	");

if (!isset($i_agree)) 
	{
	echo("
		If you click the button below to start this process all current hosting order invoices in your system will be lost.<br>You should run this file only 1 time.<br>Please back up your database before clicking the button below.<br><br><font color='#990000'><b>Proceed at your own risk.</b></font><br><br>
		<form action='".$PHP_SELF."' method='POST'><input type='submit' name='i_agree' value='Yes, I fully understand the statement above.'></form>
		");
	}
else
	{
	$buffer="Starting current run: [".date("m/d/Y h:i:s")."]\n\n";

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

	mysql_query("update hosting_order set next_due_date=''");
	$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.="order by oid asc ";

	$row0=mysql_query($query0);
	while ($rs0=mysql_fetch_row($row0))
		{
		# Query ---------------------------------------------------------------------
		$ogcreate_hosting=stripslashes(trim($rs0[12]));
		$next_due_date=stripslashes(trim($rs0[0]));
		if (trim($next_due_date)=="") { $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;
					}
				
				# ---------------------------------------------------------------------------
				
				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);

	echo nl2br($buffer);
	}
echo("
</body>
</html>
");
?>