package LIMS::Model::PrintRun;
use Moose;
extends 'LIMS::Model::Base';
with 'LIMS::Model::Roles::Query';
use namespace::clean -except => 'meta';
__PACKAGE__->meta->make_immutable;
use Data::Dumper;
use LIMS::Local::Utils;
#-------------------------------------------------------------------------------
sub get_print_run_size {
my ($self, $h) = @_; # warn Dumper $h; # hashref of start & end datetimes + optional cfg
# just forward to get_print_run_request_ids and return count:
my $ids = $self->get_print_run_request_ids($h);
my $i = scalar @$ids; # warn $i;
return $i;
}
#-------------------------------------------------------------------------------
=begin # see trend_analysis()
my ($sql, @bind) = $self->sqla_query(\%sqla_args);
my $results = $self->lims_dbix->query($sql, @bind)->list; # warn Dumper $results;
=cut
sub get_print_run_request_ids {
my ($self, $h) = @_; # warn Dumper $h; # hashref of start & end datetimes + optional cfg
my ($start_date, $end_date) = @{$h}{ qw(start_date end_date) };
# set status level required for printing reports:
my $status = $self->does_authorisation() ? 'authorised' : 'reported';
my @query = (
'request_report.updated_at' => { ge_le => [ $start_date, $end_date ] },
'status_option.description' => [ $status, 'complete' ],
);
my @required_tables = qw(request_report status_options);
my @with_tables; # for left join if required
# exclude skipped locations/departments if configured
if ( my $ref = $h->{skip_reports} ) {
my $exclusions = _get_excluded_locations($ref); # arrayref
push @query, ( or => $exclusions ); # THIS NEEDS TO BE NEGATED!!
push @required_tables, qw(parent_organisations referrer_departments);
}
# exclude clinical trial reports if configured:
if ( $h->{skip_trials} ) {
push @with_tables, 'clinical_trials'; # for left join
push @query, ( 'request_trial.request_id' => undef );
}
push @required_tables, 'referrers' if $h->{sort_by}; # only needed for sorting
my %query_args = (
select => [ 'id' ], # only need id's
query => \@query,
require_objects => $self->get_relationships(\@required_tables),
with_objects => $self->get_relationships(\@with_tables), # OK to be empty
); # warn Dumper \%query_args;
if ( defined $h->{offset} ) { # can be zero
$query_args{$_} = $h->{$_} for qw(offset limit);
}
$query_args{sort_by} = $h->{sort_by} if $h->{sort_by}; # warn Dumper \%query_args;
my $requests = LIMS::DB::Request::Manager->get_requests(%query_args);
my @request_ids = map { $_->id } @$requests; # warn scalar @request_ids;
return \@request_ids;
}
sub _get_excluded_locations {
my $ref = shift;
my @ary;
if ( my $org_codes = $ref->{organisation} ) {
push @ary, 'parent_code' => $org_codes;
}
if ( my $hospitals = $ref->{hospital} ) {
push @ary, 'organisation_code' => $hospitals;
}
if ( my $departments = $ref->{department} ) { # warn Dumper $departments;
for (@$departments) {
my ($site, $code) = split '/'; # warn Dumper [$site, $code];
my $sql = # can't be done in QueryBuilder - patch needed
sprintf q!(%s = ? AND hospital_department_code = ?)!,
length $site > 3 # 4 or more chars will be organisation code:
? 'organisation_code'
: 'parent_code'; # warn Dumper $sql;
push @ary, [ \$sql, $site, $code ];
}
} # warn Dumper \@ary;
return \@ary;
}
1;