📄 ot_coupon.php
字号:
<?php
/* -----------------------------------------------------------------------------------------
$Id: ot_coupon.php,v 1.1 2004/02/17 21:13:27 oldpa Exp $
TWE-Commerce - community made shopping
http://www.oldpa.com.tw
Copyright (c) 2003 TWE-Commerce
-----------------------------------------------------------------------------------------
based on:
(c) 2000-2001 The Exchange Project (earlier name of osCommerce)
(c) 2002-2003 osCommerce(ot_coupon.php,v 1.1.2.37.3); www.oscommerce.com
(c) 2003 xt-commerce www.xt-commerce.com
Released under the GNU General Public License
-----------------------------------------------------------------------------------------
Third Party contributions:
Credit Class/Gift Vouchers/Discount Coupons (Version 5.10)
http://www.oscommerce.com/community/contributions,282
Copyright (c) Strider | Strider@oscworks.com
Copyright (c Nick Stanko of UkiDev.com, nick@ukidev.com
Copyright (c) Andre ambidex@gmx.net
Copyright (c) 2001,2002 Ian C Wilson http://www.phesis.org
Released under the GNU General Public License
---------------------------------------------------------------------------------------*/
class ot_coupon {
var $title, $output;
function ot_coupon() {
$this->code = 'ot_coupon';
$this->header = MODULE_ORDER_TOTAL_COUPON_HEADER;
$this->title = MODULE_ORDER_TOTAL_COUPON_TITLE;
$this->description = MODULE_ORDER_TOTAL_COUPON_DESCRIPTION;
$this->user_prompt = '';
$this->enabled = MODULE_ORDER_TOTAL_COUPON_STATUS;
$this->sort_order = MODULE_ORDER_TOTAL_COUPON_SORT_ORDER;
$this->include_shipping = MODULE_ORDER_TOTAL_COUPON_INC_SHIPPING;
$this->include_tax = MODULE_ORDER_TOTAL_COUPON_INC_TAX;
$this->calculate_tax = MODULE_ORDER_TOTAL_COUPON_CALC_TAX;
$this->tax_class = MODULE_ORDER_TOTAL_COUPON_TAX_CLASS;
$this->credit_class = true;
$this->output = array();
}
function process() {
global $order, $currencies;
$order_total=$this->get_order_total();
$od_amount = $this->calculate_credit($order_total);
$tod_amount = 0.0; //Fred
$this->deduction = $od_amount;
if ($this->calculate_tax != 'None') { //Fred - changed from 'none' to 'None'!
$tod_amount = $this->calculate_tax_deduction($order_total, $this->deduction, $this->calculate_tax);
}
if ($od_amount > 0) {
$order->info['total'] = $order->info['total'] - $od_amount;
$this->output[] = array('title' => $this->title . ':' . $this->coupon_code .':','text' => '<b>-' . $currencies->format($od_amount) . '</b>', 'value' => $od_amount); //Fred added hyphen
}
}
function selection_test() {
return false;
}
function pre_confirmation_check($order_total) {
return $this->calculate_credit($order_total);
}
function use_credit_amount() {
return $output_string;
}
function credit_selection() {
global $currencies;
/*$selection_string = '';
$selection_string .= '<tr>' . "\n";
$selection_string .= ' <td width="10">' . twe_draw_separator('pixel_trans.gif', '10', '1') .'</td>';
$selection_string .= ' <td nowrap class="main">' . "\n";
$selection_string .= TEXT_ENTER_COUPON_CODE . '</td>';
$selection_string .= ' <td align="right">'. twe_draw_input_field('gv_redeem_code').'</td>';
$selection_string .= ' <td width="10">' . twe_draw_separator('pixel_trans.gif', '10', '1') . '</td>';
$selection_string .= '</tr>' . "\n";
*/
return $selection_string;
}
function collect_posts() {
global $currencies;
if ($_POST['gv_redeem_code']) {
// get some info from the coupon table
$coupon_query=twe_db_query("select coupon_id, coupon_amount, coupon_type, coupon_minimum_order,uses_per_coupon, uses_per_user, restrict_to_products,restrict_to_categories from " . TABLE_COUPONS . " where coupon_code='".$_POST['gv_redeem_code']."' and coupon_active='Y'");
$coupon_result=twe_db_fetch_array($coupon_query);
// SS ?
if ($coupon_result['coupon_type'] != 'G') {
if (twe_db_num_rows($coupon_query)==0) {
twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_INVALID_REDEEM_COUPON), 'SSL'));
}
$date_query=twe_db_query("select coupon_start_date from " . TABLE_COUPONS . " where coupon_start_date <= now() and coupon_code='".$_POST['gv_redeem_code']."'");
if (twe_db_num_rows($date_query)==0) {
twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_STARTDATE_COUPON), 'SSL'));
}
$date_query=twe_db_query("select coupon_expire_date from " . TABLE_COUPONS . " where coupon_expire_date >= now() and coupon_code='".$_POST['gv_redeem_code']."'");
if (twe_db_num_rows($date_query)==0) {
twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_FINISDATE_COUPON), 'SSL'));
}
$coupon_count = twe_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $coupon_result['coupon_id']."'");
$coupon_count_customer = twe_db_query("select coupon_id from " . TABLE_COUPON_REDEEM_TRACK . " where coupon_id = '" . $coupon_result['coupon_id']."' and customer_id = '" . $_SESSION['customer_id'] . "'");
if (twe_db_num_rows($coupon_count)>=$coupon_result['uses_per_coupon'] && $coupon_result['uses_per_coupon'] > 0) {
twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_USES_COUPON . $coupon_result['uses_per_coupon'] . TIMES ), 'SSL'));
}
if (twe_db_num_rows($coupon_count_customer)>=$coupon_result['uses_per_user'] && $coupon_result['uses_per_user'] > 0) {
twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_INVALID_USES_USER_COUPON . $coupon_result['uses_per_user'] . TIMES ), 'SSL'));
}
if ($coupon_result['coupon_type']=='S') {
$coupon_amount = $order->info['shipping_cost'];
} else {
$coupon_amount = $currencies->format($coupon_result['coupon_amount']) . ' ';
}
if ($coupon_result['coupon_type']=='P') $coupon_amount = $coupon_result['coupon_amount'] . '% ';
if ($coupon_result['coupon_minimum_order']>0) $coupon_amount .= 'on orders greater than ' . $coupon_result['coupon_minimum_order'];
$_SESSION['cc_id'] = $coupon_result['coupon_id']; //Fred ADDED, set the global and session variable
}
if ($_POST['submit_redeem_coupon_x'] && !$_POST['gv_redeem_code']) twe_redirect(twe_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . urlencode(ERROR_NO_REDEEM_CODE), 'SSL'));
}
}
function calculate_credit($amount) {
global $order;
$od_amount = 0;
if (isset($_SESSION['cc_id']) ) {
$coupon_query = twe_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $_SESSION['cc_id'] . "'");
if (twe_db_num_rows($coupon_query) !=0 ) {
$coupon_result = twe_db_fetch_array($coupon_query);
$this->coupon_code = $coupon_result['coupon_code'];
$coupon_get = twe_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS ." where coupon_code = '". $coupon_result['coupon_code'] . "'");
$get_result = twe_db_fetch_array($coupon_get);
$c_deduct = $get_result['coupon_amount'];
if ($get_result['coupon_type']=='S') $c_deduct = $order->info['shipping_cost'];
if ($get_result['coupon_minimum_order'] <= $this->get_order_total()) {
if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
for ($i=0; $i<sizeof($order->products); $i++) {
if ($get_result['restrict_to_products']) {
$pr_ids = split("[,]", $get_result['restrict_to_products']);
for ($ii = 0; $ii < count($pr_ids); $ii++) {
if ($pr_ids[$ii] == twe_get_prid($order->products[$i]['id'])) {
if ($get_result['coupon_type'] == 'P') {
/* Fixes to Gift Voucher module 5.03
=================================
Submitted by Rob Cote, robc@traininghott.com
original code: $od_amount = round($amount*10)/10*$c_deduct/100;
$pr_c = $order->products[$i]['final_price']*$order->products[$i]['qty'];
$pod_amount = round($pr_c*10)/10*$c_deduct/100;
*/
//$pr_c = $order->products[$i]['final_price']*$order->products[$i]['qty'];
$pr_c = $this->product_price($pr_ids[$ii]); //Fred 2003-10-28, fix for the row above, otherwise the discount is calc based on price excl VAT!
$pod_amount = round($pr_c*10)/10*$c_deduct/100;
$od_amount = $od_amount + $pod_amount;
} else {
$od_amount = $c_deduct;
}
}
}
} else {
$cat_ids = split("[,]", $get_result['restrict_to_categories']);
for ($i=0; $i<sizeof($order->products); $i++) {
$my_path = twe_get_product_path(twe_get_prid($order->products[$i]['id']));
$sub_cat_ids = split("[_]", $my_path);
for ($iii = 0; $iii < count($sub_cat_ids); $iii++) {
for ($ii = 0; $ii < count($cat_ids); $ii++) {
if ($sub_cat_ids[$iii] == $cat_ids[$ii]) {
if ($get_result['coupon_type'] == 'P') {
/* Category Restriction Fix to Gift Voucher module 5.04
Date: August 3, 2003
=================================
Nick Stanko of UkiDev.com, nick@ukidev.com
original code:
$od_amount = round($amount*10)/10*$c_deduct/100;
$pr_c = $order->products[$i]['final_price']*$order->products[$i]['qty'];
$pod_amount = round($pr_c*10)/10*$c_deduct/100;
*/
//$od_amount = round($amount*10)/10*$c_deduct/100;
//$pr_c = $order->products[$i]['final_price']*$order->products[$i]['qty'];
$pr_c = $this->product_price(twe_get_prid($order->products[$i]['id'])); //Fred 2003-10-28, fix for the row above, otherwise the discount is calc based on price excl VAT!
$pod_amount = round($pr_c*10)/10*$c_deduct/100;
$od_amount = $od_amount + $pod_amount;
} else {
$od_amount = $c_deduct;
}
}
}
}
}
}
}
} else {
if ($get_result['coupon_type'] !='P') {
$od_amount = $c_deduct;
} else {
$od_amount = $amount * $get_result['coupon_amount'] / 100;
}
}
}
}
if ($od_amount>$amount) $od_amount = $amount;
}
return $od_amount;
}
function calculate_tax_deduction($amount, $od_amount, $method) {
global $order;
$coupon_query = twe_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $_SESSION['cc_id'] . "'");
if (twe_db_num_rows($coupon_query) !=0 ) {
$coupon_result = twe_db_fetch_array($coupon_query);
$coupon_get = twe_db_query("select coupon_amount, coupon_minimum_order, restrict_to_products, restrict_to_categories, coupon_type from " . TABLE_COUPONS . " where coupon_code = '". $coupon_result['coupon_code'] . "'");
$get_result = twe_db_fetch_array($coupon_get);
if ($get_result['coupon_type'] != 'S') {
//RESTRICTION--------------------------------
if ($get_result['restrict_to_products'] || $get_result['restrict_to_categories']) {
// What to do here.
// Loop through all products and build a list of all product_ids, price, tax class
// at the same time create total net amount.
// then
// for percentage discounts. simply reduce tax group per product by discount percentage
// or
// for fixed payment amount
// calculate ratio based on total net
// for each product reduce tax group per product by ratio amount.
$products = $_SESSION['cart']->get_products();
$valid_product = false;
for ($i=0; $i<sizeof($products); $i++) {
$valid_product = false;
$t_prid = twe_get_prid($products[$i]['id']);
$cc_query = twe_db_query("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
$cc_result = twe_db_fetch_array($cc_query);
if ($get_result['restrict_to_products']) {
$pr_ids = split("[,]", $get_result['restrict_to_products']);
for ($p = 0; $p < sizeof($pr_ids); $p++) {
if ($pr_ids[$p] == $t_prid) $valid_product = true;
}
}
if ($get_result['restrict_to_categories']) {
$cat_ids = split("[,]", $get_result['restrict_to_categories']);
for ($c = 0; $c < sizeof($cat_ids); $c++) {
$cat_query = twe_db_query("select products_id from products_to_categories where products_id = '" . $products_id . "' and categories_id = '" . $cat_ids[$i] . "'");
if (twe_db_num_rows($cat_query) !=0 ) $valid_product = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -