function calculate_tax_deduction($amount, $od_amount, $method)
 {
     global $order;
     switch ($method) {
         case 'Standard':
             $ratio1 = olc_round($od_amount / $amount, 2);
             $tod_amount = 0;
             reset($order->info['tax_groups']);
             while (list($key, $value) = each($order->info['tax_groups'])) {
                 $tax_rate = olc_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 = olc_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 = olc_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
             $tax_desc = olc_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;
             //          $order->info['total'] -= $tod_amount;   //// ????? Strider
             break;
         default:
     }
     return $tod_amount;
 }
 function calculate_tax_deduction($amount, $od_amount, $method)
 {
     global $order;
     $coupon_query = olc_db_query("select coupon_code from " . TABLE_COUPONS . " where coupon_id = '" . $_SESSION['cc_id'] . APOS);
     if (olc_db_num_rows($coupon_query) != 0) {
         $coupon_result = olc_db_fetch_array($coupon_query);
         $coupon_get = olc_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'] . APOS);
         $get_result = olc_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 = olc_get_prid($products[$i]['id']);
                     $cc_query = olc_db_query("select products_tax_class_id from " . TABLE_PRODUCTS . " where products_id = '" . $t_prid . APOS);
                     $cc_result = olc_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 = olc_db_query("select products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . $products_id . "' and categories_id = '" . $cat_ids[$i] . APOS);
                             if (olc_db_num_rows($cat_query) != 0) {
                                 $valid_product = true;
                             }
                         }
                     }
                     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 ($valid_product) {
                     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 = olc_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tax_desc = olc_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 = olc_get_tax_rate($valid_array[$p]['products_tax_class'], $order->delivery['country']['id'], $order->delivery['zone_id']);
                             $tax_desc = olc_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 = olc_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
                         $tax_desc = olc_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_ADD_TAX . $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 = olc_get_tax_rate_from_desc(str_replace(TAX_ADD_TAX, "", $key));
                             $net = $tax_rate * $order->info['tax_groups'][$key];
                             if ($net > 0) {
                                 $god_amount = $od_amount * $tax_rate / (100 + $tax_rate);
                                 $tod_amount += $god_amount;
                                 $order->info['tax_groups'][$key] -= $god_amount;
                             }
                         }
                     }
                     $order->info['tax'] -= $tod_amount;
                 }
                 if ($get_result['coupon_type'] == 'P') {
                     $tod_amount = 0;
                     if ($method == 'Credit Note') {
                         $tax_desc = olc_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_ADD_TAX . $tax_desc] -= $tod_amount;
                     } else {
                         reset($order->info['tax_groups']);
                         while (list($key, $value) = each($order->info['tax_groups'])) {
                             $god_amout = 0;
                             $tax_rate = olc_get_tax_rate_from_desc(str_replace(TAX_ADD_TAX, "", $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['tax'] -= $tod_amount;
                 }
             }
         }
     }
     return $tod_amount;
 }