<?php
/**********************************************************************
***********************************************************************/
//include_once( "../includes/references.inc");
//-------------------------------------------------------------------------------------------------------------




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

function add_manufact_requirment_line($id, $source_type,$source_tran_no, 
                                        $source_line_id, $item_code, $manufacturing_quantity, 
                                        $manufacturing_unit, $manufacturing_unit_qty, $manufacturing_unit_cf, 
                                        $wo_end_date,$location, $project_id,
                                        $task_id, $order_type, $wo_no, $wo_start_date)
{
        $created_by = $_SESSION['wa_current_user']->user;
        $created_on = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."manufacturing_requirements(source_type, 
                        source_tran_no, 
                        source_line_id, stock_id, 
                        manufacturing_quantity, manufacturing_unit, 
                        manufacturing_unit_qty, manufacturing_unit_cf, 
                        start_on, required_by,order_type,order_no,
                        loc_code, project_id, task_id,                       
                        created_by, created_on) 
                VALUES (".db_escape($source_type).", "
                        .db_escape($source_tran_no).", "
                        .db_escape($source_line_id).", ".db_escape($item_code).", "
                        .db_escape($manufacturing_quantity).", ".db_escape($manufacturing_unit).", "
                        .db_escape($manufacturing_unit_qty).", ".db_escape($manufacturing_unit_cf).", "
                        .db_escape($wo_start_date).", ".db_escape($wo_end_date).", "
                        .db_escape($order_type).", ".db_escape($wo_no).", "
                        .db_escape($location).", ".db_escape($project_id).", ".db_escape($task_id).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";
                
	db_query($sql, "could not add manufacturing requirement item");
    
    return db_insert_id();
}


function update_manufacturing_requirement($id, $source_type,$source_tran_no, 
                                        $source_line_id, $manufacturing_quantity, $manufacturing_unit,
                                        $manufacturing_unit_qty, $manufacturing_unit_cf, $wo_end_date, 
                                        $location, $task_id, $order_type,
                                        $wo_no,$source_row_state, $mreq_row_state, $wo_start_date)        
{
    begin_transaction();
    $num_rows = update_manufacturing_requirement_line($id, $source_type,$source_tran_no, 
                                        $source_line_id, $manufacturing_quantity, $manufacturing_unit,
                                        $manufacturing_unit_qty, $manufacturing_unit_cf, $wo_end_date, 
                                        $location, $task_id, $order_type,
                                        $wo_no, $mreq_row_state, $wo_start_date);
    
    if($num_rows !== 1){
        cancel_transaction();
        return array("success" => false, "tran_no" => $id, "message" => "Could not update manufacturing requirement.");
    }
    
    $uploq_msg = update_mr_line_source_order_quantity($source_line_id, $manufacturing_quantity,$source_type, $source_row_state);
    if($uploq_msg["success"] !== true){
        cancel_transaction();
        return $uploq_msg;
    }
    
    commit_transaction();
    return array("success" => true, "message" => "");
}


function update_manufacturing_requirement_line($id, $source_type,$source_tran_no, 
                                        $source_line_id, $manufacturing_quantity, $manufacturing_unit,
                                        $manufacturing_unit_qty, $manufacturing_unit_cf, $wo_end_date,
                                        $location, $task_id, $order_type,
                                        $wo_no, $mreq_row_state, $wo_start_date)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');
        
        $sql = "UPDATE ".TB_PREF."manufacturing_requirements 
                SET source_type=".db_escape($source_type).", 
                    source_tran_no=".db_escape($source_tran_no).", 
                    source_line_id=".db_escape($source_line_id).", 
                    manufacturing_quantity=".db_escape($manufacturing_quantity).", 
                    manufacturing_unit=".db_escape($manufacturing_unit).", 
                    manufacturing_unit_qty=".db_escape($manufacturing_unit_qty).", 
                    manufacturing_unit_cf=".db_escape($manufacturing_unit_cf).", 
                    start_on=".db_escape($wo_start_date).",
                    required_by=".db_escape($wo_end_date).",
                    loc_code=".db_escape($location).",
                    task_id=".db_escape($task_id).",
                    order_type=".db_escape($order_type).",
                    order_no=".db_escape($wo_no).", 
                    updated_by=".db_escape($updated_by).",
                    updated_on=". db_escape($updated_on).",
                    row_state=". db_new_row_state($mreq_row_state)." 
                WHERE id=".db_escape($id)." AND row_state = ". db_escape($mreq_row_state);
        
	db_query($sql, "could not update manufacturing requirements detail");
    
    return db_num_affected_rows();
}



function update_mreq_line($pr_line_id, $total_qty_ordered, $close_pr_line, $row_state)
{
    $sql = "UPDATE ".TB_PREF."manufacturing_requests 
            SET quantity_ordered =".db_escape($total_qty_ordered).", 
                line_complete=".db_escape($close_pr_line).",
                row_state=". db_new_row_state($row_state)."                 
            WHERE id = ".db_escape($pr_line_id)." 
                AND row_state =". db_escape($row_state);
        
    db_query($sql, "could not update manufacturing request line");
    
    return db_num_affected_rows();
}

function update_mr_line_source_order_quantity($source_line_id, $order_quantity, $source_type,$row_state)
{
    
    $total_qty_ordered = get_total_maufacturing_requirement_qty($source_type, $source_line_id);
   
    if($source_type==ST_SALESORDER)
    {
        $sod_line = get_sod_item($source_line_id);
        $close_sod_line = 0;
        if($total_qty_ordered >= $sod_line['quantity']){
            $close_sod_line  = 1;
        }
        $num_rows = update_sod_line_ordered_qty($source_line_id, $total_qty_ordered, $close_sod_line,$row_state);
    }
    elseif($source_type==ST_MANUF_REQUEST)
    {
        $manuf_req_line = get_mreq_item($source_line_id); 
        $close_mreq_line = 0;
        if($total_qty_ordered >= $manuf_req_line['quantity']){
            $close_mreq_line  = 1;
        }
        $num_rows = update_mreq_line($source_line_id, $total_qty_ordered, $close_mreq_line,$row_state);
    }  
    else{
        $num_rows = 0;
        cancel_transaction();
        return array("success" => false, "message" => "Selected line is not from a valid source for a manufacturing requirement.");
    }
    
    if($num_rows !== 1){
        cancel_transaction();
        return array("success" => false, "message" => "Another user has updated sales order line $source_line_id.");
    }
    
    return array("success" => true, "message" => "");
}





function add_manufacturing_requirement($id, $source_type,$source_tran_no, 
                                        $source_line_id, $item_code, $manufacturing_quantity, 
                                        $manufacturing_unit, $manufacturing_unit_qty, $manufacturing_unit_cf, 
                                        $wo_end_date, $location, $project_id,
                                        $task_id, $order_type, $wo_no, 
                                        $source_row_state, $wo_start_date)
{
    begin_transaction();
    $ra_line_id = add_manufact_requirment_line($id, $source_type,$source_tran_no, 
                                        $source_line_id, $item_code, $manufacturing_quantity, 
                                        $manufacturing_unit, $manufacturing_unit_qty, $manufacturing_unit_cf, 
                                        $wo_end_date,$location, $project_id,
                                        $task_id, $order_type, $wo_no, $wo_start_date);
    
    if($ra_line_id <= 0){
        cancel_transaction();
        return array("success" => false, "message" => "Could not add manufacturing requirement details.");
    }
    
    $uploq_msg = update_mr_line_source_order_quantity($id, $manufacturing_quantity,$source_type, $source_row_state);
    if($uploq_msg["success"] !== true){
        cancel_transaction();
        return $uploq_msg;
    }
    
    commit_transaction();
    return array("success" => true, "message" => "");
}



function get_total_maufacturing_requirement_qty($source_type, $pr_line_id)
{
    $sql = "SELECT SUM(manufacturing_quantity) manufacturing_quantity  
            FROM ".TB_PREF."manufacturing_requirements 
            WHERE source_type= ".$source_type." 
                AND source_line_id = ".$pr_line_id;
    
    $result=db_query($sql, "Could not get_total_maufacturing_requirement_qty.");
    $total =  db_fetch($result);
    $total_qty = 0;
    if(isset($total['manufacturing_quantity'])){
        $total_qty = $total['manufacturing_quantity'];
    }
    return $total_qty;
    
}


function delete_manufacturing_requirement($selected_id)
{
    $manuf_req_line_data=get_manufacturing_requirements_details_for_delete($selected_id);
    if($manuf_req_line_data)
    {
        $source_line_id=$manuf_req_line_data['source_line_id'];
        $manufacturing_quantity=$manuf_req_line_data['manufacturing_quantity'];            
        $source_type=$manuf_req_line_data['source_type'];  
        $row_state=$manuf_req_line_data['row_state'];    
    }
    
    begin_transaction();
    
    $ra_line_id=delete_manufacturing_requirement_line($selected_id);
    
     if($ra_line_id <= 0){
        cancel_transaction();
        return array("success" => false, "message" => "Could not delete manufacturing requirement details.");
    }  
    
    $uploq_msg=delete_manuf_sod_quantity_data($source_line_id,$manufacturing_quantity,$source_type,$row_state);
    
    if($uploq_msg["success"] !== true){
        cancel_transaction();
        return $uploq_msg;
    }
    
    commit_transaction();
    return array("success" => true, "message" => "");
    
}

function  delete_manufacturing_requirement_line($selected_id)
{
    
    $sql = "DELETE FROM ".TB_PREF."manufacturing_requirements 
            WHERE id = ".$selected_id;
    
    db_query($sql, "Could not delete requisition allocation detail");
    
    return db_num_affected_rows();
}

function delete_sod_line_data($source_line_id, $manufacturing_quantity,$row_state)
{

	$sql = "UPDATE ".TB_PREF."sales_order_details 
                SET     quantity_ordered = quantity_ordered - ".db_escape($manufacturing_quantity)." ,
                        row_state = ".db_escape($row_state)." 
                WHERE   id = ".$source_line_id ;

	$error_msg = "Could not update Sales Order record ";

	db_query($sql, $error_msg);
         return db_num_affected_rows();

	
    
}

function delete_manuf_sod_quantity_data($source_line_id,$manufacturing_quantity,$source_type,$row_state)
{
    $num_rows=0;
    if($source_type==ST_SALESORDER)
    {
        $num_rows = delete_sod_line_data($source_line_id, $manufacturing_quantity,$row_state);
    }
    elseif($source_type==ST_MANUF_REQUEST)
    {
        $num_rows = delete_manuf_req_line_data($source_line_id, $manufacturing_quantity,$row_state);
    }  
   
   if($num_rows !== 1){
        cancel_transaction();
        return array("success" => false, "message" => "Another user has updated sales order line $source_line_id.");
    }
    
    return array("success" => true, "message" => "");
    
    if($uploq_msg["success"] !== true){
        cancel_transaction();
        return $uploq_msg;
    }
    
}

function delete_manuf_req_line_data($source_line_id, $manufacturing_quantity,$row_state)
{
        $updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');

	$sql = "UPDATE ".TB_PREF."manufacturing_requests 
                SET     quantity_ordered = quantity_ordered -".db_escape($manufacturing_quantity)." ,
                        row_state = ".db_escape($row_state)." ,                       
                        updated_by=".db_escape($updated_by)." ,
                        updated_on=".db_escape($updated_on)." 
                WHERE   id = ".$source_line_id ;

	$error_msg = "Could not update Manufacturing requests record ";

	db_query($sql, $error_msg);
        return db_num_affected_rows();
        
    
}

function get_manuf_request_details_for_edit($id)
{
    $sql="SELECT *
            FROM ".TB_PREF."manufacturing_requests                  
            WHERE  id=".$id;
       
      $result = db_query($sql,"Material request could not be added");
      return db_fetch($result);
    
}

function view_manuf_request($manuf_req_id, $allow_null=false)
{
    $sql = "SELECT id,
                   pr.description project,
                   pt.long_description task,
                   sm.description stock_item,
                   mr.tran_unit,
                   mr.tran_qty,
                   mr.req_date
            FROM ".TB_PREF."manufacturing_requests mr
                   LEFT JOIN ".TB_PREF."projects_master pr ON pr.project_id =mr.project_id
                   LEFT JOIN ".TB_PREF."project_tasks pt ON pt.task_id=mr.task_id
                   LEFT JOIN ".TB_PREF."stock_master sm ON sm.stock_id=mr.stock_id                       
            WHERE  id=". db_escape($manuf_req_id);
    $result = db_query($sql, "The manufacturing request cannot be retrieved");
    
    if (!$allow_null && db_num_rows($result) == 0){
		display_db_error("Could not find manufacturing request $manuf_req_id", $sql);
    }
	return db_fetch($result);
}
function view_manuf_requirement($manuf_req_id, $allow_null=false)
{
       $sql1 = "SELECT  
                     sod.id as id,
                     mr.id mr_id,
                     pr.description project,
                     sod.trans_type source_type,
                     sod.order_no,
                     sod.stk_code,
                     sod.description,
                     sod.order_unit tran_unit_name,
                     sod.order_unit_qty tran_unit_quantity,
                     so.delivery_date required_date,
                     mr.manufacturing_unit,
                     mr.manufacturing_quantity,
                     mr.manufacturing_unit_qty,
                     mr.required_by,
                     mr.loc_code
              FROM 
                    ".TB_PREF."sales_order_details sod 
                    INNER JOIN ".TB_PREF."sales_orders so ON sod.order_no = so.order_no
                    LEFT JOIN ".TB_PREF."projects_master pr ON so.project_id = pr.project_id
                    LEFT JOIN ".TB_PREF."manufacturing_requirements mr ON 
                    sod.id = mr.source_line_id 
              WHERE sod.line_complete=0 AND sod.quantity_ordered < sod.quantity AND mr.id=". db_escape($manuf_req_id);
    
        $sql2 = "SELECT 
                    mr.id id,
                    mrt.id mr_id,
                    pr.description project,
                    103 source_type,                    
                    mr.id order_no,
                    sm.stock_id stk_code,
                    sm.description description,
                    mr.tran_unit tran_unit_name,
                    mr.tran_qty tran_unit_quantity,                                    
                    mr.req_date required_date,
                    mrt.manufacturing_unit,
                    mrt.manufacturing_quantity,
                    mrt.manufacturing_unit_qty,
                    mrt.required_by,
                    mrt.loc_code
            FROM ".TB_PREF."manufacturing_requests mr
                   LEFT JOIN ".TB_PREF."projects_master pr ON pr.project_id =mr.project
                   LEFT JOIN ".TB_PREF."project_tasks pt ON pt.task_id=mr.task_id
                   LEFT JOIN ".TB_PREF."stock_master sm ON sm.stock_id=mr.stock_id
                   LEFT JOIN ".TB_PREF."manufacturing_requirements mrt ON mr.id = mrt.source_line_id 
            WHERE mr.line_complete=0 AND mr.quantity_ordered < mr.quantity AND mrt.id=". db_escape($manuf_req_id);
    
        $sql = $sql1." UNION ".$sql2." ORDER BY id";
   
    $result = db_query($sql, "The manufacturing requirement cannot be retrieved");
    if (!$allow_null && db_num_rows($result) == 0)
		display_db_error("Could not find manufacturing requirement $manuf_req_id", $sql);
	return db_fetch($result);
}

function update_manuf_request($id,$project_id, $task_id, $loc_code, $stock_item, $quantity,  $tran_unit, $tran_qty, $cf, $req_date, $comment)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

    $sql = "UPDATE ".TB_PREF."manufacturing_requests SET
                    project_id=".db_escape($project_id).", 
                    task_id=".db_escape($task_id).",
                    loc_code=".db_escape($loc_code).",
                    stock_id=".db_escape($stock_item).", 
                    tran_unit=".db_escape($tran_unit).",
                    quantity=".db_escape($quantity).", 
                    tran_qty=".db_escape($tran_qty).",  
                    tran_unit_cf=".db_escape($cf).",      
                    req_date=".db_escape($req_date).", 
                    comment=".db_escape($comment)." ,
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)." 
            WHERE id=".$id; 
           

     db_query($sql,"Manufacturing request could not be updated");
    
     
    
    
}

function add_manuf_request($project_id, $task_id, $loc_code, $stock_item, $quantity,  $tran_unit, $tran_qty, $cf, $req_date, $comment)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."manufacturing_requests(
                    project_id, task_id, loc_code, 
                    stock_id,quantity,tran_unit,tran_qty,tran_unit_cf,
                    req_date, comment,
                    added_by,added_on) 
            VALUES (".db_escape($project_id).", ".db_escape($task_id).", ".db_escape($loc_code).", "
                    .db_escape($stock_item).", ".db_escape($quantity).", ".db_escape($tran_unit).", "
                    .db_escape($tran_qty).", ".db_escape($cf).", "
                    .db_escape($req_date).", ".db_escape($comment).", "
                    .db_escape($id_user).", ".db_escape($datetime).")";

    db_query($sql,"Manufacturing request could not be added");
    
     $id = db_insert_id();
     return $id;
}



function get_open_manufacturing_requirements()
{
        
    $sql1 = "SELECT  
                     sod.id as id,
                     mr.id mr_id,
                     pr.description project,
                     sod.trans_type source_type,
                     sod.order_no,
                     sod.stk_code,
                     sm.description,
                     sod.order_unit tran_unit_name,
                     sod.order_unit_qty tran_unit_quantity,
                     sod.convertion_factor conversion_factor,
                     sod.quantity_ordered,
                     so.delivery_date required_date,
                     mr.order_type,
                     mr.manufacturing_unit,
                     mr.manufacturing_quantity,
                     mr.manufacturing_unit_qty,
                     mr.start_on,
                     mr.required_by,
                     mr.loc_code
              FROM 
                    ".TB_PREF."sales_order_details sod 
                    INNER JOIN ".TB_PREF."sales_orders so ON 
                            sod.order_no = so.order_no AND 
                            so.trans_type = ".ST_SALESORDER." AND 
                            (so.approval_status = ".AS_APPROVED." OR so.approval_status = ".AS_REOPENED.")  
                    INNER JOIN ".TB_PREF."stock_master sm ON 
                            sod.stk_code = sm.stock_id AND 
                            sm.mb_flag = 'M' 
                    LEFT JOIN ".TB_PREF."projects_master pr ON so.project_id = pr.project_id
                    LEFT JOIN ".TB_PREF."manufacturing_requirements mr 
                                ON sod.id = mr.source_line_id 
                                AND mr.source_type = ".ST_SALESORDER." 
                                AND mr.order_no = 0 
              WHERE sod.line_complete=0 AND sod.quantity_ordered < sod.quantity ";
    
        $sql2 = "SELECT 
                    mr.id id,
                    mrt.id mr_id,
                    pr.description project,
                    103 source_type,                    
                    mr.id order_no,
                    sm.stock_id stk_code,
                    sm.description description,
                    mr.tran_unit tran_unit_name,
                    mr.tran_qty tran_unit_quantity,  
                    mr.tran_unit_cf conversion_factor,
                    mr.quantity_ordered,
                    mr.req_date required_date,
                    mrt.order_type,
                    mrt.manufacturing_unit,
                    mrt.manufacturing_quantity,
                    mrt.manufacturing_unit_qty,
                    mrt.start_on,
                    mrt.required_by,
                    mrt.loc_code
            FROM ".TB_PREF."manufacturing_requests mr
                   LEFT JOIN ".TB_PREF."projects_master pr ON pr.project_id =mr.project_id 
                   LEFT JOIN ".TB_PREF."project_tasks pt ON pt.task_id=mr.task_id
                   LEFT JOIN ".TB_PREF."stock_master sm ON sm.stock_id=mr.stock_id
                   LEFT JOIN ".TB_PREF."manufacturing_requirements mrt 
                                ON mr.id = mrt.source_line_id 
                                AND mrt.source_type = ".ST_MANUF_REQUEST." 
                                AND mrt.order_no = 0 
            WHERE mr.line_complete=0 AND mr.quantity_ordered < mr.quantity   ";
    
        $sql = $sql1." UNION ".$sql2." ORDER BY id";
	
	$result = db_query($sql,"Could not retrieve unapproved salary adjustments");	
        
        return $result;
                
}

function get_manufacturing_requirement_by_id($selected_id)
{
    $sql=" SELECT * 
           FROM 0_manufacturing_requirements 
           WHERE id=".db_escape($selected_id);
    
    $result = db_query($sql, "could not get_manufacturing_requirement_by_id");

    return db_fetch($result);
    
    
}

function get_manufacturing_requirements_details_for_delete($selected_id)
{
    $sql=" SELECT * 
           FROM 0_manufacturing_requirements 
           WHERE id=".db_escape($selected_id);
    
    $result = db_query($sql, "could not get requisition detail");

    return db_fetch($result);
    
    
}

function get_manufacturing_requirements($update_type, $selected_id)
{
        $selected_id=substr($selected_id,5);
    
         $sql = "SELECT  
                     sod.id id,
                     mr.id mr_id,
                     pr.description project,
                     sod.trans_type source_type,
                     0 AS source_task_id,
                     sod.order_no,
                     sod.stk_code,
                     sod.description,
                     sod.order_unit tran_unit_name,
                     sod.order_unit_qty tran_unit_quantity,
                     so.delivery_date required_date,
                     so.project_id,
                     mr.manufacturing_unit,
                     mr.manufacturing_quantity,
                     mr.manufacturing_unit_qty,
                     mr.required_by,
                     mr.loc_code,
                     mr.task_id,
                     sod.row_state source_row_state,
                     mr.row_state mreq_row_state 
              FROM 
                    ".TB_PREF."sales_order_details sod 
                    INNER JOIN ".TB_PREF."sales_orders so ON sod.order_no = so.order_no
                    LEFT JOIN ".TB_PREF."projects_master pr ON so.project_id = pr.project_id
                    LEFT JOIN ".TB_PREF."manufacturing_requirements mr ON 
                    sod.id = mr.source_line_id WHERE ";
    
                if($update_type == "update_pr_po_line"){
                    $sql .= " mr.id=".db_escape($selected_id);
                }
                else{
                    $sql.= " sod.id=".db_escape($selected_id);
                }

	$result = db_query($sql, "could not get requisition detail");

	return db_fetch($result);
}

function get_manufacturing_requirements_from_manufacturing_request($update_type,$selected_id)
{
            $selected_id=substr($selected_id,5);
            
            $sql= "SELECT 
                    mr.id id,
                    mrt.id mr_id,
                    pr.description project,
                    103 source_type,                     
                    mr.id order_no,
                    mr.project_id,
                    mr.task_id source_task_id,
                    sm.stock_id stk_code,
                    sm.description description,
                    mr.tran_unit tran_unit_name,
                    mr.tran_qty tran_unit_quantity,                                    
                    mr.req_date required_date,
                    mrt.manufacturing_unit,
                    mrt.manufacturing_quantity,
                    mrt.manufacturing_unit_qty,
                    mrt.start_on,
                    mrt.required_by,
                    mrt.loc_code,
                    mrt.task_id,
                    mr.row_state source_row_state,
                    mrt.row_state mreq_row_state 
            FROM ".TB_PREF."manufacturing_requests mr
                   LEFT JOIN ".TB_PREF."projects_master pr ON pr.project_id =mr.project_id 
                   LEFT JOIN ".TB_PREF."project_tasks pt ON pt.task_id=mr.task_id
                   LEFT JOIN ".TB_PREF."stock_master sm ON sm.stock_id=mr.stock_id
                   LEFT JOIN ".TB_PREF."manufacturing_requirements mrt ON mr.id = mrt.source_line_id WHERE ";
            
        if($update_type == "update_pr_po_line"){
            $sql .= " mrt.id=".db_escape($selected_id);
                }
        else{
            $sql.= " mr.id=".db_escape($selected_id);
                }

	$result = db_query($sql, "could not get requisition detail");

	return db_fetch($result);
    
    
}



function get_sql_for_manufacturing_requests_inquiry()
{
    $sql = "SELECT id,
                   pr.description project,
                   pt.description task,
                   l.location_name,
                   sm.description stock_item,
                   mr.tran_unit,
                   mr.tran_qty,
                   (mr.quantity_ordered / tran_unit_cf) tran_unit_qty_ordered,
                   mr.req_date,
                   mr.line_complete 
            FROM ".TB_PREF."manufacturing_requests mr
                   LEFT JOIN ".TB_PREF."projects_master pr ON pr.project_id =mr.project_id 
                   LEFT JOIN ".TB_PREF."project_tasks pt ON pt.task_id=mr.task_id
                   LEFT JOIN ".TB_PREF."stock_master sm ON sm.stock_id=mr.stock_id 
                   LEFT JOIN ".TB_PREF."locations l ON l.loc_code = mr.loc_code";
       
      
      return $sql;
    
    
    
}


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




function get_mreq_item($row_id)
{
        
    $sql = "SELECT *  
            FROM ".TB_PREF."manufacturing_requests 
            WHERE id = ".$row_id ;
    
    $result = db_query($sql,"Purchase requisition item detail could not be retreived");

    return db_fetch($result);

}

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


function update_manufacturing_requirement_order_no($mreq_id, $order_type, $order_no, $completed, $row_state)		
{		
	$sql = "UPDATE ".TB_PREF."manufacturing_requirements  		
                SET order_type = $order_type ,		
                    order_no = $order_no,		
                    completed = $completed,		
                    row_state = ".db_new_row_state($row_state)."  		
                WHERE   id = ".$mreq_id." AND row_state = ". db_escape($row_state) ;		
	$error_msg = "Could not update_manufacturing_requirement_order_no";		
	db_query($sql, $error_msg);		
	$num_rows = db_num_affected_rows();
    if($num_rows == 1){
        return array("success" => true, "message" => "");
    }
    else{
        return array("success" => false, "message" => "Another user has updated manufacturing requirement $id");
    }		
}
?>
