<?php
//get shop page's title
$t = $site->pageTitle($s['shop_page']);
$seo_pageTitle = seo_str($t);
//if user doesnt have any items in cart, send back to cart page
$check = $mysql->num_rows($mysql->query("ID", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
if($check == 0)
{
header("Location: /{$s['shop_page']}/$seo_pageTitle/Basket");
exit;
}
//if method is pick up from store, set the shipping to 0
if($_POST['type'] == 'pickstore')
{
$mysql->update("shipping", 0, "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
include('inc/shop/discount/index.php');//check and update discount
include('inc/shop/tax/sales_tax.php');//check and update tax
}
//cart subtotal
$subtotal = 0;
$query = $mysql->query("(price*qty) as price", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'");
if($mysql->num_rows($query) == 0){die('Unauthorised access');exit;}
while($data = $mysql->get_array($query))
{
$subtotal += $data['price'];
}
$orderTotal = $mysql->get_array($mysql->query("shipping,sales_tax,discount_value,discount_code", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$total = number_format($subtotal+$orderTotal['shipping']-$orderTotal['sales_tax']-$orderTotal['discount_value'], 2);
//make and send email to customer
$order = $mysql->get_array($mysql->query("first_name,email_address", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1"));
$msg = "Hi ".$order['first_name'].",

Thank you for placing your order with ".str_replace(array("http://", "www."), "", $_SERVER['SERVER_NAME'])."!";
//add bank info if this order is a bank transfer type
if($_POST['type'] == 'bank')
{
$method = 'bank';
$msg .= "\n\nPlease remember to use your order reference number ({$_SESSION['owner']}) when making your payment.\n\n****Our bank account details****\n";
$bank = $site->shop_settings('bankpaymentmethod_bankname,bankpaymentmethod_accountname,bankpaymentmethod_accountnumber,bankpaymentmethod_bsb');
$msg .= "Bank Name: {$bank['bankpaymentmethod_bankname']}\n";
$msg .= "Account Name: {$bank['bankpaymentmethod_accountname']}\n";
$msg .= "Account Number: {$bank['bankpaymentmethod_accountnumber']}\n";
$msg .= "BSB: {$bank['bankpaymentmethod_bsb']}\n\n";
$msg .= "Ref: {$_SESSION['owner']}\n";
$msg .= "Amount Due: $".$total;
}
//add cash on dev message if this order is cash on dev type
if($_POST['type'] == 'cod')
{
$method = 'cod';
$msg .= "\n\n** Please remember that you have selected to pay for this order using CASH ON DELIVERY method so we will collect the payment when the order is delivered. **";
}
//pick up from our store
if($_POST['type'] == 'pickstore')
{
$method = 'pickup';
$address = $site->shop_settings('shop_address');
$msg .= "\n\n** Please remember that you have selected to pay and pick up this order at our store using PICK UP FROM OUR STORE method. **\n\nOur store address:\n".stripslashes($address['shop_address']);
$msg .= "\nOrder total: $".$total;
}
//pay by phone method
if($_POST['type'] == 'phonePay')
{
$method = 'phonePay';
$num = $site->shop_settings('payByPhone_payment_method_phonenum');
$msg .= "\n\n** Please remember that you have selected to PAY BY PHONE so you need to call us ASAP. **\n\nOur phone number: ".stripslashes($num['payByPhone_payment_method_phonenum']);
$msg .= "\nOrder total: $".$total;
}
//rest of message
$msg .= "\n\nIf you have any questions regarding your order please do not hesitate to contact us via our site.

Once your order has been processed, we will send you an email to notify you.

Your order number: {$_SESSION['owner']}
If you would like more details for this order, please download the invoice from below URL.
http://".str_replace("www.", "", $_SERVER['SERVER_NAME'])."/inc/shop/invoice/?order=".$_SESSION['owner']."

Thank you\nhttp://".str_replace("http://", "", $_SERVER['SERVER_NAME']);
//overwrite the pickup message if there is one in the database email template
if($_POST['type'] == 'pickstore')
{
$hasTemplate = $mysql->query("body", "fs_email_templates",  "token = 'shop-order-pickup-confirm' LIMIT 1");
if($mysql->num_rows($hasTemplate) == 1)
{
$hasTemplate = $mysql->get_array($hasTemplate);
$msg = $hasTemplate['body'];
$msg = str_replace('{first_name}', $order['first_name'], $msg);
$msg = str_replace('{owner}', $_SESSION['owner'], $msg);
$msg = str_replace('{total}', $total, $msg);
}
}
//email customer
$admin = $site->shop_settings('admin_email');
emailWithPhpMailer($order['email_address'], 'Your Order @ '.$_SERVER['SERVER_NAME'], $msg, $admin['admin_email'], false);
//email the admin
$msg = "Hello,

A new shop order from {$order['first_name']} for $".$total." was placed at ".$_SERVER['SERVER_NAME'].", please process it ASAP.

----Order contents----
";
$cartQuery = $mysql->query("item_name,qty", "fs_shop_cart", "owner = '{$_SESSION['owner']}'");
while($cartContents = $mysql->get_array($cartQuery))
{
$msg .= stripslashes($cartContents['qty']." X ".$cartContents['item_name'])."\n";
}
$msg .= "------------\n\nIf you would like more details for this order, please download the invoice from below URL or view the order in your account.
http://".str_replace("www.", "", $_SERVER['SERVER_NAME'])."/inc/shop/invoice/?order=".$_SESSION['owner']."

Thank you\nhttp://".str_replace("http://", "", $_SERVER['SERVER_NAME']);
emailWithPhpMailer($admin['admin_email'], 'New Order @ '.$_SERVER['SERVER_NAME'], $msg, $admin['admin_email'], false);
//mark order as paid
$mysql->update("temp,payment_method", "n,$method", "fs_shop_orderdata", "owner = '{$_SESSION['owner']}' AND temp = 'y' LIMIT 1");
$mysql->update("temp", "n", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'y'");
//update stock
$query = $mysql->query("pid,`option`,qty", "fs_shop_cart", "owner = '{$_SESSION['owner']}' AND temp = 'n'");
while($cart = $mysql->get_array($query))
{
$site->update_stock($cart['pid'], $cart['option'], $cart['qty'], false);
}
//delete one_use discount code
$mysql->Delete("fs_shop_discountcodes", "code = '{$orderTotal['discount_code']}' AND one_use = 'y' LIMIT 1");
//redirect to thank you page
header("Location: /{$s['shop_page']}/$seo_pageTitle/Thankyou");
exit;
?>