Refine "Patrons statistics" report, fix highlight, remove CGI::scrolling_lists.
authorJoe Atzberger <joe.atzberger@liblime.com>
Mon, 21 Jul 2008 23:57:45 +0000 (18:57 -0500)
committerJoshua Ferraro <jmf@liblime.com>
Tue, 22 Jul 2008 20:44:58 +0000 (15:44 -0500)
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 <jmf@liblime.com>
koha-tmpl/intranet-tmpl/prog/en/modules/reports/borrowers_stats.tmpl
reports/borrowers_stats.pl

index c83da5f..217f895 100644 (file)
@@ -2,12 +2,16 @@
 <title>Koha &rsaquo; Reports <!-- TMPL_IF NAME="do_it" -->&rsaquo; Patrons statistics &rsaquo; Results<!-- TMPL_ELSE -->&rsaquo; Patrons statistics<!-- /TMPL_IF --></title>
 <!-- TMPL_INCLUDE NAME="doc-head-close.inc" -->
 <!-- TMPL_INCLUDE NAME="calendar.inc" -->
+<style type="text/css">
+       .sql {display:none;}
+       .debug {display:none;}
+</style>
 </head>
 <body>
 <!-- TMPL_INCLUDE NAME="header.inc" -->
 <!-- TMPL_INCLUDE NAME="cat-search.inc" -->
 
-<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a> <!-- TMPL_IF NAME="do_it" -->&rsaquo; <a href="/cgi-bin/koha/reports/borrowers_stats.pl">Patrons statistics</a> &rsaquo; Results<!-- TMPL_ELSE -->&rsaquo; Patrons statistics<!-- /TMPL_IF --></div>
+<div id="breadcrumbs"><a href="/cgi-bin/koha/mainpage.pl">Home</a> &rsaquo; <a href="/cgi-bin/koha/reports/reports-home.pl">Reports</a> &rsaquo; <a href="/cgi-bin/koha/reports/borrowers_stats.pl">Patrons statistics</a><!-- TMPL_IF NAME="do_it"--> &rsaquo; Results<!-- /TMPL_IF --></div>
 
 <div id="doc3" class="yui-t2">
    
                <!-- TMPL_IF NAME="loopfilter"-->
                        <p>Filtered on</p>
                        <!-- TMPL_LOOP NAME="loopfilter" -->
-                                       <p> <!-- TMPL_VAR NAME="crit"--> =<!-- TMPL_VAR NAME="filter"--></p>
+                               <p<!-- TMPL_IF NAME="debug"--> class="debug"<!-- TMPL_ELSIF NAME="sql" --> class="sql"<!-- /TMPL_IF -->>
+                                       <!-- TMPL_VAR NAME="crit"--> = <!-- TMPL_VAR NAME="filter"-->
+                               </p>
                        <!-- /TMPL_LOOP -->
                <!-- /TMPL_IF -->
                <table>
                        <tr>
                                <th><!-- TMPL_VAR name="line" --> / <!-- TMPL_VAR name="column" --></th>
                                <!-- TMPL_LOOP NAME="loopcol" -->
-                                       <th><!-- TMPL_VAR NAME="coltitle" --></th>
+                                       <th><!-- TMPL_IF NAME="coltitle_display" --><!-- TMPL_VAR NAME="coltitle_display" --><!-- TMPL_ELSE --><!-- TMPL_VAR NAME="coltitle" --><!-- /TMPL_IF -->
+                                       </th>
                                <!-- /TMPL_LOOP -->
                                <th>TOTAL</th>
                        </tr>
                                <!-- TMPL_LOOP NAME="looprow" -->
-                                       <tr>
-                                               <!-- TMPL_IF NAME="hilighted" --><td class="hilighted"><!-- TMPL_ELSE --><td><!-- /TMPL_IF -->
-                                               <!-- TMPL_VAR NAME="rowtitle" --></td>
+                                       <tr<!-- TMPL_UNLESS NAME="__odd__" --> class="highlight"<!-- /TMPL_UNLESS -->>
+                                               <td><!-- TMPL_IF NAME="rowtitle_display" --><!-- TMPL_VAR NAME="rowtitle_display" --><!-- TMPL_ELSE --><!-- TMPL_VAR NAME="rowtitle" --><!-- /TMPL_IF -->
+                                               </td>
                                                <!-- TMPL_LOOP NAME="loopcell" -->
-                                                       <!-- TMPL_IF NAME="hilighted" --><td><!-- TMPL_ELSE --><td><!-- /TMPL_IF -->
-                                                               <!--TMPL_IF NAME="value" --><!-- TMPL_VAR NAME="value" -->
-                                                               <!--TMPL_ELSE-->
-                                                                       &nbsp;
-                                                               <!--/TMPL_IF-->
-                                                               </td>
-                                               <!-- /TMPL_LOOP -->
-                                               <!-- TMPL_IF NAME="hilighted" --><td><!-- TMPL_ELSE --><td><!-- /TMPL_IF -->
-                                                       <!-- TMPL_VAR NAME="totalrow" -->
+                                               <td><!-- TMPL_IF NAME="value" --><!-- TMPL_VAR NAME="value" --><!-- TMPL_ELSE -->&nbsp;<!-- /TMPL_IF -->
                                                </td>
+                                               <!-- /TMPL_LOOP -->
+                                               <td><!-- TMPL_VAR NAME="totalrow" --></td>
                                        </tr>
-                               <!-- /TMPL_LOOP  -->
+                               <!-- /TMPL_LOOP -->
                                <tr>
                                        <th>TOTAL</th>
                                        <!-- TMPL_LOOP NAME="loopfooter" -->
-                                               <th>
-                                                       <!-- TMPL_VAR NAME="totalcol" -->
-                                               </th>
+                                               <th><!-- TMPL_VAR NAME="totalcol" --></th>
                                        <!-- /TMPL_LOOP -->
                                        <th><!-- TMPL_VAR NAME="total" --></th>
                                </tr>
                        </tr>
                </thead>
                <tbody>
-                       <tr class="hilighted">
+                       <tr class="highlight">
                        <td>Patron category</td>
                        <td><input type="radio" name="Line" value="categorycode" /></td>
                        <td><input type="radio" name="Column" value="categorycode" /></td>
-                       <td ><!-- TMPL_VAR NAME="CGICatCode" --></td>
+                       <td><select name="Filter"  size="1" id="catcode">
+                               <option value=""></option>
+                               <!-- TMPL_LOOP NAME="CAT_LOOP" -->
+                               <option value="<!-- TMPL_VAR NAME="categorycode" -->"><!-- TMPL_VAR NAME="description" --></option>
+                               <!-- /TMPL_LOOP  -->
+                               </select>
+                       </td>
                        </tr>
                        <tr>
                        <td>Patron status</td>
-                       <td></td>
-                       <td></td>
-                       <td ><select name="status"><option value=""> </option><option value="debarred">debarred</option><option value="gonenoadress">gone no adress</option><option value="lost">lost</option></select></td>
+                       <td colspan="2"></td>
+                       <td ><select name="status"><option value=""> </option><option value="debarred">debarred</option><option value="gonenoadress">gone no address</option><option value="lost">lost</option></select></td>
                        </tr>
-                       <tr>
+                       <tr class="highlight">
                        <td>Patron activity</td>
-                       <td></td>
-                       <td></td>
-                       <td ><select name="activity"><option value=""> </option><option value="active">active</option><option value="nonactive">no active</option></select></td>
-                       </tr>
-
-                       <tr class="hilighted">
-                       <td>&nbsp;</td>
                        <td colspan="2"><select name="period" id="period">
-                       <option value ="1">1</option>
-                       <option value ="2">2</option>
-                       <option value ="3">3</option>
+                       <option value="1">1</option>
+                       <option value="2">2</option>
+                       <option value="3">3</option>
                        </select>
                        <label for="period">years of activity</label> 
                        </td>
-                       <td>&nbsp;</td>
+                       <td><select name="activity"><option value=""> </option><option value="active">active</option><option value="nonactive">no active</option></select></td>
                        </tr>
 
-                       <tr class="hilighted">
-                       <td>
-                       Zip Code
-                       </td>
+                       <tr>
+                       <td rowspan="2">Zip Code</td>
                        <td><input type="radio" name="Line" value="zipcode" /></td>
                        <td><input type="radio" name="Column" value="zipcode" /></td>
-                       <td><!-- TMPL_VAR NAME="CGIZipCode" --></td>
+                       <td><select name="Filter"  size="1" id="zipcode">
+                               <option value=""> </option>
+                               <!-- TMPL_LOOP NAME="ZIP_LOOP" -->
+                               <option value="<!-- TMPL_VAR NAME="zipcode" -->"><!-- TMPL_VAR NAME="zipcode" --></option>
+                               <!-- /TMPL_LOOP -->
+                               </select>
+                       </td>
                        </tr>
-                       <tr class="hilighted">
-                       <td>&nbsp;</td>
+                       <tr>
                        <td colspan="2"><select name="digits" id="digits">
                        <option value=""> </option>
                        <option value ="1">1</option>
                        </td>
                        <td>&nbsp;</td>
                        </tr>
-                       <tr class="hilighted">
+
+                       <tr class="highlight">
+                       <td>Library</td>
+                       <td><input type="radio" name="Line"   value="branchcode" /></td>
+                       <td><input type="radio" name="Column" value="branchcode" /></td>
                        <td>
-                       Library
+                               <select name="Filter"  size="1" id="branch">
+                               <option value=""></option>
+                               <!-- TMPL_LOOP NAME="BRANCH_LOOP" -->
+                                       <option value="<!-- TMPL_VAR NAME="branchcode" -->"><!-- TMPL_VAR NAME="branchcode" --> - <!-- TMPL_VAR NAME="branchname" --></option>
+                               <!-- /TMPL_LOOP -->
+                               </select>
                        </td>
-                       <td><input type="radio" name="Line" value="branchcode" /></td>
-                       <td><input type="radio" name="Column" value="branchcode" /></td>
-                       <td><!-- TMPL_VAR NAME="CGIBranch" --></td>
                        </tr>
-
-                         <tr class="hilighted">
-                <td>Date of Birth</td>
-                <td></td>
-                <td></td>
-                <td > <label for="from">From</label> <input type="text" readonly="readonly" size="10" id="from" name="Filter" />
+                   <tr>
+            <td>Date of Birth</td>
+            <td colspan="2"></td>
+                <td><label for="from">From</label> <input type="text" readonly="readonly" size="10" id="from" name="Filter" />
                     <img src="<!-- TMPL_VAR Name="themelang" -->/lib/calendar/cal.gif" alt="Show Calendar" border="0" id="openCalendarFrom" style="cursor: pointer;" />
                    <script type="text/javascript">
                                   //<![CDATA[
                         } else {
                                 return false;
                         }
-                        }
-                    Calendar.setup(
-                        {
+                    }
+                    Calendar.setup({
                             inputField : "from",
                             ifFormat : "<!-- TMPL_VAR NAME="DHTMLcalendar_dateformat" -->",
                             button : "openCalendarFrom",
                             disableFunc : validate1,
                             dateStatusFunc : validate1
-                        }
-                    );
+                    });
                                        //]]>
                     </script>
                     <label for="to">To</label> <input readonly="readonly" size="10" id="to" name="Filter" value="" type="text" />
                                 }
                             }
 
-                            Calendar.setup(
-                                {
+                            Calendar.setup({
                                     inputField : "to",
                                     ifFormat : "<!-- TMPL_VAR NAME="DHTMLcalendar_dateformat" -->",
                                     button : "openCalendarTo",
                                     disableFunc : validate2,
                                     dateStatusFunc : validate2
-                                }
-                            );
+                            });
                                                        //]]>
                         </script>
                     </td>
                 </tr>
 
-                       <!-- TMPL_IF name="hassort1" -->
-                               <tr>
+                       <!-- TMPL_IF name="SORT1_LOOP" -->
+                               <tr class="highlight">
                                <td>Sort1</td>
                                <td><input type="radio" name="Line" value="sort1" /></td>
                                <td><input type="radio" name="Column" value="sort1" /></td>
-                               <td ><!-- TMPL_VAR NAME="CGISort1" --></td>
+                               <td>
+                                       <select id="sort1" size="1" name="Filter">
+                                       <option value=""/>
+                                       <!-- TMPL_LOOP NAME="SORT1_LOOP" -->
+                                               <option value="<!-- TMPL_VAR NAME="authorized_value" -->"><!-- TMPL_VAR NAME="lib" --></option>
+                                       <!-- /TMPL_LOOP -->
+                                       </select>
+                               </td>
                                </tr>
                        <!-- /TMPL_IF -->
-                       <!-- TMPL_IF name="hassort2" -->
-                               <tr class="hilighted" >
+                       <!-- TMPL_IF name="SORT2_LOOP" -->
+                               <tr<!-- TMPL_UNLESS NAME="SORT1_LOOP" --> class="highlight"<!-- /TMPL_UNLESS -->>
                                <td>Sort2</td>
                                <td><input type="radio" name="Line" value="sort2" /></td>
                                <td><input type="radio" name="Column" value="sort2" /></td>
-                               <td><!-- TMPL_VAR NAME="CGISort2" --></td>
+                               <td>
+                                       <select id="sort2" size="1" name="Filter">
+                                       <option value=""/>
+                                       <!-- TMPL_LOOP NAME="SORT2_LOOP" -->
+                                               <option value="<!-- TMPL_VAR NAME="value" -->"><!-- TMPL_VAR NAME="value" --></option>
+                                       <!-- /TMPL_LOOP -->
+                                       </select>
                                </tr>
                        <!-- /TMPL_IF -->
                </tbody>
index 45e6bc7..df9e4df 100755 (executable)
@@ -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 ) {