function recalc_total($target_oID)
{
    global $db;
    global $currencies;
    $ot_subtotal = 0;
    $ot_tax = 0;
    $ot_total = 0;
    $products = $db->Execute("SELECT * FROM " . TABLE_ORDERS_PRODUCTS . "\n                            WHERE orders_id = '" . $target_oID . "'");
    // recalculate subtotal and tax from products in order
    while (!$products->EOF) {
        $this_subtotal = 0;
        $this_tax = 0;
        $this_subtotal = $products->fields['final_price'] * $products->fields['products_quantity'];
        $ot_subtotal += $this_subtotal;
        // not everyone charges tax, so we check to see if it exists first
        if ($products->fields['products_tax'] > 0) {
            $this_tax = $this_subtotal * ($products->fields['products_tax'] / 100);
            $ot_tax += $this_tax;
        }
        $products->MoveNext();
    }
    // apply new subtotal and tax values to the record
    $db->Execute("UPDATE " . TABLE_ORDERS_TOTAL . " SET\n                text = '" . $currencies->format($ot_subtotal) . "',\n                value = '" . $ot_subtotal . "'\n                WHERE orders_id = '" . $target_oID . "'\n                AND class = 'ot_subtotal'");
    if ($ot_tax > 0) {
        $db->Execute("UPDATE " . TABLE_ORDERS_TOTAL . " SET\n                  text = '" . $currencies->format($ot_tax) . "',\n                  value = '" . $ot_tax . "'\n                  WHERE orders_id = '" . $target_oID . "'\n                  AND class = 'ot_tax'");
    }
    // add up all the records for the order (except ot_total)
    $all_totals = $db->Execute("SELECT * FROM " . TABLE_ORDERS_TOTAL . "\n                              WHERE orders_id = '" . $target_oID . "'\n                              ORDER BY sort_order ASC");
    while (!$all_totals->EOF) {
        $orders_total_id = $all_totals->fields['orders_total_id'];
        if ($all_totals->fields['class'] != 'ot_total') {
            if (is_discount_module($all_totals->fields['class'])) {
                $ot_total -= $all_totals->fields['value'];
            } else {
                $ot_total += $all_totals->fields['value'];
            }
        }
        $all_totals->MoveNext();
    }
    // apply new total value
    $db->Execute("UPDATE " . TABLE_ORDERS_TOTAL . " SET\n                text = '" . $currencies->format($ot_total) . "',\n                value = '" . $ot_total . "'\n                WHERE orders_id = '" . $target_oID . "'\n                AND class = 'ot_total'");
    $db->Execute("UPDATE " . TABLE_ORDERS . " SET\n                order_tax = '" . $ot_tax . "',\n                order_total = '" . $ot_total . "'\n                WHERE orders_id = '" . $target_oID . "'");
    //return $ot_total;
}
         $sort_order++;
         // add values to running_total
         if ($ot_class == "ot_subtotal") {
             $running_total += $ot_value;
         } elseif ($ot_class == "ot_tax") {
             $running_total += $ot_value;
         } elseif (is_discount_module($ot_class)) {
             $running_total -= $ot_value;
         } elseif ($ot_class == "ot_total") {
             $ot_value = $running_total;
             $db->Execute("update " . TABLE_ORDERS . " set\n                            order_total = '" . $ot_value . "'\n                            where orders_id = '" . $oID . "'");
         } else {
             $running_total += $ot_value;
         }
         // format the text version of the amount modified per http://thecartblog.com/2009/12/21/zen-cart-edit-orders-and-my-discounting-mods
         if (is_discount_module($ot_class)) {
             $ot_text = "-" . $currencies->format($ot_value);
         } else {
             $ot_text = $currencies->format($ot_value);
         }
         if ($ot_total_id > 0) {
             $query = "UPDATE " . TABLE_ORDERS_TOTAL . " SET\n                        title = '" . $ot_title . "',\n                        text = '" . $ot_text . "',\n                        value = '" . $ot_value . "',\n                        sort_order = '" . $sort_order . "'\n                        WHERE orders_total_id = '" . $ot_total_id . "'";
             $db->Execute($query);
         } else {
             $query = "INSERT INTO " . TABLE_ORDERS_TOTAL . " SET\n                        orders_id = '" . $oID . "',\n                        title = '" . $ot_title . "',\n                        text = '" . $ot_text . "',\n                        value = '" . $ot_value . "',\n                        class = '" . $ot_class . "',\n                        sort_order = '" . $sort_order . "'";
             $db->Execute($query);
         }
     } elseif ($ot_total_id > 0) {
         zen_db_delete(TABLE_ORDERS_TOTAL, "orders_total_id = '" . $ot_total_id . "'");
     }
 }