<?php
/**********************************************************************
***********************************************************************/
/*
	item_codes table is used to store both multiply foreign codes and 
	sale kits definition.
*/
function update_jobq_request($selected_id, $list_code, $value_code, $value_name, $value_description, $inactive=0)
{
	$sql = "UPDATE ".TB_PREF."jobq_requests SET
	 	list_code = ".db_escape($list_code).",
                value_code = ".db_escape($value_code).",
	 	value_name = ".db_escape($value_name).",
                value_description = ".db_escape($value_description).",
	 	inactive = ".db_escape($inactive)."
        	WHERE id = ".db_escape($selected_id);

	db_query($sql,"List value could not be updated");
}

function add_jobq_request($jobq_id, $type, $description,  
                        $submitted_by, $parameters)
{
        $submitted_on = date('Y-m-d H:i:s');
        $status_id = 10;
    
        $sql = "INSERT INTO ".TB_PREF."jobq_requests
			(jobq_id, type, description,  
                        submitted_by, submitted_on, 
                        parameters,status_id) 
                VALUES( ".db_escape($jobq_id).",".db_escape($type).",".db_escape($description).",
                        ".db_escape($submitted_by).",".db_escape($submitted_on).", 
                        ".db_escape($parameters).",".db_escape($status_id).")";

	db_query($sql,"New jobq request could not be added");
        
        return db_insert_id();
}

function start_processing_jobq_request($id)
{
	$started_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."jobq_requests 
                SET
                        started_on = ".db_escape($started_on).",
                        status_id = ".db_escape(JQ_PROCESSING)." 
        	WHERE id = ".db_escape($id);

	db_query($sql,"Jobq requested could not be updated");
}

function complete_processing_jobq_request($id, $document_id = 0, $comments = '')
{
        $completed_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."jobq_requests 
                SET
                        completed_on = ".db_escape($completed_on).",
                        status_id = ".db_escape(JQ_COMPLETED).",
                        document_id = ".db_escape($document_id).",
                        comments = ".db_escape($comments)." 
        	WHERE id = ".db_escape($id);

	db_query($sql,"Jobq requested could not be updated");
}

function end_processing_jobq_request($id, $comments = '')
{
	$completed_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."jobq_requests 
                SET
                        completed_on = ".db_escape($completed_on).",
                        status_id = ".db_escape(JQ_ENDED).",
                        comments = ".db_escape($comments)." 
        	WHERE id = ".db_escape($id);

	db_query($sql,"Jobq requested could not be updated");
}

function error_complete_jobq_request($id, $comments = '')
{
	$completed_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."jobq_requests 
                SET
                        completed_on = ".db_escape($completed_on).",
                        status_id = ".db_escape(JQ_ERROR).",
                        comments = ".db_escape($comments)."  
        	WHERE id = ".db_escape($id);

	db_query($sql,"Jobq requested could not be updated");
}

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

	$result = db_query($sql,"Jobq request could not be retrieved");

	return db_fetch($result);
}

function view_jobq_request($id)
{
    
    $sql = "SELECT 
                    j.*,  
                    u.real_name submitted_user_name                    
            FROM ".TB_PREF."jobq_requests j,
                    ".TB_PREF."users u 
            WHERE j.id = ".db_escape($id);

        $result = db_query($sql,"Jobq request details could not be retrieved");

	return db_fetch($result);
}

function get_sql_for_jobq_requests($submitted_user_id = 0, $from='', $to='', $description = '')
{
    
    $sql = "SELECT 
                    j.id, 
                    j.type, 
                    j.description, 
                    u.real_name submitted_user_name, 
                    j.submitted_on,  
                    j.started_on, 
                    j.completed_on, 
                    j.document_id,
                    j.comments,
                    j.status_id                     
            FROM ".TB_PREF."jobq_requests j,
                    ".TB_PREF."users u 
            WHERE j.submitted_on >= '" . date2sql($from) . "'
		AND j.submitted_on <= '" . date2sql($to)."' 
                AND j.submitted_by = u.id ";
    
        if ($submitted_user_id > 0) {
		$sql .= " AND submitted_by = ". db_escape($submitted_user_id);
	}
        
        if ($description) {
		$sql .= " AND description LIKE ". db_escape("%$description%");
	}

	$sql .= " ORDER BY j.id DESC";
	return $sql;
}

?>