<?php



//--------------------------------------------------------------------------------------------------

function get_stock_item_edit_info($stock_id)
{
	$sql = "SELECT sm.*, material_cost + labour_cost + overhead_cost AS standard_cost, u.decimals, u.name unit_name 
		FROM ".TB_PREF."stock_master sm,".TB_PREF."item_units u
		WHERE stock_id = ".db_escape($stock_id)." AND 
                sm.units = u.abbr";
	$result = db_query($sql, "Stock item edit info could not be retrieved");

	return db_fetch($result);
}

//--------------------------------------------------------------------------------------

function update_stock_item_material_cost($stock_id, $material_cost)
{
	$sql = "UPDATE ".TB_PREF."stock_master 
            SET material_cost=".db_escape($material_cost)." 
            WHERE stock_id=".db_escape($stock_id);
    
	db_query($sql, "Could not update_stock_item_material_cost");
    
}

function get_stock_item_description_info($stock_id) {
    $sql = "SELECT editable, description 
            FROM ".TB_PREF."stock_master  
            WHERE stock_id = ".db_escape($stock_id);
    
	$result = db_query($sql, "get_stock_item_description_info");
        return db_fetch($result);
}

function get_item_base_unit_name($item_code) {
    $sql = "SELECT unt.name  
            FROM ".TB_PREF."stock_master sm
                JOIN ".TB_PREF."item_units unt  
                    ON sm.units = unt.abbr  
            WHERE sm.stock_id = ".db_escape($item_code);
	
	$result = db_query($sql, "get_item_base_unit_name");
	return db_result_value($result, 'name', '');
}

function add_non_stock_move($type, $stock_id, $trans_no, $location, $date_, $reference, $quantity, $std_cost, $person_id=0, $show_or_hide=1, 
                            $price=0, $discount_percent=0, $tran_unit="", $tran_qty=0, $conversion_factor=1, $task_id=0, $project_site_id=0, 
                            $reference_line_id=0, $item_description='', $project_id=0) {
    $date = date2sql($date_);
    
	$sql = "INSERT INTO ".TB_PREF."non_stock_moves (stock_id, trans_no, 
                    type, loc_code,
                    tran_date, person_id, 
                    reference, qty, 
                    standard_cost, visible, 
                    price, discount_percent, 
                    tran_unit, tran_qty, 
                    conversion_factor, task_id,
                    project_site_id, ref_line_id, 
                    item_description, project_id) 
            VALUES (".db_escape($stock_id).", ".db_escape($trans_no).", "
                    .db_escape($type).",".db_escape($location).", "
                    .db_escape($date).", ".db_escape($person_id).", "
                    .db_escape($reference).", ".db_escape($quantity).", "
                    .db_escape($std_cost).",".db_escape($show_or_hide).", "
                    .db_escape($price).", ".db_escape($discount_percent).","
                    .db_escape($tran_unit).", ".db_escape($tran_qty).","
                    .db_escape($conversion_factor).", ".db_escape($task_id).", "
                    .db_escape($project_site_id).", ".db_escape($reference_line_id).", "
                    .db_escape($item_description).", ".db_escape($project_id).")";

	db_query($sql, 'add_non_stock_move');
	return db_insert_id();
}
