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


function get_document_header_record($tran_type, $document_no, &$h_rec)
{   
    global $systypes_array;
    
    $info = array();
    
    $table = $num_field = $type_field = "";
    
    $info['tran_type'] = $tran_type;
    get_table_info($info, $table, $num_field, $type_field);    
    if(!(strlen($table) > 3)){
        return array("success" => false, "message" => "Unsupported transaction type - ".$systypes_array[$tran_type]);
    }

    $sql = "SELECT * FROM ".TB_PREF.$table." 
            WHERE $num_field = ".db_escape($document_no);
    
    if($type_field != null && strlen($type_field) > 0){
        $sql .= " AND ".$type_field = db_escape($tran_type);
    }

    $result = db_query($sql, "get_document_header_record");
    
    if(db_num_rows($result) > 0){
        $h_rec = db_fetch($result);
        return array("success" => true, "message" => "");
    }
    else{
        $h_rec = null;
        return array("success" => false, "message" => $systypes_array[$tran_type]." #".$document_no." not found");
    }
    
}


function update_document_next_approval_group($tran_type, $tran_no, $approval_status, $row_state, 
                                $message='', $display_status="", $project_id=0, $position_code="", $next_approval_group_id=0)
{
        
        $info['tran_type'] = $tran_type;
        $info['tran_no'] = $tran_no;
        $info['approval_status'] = $approval_status;
        $info['next_approval_group_id'] = $next_approval_group_id;
        $info['display_status'] = $display_status;
        $info['position_code'] = $position_code;
        $info['project_id'] = $project_id;
        $info['message'] = $message;
        $info['current_user_id'] = $_SESSION['wa_current_user']->user;
        $info['row_state'] = $row_state;

        get_table_info($info, $table, $num_field, $type_field);
        
        $sql = "UPDATE ".TB_PREF.$table."
                SET next_approval_group_id = ".db_escape($info['next_approval_group_id']).", 
                    row_state = ".db_escape(db_new_row_state($info['row_state']))."             
                        
                WHERE $num_field = ".db_escape($info['tran_no']);
                
                if(isset($type_field) && strlen($type_field) >  0){
                    $sql .= " AND $type_field = ".db_escape($info['tran_type']);
                }
                        
                $sql .= " AND row_state = ".db_escape($info['row_state']);

    	db_query($sql, "update_document_next_approval_group");
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            write_to_approval_log($info);
            return array("success" => true, "message" => $info['message']);
        }
        else{
            $info['message'] = "Document has been updated by another user. 
                                        Not possible to update this document.";  
            write_to_approval_log($info);
            return array("success" => false, "message" => $info['message']);
        }

}