#!/usr/local/bin/perl5.6.1
#######################################################################
# This software has been created by Genome Research Limited (GRL).    #
# GRL hereby grants permission to use, copy, modify and distribute    #
# this software and its documentation for non-commercial purposes     #
# without fee at the user's own risk on the basis set out below.      #
# GRL neither undertakes nor accepts any duty whether contractual or  #
# otherwise in connection with the software, its use or the use of    #
# any derivative, and makes no representations or warranties, express #
# or implied, concerning the software, its suitability, fitness for   #
# a particular purpose or non-infringement.                           #
# In no event shall the authors of the software or GRL be responsible #
# or liable for any loss or damage whatsoever arising in any way      #
# directly or indirectly out of the use of this software or its       #
# derivatives, even if advised of the possibility of such damage.     #
# Our software can be freely distributed under the conditions set out #
# above, and must contain this copyright notice.                      #
#######################################################################
# $Id: ETS_request_cron.pl,v 1.3 2003/07/15 13:26:16 holroyd Exp $

#######################################################################
#                                                                     #
# This script sends a reports detailing requests that have been       # 
# submitted for that team in the Entity Tracking System.              #
#                                                                     #
# The stored entity is owned by the team that created the entity.     #
# This is the team that will receive the email report.                #
#                                                                     #
# Data is gathered from the stored_entity, request_storage and        #
# freezer_request tables in the ETS database.                         #
#                                                                     #
# Author: Simon Holroyd (holroyd@sanger.ac.uk)                        #
# Date  : 14/05/2003                                                  #
#                                                                     #
#######################################################################

use strict;

################
# For Testing. #
################
#use lib "/nfs/team71/usg/holroyd/badger-bin/";

use ETS::EntityTracking::Report;
use ETS::EntityTracking::Request;
use ETS::EntityTracking::Email;
use ETS::EntityTracking::ErrorHandling;

my $summary = "\n\n"; ## A summary of this execution;

my @request_data = (
                    {
                        'team'  =>76,
                        'email' =>'team76g'
                        }
                    );

##############################
# Create the generic header. #
##############################
my $header = 
qq(
----------------------------------------------------------

This email contains a summary of the outstanding requests 
in the Entity Tracking System (ETS).

To complete the requests type ets on a command line and 
press the 'Requests' button.

----------------------------------------------------------

);


foreach (@request_data)
{
    my $request_count = 0;
    my $error_count   = 0;

    my $report_message = $header; ## string to contain the report requests;
                                  ## initialised to hold the generic header;

    my $output;  ## the outstanding requests or errors.
    
    ###############################################################
    # Run the report that returns an outstanding requests summary #
    # for this team.                                              #
    ###############################################################
    $output = Report->request_report(-team=>$_->{team});
   
    #########################################
    # Check for errors and store the count. #
    #########################################
    if($error_count = ErrorHandling->get_action_count()) 
    {
        ###########################################
        # Set the message to be an error warning. #
        ###########################################
        $output = ErrorHandling->get_technical_errors();
        
        #########################################
        # Append an error to the message so the #
        # user knows what's going on.           #
        #########################################
        $report_message = $report_message . 
            "ERROR: Problems gathering a list of outstanding requests for your team.\n" . 
            "ERROR: Please send this mail to bug\@sanger.ac.uk\n\n";

        ############################
        # Clean the action errors. #
        ############################
        ErrorHandling->clean_action_errors();

        ################################
        # Send me a copy of the email. #
        ################################
        $_->{email} .= ",holroyd";

    }
    else
    {
        #############################
        # Store the requests count. #
        #############################
        $request_count = scalar(@{$output}) - 2; ## Minus 2 to remove header and underlines;
        
        #################################
        # Append the count of requests. #
        #################################
        $report_message .= "No. of Requests: $request_count\n\n";
    }
    
    ###################################################
    # Concatenate the report elements in to a string. #
    ###################################################
    foreach(@{$output})
    {
        $report_message = $report_message . $_ . "\n";
    }
    
    ####################################
    # To be tidy append two new lines. #
    ####################################
    $report_message .= "\n\n";

    ##################
    # Send an email. #
    ##################
    send_email($_->{email},"ETS Request Summary for Team$_->{team}",$report_message);

    ############################################
    # Check for errors when sending the email. #
    ############################################
    if(ErrorHandling->get_action_count())
    {
        ############################
        # Clean the action errors. #
        ############################
        ErrorHandling->clean_action_errors();
    }
    else
    {
        ###################
        # Create summary. #
        ###################
        $summary .= "o team$_->{team}: $request_count requests | $error_count errors | email sent to $_->{email}\n";
    }
}

#########################################
# Send me a summary of all of the data. #
#########################################
send_email("holroyd","ETS Request Summary","\nThis is a summary of ETS_request_cron.pl.\n" . $summary);


############################################## SUB ROUTINES ##########################################################

sub send_email
{
    my ($to,$subject,$message) = @_;

    ##########################################
    # Email the message to this teams email. #
    ##########################################
    Email->send_email(-to     =>$to,
                      -subject=>$subject,
                      -message=>$message);
    
    return();
}


__END__

=head1 NAME

 ETS_request_cron.pl

=head1 SYNOPSIS

 ETS_request_cron.pl

=head1 DESCRIPTION

 Script to report request details from the ETS system. Teams and their emails
 are hardcoded.

 This script should run every morning.

=head1 AUTHOR

 Simon Holroyd

=head1 HISTORY

 # $Log: ETS_request_cron.pl,v $
% Revision 1.3  2003/07/15  13:26:16  holroyd
% *** empty log message ***
%
% Revision 1.2  2003/06/25  09:55:58  holroyd
% *** empty log message ***
%
% Revision 1.1  2003/06/24  13:47:09  holroyd
% Initial revision
%

=cut
