<?
/*
Copyright (C)2004 Jeff Holman, Downhill Battle, et al
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
session_name("postal");
session_start();
include "config.inc.php";
require "inventory.inc.php";
// Update shipping for users w/o javascript
if ($_POST['checkout']) $_SESSION['intl'] = $_POST['intl'];
// Update shipping for users w/ javascript
if ($_GET['intl'] == "true") $_SESSION['intl'] = 1;
if ($_GET['intl'] == "false") $_SESSION['intl'] = 0;
if (!$_SESSION['intl']) $_SESSION['intl'] = 0;
// Check for custom quantity
if ($_POST['quantity'] == "o") $_POST['quantity'] = $_POST['quantitybox'];
// Check if item type has options, and if so, process it
if (array_key_exists($code = $_POST['code'], $types2)) {
$valid_options = 1;
// Check that all options for the item type were posted and have valid values
foreach(array_keys($options[$code]) as $i) {
if (!array_key_exists(${$i} = $_POST[$i], $options[$code][$i])) {
$valid_options = 0; break;
}
}
// Add item w/ specific options to order
if ($valid_options) {
$newtype = $code;
foreach(array_keys($options[$code]) as $i) {
$newtype .= "-".${$i};
}
// Update session inventory with new item
$_SESSION['items'][$newtype] = $types2[$code];
$_SESSION['pricing'][$newtype] = $_SESSION['pricing'][$code];
$_SESSION['shipping'][$newtype] = $_SESSION['shipping'][$code];
// Create new item code based on item type and options
$_POST['code'] = $newtype;
}
}
// Process remove request
if ($_GET['remove']) {
$_POST['code'] = $_GET['remove'];
$_POST['quantity'] = 0;
$_POST['update'] = 1;
}
// Process posted item and add to quantity in cart (or remove item)
if (($code = $_POST['code']) && (($quantity = floor($_POST['quantity'])) >= 0) && array_key_exists($code, $_SESSION['items'])) {
if ($_POST['remove']) $_SESSION['order'][$code] = NULL;
else if ($_SESSION['order'][$code] > 0 && !$_POST['update']) $_SESSION['order'][$code] += $quantity;
else $_SESSION['order'][$code] = $quantity;
}
// Convert number to dollars
function num_to_dollar($num) {
$num = "$num";
$len = strlen($num);
if ($num == round($num)) return "$num.00";
else if ($num[$len-2] == ".") return "$num"."0";
else return round($num,2);
}
// Create item "box" for updating quantity
function quantityBox($code, $quantity, $total, $options = NULL) {
?>
<a name="<? echo $code; ?>"></a>
<form method="post" target="orderbar" action="order.php#<? echo $code; ?>">
<? if ($code == $_POST['code']) { ?>
<a name="new"></a>
<div align="left" class="newItem">
<? } else { ?>
<div align="left" id="orderSummary">
<? } ?>
<h2 class="itemName"><? echo $_SESSION['items'][$code]; ?></h2>
<? if ($_GET['modify'] != $code) {
$optcode = explode("-", $code);
$i=0;
if (sizeof($options[$optcode[0]]) > 0) {
foreach (array_keys($options[$optcode[0]]) as $j) {
$i++;
?>
</b><? echo $options[$optcode[0]][$j][$optcode[$i]]; ?><br />
<?
}
}
}
if (sizeof($optcode) > 1) echo "<BR />";
?>
<? if ($_GET['modify'] != $code) echo $quantity." for "."<font color=\"#990000\" size=\"2\">$".num_to_dollar($total)."</font>"; else { ?>
<input class="quantityBox" type="text" name="quantity" size="5" value="<? echo $quantity ?>" />
<br /><br />
<input type="image" src="cart-images/update-quantity.gif" name="update" value="1" />
<?
}
?>
<input type="hidden" name="code" value="<? echo $code ?>" />
<input type="hidden" name="update" value="1" />
<? if ($_GET['modify'] != $code) { ?>
<br />
<br />
(<a href="order.php?remove=<? echo urlencode($code); ?>&s=<? echo session_id()?>#<? echo urlencode($code); ?>">remove</a>
| <a href="order.php?modify=<? echo urlencode($code); ?>&s=<? echo session_id()?>#<? echo urlencode($code); ?>">edit
quantity</a>)
<? } ?>
</div>
</form>
<?
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Your Order</title>
<link rel="stylesheet" type="text/css" href="common.css" />
<link rel="stylesheet" type="text/css" href="order.css" />
</head>
<div class="cartMain">
<h1>Your Order</h1><br />
<?
// Enable checkout for non javascript users
if ($_POST['checkout']) {
?>
<form target="_top" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<div class="itemError"> You can now proceed to checkout.<br />
<br />
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<? echo BUSINESS; ?>">
<input type="hidden" name="item_name" value="<? echo ITEM_NAME; ?>">
<input type="hidden" name="item_number" value="<? echo $_SESSION['ppcode']." ".$_SESSION['intl']; ?>">
<input type="hidden" name="amount" value="<? echo $_SESSION['total']; ?>">
<input type="hidden" name="page_style" value="Primary">
<input type="hidden" name="return" value="<? echo SUCCESS; ?>">
<input type="hidden" name="cancel_return" value="<? echo FAILED; ?>">
<input type="hidden" name="cn" value="Special Instructions">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="cart-images/checkout.gif" border="0" name="submit" alt="Checkout">
<a href="order.php?s=<? echo session_id(); ?>"><img src="cart-images/modify-order.gif" alt="Modify Order" width="75" height="14" border="0" /></a></div>
</form>
<?
}
$_SESSION['ppcode'] = "";
$shippingTotal = 0;
// Calculate number of items in each shipping category
if (sizeof($shippingCategory) > 0) {
foreach(array_keys($shippingCategory) as $i) {
$categoryCount[$i] = 0;
}
}
if (sizeof($_SESSION['order']) > 0) {
foreach(array_keys($_SESSION['order']) as $typeCode) {
if ($_SESSION['order'][$typeCode] > 0) {
$typeQuantity = $_SESSION['order'][$typeCode];
$typeTotal[$typeCode] = 0;
foreach (array_reverse(array_keys($_SESSION['pricing'][$typeCode])) as $typePricing) {
if ($nocustom[$typeCode] && $typeTotal[$typeCode] != ($typeTotal[$typeCode] += floor($typeQuantity/$typePricing)*$_SESSION['pricing'][$typeCode][$typePricing])) {
$typeQuantity %= $typePricing;
} else if (!$nocustom[$typeCode] && !$largestQuantity[$typeCode] && floor($typeQuantity/$typePricing)*$_SESSION['pricing'][$typeCode][$typePricing] >= 1) {
$largestQuantity[$typeCode] = $typePricing;
$typeTotal[$typeCode] = $_SESSION['order'][$typeCode]*($_SESSION['pricing'][$typeCode][$typePricing]/$largestQuantity[$typeCode]);
$typeQuantity = 0;
}
}
$_SESSION['order'][$typeCode] -= $typeQuantity;
if ($_SESSION['order'][$typeCode] > 0) {
if ($typeQuantity > 0 && $nocustom[$typeCode]) {
// Round down to the nearest increment if custom quantities aren't allowed for the item
echo "<div class=\"itemError\">You selected an invalid quantity of ".$_SESSION['items'][$typeCode]." - it has been rounded down to the first valid increment (".($_SESSION['order'][$typeCode]).").</div><BR /><BR />";
}
if ($currentCategory = $_SESSION['shipping'][$typeCode]) {
$categoryCount[$currentCategory] += $_SESSION['order'][$typeCode];
}
//echo "<h2>".$_SESSION['items'][$typeCode]."</h2>";
$_SESSION['ppcode'] .= "$typeCode-".$_SESSION['order'][$typeCode]." ";
quantityBox($typeCode, $_SESSION['order'][$typeCode], $typeTotal[$typeCode], $options);
} else {
echo "<div class=itemError>Sorry, that is below the minimum quantity for ".$_SESSION['items'][$typeCode].".</div><BR /><BR />";
}
}
}
}
// Calculate total shipping
foreach(array_keys($shippingCategory) as $i) {
if ($_SESSION['intl']) {
$shippingTotal += ($categoryCount[$i] * $shippingCategory[$i][2]);
} else if ($categoryCount[$i] > 0) {
$shippingTotal += ($shippingCategory[$i][0] + ($categoryCount[$i] - 1) * $shippingCategory[$i][1]);
}
}
$_SESSION['ppcode'] = trim($_SESSION['ppcode']);
// Add up item totals
$total = 0;
if (sizeof($typeTotal) > 0) {
foreach ($typeTotal as $itemTotal) {
$total += $itemTotal;
}
}
// Order summary box
if ($total > 0) {
?>
<div align="left" id="orderSummary">
<form method="post" action="order.php">
<a name="summary"></a><h2 class="itemName">Order Summary</h2> <b>Shipping Location</b><br />
<input onclick="location.replace('order.php?s=<? echo session_id(); ?>&intl=false#summary');" type="radio" name="intl" value="0"<? if (!$_SESSION['intl']) echo " CHECKED"; ?><? if ($_POST['checkout']) echo " DISABLED"; ?> />
US, Mexico, Canada<br />
<input onclick="location.replace('order.php?s=<? echo session_id(); ?>&intl=true#summary');" id="intl" type="radio" name="intl" value="1"<? if ($_SESSION['intl']) echo " CHECKED"; ?><? if ($_POST['checkout']) echo " DISABLED"; ?> />
International<br />
<br />
<b>Subtotal:</b> $<? echo num_to_dollar($total); ?><br />
<b>Shipping:</b> $<? echo num_to_dollar($shippingTotal); ?><br />
<b><font size="2" color="#990000">Total: $<? echo $_SESSION['total'] = num_to_dollar($total + $shippingTotal); ?></font></b><br />
<br />
<input type="hidden" name="checkout" value="1" />
<? if (!$_POST['checkout']) { ?>
<noscript>
<span style="color:red">Important: You must click "update shipping"
to enable the "checkout" button!</span><br />
<input name="checkout" value="1" type="image" src="cart-images/update-shipping.gif" /></noscript>
<?
}
?>
</form>
<script language="JavaScript">document.write('<form target=\"_top\" action=\"https://www.paypal.com/cgi-bin/webscr\" method=\"post\">');</script>
<noscript><? if ($_POST['checkout']) { ?><form target="_top" action="https://www.paypal.com/cgi-bin/webscr" method="post"><? } ?></noscript>
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<? echo BUSINESS; ?>">
<input type="hidden" name="item_name" value="<? echo ITEM_NAME; ?>">
<input type="hidden" name="item_number" value="<? echo $_SESSION['ppcode']." ".$_SESSION['intl']; ?>">
<input type="hidden" name="amount" value="<? echo $_SESSION['total']; ?>">
<input type="hidden" name="page_style" value="Primary">
<input type="hidden" name="return" value="<? echo SUCCESS; ?>">
<input type="hidden" name="cancel_return" value="<? echo FAILED; ?>">
<input type="hidden" name="cn" value="Special Instructions">
<input type="hidden" name="currency_code" value="USD">
<input id="checkout" type="image" src="cart-images/checkout.gif" border="0" name="submit" alt="Checkout">
<a onclick="if(!confirm('Are you sure you want to clear your entire order?')) return false;" href="index.php?reset=1" target="_top"><img src="cart-images/clear-order.gif" alt="Clear Order" border="0" /></a>
<script language="JavaScript">document.write('</form>');</script>
<noscript><? if ($_POST['checkout']) { ?></form><? } ?></noscript>
</div>
<?
} else {
echo "<b>Go on, order something!</b>";
}
?>
</div>