<?php
/**********************************************************************
***********************************************************************/
function add_item_category($description, $tax_type_id, $sales_account, 
	$cogs_account, $coc_account, $cost_account, $inventory_account, $adjustment_account, $assembly_account, 
	$units, $mb_flag, $dim1, $dim2, $no_sale)
{
	$id_user = $_SESSION['wa_current_user']->user;
	$datetime = date('Y-m-d H:i:s');

	$sql = "INSERT INTO ".TB_PREF."stock_category (description, dflt_tax_type,
			dflt_units, dflt_mb_flag, dflt_sales_act, 
                        dflt_cogs_act, dflt_coc_act, dflt_cost_act,
			dflt_inventory_act, dflt_adjustment_act, dflt_assembly_act, 
			dflt_dim1, dflt_dim2, dflt_no_sale,created_by,created_on)
		VALUES ("
		.db_escape($description).","
		.db_escape($tax_type_id).","
		.db_escape($units).","
		.db_escape($mb_flag).","
		.db_escape($sales_account).","
		.db_escape($cogs_account).","
		.db_escape($coc_account).","
		.db_escape($cost_account).","
		.db_escape($inventory_account).","
		.db_escape($adjustment_account).","
		.db_escape($assembly_account).","
		.db_escape($dim1).","
		.db_escape($dim2).","
		.db_escape($no_sale).","
                .db_escape($id_user).","
                .db_escape($datetime)
                .")";

	db_query($sql,"an item category could not be added");
}

function update_item_category($id, $description, $tax_type_id, 
	$sales_account, $cogs_account, $coc_account, $cost_account, $inventory_account, $adjustment_account, 
	$assembly_account, $units, $mb_flag, $dim1, $dim2, $no_sale)

{
		$id_user = $_SESSION['wa_current_user']->user;
	$datetime = date('Y-m-d H:i:s');
	
	$sql = "UPDATE ".TB_PREF."stock_category SET "
		."description = ".db_escape($description).","
		."dflt_tax_type = ".db_escape($tax_type_id).","
		."dflt_units = ".db_escape($units).","
		."dflt_mb_flag = ".db_escape($mb_flag).","
		."dflt_sales_act = ".db_escape($sales_account).","
		."dflt_cogs_act = ".db_escape($cogs_account).","
		."dflt_coc_act = ".db_escape($coc_account).","
		."dflt_cost_act = ".db_escape($cost_account).","
		."dflt_inventory_act = ".db_escape($inventory_account).","
		."dflt_adjustment_act = ".db_escape($adjustment_account).","
		."dflt_assembly_act = ".db_escape($assembly_account).","
		."dflt_dim1 = ".db_escape($dim1).","
		."dflt_dim2 = ".db_escape($dim2).","
                ."updated_by = ".  db_escape($id_user).","
                ."updated_on = ".  db_escape($datetime).","
		."dflt_no_sale = ".db_escape($no_sale)
        ."WHERE category_id = ".db_escape($id);

	db_query($sql,"an item category could not be updated");
}

function delete_item_category($id)
{
	$sql="DELETE FROM ".TB_PREF."stock_category WHERE category_id=".db_escape($id);

	db_query($sql,"an item category could not be deleted");
}

function get_item_categories($show_inactive)
{
	$sql = "SELECT c.*, t.name as tax_name FROM ".TB_PREF."stock_category c, "
		.TB_PREF."item_tax_types t WHERE c.dflt_tax_type=t.id";
	if (!$show_inactive)
		$sql .= " AND !c.inactive";
	$sql .= " ORDER by description";

	return db_query($sql, "could not get stock categories");
}

function get_item_category($id)
{
	$sql="SELECT * FROM ".TB_PREF."stock_category WHERE category_id=".db_escape($id);

	$result = db_query($sql,"an item category could not be retrieved");

	return db_fetch($result);
}

function get_category_name($id)
{
	$sql = "SELECT description FROM ".TB_PREF."stock_category WHERE category_id=".db_escape($id);

	$result = db_query($sql, "could not get sales type");

	$row = db_fetch_row($result);
        if (isset($row[0]) && strlen($row[0]) > 0) {
            return $row[0];
        } else {
            return '';
        }
}

?>