<?php

function add_stock_uom($stock_id, $uom) {
    $sql = "REPLACE INTO `" . TB_PREF . "stock_uom` SET  `stock_id`=" . db_escape($stock_id) . ", "
            . "`uom`=" . db_escape($uom);
    return db_query($sql, "cannot set stock UOM");
}

function get_stock_uom($stock_id) {
    $sql = "select * from `" . TB_PREF . "stock_uom` where  `stock_id`=" . db_escape($stock_id);
    $result = db_query($sql, "could query stock UOM");

    return db_fetch($result);
}

function add_stock_trans($order_items, $location, $date_, $reference) {
    $date = date2sql($date_);
    $trans_no = get_next_trans_no(ST_LOCTRANSFER);

    foreach ($order_items as $line_item) {

        $sql = "INSERT INTO " . TB_PREF . "stock_control (stock_id, type, loc_code,
		trans_date, reference, qty, memo, trans_no) VALUES ("
                . db_escape($line_item->stock_id) . ", " . db_escape($line_item->trans_type) . ", "
                . db_escape($location) . ", '$date', "
                . db_escape($reference) . ", "
                . db_escape($line_item->quantity) . ", " . db_escape($line_item->memo) . ", " . db_escape($trans_no). ")";

        db_query($sql, "The stock transaction record cannot be inserted");
    }

    return db_insert_id();
}

function get_inventory_location($loc_code) {
    $sql = "SELECT * FROM " . TB_PREF . "locations
		WHERE loc_code=" . db_escape($loc_code);

    $result = db_query($sql, "could not retreive the location name for $loc_code");

    if (db_num_rows($result) == 1) {
        $row = db_fetch_assoc($result);
        return $row;
    }

    return FALSE;
}
function get_item_tax($items_id, $tax_group) {
    $tax_multiplier = 0;
        $ret_tax_array = get_tax_group_items_as_array($tax_group);

        $fully_exempt = true;
        foreach ($ret_tax_array as $k => $t) {
            if ($t['rate'] !== null)
                $fully_exempt = false;
            $ret_tax_array[$k]['Net'] = 0;
        }

        $ret_tax_array['exempt'] = array('Value' => 0, 'Net' => 0, 'rate' => null, 'tax_type_id' => '');
        // loop for all items
        $item_taxes = get_taxes_for_item($items_id, $ret_tax_array);
        if ($item_taxes == null || $fully_exempt) {
            
        } else {
            $tax_multiplier = 0;
            foreach ($item_taxes as $taxitem) {
                $tax_multiplier += $taxitem['rate'];
            }
        }
        return $tax_multiplier;
    }