Code: Select all
<?php
require_once 'config.php';
/*********************************************************
* CHECKOUT FUNCTIONS
*********************************************************/
function saveOrder()
{
$orderId = 0;
$shippingCost = 10;
$requiredField = array('hidShippingFirstName', 'hidShippingLastName', 'hidShippingAddress1', 'hidShippingCity', 'hidShippingPostalCode',
'hidPaymentFirstName', 'hidPaymentLastName', 'hidPaymentAddress1', 'hidPaymentCity', 'hidPaymentPostalCode');
if (checkRequiredPost($requiredField)) {
extract($_POST);
// make sure the first character in the
// customer and city name are properly upper cased
$hidShippingFirstName = ucwords($hidShippingFirstName);
$hidShippingLastName = ucwords($hidShippingLastName);
$hidPaymentFirstName = ucwords($hidPaymentFirstName);
$hidPaymentLastName = ucwords($hidPaymentLastName);
$hidShippingCity = ucwords($hidShippingCity);
$hidPaymentCity = ucwords($hidPaymentCity);
$cartContent = getCartContent();
$numItem = count($cartContent);
// save order & get order id
$sql = "INSERT INTO tbl_order(od_date, member_username, od_last_update, od_shipping_first_name, od_shipping_last_name, od_shipping_address1,
od_shipping_address2, od_shipping_phone, od_shipping_state, od_shipping_city, od_shipping_postal_code, od_shipping_cost, od_vat, od_shipping_total, od_vat_total,
od_payment_first_name, od_payment_last_name, od_payment_address1, od_payment_address2,
od_payment_phone, od_payment_state, od_payment_city, od_payment_postal_code)
VALUES (NOW(), '$hidClient', NOW(), '$hidShippingFirstName', '$hidShippingLastName', '$hidShippingAddress1',
'$hidShippingAddress2', '$hidShippingPhone', '$hidShippingState', '$hidShippingCity', '$hidShippingPostalCode', '$shippingCost', '$vat', '$totalShipping', '$totalVat',
'$hidPaymentFirstName', '$hidPaymentLastName', '$hidPaymentAddress1',
'$hidPaymentAddress2', '$hidPaymentPhone', '$hidPaymentState', '$hidPaymentCity', '$hidPaymentPostalCode')";
$result = dbQuery($sql);
// get the order id
$orderId = dbInsertId();
echo "vat= $totalVat";
if ($orderId) {
// save order items
for ($i = 0; $i < $numItem; $i++) {
$sql = "INSERT INTO tbl_order_item(od_id, pd_id, od_qty)
VALUES ($orderId, {$cartContent[$i]['pd_id']}, {$cartContent[$i]['ct_qty']})";
$result = dbQuery($sql);
}
// update product stock
for ($i = 0; $i < $numItem; $i++) {
$sql = "UPDATE tbl_product
SET pd_qty = pd_qty - {$cartContent[$i]['ct_qty']}
WHERE pd_id = {$cartContent[$i]['pd_id']}";
$result = dbQuery($sql);
}
// then remove the ordered items from cart
for ($i = 0; $i < $numItem; $i++) {
$sql = "DELETE FROM tbl_cart
WHERE ct_id = {$cartContent[$i]['ct_id']}";
$result = dbQuery($sql);
}
}
}
return $orderId;
}
/*
Get order total amount ( total purchase + shipping cost + vat)
Little niggle with the VAT. to calculate it is e.g ((21/100) * 13.72) = 2.8812 and for cents due to 3rd party is
2.8812 * 100 = 288.12. Need to get rid of the decimal points..........
*/
function getOrderAmount($orderId)
{
//$orderTotal = str_replace(',', '', $_POST['totalTotal']);
//echo "orderTotal= $orderTotal";
//return $orderTotal;
$orderAmount = 0;
$sql = "SELECT SUM(pd_price * od_qty)
FROM tbl_order_item oi, tbl_product p
WHERE oi.pd_id = p.pd_id and oi.od_id = $orderId
UNION
SELECT SUM(od_shipping_total)
FROM tbl_order
WHERE od_id = $orderId
UNION
SELECT SUM(od_vat_total)
FROM tbl_order
WHERE od_id = $orderId";
$result = dbQuery($sql);
if (dbNumRows($result) == 3) {
$row = dbFetchRow($result);
$totalPurchase = $row[0];
$row = dbFetchRow($result);
$shippingCost = $row[0];
$row = dbFetchRow($result);
$vat = $row[0];
$orderAmount = ($totalPurchase * 100) + ($totalShipping * 100) + ($totalVat * 100);
//echo "orderTotal= $orderTotal";
}
return $orderAmount;
//echo "order= $orderTotal";
}
?>Code: Select all
CREATE TABLE `tbl_order` (
`od_id` int(10) unsigned NOT NULL auto_increment,
`member_username` varchar(30),
`od_date` datetime default NULL,
`od_last_update` datetime NOT NULL default '0000-00-00 00:00:00',
`od_status` enum('New', 'Paid', 'Shipped','Completed','Cancelled') NOT NULL default 'New',
`od_memo` varchar(255) NOT NULL default '',
`od_shipping_first_name` varchar(50) NOT NULL default '',
`od_shipping_last_name` varchar(50) NOT NULL default '',
`od_shipping_address1` varchar(100) NOT NULL default '',
`od_shipping_address2` varchar(100) NOT NULL default '',
`od_shipping_phone` varchar(32) NOT NULL default '',
`od_shipping_city` varchar(100) NOT NULL default '',
`od_shipping_state` varchar(32) NOT NULL default '',
`od_shipping_postal_code` varchar(10) NOT NULL default '',
`od_shipping_cost` decimal(5,2) default '0.00',
`od_vat` int(2) NOT NULL default '21',
`od_shipping_total` decimal(5,2) default '0.00',
`od_vat_total` decimal(5,2) default '0.00',
`od_payment_first_name` varchar(50) NOT NULL default '',
`od_payment_last_name` varchar(50) NOT NULL default '',
`od_payment_address1` varchar(100) NOT NULL default '',
`od_payment_address2` varchar(100) NOT NULL default '',
`od_payment_phone` varchar(32) NOT NULL default '',
`od_payment_city` varchar(100) NOT NULL default '',
`od_payment_state` varchar(32) NOT NULL default '',
`od_payment_postal_code` varchar(10) NOT NULL default '',
PRIMARY KEY (`od_id`,`member_username`)
) TYPE=MyISAM AUTO_INCREMENT=1780 ;