<?php
	/*
	** File: UPDATE_INVOICE_SKU
	** Description: update quantity and giftwrap for items on an invoice
	** Version: $Revision: 1.10 $
	** Created: 07/07/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 2000/02/04 19:04:02 $
	**
	** Log : $Log: UPDATE_INVOICE_SKU,v $
	** Log : Revision 1.10  2000/02/04 19:04:02  leon
	** Log : Updating giftwrap checkboxes only takes one query now.
	** Log :
	** Log : Revision 1.9  2000/01/03 21:35:26  leon
	** Log : Added upper limit to quantity
	** Log :
	** Log : Revision 1.8  1999/12/09 23:23:25  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.7  1999/11/05 18:17:49  leon
	** Log : Reorganized coupon code.
	** Log :
	** Log : Revision 1.6  1999/11/02 19:09:46  leon
	** Log : Localized the UPDATE action modules
	** Log :
	** Log : Revision 1.5  1999/10/04 22:09:52  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/18 22:31:54  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.3  1999/08/18 19:12:06  jj
	** Log : Painstakingly dealt with the following question: which address fields should we ask for, which should we require. Reasonable defaults set up.
	** Log :
	** Log : Revision 1.2  1999/08/10 00:05:47  jj
	** Log : Created and made use of a new constant to specify gift wrap cost.
	** Log :
	** Log : Revision 1.1  1999/07/28 21:16:42  leon
	** Log : New actions based on code donation from a live project
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	//getFeeID function
	include(APPLICATION_ROOT . "/modules/include/fee");

	

	//set quantities
	for(reset($inputBasket); $key = key($inputBasket); next($inputBasket))
	{
		//clean up input
		$key = (integer)$key;
		$inputBasket[$key] = (integer)$inputBasket[$key];

		if($inputBasket[$key] > MAX_QUANTITY)
		{
			$ActionResults[] = L_UPDATEINVOICESKU_MAXEXCEEDED;
		}
		else
		{
			if($inputBasket[$key] <= 0)
			{
				//delete item
				$Query = "DELETE FROM invoice_sku WHERE ID=" . intval($key);
			}
			else
			{
				//update quantity
				$Query = "UPDATE invoice_sku ";
				$Query .= "SET Quantity=" . intval($inputBasket[$key]) . " ";
				$Query .= "WHERE ID=" . intval($key);
			}
		
			//execute query
			if(!mysql_query($Query, $DatabaseLink))
			{
				$ActionResults[] = mysql_errno() . ": " . mysql_error();
			}
		}
	}


	//set all gift wrap to no
	$Query = "UPDATE invoice_sku ";
	$Query .= "SET GiftWrap = 'N' ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	//execute query
	if(!mysql_query($Query, $DatabaseLink))
	{
		$ActionResults[] =  mysql_errno() . ": " . mysql_error();
	}
	
	//set gift wrap to yes where given
	if(is_array($inputGiftWrap))
	{
		$Query = "UPDATE invoice_sku ";
		$Query .= "SET GiftWrap='Y' ";
		$Query .= "WHERE ID in (0";

		for(reset($inputGiftWrap); $key = key($inputGiftWrap); next($inputGiftWrap))
		{
			$Query .= ", $key";
		}

		$Query .= ") ";

		if(!mysql_query($Query, $DatabaseLink))
		{
			$ActionResults[] =  mysql_errno() . ": " . mysql_error();
		}
	}	

	//wipe out any previous gift wrap fees
	$Query = "DELETE FROM invoice_fee ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	$Query .= "AND Fee = ".getFeeID('Gift Wrap')." ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);

	//get number of items to be giftwrapped
	/*
	this query gives you total items to be gift wrapped
	instead of ignoring quantity as is below
	$Query = "SELECT SUM(Quantity) ";
	$Query .= "FROM invoice_sku ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	$Query .= "AND GiftWrap = 'Y' ";
	*/
	
	$Query = "SELECT COUNT(*) ";
	$Query .= "FROM invoice_sku ";
	$Query .= "WHERE Invoice = " . $UserInfo['Invoice'] . " ";
	$Query .= "AND GiftWrap = 'Y' ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	list($numGiftWrap) = mysql_fetch_row($DatabaseResult);
								
	if($numGiftWrap > 0)
	{
		//add fee for gift wrapping
		$Query = "INSERT INTO invoice_fee VALUES (" . 
			$UserInfo['Invoice'] . ", ".getFeeID('Gift Wrap').", " . 
			($numGiftWrap * GIFTWRAP_COST) . ") ";
		$DatabaseResult = mysql_query($Query, $DatabaseLink);
	}


	$ActionResults[] =  L_UPDATEINVOICESKU_SUCCESS;

?>
