#!/usr/bin/env perl

# ------------------------------------------------------------------------------
# emails genomics data report
# ------------------------------------------------------------------------------

BEGIN {	$ENV{CENTRE} = 'genomics' } # no need to set in cron

#===============================================================================
my $JUST_TESTING = 0; # skips email, saves xls file to disk
my @contacts = ( 'james.mcleod', 'parminder.kaur', 'richard.holmes', 'raj' );
#===============================================================================

use lib (
    '/home/raj/perl5/lib/perl5',
    '/home/raj/apps/HILIS4/lib',
);

use Spreadsheet::WriteExcel::Simple;
use LIMS::Local::ScriptHelpers;
use Data::Printer;
use Modern::Perl;
use FindBin; # warn $FindBin::Bin;

my $tools = LIMS::Local::ScriptHelpers->new();
$tools->test_only($JUST_TESTING);

my $config = $tools->config();
my $dbix   = $tools->dbix();

my $filename = 'genomics_data.xls';
my $subject  = sprintf 'Genomics data [%s]', $tools->time_now->dmy; # p $subject;

my $xl = Spreadsheet::WriteExcel::Simple->new;

# xl file headers:
chomp( my @headers = <DATA> ); # p \@headers; exit;
$xl->write_bold_row(\@headers);

my @data = do{
	my $sql = $tools->sql_lib->retr('genomics_data'); # p $sql; exit;
	$dbix->query($sql)->arrays;
};
$xl->write_row($_) for @data;

if ($JUST_TESTING) {
    $xl->save($FindBin::Bin . '/genomics_data.xls');
}
else {
    my %mail = (
        attachment => $xl->data,
        filename   => $filename,
        config     => $config,
        subject    => $subject,
    );
    $tools->send_mail(\%mail, \@contacts);
}

__DATA__
Username
Source
Lab Number
Participant ID
Presentation
Registration Date
Participant Type
Family ID
Approach Date
Follow up Date
Total Samples in Group
Consent Date
Blood Sample Taken
PaXgene RNA
EDTA #1 QC DateTime
Blood DNA QC Summary
Blood DNA Qubit
Blood DNA Lab Number
Clinic Sample Type
Tumour Sample Taken
Reason Sample Not Sent
Tumour Type
Tumour Size
Tumour DNA Lab Number
Tumour DNA Qubit
Tumour DNA QC Summary
Tumour DNA Qubit DateTime
Saliva Sample Taken
Bone Marrow Sample Taken
Reasons BM Sample Not Sent
BM Sample ID
BM DNA Lab Number
BM DNA Qubit
BM DNA EDTA #1 QC DateTime
BM DNA QC Summary
Saliva DNA Lab Number
Saliva DNA QC Summary
Saliva DNA Qubit
Saliva DNA Oragene #1 QC DateTime
DNA Sent to CCP
DNA Arrived CCP
DNA Dispatched DateTime
DNA Consignment Number
OMICS Sent to CCP
OMICS Arrived CCP
OMICS Dispatched DateTime
OMICS Consignment Number
Consent Withdrawal Date
Sent from Histopath
Received by Genetics
