From 64ba3ffe8309da1e109bbbae49b6c567b6f9d49f Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Mon, 21 Jul 2008 18:57:45 -0500 Subject: [PATCH] Refine "Patrons statistics" report, fix highlight, remove CGI::scrolling_lists. At client request, I added code for a rowtitle_display and coltitle_display. This allows the script to substitute human-readable lables into the table instead of just the literal hashkeys. For this client with dozens of numerical patron categorycodes having a row titled "29" was not very useful. I cleaned up the templates and a lot of the report code. It is of the same style as the other copy/paste reports and still need further cleanup. Added sql and debug feedback (default styled to hidden), as I have for other reports. Signed-off-by: Joshua Ferraro --- .../en/modules/reports/borrowers_stats.tmpl | 151 ++++++----- reports/borrowers_stats.pl | 239 ++++++------------ 2 files changed, 161 insertions(+), 229 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/borrowers_stats.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/borrowers_stats.tmpl index c83da5fbeb..217f895901 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/reports/borrowers_stats.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reports/borrowers_stats.tmpl @@ -2,12 +2,16 @@ Koha › Reports <!-- TMPL_IF NAME="do_it" -->› Patrons statistics › Results<!-- TMPL_ELSE -->› Patrons statistics<!-- /TMPL_IF --> + - +
@@ -21,40 +25,35 @@

Filtered on

-

=

+ class="debug" class="sql"> + = +

- + - - + class="highlight"> + - - - + + - + - + @@ -74,47 +73,48 @@ - + - + - - - + + - + - - - - - - - - + - - + + - + - - + - + + + + + - - - - - - - - - + + + - - + + - + - - + + class="highlight"> - + diff --git a/reports/borrowers_stats.pl b/reports/borrowers_stats.pl index 45e6bc777d..df9e4df2e1 100755 --- a/reports/borrowers_stats.pl +++ b/reports/borrowers_stats.pl @@ -56,6 +56,9 @@ my $output = $input->param("output"); my $basename = $input->param("basename"); my $mime = $input->param("MIME"); my $del = $input->param("sep"); +my $selected_branch; # = $input->param("?"); + +our $branches = GetBranches; my ($template, $borrowernumber, $cookie) = get_template_and_user({template_name => $fullreportname, @@ -71,12 +74,11 @@ if ($do_it) { if ($output eq "screen"){ $template->param(mainloop => $results); output_html_with_http_headers $input, $cookie, $template->output; - exit(1); } else { print $input->header(-type => 'application/vnd.sun.xml.calc', - -encoding => 'utf-8', - -name=>"$basename.csv", - -attachment=>"$basename.csv"); + -encoding => 'utf-8', + -name => "$basename.csv", + -attachment => "$basename.csv"); my $cols = @$results[0]->{loopcol}; my $lines = @$results[0]->{looprow}; my $sep; @@ -101,109 +103,29 @@ if ($do_it) { print $sep.$col->{totalcol}; } print $sep.@$results[0]->{total}; - exit(1); } + exit(1); # exit after do_it, regardless } else { my $dbh = C4::Context->dbh; - my @values; - my %labels; my $req; - $req = $dbh->prepare( "select categorycode, description from categories order by description"); - $req->execute; - my %select_catcode; - my @select_catcode; - push @select_catcode,""; - $select_catcode{""} =""; - while (my ($catcode, $description) =$req->fetchrow) { - push @select_catcode, $catcode; - $select_catcode{$catcode} = $description; - } - my $CGICatCode=CGI::scrolling_list( -name => 'Filter', - -id => 'catcode', - -values => \@select_catcode, - -labels => \%select_catcode, - -size => 1, - -multiple => 0 ); - - -my $branches = GetBranches; -my @branchloop; -my @select_branch; -#my %select_branches; -push @select_branch,""; -#$select_branches{""}=""; -foreach my $thisbranch (sort keys %$branches) { - push @select_branch,$thisbranch; - # my $selected = 1 if $thisbranch eq $branch; - my %row =(value => $thisbranch, -# selected => $selected, - branchname => $branches->{$thisbranch}->{'branchname'}, - ); - push @branchloop, \%row; -} - my $CGIBranch=CGI::scrolling_list( -name => 'Filter', - -id => 'branch', - -values => \@select_branch, -# -labels => \%select_branches, - -size => 1, - -multiple => 0 ); - - - $req = $dbh->prepare( "select distinctrow zipcode from borrowers order by zipcode"); + $template->param( CAT_LOOP => &catcode_aref); + my @branchloop; + foreach (sort {$branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}} keys %$branches) { + my $line = {branchcode => $_, branchname => $branches->{$_}->{branchname} || 'UNKNOWN'}; + $line->{selected} = 'selected' if ($selected_branch and $selected_branch eq $_); + push @branchloop, $line; + } + $template->param(BRANCH_LOOP => \@branchloop); + $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode"); $req->execute; - my @select_zipcode; - push @select_zipcode,""; - while (my ($value) =$req->fetchrow) { - if ($value) { - push @select_zipcode, $value; - } - } -# - my $CGIZipCode=CGI::scrolling_list( -name => 'Filter', - -id => 'zipcode', - -values => \@select_zipcode, - -size => 1, - -multiple => 0 ); - - - $req = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' order by lib"); + $template->param( ZIP_LOOP => $req->fetchall_arrayref({})); + $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib"); $req->execute; - my @select_sort1; - my %select_sort1; - push @select_sort1,""; - $select_sort1{""}=""; - my $hassort1; - while (my ($auth_value,$lib) =$req->fetchrow) { - if ($auth_value) { - $hassort1=1; - push @select_sort1, $auth_value; - $select_sort1{$auth_value}=$lib - } - } -# - my $CGIsort1=CGI::scrolling_list( -name => 'Filter', - -id => 'sort1', - -values => \@select_sort1, - -labels =>\%select_sort1, - -size => 1, - -multiple => 0 ); - - $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2"); + $template->param( SORT1_LOOP => $req->fetchall_arrayref({})); + $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort <> '' ORDER BY sort2 LIMIT 200"); + # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance. $req->execute; - my @select_sort2; - push @select_sort2,""; - my $hassort2; - while (my ($value) =$req->fetchrow) { - if ($value) { - $hassort2 = 1; - push @select_sort2, $value; - } - } - my $CGIsort2=CGI::scrolling_list( -name => 'Filter', - -id => 'sort2', - -values => \@select_sort2, - -size => 1, - -multiple => 0 ); + $template->param( SORT2_LOOP => $req->fetchall_arrayref({})); my @mime = ( C4::Context->preference("MIME") ); # warn 'MIME(s): ' . join ' ', @mime; @@ -213,7 +135,6 @@ foreach my $thisbranch (sort keys %$branches) { -values => \@mime, -size => 1, -multiple => 0 ); - my @dels = ( C4::Context->preference("delimiter") ); my $CGIsepChoice=CGI::scrolling_list( -name => 'sep', @@ -221,23 +142,28 @@ foreach my $thisbranch (sort keys %$branches) { -values => \@dels, -size => 1, -multiple => 0 ); - - $template->param( CGICatCode => $CGICatCode, - CGIZipCode => $CGIZipCode, - CGISort1 => $CGIsort1, - hassort1 => $hassort1, - CGISort2 => $CGIsort2, - hassort2 => $hassort2, - CGIextChoice => $CGIextChoice, - CGIsepChoice => $CGIsepChoice, - CGIBranch => $CGIBranch, - DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), - ); + $template->param( + CGIextChoice => $CGIextChoice, + CGIsepChoice => $CGIsepChoice, + DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(), + ); } output_html_with_http_headers $input, $cookie, $template->output; - +sub catcode_aref() { + my $req = C4::Context->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description"); + $req->execute; + return $req->fetchall_arrayref({}); +} +sub catcodes_hash() { + my %cathash; + my $catcodes = &catcode_aref; + foreach (@$catcodes) { + $cathash{$_->{categorycode}} = ($_->{description} || 'NO_DESCRIPTION') . " ($_->{categorycode})"; + } + return %cathash; +} sub calculate { my ($line, $column, $digits, $status, $activity, $filters) = @_; @@ -258,7 +184,6 @@ sub calculate { # warn "filtres ".@filters[5]; # warn "filtres ".@filters[6]; - $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ; $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ; $linefilter = @$filters[2] if ($line =~ /branchcode/ ) ; @@ -287,17 +212,13 @@ sub calculate { push @loopfilter, \%cell; } } - if ($status) { - push @loopfilter,{crit=>"Status",filter=>$status} - } - - if ($activity) { - push @loopfilter,{crit=>"Activity",filter=>$activity}; - } + ($status ) and push @loopfilter,{crit=>"Status", filter=>$status }; + ($activity) and push @loopfilter,{crit=>"Activity",filter=>$activity}; + push @loopfilter,{debug=>1, crit=>"Branches",filter=>join(" ", sort keys %$branches)}; + push @loopfilter,{debug=>1, crit=>"(line, column)", filter=>"($line,$column)"}; # year of activity my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0); my $newperioddate=$period_year."-".$period_month."-".$period_day; -# warn "PERIOD".$period; # 1st, loop rows. my $linefield; if (($line =~/zipcode/) and ($digits)) { @@ -305,29 +226,31 @@ sub calculate { } else{ $linefield .= $line; } - - my $strsth; - $strsth .= "select distinctrow $linefield from borrowers where $line is not null "; + + my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ? &catcodes_hash : (); + push @loopfilter, {debug=>1, crit=>"\%cathash", filter=>join(", ", map {$cathash{$_}} sort keys %cathash)}; + + my $strsth = "SELECT distinctrow $linefield FROM borrowers WHERE $line IS NOT NULL "; $linefilter =~ s/\*/%/g; if ( $linefilter ) { - $strsth .= " and $linefield LIKE ? " ; + $strsth .= " AND $linefield LIKE ? " ; } - $strsth .= " and $status='1' " if ($status); + $strsth .= " AND $status='1' " if ($status); $strsth .=" order by $linefield"; -# warn "". $strsth; - my $sth = $dbh->prepare( $strsth ); + push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth}; + my $sth = $dbh->prepare($strsth); if ( $linefilter ) { $sth->execute($linefilter); } else { $sth->execute; } - while ( my ($celvalue) = $sth->fetchrow) { + while (my ($celvalue) = $sth->fetchrow) { my %cell; if ($celvalue) { $cell{rowtitle} = $celvalue; -# } else { -# $cell{rowtitle} = ""; + # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue; + $cell{rowtitle_display} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode'); } $cell{totalrow} = 0; push @loopline, \%cell; @@ -336,20 +259,19 @@ sub calculate { # 2nd, loop cols. my $colfield; if (($column =~/zipcode/) and ($digits)) { - $colfield .= "left($column,$digits)"; - } else{ - $colfield .= $column; + $colfield = "left($column,$digits)"; + } else { + $colfield = $column; } - my $strsth2; - $colfilter =~ s/\*/%/g; - $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null"; - if ( $colfilter ) { - $strsth2 .= " and $colfield LIKE ? "; + my $strsth2 = "select distinctrow $colfield from borrowers where $column is not null"; + if ($colfilter) { + $colfilter =~ s/\*/%/g; + $strsth2 .= " AND $colfield LIKE ? "; } - $strsth2 .= " and $status='1' " if ($status); + $strsth2 .= " AND $status='1' " if ($status); $strsth2 .= " order by $colfield"; -# warn "". $strsth2; - my $sth2 = $dbh->prepare( $strsth2 ); + push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth2}; + my $sth2 = $dbh->prepare($strsth2); if ($colfilter) { $sth2->execute($colfilter); } else { @@ -357,27 +279,25 @@ sub calculate { } while (my ($celvalue) = $sth2->fetchrow) { my %cell; - my %ft; if ($celvalue) { $cell{coltitle} = $celvalue; + # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue; + $cell{coltitle_display} = $cathash{$celvalue} if ($column eq 'categorycode'); } push @loopcol, \%cell; } - my $i=0; - my @totalcol; - my $hilighted=-1; - #Initialization of cell values..... my %table; # warn "init table"; - foreach my $row ( @loopline ) { + foreach my $row (@loopline) { foreach my $col ( @loopcol ) { # warn " init table : $row->{rowtitle} / $col->{coltitle} "; $table{$row->{rowtitle}}->{$col->{coltitle}}=0; } $table{$row->{rowtitle}}->{totalrow}=0; + $table{$row->{rowtitle}}->{rowtitle_display} = $row->{rowtitle_display}; } # preparing calculation @@ -400,10 +320,9 @@ sub calculate { $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive'); $strcalc .= " AND $status='1' " if ($status); $strcalc .= " group by $linefield, $colfield"; -# warn "". $strcalc; + push @loopfilter, {sql=>1, crit=>"Query", filter=>$strcalc}; my $dbcalc = $dbh->prepare($strcalc); $dbcalc->execute; -# warn "filling table"; my $emptycol; while (my ($row, $col, $value) = $dbcalc->fetchrow) { @@ -419,20 +338,20 @@ sub calculate { push @loopcol,{coltitle => "NULL"} if ($emptycol); - foreach my $row ( sort keys %table ) { + foreach my $row (sort keys %table) { my @loopcell; #@loopcol ensures the order for columns is common with column titles # and the number matches the number of columns foreach my $col ( @loopcol ) { my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}}; - push @loopcell, {value => $value } ; + push @loopcell, {value => $value}; } - push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row, - 'loopcell' => \@loopcell, - 'hilighted' => ($hilighted >0), - 'totalrow' => $table{$row}->{totalrow} - }; - $hilighted = -$hilighted; + push @looprow,{ + 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row, + 'rowtitle_display' => $table{$row}->{rowtitle_display} || $row, + 'loopcell' => \@loopcell, + 'totalrow' => $table{$row}->{totalrow} + }; } foreach my $col ( @loopcol ) { -- 2.39.5
/ + TOTAL
- + - - -   - - - +  
TOTAL - -
Patron category +
Patron status
Patron activity
   
- Zip Code -
Zip Code +
 
 
Library - Library +
Date of Birth +
Date of Birth /lib/calendar/cal.gif" alt="Show Calendar" border="0" id="openCalendarFrom" style="cursor: pointer;" /> @@ -195,34 +197,45 @@ } } - Calendar.setup( - { + Calendar.setup({ inputField : "to", ifFormat : "", button : "openCalendarTo", disableFunc : validate2, dateStatusFunc : validate2 - } - ); + }); //]]>
Sort1 + +
Sort2 +