Esempio n. 1
0
 function calculate_tax_deduction($amount, $od_amount, $method)
 {
     global $customer_id, $order, $cc_id, $cart;
     $coupon_query = smn_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $cc_id . "'");
     if (smn_db_num_rows($coupon_query) != 0) {
         $coupon_result = smn_db_fetch_array($coupon_query);
         $coupon_get = smn_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 = smn_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 = $cart->get_products();
                 $valid_product = false;
                 for ($i = 0; $i < sizeof($products); $i++) {
                     $valid_product = false;
                     $t_prid = smn_get_prid($products[$i]['id']);
                     $cc_query = smn_db_query("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . "'");
                     $cc_result = smn_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']);
                         $my_path = smn_get_product_path($t_prid);
                         $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]) {
                                     $valid_product = true;
                                     continue 2;
                                 }
                             }
                         }
                     }
                     if ($valid_product) {
                         $price_excl_vat = $products[$i]['final_price'] * $products[$i]['quantity'];
                         $price_incl_vat = $this->product_price($t_prid);
                         $valid_array[] = array('product_id' => $t_prid, 'products_price' => $price_excl_vat, 'products_tax_class' => $cc_result['products_tax_class_id']);
                         $total_price += $price_excl_vat;
                     }
                 }
                 if (sizeof($valid_array) > 0) {
                     if ($get_result['coupon_type'] == 'P') {
                         $ratio = $get_result['coupon_amount'] / 100;
                     } else {
                         $ratio = $od_amount / $total_price;
                     }
                     if ($get_result['coupon_type'] == 'S') {
                         $ratio = 1;
                     }
                     if ($method == 'Credit Note') {
                         $tax_rate = smn_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tax_desc = smn_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         if ($get_result['coupon_type'] == 'P') {
                             $tod_amount = $od_amount / (100 + $tax_rate) * $tax_rate;
                         } else {
                             $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount / 100;
                         }
                         $order->info['tax_groups'][$tax_desc] -= $tod_amount;
                         $order->info['total'] -= $tod_amount;
                         $order->info['tax'] -= $tod_amount;
                     } else {
                         for ($p = 0; $p < sizeof($valid_array); $p++) {
                             $tax_rate = smn_get_tax_rate($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
                             $tax_desc = smn_get_tax_description($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
                             if ($tax_rate > 0) {
                                 $tod_amount = $valid_array[$p]['products_price'] * $tax_rate / 100 * $ratio;
                                 $order->info['tax_groups'][$tax_desc] -= $valid_array[$p]['products_price'] * $tax_rate / 100 * $ratio;
                                 $order->info['total'] -= $valid_array[$p]['products_price'] * $tax_rate / 100 * $ratio;
                                 $order->info['tax'] -= $valid_array[$p]['products_price'] * $tax_rate / 100 * $ratio;
                             }
                         }
                     }
                 }
                 //NO RESTRICTION--------------------------------
             } else {
                 if ($get_result['coupon_type'] == 'F') {
                     $tod_amount = 0;
                     if ($method == 'Credit Note') {
                         $tax_rate = smn_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tax_desc = smn_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tod_amount = $od_amount / (100 + $tax_rate) * $tax_rate;
                         $order->info['tax_groups'][$tax_desc] -= $tod_amount;
                     } else {
                         reset($order->info['tax_groups']);
                         while (list($key, $value) = each($order->info['tax_groups'])) {
                             $ratio1 = $od_amount / ($amount - $order->info['tax_groups'][$key]);
                             $tax_rate = smn_get_tax_rate_from_desc($key);
                             $net = $tax_rate * $order->info['tax_groups'][$key];
                             if ($net > 0) {
                                 $god_amount = $order->info['tax_groups'][$key] * $ratio1;
                                 $tod_amount += $god_amount;
                                 $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
                             }
                         }
                     }
                     $order->info['total'] -= $tod_amount;
                     $order->info['tax'] -= $tod_amount;
                 }
                 if ($get_result['coupon_type'] == 'P') {
                     $tod_amount = 0;
                     if ($method == 'Credit Note') {
                         $tax_desc = smn_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tod_amount = $order->info['tax_groups'][$tax_desc] * $od_amount / 100;
                         $order->info['tax_groups'][$tax_desc] -= $tod_amount;
                     } else {
                         reset($order->info['tax_groups']);
                         while (list($key, $value) = each($order->info['tax_groups'])) {
                             $god_amount = 0;
                             $tax_rate = smn_get_tax_rate_from_desc($key);
                             $net = $tax_rate * $order->info['tax_groups'][$key];
                             if ($net > 0) {
                                 $god_amount = $order->info['tax_groups'][$key] * $get_result['coupon_amount'] / 100;
                                 $tod_amount += $god_amount;
                                 $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
                             }
                         }
                     }
                     $order->info['total'] -= $tod_amount;
                     // have to modify total also
                     $order->info['tax'] -= $tod_amount;
                 }
             }
         }
     }
     return $tod_amount;
 }
Esempio n. 2
0
 function calculate_tax_deduction($amount, $od_amount, $method)
 {
     global $order;
     switch ($method) {
         case 'Standard':
             $ratio1 = $amount == 0 ? 0 : smn_round($od_amount / $amount, 2);
             $tod_amount = 0;
             reset($order->info['tax_groups']);
             while (list($key, $value) = each($order->info['tax_groups'])) {
                 $tax_rate = smn_get_tax_rate_from_desc($key);
                 $total_net += $tax_rate * $order->info['tax_groups'][$key];
             }
             if ($od_amount > $total_net) {
                 $od_amount = $total_net;
             }
             reset($order->info['tax_groups']);
             while (list($key, $value) = each($order->info['tax_groups'])) {
                 $tax_rate = smn_get_tax_rate_from_desc($key);
                 $net = $tax_rate * $order->info['tax_groups'][$key];
                 if ($net > 0) {
                     $god_amount = $order->info['tax_groups'][$key] * $ratio1;
                     $tod_amount += $god_amount;
                     $order->info['tax_groups'][$key] = $order->info['tax_groups'][$key] - $god_amount;
                 }
             }
             $order->info['tax'] -= $tod_amount;
             $order->info['total'] -= $tod_amount;
             break;
         case 'Credit Note':
             $tax_rate = smn_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
             $tax_desc = smn_get_tax_description($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
             $tod_amount = $this->deduction / (100 + $tax_rate) * $tax_rate;
             $order->info['tax_groups'][$tax_desc] -= $tod_amount;
             break;
         default:
     }
     return $tod_amount;
 }