%# BEGIN LICENSE BLOCK %# %# Copyright (c) 1996-2003 Jesse Vincent %# %# (Except where explictly superceded by other copyright notices) %# %# This work is made available to you under the terms of Version 2 of %# the GNU General Public License. A copy of that license should have %# been provided with this software, but in any event can be snarfed %# from www.gnu.org %# %# This work is distributed in the hope that it will be useful, but %# WITHOUT ANY WARRANTY; without even the implied warranty of %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU %# General Public License for more details. %# %# %# Unless otherwise specified, all modifications, corrections or %# extensions to this work which alter its source code become the %# property of Best Practical Solutions, LLC when submitted for %# inclusion in the work. %# %# %# END LICENSE BLOCK <%PERL> $m->comp("SELF:Heading$ReportAs", Text => loc('RTIR Periodic Report for [_1]', $Constituency) ); $m->comp("SELF:TwoColumnsTable$ReportAs", Body => [ [ loc('Start date'), $rtstart->AsString ], [ loc('End date'), $rtend->AsString ], ] ); $m->comp("SELF:TwoColumnsTable$ReportAs", Title => loc('Incident Report Summary (including unlinked Incident Reports)'), Body => [ [ loc('Outstanding reports at the beginning of this period'), scalar keys %$outstanding_pruned ], [ loc('Total new reports for this period'), scalar keys %$tix_created_pruned ], [ loc('Incident Reports resolved during this period'), scalar keys %$tix_resolved_pruned ], [ loc('Reports unresolved at the end of the period'), scalar keys %$tix_unresolved_pruned ], ], ); foreach my $incident_type( @functions ) { $m->comp("SELF:Heading$ReportAs", Level => 2, Text => $incident_type, ); my @table; foreach my $class (@classifications) { my $class_count = 0; $tix_created->GotoFirstItem(); my $filtered = $filter_by_incident_field->( $filter_by_incident_field->( $filter_by_incident_field->( $tix_created, 'Constituency', $Constituency ), 'Function', $incident_type, ), 'Classification', $class ne 'Unclassified'? $class: '', ); push @table, [ $class, scalar keys %{$filtered} ]; } $m->comp("SELF:TwoColumnsTable$ReportAs", Title => loc('Incident reports received'), Body => \@table, ); } $m->abort if $TextReport || $SpreadsheetReport; <%INIT> my $get_incident_field = sub { my $t = shift; my $field = shift; my $parent = RT::IR->Incidents( $t )->First; return undef unless $parent; return $parent->FirstCustomFieldValue( $field ); }; my $filter_by_incident_field = sub { my ($tickets, $field, $condition) = (@_); my $res = {}; unless ( ref $tickets eq 'HASH' ) { while ( my $t = $tickets->Next ) { $res->{ $t->Id } = $t } } else { %$res = %$tickets; } while( my($id, $t) = each %$res ) { my $value = $get_incident_field->( $t, $field ); delete $res->{$id} if $value? $value ne $condition: $condition; } return $res; }; my $ReportAs = 'HTML'; $ReportAs = 'Text' if $TextReport; $ReportAs = 'TSV' if $SpreadsheetReport; my $start = ParseDateToISO( $StartDate ); my $end = ParseDateToISO( $EndDate ); my $rtstart = RT::Date->new( $session{'CurrentUser'} ); $rtstart->Set( Format => 'ISO', Value => $start ); my $rtend = RT::Date->new( $session{'CurrentUser'} ); $rtend->Set( Format => 'ISO', Value => $end ); # of new reports created during the period broken down by function my @functions; { my $cf = RT::CustomField->new( $session{'CurrentUser'} ); $cf->LoadByNameAndQueue( Queue => 'Incidents', Name => 'Function' ); my $values = $cf->Values; while ( my $value = $values->Next ) { push @functions, $value->Name; } } # of new reports created during the period broken down by classification my @classifications; { my $cf = RT::CustomField->new( $session{'CurrentUser'} ); $cf->LoadByNameAndQueue( Queue => 'Incidents', Name => 'Classification' ); my $values = $cf->Values; while ( my $value = $values->Next ) { push @classifications, $value->Name; } push @classifications, 'Unclassified'; } # of new reports outstanding at start of the period my $outstanding = RT::Tickets->new( $session{'CurrentUser'} ); $outstanding->FromSQL("Queue = 'Incident Reports' AND Created < '$start' AND (Resolved = '1970-01-01 00:00:01' OR Resolved > '$start')"); my $outstanding_pruned = $filter_by_incident_field->($outstanding, 'Constituency', $Constituency); # of new reports created during the period my $tix_created = RT::Tickets->new( $session{'CurrentUser'} ); $tix_created->FromSQL("Queue = 'Incident Reports' AND Created > '$start' AND Created < '$end'"); my $tix_created_pruned = $filter_by_incident_field->( $tix_created, 'Constituency', $Constituency ); # of new reports resolved/closed/deleted during the period # this means "number of reports created during the period that were # also closed during the period(or before which is insane but possible)" my $tix_resolved = RT::Tickets->new( $session{'CurrentUser'} ); $tix_resolved->FromSQL("Queue = 'Incident Reports' AND Created > '$start' AND Created < '$end' AND (Resolved > '1970-01-01 00:00:01' AND Resolved < '$end')"); my $tix_resolved_pruned = $filter_by_incident_field->($tix_resolved, 'Constituency', $Constituency); # of new reports oustanding at end of the period # this is "number of reports created during the period that were also # closed during the period" my $tix_unresolved = RT::Tickets->new( $session{'CurrentUser'} ); $tix_unresolved->FromSQL("Queue = 'Incident Reports' AND Created > '$start' AND Created < '$end' AND ( Resolved = '1970-01-01 00:00:01' OR Resolved > '$end' )"); my $tix_unresolved_pruned = $filter_by_incident_field->($tix_unresolved, 'Constituency', $Constituency); if ( $TextReport ) { $r->content_type('text/plain'); } elsif( $SpreadsheetReport ) { $r->content_type('application/vnd.ms-excel'); } <%ARGS> $StartDate => undef $EndDate => undef $Constituency => undef $HTMLReport => 1 $TextReport => !$HTMLReport $SpreadsheetReport => !($HTMLReport || $TextReport) <%METHOD TwoColumnsTableText> <%ARGS> $Title => undef $Body => [] % if ( $Title ) { <% $Title |n %> % } % foreach my $line( @$Body ) { <% sprintf "%-$max[0]s", $line->[0] |n %> <% $line->[1] |n %> % } <%INIT> my @max = (0, 0); foreach my $line( @$Body ) { for( my $col = 0; $col < @$line; $col++ ) { $max[$col] = length $line->[$col] if length $line->[$col] > $max[$col]; } } <%METHOD TwoColumnsTableTSV> <%ARGS> $Title => undef $Body => [] % if ( $Title ) { <% $Title |n %> % } % foreach my $line( @$Body ) { <% $line->[0] |n %> <% $line->[1] |n %> % } <%METHOD TwoColumnsTableHTML> <%ARGS> $Title => undef $Body => [] % if ( $Title ) { % } % foreach my $line( @$Body ) { % }
<% $Title %>
<% $line->[0] %><% $line->[1] %>
<%METHOD HeadingText> <%ARGS> $Text => undef <% $Text |n %> <%METHOD HeadingTSV> <%ARGS> $Text => undef <% $Text |n %> <%METHOD HeadingHTML> <%ARGS> $Text => undef $Level => 1 ><% $Text %>>