<?php

function get_direct_purchase_requisition_headers($jo_id) {
    $sql = "SELECT sa.id,
                sa.trans_no,
                sa.reference,
                sa.type,
                l.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 
            FROM ".TB_PREF."direct_purchase_requisitions sa  
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code        
            WHERE sa.type = ".db_escape(ST_DIRECT_PURCHASE_REQ)." 
                AND sa.jo_id = ".db_escape($jo_id);
    
    return db_query($sql, "get_direct_purchase_requisition_headers");
}

function write_direct_purchase_requisition_header($type, &$trans_no, $id, $reference, $project_id, $location, $comments, $date_, $jo_id=0, $source_type=-1, $source_trans_no=0, 
                                                  &$row_state=0) {
    global $Refs;
     
        if ($id > 0) {
            $af_row = update_direct_purchase_requisition_header($id, $reference, $location, $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_direct_purchase_requisition_header($type, $trans_no, $reference, $project_id, $location, $comments, $date_, $jo_id, $source_type, $source_trans_no);
        }
    
        $Refs->save($type, $trans_no, $reference);

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

        return $id;
}

function write_direct_purchase_requisition_header_by_type_trans_no($type, $trans_no, $reference, $project_id, $location, $comments, $date, $source_id=0) {
    global $Refs;
     
        if ($trans_no > 0) {
            $af_row = update_direct_purchase_requisition_header_by_type_trans_no($type, $trans_no, $reference, $location, $comments, $date);
                if (! ($af_row > 0)) {
                    return 0;
                }
        } else {
            $trans_no = get_next_trans_no($type);
            $inser_id = insert_direct_purchase_requisition_header($type, $trans_no, $reference, $project_id, $location, $comments, $date, $source_id);
                if (! ($inser_id > 0)) {
                    return 0;
                }
        }
    
        $Refs->save($type, $trans_no, $reference);

        return $trans_no;
}
     
function insert_direct_purchase_requisition_header($type, $trans_no, $reference, $project_id, $location, $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."direct_purchase_requisitions
                      ( type, trans_no, reference, project_id, 
                        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($location).", ".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_direct_purchase_requisition_header');
            return db_insert_id();        
}

function update_direct_purchase_requisition_header($id, $reference, $location, $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."direct_purchase_requisitions
                SET reference = ".db_escape($reference).",
                    loc_code = ".db_escape($location).",                          
                    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_direct_purchase_requisition_header");
            return db_num_affected_rows();
}

function update_direct_purchase_requisition_header_by_type_trans_no($type, $trans_no, $reference, $location, $comments, $date) {   
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."direct_purchase_requisitions
                SET reference = ".db_escape($reference).",
                    loc_code = ".db_escape($location).",                          
                    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_direct_purchase_requisition_header_by_type_trans_no");
            return db_num_affected_rows();
}

function write_direct_purchase_requisition_line($trans_no, &$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=-1, $source_trans_no=0, $source_line_id=0) {
    if ($id > 0) {
        return update_direct_purchase_requisition_line($id, $stock_id, $item_type, $item_description, $tran_unit, $tran_qty, $qty, $conversion_factor, $standard_cost, 
                                                       $project_site_id, $line_comment, $task_id);
    } else {
        $id = insert_direct_purchase_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);
        
        return $id;
    }
}

function update_direct_purchase_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) {
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
        $sql = "UPDATE ".TB_PREF."direct_purchase_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).",
                    updated_by = ".db_escape($updated_by).",
                    updated_on = ".db_escape($updated_on)." 
                WHERE trans_id = ".db_escape($line_id);

            db_query($sql, 'update_direct_purchase_requisition_line');
            return db_num_affected_rows();
}
        
function insert_direct_purchase_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=-1, $source_trans_no=0, $source_line_id=0) {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    if ($source_trans_no > 0 && $source_line_id > 0) {
        $sql = "INSERT INTO ".TB_PREF."direct_purchase_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, source_qty)        
                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_escape($qty).")";  
    } else {
        $sql = "INSERT INTO ".TB_PREF."direct_purchase_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_direct_purchase_requisition_line');
            return db_insert_id();  
}

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

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

function get_direct_purchase_requisition_header_type_trans_no($type, $trans_no) {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."direct_purchase_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.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_direct_purchase_requisition_header_type_trans_no");
        return db_fetch($result);
}

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

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

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

function get_direct_purchase_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."direct_purchase_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_direct_purchase_requisition_details");
}

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

function get_direct_purchase_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."direct_purchase_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_direct_purchase_requisition_detail_line_details");
        return db_fetch($result);
}

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

function update_direct_purchase_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."direct_purchase_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_direct_purchase_requisition_row_state");
    return db_num_affected_rows();
}

function get_sql_for_direct_purchase_requisitions_inquiry($trans_no, $jo_id, $has_job, $reference, $project_id, $from_date, $to_date, $loc_code, $created_via=-1, 
                                                          $ap_status=-1, $only_outstanding=false) {
    $sql = "SELECT sa.id,
                sa.jo_id,
                sa.trans_no,
                sa.reference,
                sa.type,
                l.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."direct_purchase_requisitions sa  
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.loc_code        
            WHERE sa.type = ".db_escape(ST_DIRECT_PURCHASE_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 ($loc_code != '') {        
                    $sql .= " AND sa.loc_code = ".db_escape($loc_code);
                }

                if ($created_via > -1) {        
                    $sql .= " AND sa.source_type = ".db_escape($created_via);
                }                      

                if ($ap_status > -1) {        
                    $sql .= " AND sa.approval_status = ".db_escape($ap_status);
                }                      
            }
    
        $sql .= " ORDER BY sa.tran_date DESC, sa.trans_no DESC";
    
        return $sql;
}

function update_direct_purchase_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."direct_purchase_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_direct_purchase_requisition_header_approval_status");
            return db_num_affected_rows();
}

function update_direct_purchase_requisition_header_totals($type, $trans_no, $row_state=0) {
    $lines_total = get_direct_purchase_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."direct_purchase_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_direct_purchase_requisition_header_totals");   
        return db_num_affected_rows();
}

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

function check_all_direct_purchase_requisition_lines_fully_checked($type, $trans_no) {
    $sql = "SELECT * 
            FROM ".TB_PREF."direct_purchase_requisition_details 
            WHERE type = ".db_escape($type)." 
                AND trans_no = ".db_escape($trans_no)." 
                AND qty > checked_quantity - returned_quantity";
            
        $result = db_query($sql, "check_all_direct_purchase_requisition_lines_fully_checked");
            if (db_num_rows($result) > 0) {
                return false;
            } 
            
        return true;
}

function get_direct_purchase_requisition_lines_quantity_by_source($source_type=-1, $source_trans_no=0, $source_line_id=0, $ap_array=array(), $source_qty=false, 
                                                                  $not_trans_id=0) {
    $qty_feild_name = 'ln.qty';
    
    if ($source_qty === true) {
        $qty_feild_name = 'ln.source_qty';
    }
    
    $sql = "SELECT SUM($qty_feild_name) total_base_qty 
            FROM ".TB_PREF."direct_purchase_requisition_details ln 
                JOIN ".TB_PREF."direct_purchase_requisitions hd  
                    ON ln.type = hd.type 
                        AND ln.trans_no = hd.trans_no  
            WHERE 1 = 1";
    
        if ($source_type > -1) {
            $sql .= " AND ln.source_type = ".db_escape($source_type);
        }
        if ($source_trans_no > 0) {
            $sql .= " AND ln.source_trans_no = ".db_escape($source_trans_no);
        }
        if ($source_line_id > 0) {
            $sql .= " AND ln.source_line_id = ".db_escape($source_line_id);
        }
        if (count($ap_array) > 0) {
            $sql .= " AND hd.approval_status IN (".implode(',', $ap_array).")";
        }
        if ($not_trans_id > 0) {
            $sql .= " AND ln.trans_id <> ".db_escape($not_trans_id);
        }

        $result = db_query($sql, "get_direct_purchase_requisition_lines_quantity_by_source");
        return db_result_value($result, 'total_base_qty');
}

function get_direct_purchase_requisition_headers_by_sources($source_type, $source_trans_no) {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."direct_purchase_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.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_direct_purchase_requisition_headers_by_sources");
}

function get_direct_purchase_requisition_lines_total_qty($type, $source_line_id, $ap_array=array(), $not_trans_id=0) {
    $sql = "SELECT SUM(ln.qty) total_qty 
            FROM ".TB_PREF."direct_purchase_requisition_details ln 
                JOIN ".TB_PREF."direct_purchase_requisitions hd  
                    ON ln.type = hd.type 
                        AND ln.trans_no = hd.trans_no  
            WHERE ln.source_line_id = ".db_escape($source_line_id)." 
                AND hd.type = ".db_escape($type);
    
        if (count($ap_array) > 0) {
            $sql .= " AND hd.approval_status IN (".implode(',', $ap_array).")";
        }
    
        if ($not_trans_id > 0) {
            $sql .= " AND ln.trans_id <> ".db_escape($not_trans_id);
        }
    
        $result = db_query($sql, 'get_direct_purchase_requisition_lines_total_qty');
        return db_result_value($result, 'total_qty');
}

function get_direct_purchase_requisition_headers_by_filters($jo_id=0, $id=0, $type=0, $trans_no=0, $loc_code='', $project_id=0, $from_date=null, $to_date=null, 
                                                            $order_by=null) {
    $sql = "SELECT sa.*,
                l.location_name,
                pm.description project_description,
                u.real_name requestor_name 
            FROM ".TB_PREF."direct_purchase_requisitions sa 
                JOIN ".TB_PREF."projects_master pm 
                    ON sa.project_id = pm.project_id 
                JOIN ".TB_PREF."locations l 
                    ON sa.loc_code = l.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($loc_code) > 0) {
            $sql .= " AND sa.loc_code = ".db_escape($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);
        }
        
        if ($order_by === null) {
            $sql .= " ORDER BY sa.tran_date, sa.id";
        } else {
            $sql .= " ORDER BY $order_by";
        }
    
        return db_query($sql, "get_direct_purchase_requisition_headers_by_filters");
}

function get_direct_purchase_requisition_lines_by_filters($jo_id=0, $id=0, $type=0, $trans_no=0, $tock_id='', $loc_code='', $project_id=0, $from_date=null, $to_date=null, 
                                                          $approval_status_array=array(), $order_by=null) {
    $sql = "SELECT dpr_d.*, dpr.tran_date, dpr.loc_code, dpr.total, dpr.approval_status, dpr.project_id  
            FROM ".TB_PREF."direct_purchase_requisition_details dpr_d 
                JOIN ".TB_PREF."direct_purchase_requisitions dpr  
                    ON dpr_d.type = dpr.type  
                        AND dpr_d.trans_no = dpr.trans_no   
            WHERE 1 = 1 ";
    
        if ($jo_id > 0) {
            $sql .= " AND dpr.jo_id = ".db_escape($jo_id);
        }
        if ($id > 0) {
            $sql .= " AND dpr_d.id = ".db_escape($id);
        }
        if ($type > 0) {
            $sql .= " AND dpr_d.type = ".db_escape($type);
        }
        if ($trans_no > 0) {
            $sql .= " AND dpr_d.trans_no = ".db_escape($trans_no);
        }
        if (strlen($loc_code) > 0) {
            $sql .= " AND dpr_d.loc_code = ".db_escape($loc_code);
        }
        if (strlen($tock_id) > 0) {
            $sql .= " AND dpr_d.stock_id = ".db_escape($tock_id);
        }
        if ($project_id > 0) {
            $sql .= " AND dpr_d.project_id = ".db_escape($project_id);
        }
        if ($from_date !== null) {
            $sql .= " AND dpr_d.tran_date >= ".db_escape($from_date);
        }
        if ($to_date !== null) {
            $sql .= " AND dpr_d.tran_date <= ".db_escape($to_date);
        }
        if (count($approval_status_array) > 0) {
            $sql .= " AND dpr.approval_status IN (".implode(',', $approval_status_array).")";
        }
        
        if ($order_by === null) {
            $sql .= " ORDER BY dpr.tran_date, dpr_d.trans_no";
        } else {
            $sql .= " ORDER BY $order_by";
        }
    
        return db_query($sql, "get_direct_purchase_requisition_lines_by_filters");
}

function get_sql_for_approve_direct_purchase_requisitions($current_user_id) {
    $sql = "SELECT dpr.id,
                dpr.trans_no,
                dpr.type,
                dpr.jo_id,
                dpr.reference,
                dpr.tran_date,                   
                l.location_name,                   
                dpr.project_id,                   
                pm.description project_description,
                dpr.total,
                c.real_name,
                dpr.created_on,
                dpr.approval_status 
            FROM ".TB_PREF."direct_purchase_requisitions dpr
                JOIN ".TB_PREF."projects_master pm 
                    ON dpr.project_id = pm.project_id
                JOIN ".TB_PREF."users c 
                    ON dpr.created_by = c.id  
                JOIN ".TB_PREF."approvers apr 
                    ON dpr.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 dpr.loc_code = l.loc_code   
            WHERE dpr.approval_status IN (".implode(',', array(AS_AWAITING_APPROVAL, AS_WITH_THE_APPROVER)).") 
                AND apr.user_id = ".db_escape($current_user_id);
      
        return $sql;
}

?>