<?php

function get_outside_material_requisition_headers($jo_id) {
    $sql = "SELECT sa.id,
                sa.trans_no,
                sa.reference,
                sa.type,
                sa.from_location,
                l2.location_name to_location_name,
                sa.tran_date,
                IF(sa.increase = 1 , 'Yes', 'No') increase,  
                sa.comments,
                IF(sa.sample_items = 1 , 'Yes', 'No') sample_items,
                sa.approval_status, 
                sa.source_trans_no, 
                sa.source_type  
            FROM ".TB_PREF."outside_material_requisitions sa  
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code        
            WHERE sa.type = ".db_escape(ST_OUTSIDE_MATERIAL_REQ)." 
                AND sa.jo_id = ".db_escape($jo_id)." 
            ORDER BY sa.id";
    
    return db_query($sql, "get_outside_material_requisition_headers");
}

function write_outside_material_requisition_header($type, &$trans_no, $id, $reference, $project_id, $from_location,  $to_loc_code, $comments, $date, $jo_id=0, $source_type=-1, 
                                                   $source_trans_no=0, &$row_state=0) {
    global $Refs;
     
        if ($id > 0) {
            $af_row = update_outside_material_requisition_header($id, $reference, $from_location, $to_loc_code, $comments, $date, $row_state);
                if (! ($af_row > 0)) {
                    return 0;
                }
            $row_state = db_new_row_state($row_state);  
        } else {
            $trans_no = get_next_trans_no($type);
            $id = insert_outside_material_requisition_header($type, $trans_no, $reference, $project_id, $from_location, $to_loc_code, $comments, $date, $jo_id, $source_type, 
                                                             $source_trans_no);
        }
    
        $Refs->save($type, $trans_no, $reference);

        if (! ($id > 0)) {
            return 0;
        }

        return $id;
}

function write_outside_material_requisition_header_by_type_trans_no($type, $trans_no, $reference, $project_id, $from_location, $to_loc_code, $comments, $date, $source_id=0) {
    global $Refs;
     
        if ($trans_no > 0) {
            $af_row = update_outside_material_requisition_header_by_type_trans_no($type, $trans_no, $reference, $from_location, $to_loc_code, $comments, $date);
                if (! ($af_row > 0)) {
                    return 0;
                }
        } else {
            $trans_no = get_next_trans_no($type);
            $inser_id = insert_outside_material_requisition_header($type, $trans_no, $reference, $project_id, $from_location, $to_loc_code, $comments, $date, $source_id);
                if (! ($inser_id > 0)) {
                    return 0;
                }
        }
    
        $Refs->save($type, $trans_no, $reference);

        return $trans_no;
}
     
function insert_outside_material_requisition_header($type, $trans_no, $reference, $project_id, $from_location, $to_loc_code, $comments, $date, $jo_id=0, $source_type=-1, 
                                                    $source_trans_no=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."outside_material_requisitions
                      ( type, trans_no, reference, project_id, from_location, 
                        to_loc_code, comments, tran_date, jo_id, 
                        source_type, source_trans_no, created_by, created_on)         
                VALUES( ".db_escape($type).", ".db_escape($trans_no).", ".db_escape($reference).", ".db_escape($project_id).", ".db_escape($from_location).", 
                        ".db_escape($to_loc_code).", ".db_escape($comments).", ".db_escape(date2sql($date)).", ".db_escape($jo_id).", 
                        ".db_escape($source_type).", ".db_escape($source_trans_no).", ".db_escape($created_by).", ".db_escape($created_on).")";

            db_query($sql, 'insert_outside_material_requisition_header');
            return db_insert_id();        
}

function update_outside_material_requisition_header($id, $reference, $from_location, $to_loc_code, $comments, $date, $row_state=0) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."outside_material_requisitions
                SET reference = ".db_escape($reference).",
                    from_location = ".db_escape($from_location).",                          
                    to_loc_code = ".db_escape($to_loc_code).",                          
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).", 
                    row_state = ".db_new_row_state($row_state).", 
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE id = ".db_escape($id)."   
                    AND row_state = ".db_escape($row_state);  

            db_query($sql, "update_outside_material_requisition_header");
            return db_num_affected_rows();
}

function update_outside_material_requisition_header_by_type_trans_no($type, $trans_no, $reference, $from_location, $to_loc_code, $comments, $date) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."outside_material_requisitions
                SET reference = ".db_escape($reference).",
                    from_location = ".db_escape($from_location).",                          
                    to_loc_code = ".db_escape($to_loc_code).",                          
                    tran_date = ".db_escape(date2sql($date)).",
                    comments = ".db_escape($comments).", 
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE type = ".db_escape($type)."     
                    AND trans_no = ".db_escape($trans_no);    

            db_query($sql, "update_outside_material_requisition_header_by_type_trans_no");
            return db_num_affected_rows();
}

function write_outside_material_requisition_line($trans_no, $line_id, $type, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, 
                                                 $standard_cost, $project_site_id, $line_comment, $task_id, $source_type=0, $source_trans_no=0, $source_line_id=0) {
    if ($line_id > 0) {
        return update_outside_material_requisition_line($line_id, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                                        $project_site_id, $line_comment, $task_id, $source_type, $source_trans_no, $source_line_id);
    } else {
        return insert_outside_material_requisition_line($trans_no, $type, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, 
                                                        $standard_cost, $project_site_id, $line_comment, $task_id, $source_type, $source_trans_no, $source_line_id);
    }
}

function update_outside_material_requisition_line($line_id, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                                  $project_site_id, $line_comment, $task_id, $source_type=0, $source_trans_no=0, $source_line_id=0) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."outside_material_requisition_details
                SET stock_id = ".db_escape($stock_id).", 
                    item_type = ".db_escape($item_type).", 
                    item_description = ".db_escape($item_description).", 
                    tran_unit = ".db_escape($tran_unit).",
                    tran_qty = ".db_escape($tran_qty).",
                    qty = ".db_escape($qty).",
                    conversion_factor = ".db_escape($conversion_factor).",
                    standard_cost = ".db_escape($standard_cost).",
                    project_site_id = ".db_escape($project_site_id).",
                    line_comment = ".db_escape($line_comment).",
                    task_id = ".db_escape($task_id).",
                    source_type = ".db_escape($source_type).", 
                    source_trans_no = ".db_escape($source_trans_no).", 
                    source_line_id = ".db_escape($source_line_id).", 
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)."  
                WHERE trans_id = ".db_escape($line_id);

            db_query($sql, 'update_outside_material_requisition_line');
            return db_num_affected_rows();
}
        
function insert_outside_material_requisition_line($trans_no, $type, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                                  $project_site_id, $line_comment, $task_id, $source_type=0, $source_trans_no=0, $source_line_id=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
        $sql = "INSERT INTO ".TB_PREF."outside_material_requisition_details
                      ( trans_no, type, stock_id, tran_unit, 
                        item_type, item_description, tran_qty, qty, 
                        conversion_factor, task_id, standard_cost, project_site_id, 
                        line_comment, source_type, source_trans_no, source_line_id, 
                        created_by, created_on)        
                VALUE ( ".db_escape($trans_no).", ".db_escape($type).", ".db_escape($stock_id).", ".db_escape($tran_unit).", 
                        ".db_escape($item_type).", ".db_escape($item_description).", ".db_escape($tran_qty).", ".db_escape($qty).", 
                        ".db_escape($conversion_factor).", ".db_escape($task_id).", ".db_escape($standard_cost).", ".db_escape($project_site_id).", 
                        ".db_escape($line_comment).", ".db_escape($source_type).", ".db_escape($source_trans_no).", ".db_escape($source_line_id).", 
                        ".db_escape($created_by).", ".db_escape($created_on).")";      

            db_query($sql, 'insert_outside_material_requisition_line');
            return db_insert_id();  
}

function get_outside_material_requisition_header_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."outside_material_requisitions    
            WHERE id = ".db_escape($id);
    
        $result = db_query($sql, "get_outside_material_requisition_header_record");
        return db_fetch($result);
}

function get_outside_material_requisition_header($id) {
    $sql = "SELECT sa.*,
                l2.location_name to_location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."outside_material_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.id = ".db_escape($id);
    
        $result = db_query($sql, "get_outside_material_requisition_header");
        return db_fetch($result);
}

function get_outside_material_requisition_header_type_trans_no($type, $trans_no) {
    $sql = "SELECT sa.*,
                l2.location_name to_location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."outside_material_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code     
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.type = ".db_escape($type)." 
                AND sa.trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_outside_material_requisition_header_type_trans_no");
        return db_fetch($result);
}

function get_outside_material_requisition_header_record_type_trans_no($type, $trans_no) {
    $sql = "SELECT * 
            FROM ".TB_PREF."outside_material_requisitions    
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no);
    
        $result = db_query($sql, "get_outside_material_requisition_header_record_type_trans_no");
        return db_fetch($result);
}

function delete_outside_material_requisition_details_record($selected_id) {
    $sql = "DELETE 
            FROM ".TB_PREF."outside_material_requisition_details 
            WHERE trans_id = ".db_escape($selected_id);
    
        db_query($sql, 'delete_outside_material_requisition_details_record');
        return db_num_affected_rows();
}

function get_outside_material_requisition_lines($trans_no, $type) {
    $sql = "SELECT * 
            FROM ".TB_PREF."outside_material_requisition_details  
            WHERE trans_no = ".db_escape($trans_no)." 
                AND type = ".db_escape($type);
    
        return db_query($sql, "get_outside_material_requisition_lines");
}

function get_outside_material_requisition_details($trans_no, $type) {
    $sql = "SELECT ard.*,
                am.description stock_description,
                am.stock_id,
                am.mb_flag,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description 
            FROM ".TB_PREF."outside_material_requisition_details ard 
                LEFT JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                LEFT JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.project_site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.trans_no = ".db_escape($trans_no)." 
                AND ard.type = ".db_escape($type);
    
        return db_query($sql, "get_outside_material_requisition_details");
}

function get_outside_material_requisition_details_record($id) {
    $sql = "SELECT * 
            FROM ".TB_PREF."outside_material_requisition_details  
            WHERE trans_id = ".db_escape($id);
    
        $result = db_query($sql, "get_outside_material_requisition_details_record");
        return db_fetch($result);
}

function get_outside_material_requisition_detail_line_details($trans_id) {
    $sql = "SELECT ard.*,
                am.description stock_description,
                am.stock_id,
                am.mb_flag,
                un.name unit_name,
                ps.description site_description, 
                pt.description task_description 
            FROM ".TB_PREF."outside_material_requisition_details ard 
                LEFT JOIN ".TB_PREF."stock_master am 
                    ON ard.stock_id = am.stock_id 
                LEFT JOIN ".TB_PREF."item_units un  
                    ON ard.tran_unit = un.abbr  
                LEFT JOIN ".TB_PREF."project_sites ps 
                    ON ard.project_site_id = ps.id 
                LEFT JOIN ".TB_PREF."project_tasks pt 
                    ON ard.task_id = pt.task_id 
            WHERE ard.trans_id = ".db_escape($trans_id);
    
        $result = db_query($sql, "get_outside_material_requisition_detail_line_details");
        return db_fetch($result);
}

function get_outside_material_requisition_details_lines_count($trans_no, $type) {
    $sql = "SELECT COUNT(trans_id) lines_count 
            FROM ".TB_PREF."outside_material_requisition_details 
            WHERE trans_no = ".db_escape($trans_no)." 
                AND type = ".db_escape($type);
    
        $result = db_query($sql, 'get_outside_material_requisition_details_lines_count');
        return db_result_value($result, 'lines_count');
}

function update_outside_material_requisition_row_state($id, $row_state) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."outside_material_requisitions
            SET row_state = ".db_new_row_state($row_state).",
                updated_by = ".db_escape($updated_by).", 
                updated_on = ".db_escape($updated_on)." 
            WHERE id = ".db_escape($id)." 
                AND row_state = ".db_escape($row_state);
            
    db_query($sql, "update_outside_material_requisition_row_state");
    return db_num_affected_rows();
}

function update_outside_material_requisition_row_state_by_type_trans_no($type, $trans_no, $row_state) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."outside_material_requisitions
                SET row_state = ".db_new_row_state($row_state).",
                    updated_by = ".db_escape($updated_by).", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE type = ".db_escape($type)." 
                    AND trans_no = ".db_escape($trans_no)." 
                    AND row_state = ".db_escape($row_state);

            db_query($sql, "update_outside_material_requisition_row_state_by_type_trans_no");
            return db_num_affected_rows();
}

function get_sql_for_outside_material_requisitions_inquiry($trans_no, $jo_id, $has_job, $reference, $project_id, $from_date, $to_date, $to_loc_code, $trans_status=-1, 
                                                           $only_outstanding=false) {
    $sql = "SELECT sa.id,
                sa.jo_id,
                sa.trans_no,
                sa.reference,
                sa.type,
                sa.from_location,
                l2.location_name to_location_name,
                sa.tran_date,
                pm.description project_description,
                IF(sa.increase = 1 , 'Yes', 'No') increase,  
                sa.comments,
                IF(sa.sample_items = 1 , 'Yes', 'No') sample_items,
                sa.approval_status,  
                sa.source_type,   
                sa.project_id     
            FROM ".TB_PREF."outside_material_requisitions sa  
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code 
            WHERE sa.type = ".db_escape(ST_OUTSIDE_MATERIAL_REQ);
    
        if ($only_outstanding === true) {        
            $sql .= " AND sa.approval_status = ".db_escape(AS_APPROVED);
        }
    
        if ($trans_no > 0){
            $sql .= " AND sa.trans_no = ".db_escape($trans_no);
        } elseif ($jo_id > 0){
            $sql .= " AND sa.jo_id = ".db_escape($jo_id);
        } else {
            
            if ($has_job == 'Yes') {
                $sql .= " AND sa.jo_id > 0";
            } elseif ($has_job == 'No') {
                $sql .= " AND sa.jo_id = 0";
            } 
        
            if (strlen($reference) > 0) {
                $sql .= " AND sa.reference = ".db_escape($reference);
            }

            if ($project_id > 0) {
                $sql .= " AND sa.project_id = ".db_escape($project_id);
            }

            $sql .= " AND sa.tran_date >= ".db_escape(date2sql($from_date))." AND sa.tran_date <= ".db_escape(date2sql($to_date)); 

            if ($to_loc_code != '') {        
                $sql .= " AND sa.to_loc_code = ".db_escape($to_loc_code);
            }
            
            if ($trans_status > -1) {        
                $sql .= " AND sa.approval_status = ".db_escape($trans_status);
            }                      
        }
    
        $sql .= " ORDER BY sa.tran_date DESC";
    
    return $sql;
}

function update_outside_material_requisition_header_approval_status($id, $pre_status, $new_status) {
    $approved_by = $_SESSION['wa_current_user']->user;
    $approved_on = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."outside_material_requisitions
                SET approval_status = ".db_escape($new_status).",
                    approved_by = ".db_escape($approved_by).", 
                    approved_on = ".db_escape($approved_on)." 
                WHERE id = ".db_escape($id)."   
                    AND approval_status = ".db_escape($pre_status);    

            db_query($sql, "update_outside_material_requisition_header_approval_status");
            return db_num_affected_rows();
}

function update_outside_material_requisition_header_totals($type, $trans_no, $row_state=0) {
    $lines_total = get_outside_material_requisition_lines_total($type, $trans_no);
    
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql =  "UPDATE ".TB_PREF."outside_material_requisitions  
                    SET total = ".db_escape($lines_total).", 
                        row_state = ".db_new_row_state($row_state).", 
                        updated_by = ".db_escape($updated_by).", 
                        updated_on = ".db_escape($updated_on)." 
                    WHERE type = ".db_escape($type)." 
                        AND trans_no = ".db_escape($trans_no)." 
                        AND row_state = ".db_escape($row_state);

        db_query($sql, "update_outside_material_requisition_header_totals");   
        return db_num_affected_rows();
}

function get_outside_material_requisition_lines_total($type, $trans_no) {
    $sql = "SELECT SUM(standard_cost * qty) lines_amount_total
            FROM ".TB_PREF."outside_material_requisition_details 
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no);
    
    $result = db_query($sql, "get_outside_material_requisition_lines_total");
    return db_result_value($result, 'lines_amount_total');
}

function check_all_outside_material_requisition_lines_fully_processed($type, $trans_no) {
    $sql = "SELECT qty, SUM(receipt_quantity + receipt_alloc - returned_quantity) total_qty 
            FROM ".TB_PREF."outside_material_requisition_details 
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no)." 
            GROUP BY trans_id 
            HAVING total_qty < qty ";
            
        $result = db_query($sql, "check_all_outside_material_requisition_lines_fully_processed");
            if (db_num_rows($result) > 0) {
                return false;
            } 
            
        return true;
}

function get_outside_material_requisition_lines_quantity_by_source($source_type=-1, $source_trans_no=0, $source_line_id=0) {
    $sql = "SELECT SUM(qty) total_base_qty 
            FROM ".TB_PREF."outside_material_requisition_details 
            WHERE 1 = 1";
    
    if ($source_type > -1) {
        $sql .= " AND source_type = ".db_escape($source_type);
    }
    if ($source_trans_no > 0) {
        $sql .= " AND source_trans_no = ".db_escape($source_trans_no);
    }
    if ($source_line_id > 0) {
        $sql .= " AND source_line_id = ".db_escape($source_line_id);
    }
    
    $result = db_query($sql, "get_outside_material_requisition_lines_quantity_by_source");
    return db_result_value($result, 'total_base_qty');
}

function get_outside_material_requisition_headers_by_sources($source_type, $source_trans_no) {
    $sql = "SELECT sa.*,
                l2.location_name to_location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."outside_material_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE sa.source_type = ".db_escape($source_type)." 
                AND sa.source_trans_no = ".db_escape($source_trans_no);
    
        return db_query($sql, "get_outside_material_requisition_headers_by_sources");
}

function get_outside_material_requisition_headers_by_filters($jo_id=0, $id=0, $type=0, $trans_no=0, $to_loc_code='', $project_id=0, $from_date=null, $to_date=null) {
    $sql = "SELECT sa.*,
                l2.location_name to_location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."outside_material_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l2 
                    ON sa.to_loc_code = l2.loc_code 
                JOIN ".TB_PREF."users u 
                    ON sa.created_by = u.id 
            WHERE 1 = 1 ";
    
        if ($jo_id > 0) {
            $sql .= " AND sa.jo_id = ".db_escape($jo_id);
        }
        if ($id > 0) {
            $sql .= " AND sa.id = ".db_escape($id);
        }
        if ($type > 0) {
            $sql .= " AND sa.type = ".db_escape($type);
        }
        if ($trans_no > 0) {
            $sql .= " AND sa.trans_no = ".db_escape($trans_no);
        }
        if (strlen($to_loc_code) > 0) {
            $sql .= " AND sa.to_loc_code = ".db_escape($to_loc_code);
        }
        if ($project_id > 0) {
            $sql .= " AND sa.project_id = ".db_escape($project_id);
        }
        if ($from_date !== null) {
            $sql .= " AND sa.tran_date >= ".db_escape($from_date);
        }
        if ($to_date !== null) {
            $sql .= " AND sa.tran_date <= ".db_escape($to_date);
        }
    
        return db_query($sql, "get_outside_material_requisition_headers_by_filters");
}

function get_sql_for_approve_outside_material_requisitions($current_user_id) {
    $sql = "SELECT omr.id,
                omr.trans_no,
                omr.type,
                omr.jo_id,
                omr.reference,
                omr.tran_date,                   
                omr.from_location,                   
                l.location_name,                   
                omr.project_id,                   
                pm.description project_description,
                omr.total,
                c.real_name,
                omr.created_on,
                omr.approval_status 
            FROM ".TB_PREF."outside_material_requisitions omr
                JOIN ".TB_PREF."projects_master pm 
                    ON omr.project_id = pm.project_id
                JOIN ".TB_PREF."users c 
                    ON omr.created_by = c.id  
                JOIN ".TB_PREF."approvers apr 
                    ON omr.next_approval_group_id = apr.approval_group_id  
                JOIN ".TB_PREF."user_data_access_groups udag 
                    ON pm.dag_id = udag.dag_id 
                        AND udag.user_id = apr.user_id  
                JOIN ".TB_PREF."users u 
                    ON apr.user_id = u.id 
                LEFT JOIN ".TB_PREF."locations l 
                    ON omr.to_loc_code = l.loc_code   
            WHERE omr.approval_status IN (".implode(',', array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER)).") 
                AND apr.user_id = ".db_escape($current_user_id);
      
        return $sql;
}

?>