Exemple #1
0
    $_REQUEST['action'] = '';
    $_REQUEST['old_action'] = '';
}
$getAction = !empty($_REQUEST['action']) ? $_REQUEST['action'] : '';
if (!empty($_REQUEST['cid'])) {
    $gCoupon = new CommerceVoucher($_REQUEST['cid']);
    $gCoupon->load(NULL, FALSE);
} else {
    $gCoupon = new CommerceVoucher();
}
switch ($getAction) {
    case 'delete':
        if ($gCoupon->isValid()) {
            // confirm first
            if (isset($_REQUEST["confirm"])) {
                $feedback['success'] = tra('Coupon deleted:') . ' ' . $gCoupon->getField('coupon_code');
                $gCoupon->expunge();
            } else {
                //				$gBitSystem->setBrowserTitle( tra('Confirm removal of') . ' ' . $gContent->getTitle()); // crossposting from Blog \''.'addblognamehere'.'\'' );
                $formHash['action'] = 'delete';
                $formHash['cid'] = $_REQUEST['cid'];
                $msgHash = array('label' => 'Delete Coupon', 'confirm_item' => $gCoupon->getField('coupon_code') . ' - ' . $gCoupon->getField('coupon_description'), 'warning' => tra('This cannot be undone!'));
                $gBitSystem->confirmDialog($formHash, $msgHash);
            }
        }
        break;
    case 'store':
        $_REQUEST['coupon_id'] = $_REQUEST['cid'];
        if ($gCoupon->store($_REQUEST)) {
            $feedback['success'] = tra('Coupon Saved:') . ' ' . $gCoupon->getField('coupon_code');
        } else {
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;
 }