#!/usr/bin/perl
=begin -------------------------------------------------------------------------
calculates records overdue for authorisation > 2 working days after report date
=cut ---------------------------------------------------------------------------
use Getopt::Std;
getopts('d:t'); # days, testing
our($opt_d,$opt_t);
use strict;
use warnings;
my $JUST_TESTING = $opt_t || 0; # email to ra.jones only
################################################################################
my @recipients = qw( douglas bagguley raj );
my $duration = $opt_d || 2; # how many working days since report date
################################################################################
use lib '/home/raj/perl5/lib/perl5';
use Data::Dumper;
use Date::Calc qw(Delta_Days);
use FindBin qw($Bin); # warn $Bin;
use lib "$Bin/../../../lib";
use LIMS::Local::ScriptHelpers;
# get tools from LIMS::Local::ScriptHelpers:
my $tools = LIMS::Local::ScriptHelpers->new();
$tools->test_only($JUST_TESTING);
my $sql_lib = $tools->sql_lib();
my $config = $tools->config();
my $dbix = $tools->dbix();
my @rows = sprintf "%-8s %s %s\n", qw(LabNo Days Diagnosis);
my @trials;
my $query = $sql_lib->retr('unauthorised_requests');
my $result = $dbix->query($query);
RESULT:
while (my $ref = $result->hash) { # warn Dumper $ref; next;
# calculate working days since report date:
my $from = DateTime::Format::MySQL->parse_datetime($ref->{reported})->ymd;
my $to = $tools->time_now->ymd;
my $delta = LIMS::Local::Utils::delta_business_days($from, $to);
# skip if less than 3 days since report:
next RESULT if $delta <= $duration;
# put labno in brackets if CMP:
$ref->{labno} = sprintf '[%s]',
$ref->{labno} if $ref->{screened_as} =~ /Outreach/;
my $row = sprintf "%-8s %s %s",
$ref->{labno},
$delta,
$ref->{diagnosis};
$ref->{trial_case} ? push @trials, $row : push @rows, $row;
}
my $message = join "\n", @rows;
if ( @trials ) {
$message .= "\n\nClinical trial requests\n" . join "\n", @trials;
}
my %mail = (
config => $config,
subject => "Reports > $duration days unauthorised",
message => $message,
); # warn Dumper \%mail;
$tools->send_mail(\%mail, \@recipients);