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

global $path_to_root;

include_once($path_to_root . "/jobq/includes/db/jobq_requests_db.inc");


function create_jobq_request($type, $submitted_by, $parameters = "", $description = "")
{
	global $systypes_array;
	
	if($systypes_array[$type] == false || $systypes_array[$type] == ""){
            return array("success" => false, "message" => "Unknown system type");
    }
        
    $user = get_user($submitted_by);
    if(!$user){
        return array("success" => false, "message" => "Unknown user id");
    }

    $jobq_id = 0; // get user jobq id;
        
    $jobq_request_id = add_jobq_request($jobq_id, $type, $description,  
                                            $submitted_by, $parameters);
        
    if(!$jobq_request_id > 0){
        return array("success" => false, "message" => "Could not add jobq request");
    }

    submit_jobq_request($jobq_request_id);
        
    return array("success" => true, "message" => $jobq_request_id);
        
}

function submit_jobq_request($id)
{

    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    //curl_setopt($ch, CURLOPT_URL, "http://sunethrait.com/erp/modules/api/items/1/BAR/2014-07-04");
    //curl_setopt($ch, CURLOPT_URL, "http://sunethrait.com/erp/modules/api/process_job/1");
    curl_setopt($ch, CURLOPT_URL, "http://localhost/saw/modules/api/process_job/".$id);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $max_exe_time = 250; // time in milliseconds
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, $max_exe_time);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Iron/31.0.1700.0 Chrome/31.0.1700.0 Safari/537.36');

    // grab URL and pass it to the browser
    curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);
    
    //meta_forward($_SERVER['PHP_SELF'], "AddedID=2&Updated=1");
    
}

function process_jobq_request($job_id)
{
    global $path_to_root;
    
    $jqr = get_jobq_request($job_id);
    if(substr($jqr['started_on'],0,10) == "0000-00-00" || $jqr['alllow_rerun'] == 1){
        switch ($jqr['type']) {
            case ST_PAYROLL:
                include_once($path_to_root."/payroll/includes/process_jobq_payroll.inc");
                process_jobq_payroll($jqr);
                break;
            case ST_STOCK_COUNT:
                include_once($path_to_root."/inventory/includes/process_jobq_stock_count.inc");
                process_jobq_stock_count($jqr);
                break;
            default:
                break;
        }
    }
    else{
        
    }
    
}

register_shutdown_function( "fatal_handler" );

function fatal_handler() {
  $errfile = "unknown file";
  $errstr  = "shutdown";
  $errno   = E_CORE_ERROR;
  $errline = 0;

  $error = error_get_last();

  if( $error !== NULL) {
    $errno   = $error["type"];
    $errfile = $error["file"];
    $errline = $error["line"];
    $errstr  = $error["message"];

    //error_mail(format_error( $errno, $errstr, $errfile, $errline));
  }
}

function complete_jobq_request($jqr, $msg, $comments = "")
{
    if($msg["success"] == true){
        complete_processing_jobq_request($jqr['id'], $msg['trans_no'], $msg["message"]);
    }
    else{
        if($msg_cp["message"] == "Ended"){
            end_processing_jobq_request($jqr['id'], $msg['message']);
        }
        else{
            error_complete_jobq_request($jqr['id'], $msg['message']);
        }
        
    }
}

?>