RSS Git Download  Clone
Raw Blame History
package LIMS::Model::Audit;

use Moose;
extends 'LIMS::Model::Base';
use namespace::clean -except => 'meta';
__PACKAGE__->meta->make_immutable;

use Data::Dumper;
use LIMS::Local::Utils;

#-------------------------------------------------------------------------------
sub turnaround_times {
    my ($self, $selection) = @_;
    
    my $sql = $self->_get_turnaround_times_query_for($selection); # $self->debug($args);
    
	my $dbix = $self->lims_dbix;

    my $duration = DateTime->today->subtract( years => 1 )->ymd;
    
    my $query = $dbix->query( $sql, $duration ); # warn Dumper $query;
    
    # for calculation of delta workings days:
    my $delta_business_days = sub { LIMS::Local::Utils::delta_business_days(@_) };
    
    my %data; # controller needs array for sorting, but easier to create hashref for 'count'
    while ( my $vars = $query->hash ) {
        my $specimen = $vars->{sample_code}; # warn $specimen;
        my $screened = $vars->{description};
        
        # need to split PB's according to request:
        if ($specimen eq 'PB') {
            $specimen = 'PB [PNH]' if $screened eq 'PNH';
            $specimen = 'PB [HIV]' if $screened eq 'HIV';
            $specimen = 'PB [CMP]' if $screened eq 'Community monitoring';
            $specimen = 'PB [Mol]' if $screened =~ /^(CMPD|Chimerism|Molec|CML follow-up)/;
        }
        
        # get registered, reported & authorised dates; using DATE_FORMAT in sql
        # MUCH faster than dt conversion afterwards:
        my ($registered, $reported, $authorised)
            = @{$vars}{ qw(registered reported authorised) };

        # calculate registered => reported & registered => auth'ed durations:
        my $delta_report    = &$delta_business_days($registered, $reported);
        my $delta_authorise = &$delta_business_days($registered, $authorised);

        $data{$specimen}{count}++; # increment specimen count
        $data{$specimen}{delta_report} += $delta_report;
        $data{$specimen}{delta_authorise} += $delta_authorise;
    } # warn Dumper \%data;
    return \%data;
}

#-------------------------------------------------------------------------------
sub nhs_number_supplied {
    my $self = shift;
    
	my $dbix = $self->lims_dbix;

    my $duration = DateTime->today->subtract( years => 1 )->ymd;

    my $sql = $self->sql_lib->retr('nhs_number_compliance');
    
    my $data = $dbix->query( $sql, $duration )->hashes; # warn Dumper $data;
    return $data;    
}

#-------------------------------------------------------------------------------
sub revised_diagnoses {
    my $self = shift;
    
	my $dbix = $self->lims_dbix;

    my $duration = DateTime->today->subtract( months => 3 )->ymd;

    my $sql = $self->sql_lib->retr('revised_diagnoses');
    
    my $data = $dbix->query( $sql, $duration )->hashes; # warn Dumper $data;
    return $data;    
}

#-------------------------------------------------------------------------------
sub _get_turnaround_times_query_for {
    my ($self, $selection) = @_;
    
    my %t = (
        specimen => 'turnaround_times_specimen',
    );
    
	my $authorisation_active = $self->does_authorisation();
	
    my $sql = $self->sql_lib->retr($t{$selection});
    return $sql;
}

1;