// | available through the world-wide-web at the following url:			|
// | http://www.zen-cart.com/license/2_0.txt.							|
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to		|
// | license@zen-cart.com so we can mail you a copy immediately.		|
// +----------------------------------------------------------------------+
//	$Id$
//
define('MAX_DISPLAY_RESTRICT_ENTRIES', 5);
require 'includes/application_top.php';
$restrict_array = array();
$restrict_array[] = array('id' => 'Deny', 'text' => 'Deny');
$restrict_array[] = array('id' => 'Allow', 'text' => 'Allow');
if (!empty($_REQUEST['cid'])) {
    $gCoupon = new CommerceVoucher($_REQUEST['cid']);
    $gCoupon->load(NULL, FALSE);
} else {
    $gCoupon = new CommerceVoucher();
}
if (!empty($_REQUEST['action']) && $gCoupon->isValid()) {
    switch ($_REQUEST['action']) {
        case 'switch_status':
            $status = $gBitDb->getOne("SELECT coupon_restrict FROM " . TABLE_COUPON_RESTRICT . " WHERE restrict_id = ?", array($_GET['info']));
            $new_status = $status == 'N' ? 'Y' : 'N';
            $gBitDb->query("UPDATE " . TABLE_COUPON_RESTRICT . " SET coupon_restrict = ? WHERE restrict_id = ?", array($new_status, $_GET['info']));
            bit_redirect($_SERVER['SCRIPT_NAME'] . '?cid=' . $gCoupon->mCouponId);
            break;
        case 'Add':
            $gCoupon->storeRestriction($_REQUEST);
            bit_redirect($_SERVER['SCRIPT_NAME'] . '?cid=' . $gCoupon->mCouponId);
            break;
Exemple #2
0
 function calculate_deductions($order_total)
 {
     global $gBitDb, $gBitCustomer, $order;
     $tax_address = zen_get_tax_locations();
     $od_amount['total'] = 0;
     $od_amount['tax'] = 0;
     if ($_SESSION['cc_id']) {
         $coupon = new CommerceVoucher($_SESSION['cc_id']);
         if ($coupon->load() && $coupon->isRedeemable()) {
             if ($coupon->getField('coupon_minimum_order') <= $order_total) {
                 if ($coupon->getField('coupon_type') == 'S') {
                     if ($coupon->getField('restrict_to_shipping')) {
                         $shippingMethods = explode(',', $coupon->getField('restrict_to_shipping'));
                         if (in_array($order->info['shipping_method_code'], $shippingMethods)) {
                             $od_amount['total'] = $order->info['shipping_cost'];
                         }
                     } else {
                         $od_amount['total'] = $order->info['shipping_cost'];
                     }
                     $od_amount['type'] = 'S';
                 } else {
                     if ($coupon->getField('coupon_type') == 'P') {
                         // Max discount is a sum of percentages of valid products
                         $totalDiscount = 0;
                     } else {
                         $totalDiscount = $coupon->getField('coupon_amount') * ($order_total > 0);
                     }
                     $runningDiscount = 0;
                     $runningDiscountQuantity = 0;
                     foreach (array_keys($gBitCustomer->mCart->contents) as $productKey) {
                         $productHash = $gBitCustomer->mCart->getProductHash($productKey);
                         if ($coupon->getField('quantity_max')) {
                             if ($discountQuantity = $coupon->getField('quantity_max') - $runningDiscountQuantity) {
                                 if ($discountQuantity > $productHash['products_quantity']) {
                                     $discountQuantity = $productHash['products_quantity'];
                                 }
                             }
                         } else {
                             $discountQuantity = $productHash['products_quantity'];
                         }
                         if ($productHash && $discountQuantity && $this->is_product_valid($productHash, $_SESSION['cc_id'])) {
                             // _P_ercentage discount
                             if ($coupon->getField('coupon_type') == 'P') {
                                 $runningDiscountQuantity += $discountQuantity;
                                 $itemDiscount = round($productHash['final_price'] * $discountQuantity * ($coupon->getField('coupon_amount') / 100), 2);
                                 $totalDiscount += $itemDiscount;
                                 if ($runningDiscount < $totalDiscount) {
                                     $runningDiscount += $itemDiscount;
                                 }
                                 if ($runningDiscount > $totalDiscount) {
                                     $runningDiscount = $totalDiscount;
                                     $itemDiscount = 0;
                                 }
                                 switch ($this->calculate_tax) {
                                     case 'Credit Note':
                                         $tax_rate = zen_get_tax_rate($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                                         $tax_desc = zen_get_tax_description($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                                         $od_amount[$tax_desc] = $runningDiscount / 100 * $tax_rate;
                                         $od_amount['tax'] += $od_amount[$tax_desc];
                                         break;
                                     case 'Standard':
                                         $ratio = $runningDiscount / $this->get_order_total();
                                         $tax_rate = zen_get_tax_rate($productHash['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                                         $tax_desc = zen_get_tax_description($productHash['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                                         if ($tax_rate > 0) {
                                             if (empty($od_amount[$tax_desc])) {
                                                 $od_amount[$tax_desc] = 0;
                                             }
                                             $od_amount[$tax_desc] += $productHash['final_price'] * $discountQuantity * $tax_rate / 100 * $ratio;
                                             $od_amount['tax'] += $od_amount[$tax_desc];
                                         }
                                         break;
                                 }
                                 // _F_ixed discount
                             } elseif ($coupon->getField('coupon_type') == 'F') {
                                 switch ($this->calculate_tax) {
                                     case 'Credit Note':
                                         $tax_rate = zen_get_tax_rate($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                                         $tax_desc = zen_get_tax_description($this->tax_class, $tax_address['country_id'], $tax_address['zone_id']);
                                         $od_amount[$tax_desc] = $runningDiscount / 100 * $tax_rate;
                                         $od_amount['tax'] += $od_amount[$tax_desc];
                                         break;
                                     case 'Standard':
                                         $ratio = $runningDiscount / $this->get_order_total();
                                         $t_prid = zen_get_prid($productKey);
                                         $cc_result = $gBitDb->query("select `products_tax_class_id` from " . TABLE_PRODUCTS . " where `products_id` = ?", array($t_prid));
                                         if ($this->is_product_valid($productHash, $_SESSION['cc_id'])) {
                                             if ($runningDiscount < $totalDiscount) {
                                                 $runningDiscount += $productHash['final_price'] * $discountQuantity;
                                             }
                                             if ($runningDiscount > $totalDiscount) {
                                                 $runningDiscount = $totalDiscount;
                                             }
                                             $tax_rate = zen_get_tax_rate($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                                             $tax_desc = zen_get_tax_description($cc_result->fields['products_tax_class_id'], $tax_address['country_id'], $tax_address['zone_id']);
                                             if ($tax_rate > 0) {
                                                 if (empty($od_amount[$tax_desc])) {
                                                     $od_amount[$tax_desc] = 0;
                                                 }
                                                 $od_amount[$tax_desc] += $productHash['final_price'] * $discountQuantity * $tax_rate / 100 * $ratio;
                                                 $od_amount['tax'] += $od_amount[$tax_desc];
                                             }
                                         }
                                         break;
                                 }
                             }
                         }
                     }
                     $od_amount['total'] = $runningDiscount;
                     if ($od_amount['total'] > $order_total) {
                         $od_amount['total'] = $order_total;
                     }
                 }
             }
         }
     }
     return $od_amount;
 }