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


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

function update_import_item_record($id, $message)
{
    $sql = "UPDATE import_items SET message = ".db_escape($message)." WHERE id = ".$id;
    db_query($sql);
}

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


function get_category_id($category_name)
{
    $sql = "SELECT IFNULL((SELECT category_id FROM ".TB_PREF."stock_category 
                                WHERE description =".db_escape($category_name)."), 0) as id";
    
    $result = db_query($sql);
    $category_id = db_fetch($result);
    
    if($category_id['id'] == 0){
        $msg = "Unknown item category";
        return array("passed" => false, "message" => $msg);
    }
    else {
           return array("passed" => true, "message" => $category_id['id']);
    }
}

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


function get_item_type_code($item_type_name)
{
    $item_type_name = strtoupper($item_type_name);
    
    if($item_type_name == "STOCK"){
            return array("passed" => true, "message" => "B");
    }
    elseif($item_type_name == "NON-STOCK"){
            return array("passed" => true, "message" => "D");
    }
    elseif($item_type_name == "MANUFACTURED"){
            return array("passed" => true, "message" => "M");
    }
    else{
            return array("passed" => false, "message" => "Unknown item type");
    }
}



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


function get_stock_control_method_code($stock_control_method_name)
{
    if($stock_control_method_name == "Lot"){
            return array("passed" => true, "message" => SKCM_LOTS);
    }
    elseif($stock_control_method_name == "Serial"){
            return array("passed" => true, "message" => SKCM_SERIAL_NOS);
    }
    elseif($stock_control_method_name == ""){
            return array("passed" => true, "message" => SKCM_NORMAL);
    }
    else{
            return array("passed" => false, "message" => "Unknown stock control method");
    }
}


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


function get_item_tax_type_id($item_tax_type_name)
{
    $sql = "SELECT IFNULL((SELECT id FROM ".TB_PREF."item_tax_types 
                                WHERE name =".db_escape($item_tax_type_name)."), 0) as id";
    
    $result = db_query($sql);
    $item_tax_type_id = db_fetch($result);
    
    if($item_tax_type_id['id'] == 0){
        $msg = "Unknown item tax type";
        return array("passed" => false, "message" => $msg);
    }
    else {
           return array("passed" => true, "message" => $item_tax_type_id['id']);
    }
}

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


function get_units_code($units_name)
{
    $sql = "SELECT IFNULL((SELECT abbr FROM ".TB_PREF."item_units 
                                WHERE name =".db_escape($units_name)."), '') as units_code";
    
    $result = db_query($sql);
    $units_code = db_fetch($result);
    
    if(strlen($units_code['units_code']) > 0){
        return array("passed" => true, "message" => $units_code['units_code']);
    }
    else{
        $msg = "Unknown item unit";
        return array("passed" => false, "message" => $msg);
    }

}

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



function import_items($import=false)
{
    $import_error = 0;
    $data_array = array();
    $sql="SELECT * FROM import_items";
    $result = db_query($sql,"Import item records could not be retrieved");
    while ($myrow = db_fetch($result))
    {
        $data_array[] = $myrow;
    }

    foreach ($data_array as $myrow){
        $data = array();
        $visid = validate_import_stock_item_data($myrow, $data);
        if($visid['success'] !==  true){
            update_import_item_record($myrow['id'],$visid['message']);
            continue;
        }
        
        $sql = "SELECT IFNULL((SELECT stock_id FROM ".TB_PREF."stock_master 
                            WHERE stock_id =".db_escape($data['NewStockID'])."), '') as NewStockID";    
        $result = db_query($sql);

        $item_code = db_fetch($result);
        if($item_code && strlen($item_code['NewStockID']) > 0){
            $data['new_item'] = 0;
        }
        else{
            $data['new_item'] = 1;
        }
        
        $vsid = validate_stock_item_data($data);
        if($vsid['success'] !== true){
            update_import_item_record($data['id'],$vsid['message']);
            continue;
        }
        
        if($import){
            $si = save_item($data);
            update_import_item_record($data['id'],$si['message']);
        }
        //break;
    }
}

function run_process()
{
    //include_once("../admin/includes/process_job.inc");
    //$ee = shell_exec("../admin/includes/process_job.inc");
    //exec('php ../admin/includes/process_job.inc > /dev/null 2>&1 &');
    
    $id = rand(1,9);
    // 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/lakecity/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);

    // 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");
    display_notification("Request [".$id."]has been submitted to job queue");
    
    
}



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


?>
