<?php
/**********************************************************************
***********************************************************************/
function write_approval_group($selected, $approval_group_code, $approval_group_description)
{
    $sql = "SELECT * FROM ".TB_PREF.$table."
            WHERE $num_field = ".db_escape($document_no);

    $result = db_query($sql, "could not update document approval group");
    $row = db_fetch($result);
}

function update_document_approval_group($new_approval_group_id, $row_state)
{
        $time = date('Y-m-d H:i:s');

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

            $sql .= " AND approval_status IN (".db_escape(AS_APPROVED).", ".db_escape(AS_REJECTED).")"; 
        

    	db_query($sql, "could not update document approval status");
        
        $num_recs = db_num_affected_rows();
        if($num_recs == 1){
            write_to_approval_log($info);
            return array("success" => true, "message" => $info['message']);
        }
        else{
            switch ($info['change_type']) {
                case "REJECT":
                    $info['message'] = "Document has been updated by another user. 
                                        Not possible to update this document.";  
                    break;
                case "REOPEN":
                    $info['message'] = "Trying to reopen an unapproved document or the document has been updated by another user. 
                                        Not possible to update this document.";  
                    break;
                default:
                    $info['message'] = "Document has been updated by another user. 
                                        Not possible to update this document.";  
                    break;
            }
            
            write_to_approval_log($info);
            return array("success" => false, "message" => $info['message']);
        }

}

?>