<?php
	/*
	** File: CREATE_INVOICE
	** Description: create a blank invoice, no SKUs, set to inactive
	** Version: $Revision: 1.7 $
	** Created: 06/24/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/09 23:23:19 $
	**
	** Log : $Log: CREATE_INVOICE,v $
	** Log : Revision 1.7  1999/12/09 23:23:19  leon
	** Log : Changed ActionResults use so that it's an array instead of a
	** Log : continually-growing string.  Note the little function in standard_library
	** Log : for printing out all the action results.
	** Log :
	** Log : Revision 1.6  1999/11/09 22:21:13  leon
	** Log : More localization work.
	** Log :
	** Log : Revision 1.5  1999/10/04 22:09:50  leon
	** Log : Removed getInvoiceID.  The active invoice is now added to $UserInfo
	** Log : by the check sid functionality in init.
	** Log :
	** Log : Revision 1.4  1999/08/20 20:03:12  leon
	** Log : Added support to allow checking out if you aren't logged in.  To do this,
	** Log : I added a column to the session table to track the ID of current invoice
	** Log : for the session.  Before, we were getting the current invoice by looking
	** Log : in the invoice table for an inactive invoice for the same user.  This
	** Log : doesn't work for anonymous sessions because they all have user=0.
	** Log :
	** Log : Note that the invoice column is now set or unset by LOGIN_USER and
	** Log : LOGOUT_USER.  If a session creates an invoice, then logs into an
	** Log : account that has an inactive invoice, the invoice the session already
	** Log : has will be preserved.
	** Log :
	** Log : The purgeInvoice function was changed to set Invoice=0 for any session
	** Log : that is associated with the invoice to be purged.
	** Log :
	** Log : Revision 1.3  1999/08/18 22:31:47  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.2  1999/08/09 18:01:41  jj
	** Log : Renovated the order confirmation process.
	** Log :
	** Log : Revision 1.1  1999/07/28 21:16:41  leon
	** Log : New actions based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/


	//if no invoice is in progress, create one
	if($UserInfo['Invoice'] == 0)
	{	

		//create invoice row
		$Query = "INSERT INTO invoice VALUES (0, "; //auto_increment
		$Query .= intval($UserInfo["ID"]) . ", "; //current user ID
		$Query .= "'N', "; //create invoice as inactive
		$Query .= "now())"; //mark creation as now so we can expire it if necessary
	
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		if(!$DatabaseResult)
		{
			print("<!--\n");
			print("$Query \n");
			print(mysql_errno() . ": " . mysql_error()) . "\n"; 
			print("-->\n");
			$ActionResults[] = L_CREATEINVOICE_STEP1;
		}
	
		//get the ID of the new row in the invoice table
		$UserInfo['Invoice'] = mysql_insert_id($DatabaseLink);
		
		//update the session table
		$Query = "UPDATE session ";
		$Query .= "SET Invoice = " . $UserInfo['Invoice'] . " ";
		$Query .= "WHERE ID = '$sid' ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
		if(!$DatabaseResult)
		{
			print("<!--\n");
			print("$Query \n");
			print(mysql_errno() . ": " . mysql_error()) . "\n"; 
			print("-->\n");
			$ActionResults[] = L_CREATEINVOICE_STEP2;
		}
		
	}
	else
	{
		$ActionResults[] = "Continuing with checkout process...";
	}
	
	//add all the invoice_sku rows
	$Query = "INSERT INTO invoice_sku ";
	$Query .= "SELECT 0, " . $UserInfo['Invoice'] . ", ss.SKU, ss.Quantity, s.ExternalSKU, s.Name, ";
	$Query .= "s.ListPrice, s.SalePrice, s.Shipping, ss.Notes, 0, 'N' ";
	$Query .= "FROM session_sku ss, sku s ";
	$Query .= "WHERE ss.SKU = s.ID ";
	$Query .= "AND ss.Session = '" . $sid . "' ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	if(!$DatabaseResult)
	{
		print("<!--\n");
		print("$Query \n");
		print(mysql_errno() . ": " . mysql_error()) . "\n"; 
		print("-->\n");
		$ActionResults[] = L_CREATEINVOICE_STEP3;
	}


	//remove SKUs from basket
	$Query = "DELETE FROM session_sku WHERE Session='$sid'";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	if(!$DatabaseResult)
	{
		print("<!--\n");
		print("$Query \n");
		print(mysql_errno() . ": " . mysql_error()) . "\n"; 
		print("-->\n");
		$ActionResults[] = L_CREATEINVOICE_STEP4;
	}

?>
