<?php
/**********************************************************************
***********************************************************************/

function add_order_category_parameters($order_category_id, $parameter_id, $parameter_value)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
        
	$sql = "INSERT INTO ".TB_PREF."order_category_parameters             
                    ( order_category_id, parameter_id, parameter_value,                   
                        created_by, created_on )            

		VALUES (".db_escape($order_category_id).",".db_escape($parameter_id).",
                    ".db_escape($parameter_value).",                 
                    ".db_escape($id_user).", ".db_escape($datetime).")";
         
	db_query($sql, "could not add_order_category_parameters");
}


function update_order_category_parameters($selected_id, $parameter_id, $parameter_value)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
    
	$sql = "UPDATE ".TB_PREF."order_category_parameters SET                 
                    parameter_id =".db_escape($parameter_id).", 
                    parameter_value =".db_escape($parameter_value).",                    
                    updated_by =".db_escape($id_user).",    
                    updated_on =".db_escape($datetime)." 
		WHERE id=".db_escape($selected_id);

	db_query($sql, "could not update_order_category_parameters");
}


function get_all_order_category_parameters($order_category_id)
{
	$sql = "SELECT ocp.*,
                        lv.value_name parameter_name

                FROM ".TB_PREF."order_category_parameters ocp,
                    ".TB_PREF."list_values lv
            
                WHERE ocp.parameter_id = lv.id AND
                        ocp.order_category_id=".db_escape($order_category_id);
            
	return db_query($sql, "could not get_all_order_category_parameters");
}


function get_order_category_parameters($id)
{
	$sql = "SELECT * FROM ".TB_PREF."order_category_parameters WHERE id=".db_escape($id);

	$result = db_query($sql, "could not get_order_category");

	return db_fetch($result);
}

function get_order_category_tran_info($order_category_id)
{
	$sql = "SELECT prefix FROM ".TB_PREF."order_category_parameters WHERE id=".db_escape($order_category_id);

	$result = db_query($sql, "Could not get_order_category_tran_info");

	return db_fetch($result);
}

function get_next_order_category_number($tran_type, $order_category_id)
{
	$sql = "SELECT MAX(order_category_no) order_category_no 
            FROM ".TB_PREF."sales_order_category_transactions 
            WHERE tran_type =".db_escape($tran_type)." AND order_category_id=".db_escape($order_category_id);

	$result = db_query($sql, "Could not get_order_category_tran_info");

	$m = db_fetch($result);
    if($m){
        return $m[0] + 1;
    }
    else{
        return 1;
    }
}


function delete_order_category_parameters($selected_id)
{
	$sql="DELETE FROM ".TB_PREF."order_category_parameters WHERE id=".db_escape($selected_id);

	db_query($sql, "could not delete_order_category_parameters.");
}


function get_order_category_transaction_details($tran_type, $tran_type_no)
{
	$sql = "SELECT ocd.*, oc.name order_category_name  
            FROM ".TB_PREF."sales_order_category_transactions ocd 
                    JOIN ".TB_PREF."order_category_parameters oc ON 
                            ocd.order_category_id = oc.id 
            WHERE ocd.tran_type = ".db_escape($tran_type)." AND ocd.tran_type_no = ".db_escape($tran_type_no);

	$result = db_query($sql, "could not get_order_category_transaction_details");

	return db_fetch($result);
}

function add_order_category_transaction($tran_type, $tran_type_no, $order_category_id)
{
    $info = get_order_category_tran_info($order_category_id);
    $next_order_category_number = get_next_order_category_number($tran_type, $order_category_id);
    $str_order_category_number = str_pad($next_order_category_number, 5, "0", STR_PAD_LEFT);
    $order_category_reference = $info['prefix'].$str_order_category_number;
    
	$sql = "INSERT INTO ".TB_PREF."sales_order_category_transactions (
                            tran_type, tran_type_no, 
                            order_category_id, order_category_no, 
                            order_category_reference )            

            VALUES (".db_escape($tran_type).",".db_escape($tran_type_no).",
                    ".db_escape($order_category_id).", ".db_escape($next_order_category_number).", 
                    ".db_escape($order_category_reference).")";

	db_query($sql, "could not add_order_category_transaction");

}



?>
