Adding reports
[koha.git] / reports / borrowers_stats.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA  02111-1307 USA
21
22 use strict;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26 use HTML::Template;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39
40 =over2
41
42 =cut
43
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $digits = $input->param("digits");
51 my $borstat = $input->param("status");
52 my ($template, $borrowernumber, $cookie)
53         = get_template_and_user({template_name => $fullreportname,
54                                 query => $input,
55                                 type => "intranet",
56                                 authnotrequired => 0,
57                                 flagsrequired => {editcatalogue => 1},
58                                 debug => 1,
59                                 });
60 $template->param(do_it => $do_it);
61 if ($do_it) {
62         my $results = calculate($line, $column, $digits, $borstat, \@filters);
63         $template->param(mainloop => $results);
64 #       print $input->header(-type => 'application/vnd.ms-excel', -name=>"export.csv");
65 #       my $lines = @$results[0]->{looprow};
66 #       foreach my $line (@$lines) {
67 #               my $x = $line->{loopcell};
68 #               foreach my $cell (@$x) {
69 #                       print $cell->{value}.";";
70 #               }
71 #               print "\n";
72 #       }
73 } else {
74         my $dbh = C4::Context->dbh;
75         my @values;
76         my %labels;
77         my $req;
78         $req = $dbh->prepare( "select categorycode, description from categories");
79         $req->execute;
80         my %select_catcode;
81         my @select_catcode;
82         push @select_catcode,"";
83         $select_catcode{""} = "";
84         while (my ($catcode, $description) =$req->fetchrow) {
85                 push @select_catcode, $catcode;
86                 $select_catcode{$catcode} = $description
87         }
88         my $CGICatCode=CGI::scrolling_list( -name     => 'Filter',
89                                 -id => 'Filter',
90                                 -values   => \@select_catcode,
91                                 -labels   => \%select_catcode,
92                                 -size     => 1,
93                                 -multiple => 0 );
94         
95         $req = $dbh->prepare( "select distinctrow sort1 from borrowers");
96         $req->execute;
97         my @select_sort1;
98         push @select_sort1,"";
99         while (my ($value) =$req->fetchrow) {
100                 push @select_sort1, $value;
101         }
102         my $CGIsort1=CGI::scrolling_list( -name     => 'Filter',
103                                 -id => 'Filter',
104                                 -values   => \@select_sort1,
105                                 -size     => 1,
106                                 -multiple => 0 );
107         
108         $req = $dbh->prepare( "select distinctrow sort2 from borrowers");
109         $req->execute;
110         my @select_sort2;
111         push @select_sort2,"";
112         while (my ($value) =$req->fetchrow) {
113                 push @select_sort2, $value;
114         }
115         my $CGIsort2=CGI::scrolling_list( -name     => 'Filter',
116                                 -id => 'Filter',
117                                 -values   => \@select_sort2,
118                                 -size     => 1,
119                                 -multiple => 0 );
120         $template->param(CGICatcode => $CGICatCode,
121                                         CGISort1 => $CGIsort1,
122                                         CGISort2 => $CGIsort2
123                                         );
124
125 }
126 output_html_with_http_headers $input, $cookie, $template->output;
127
128
129
130 sub calculate {
131         my ($line, $column, $digits, $status, $filters) = @_;
132         my @mainloop;
133         my @loopfooter;
134         my @loopcol;
135         my @looprow;
136         my %globalline;
137         my $grantotal =0;
138 # extract parameters
139         my $dbh = C4::Context->dbh;
140
141 # Filters
142         my $linefilter = "";
143         $linefilter = @$filters[0] if ($line =~ /categorycode/ )  ;
144         $linefilter = @$filters[1] if ($line =~ /zipcode/ )  ;
145         $linefilter = @$filters[2] if ($line =~ /sort1/ ) ;
146         $linefilter = @$filters[3] if ($line =~ /sort2/ ) ;
147
148         my $colfilter = "";
149         $colfilter = @$filters[0] if ($column =~ /categorycode/);
150         $colfilter = @$filters[1] if ($column =~ /zipcode/);
151         $colfilter = @$filters[2] if ($column =~ /sort1/);
152         $colfilter = @$filters[3] if ($column =~ /sort2/);
153
154         my @loopfilter;
155         for (my $i=0;$i<=3;$i++) {
156                 my %cell;
157                 if ( @$filters[$i] ) {
158                         $cell{filter} .= @$filters[$i];
159                         $cell{crit} .="Category Code " if ($i==0);
160                         $cell{crit} .="Zip Code" if ($i==1);
161                         $cell{crit} .="Sort1" if ($i==2);
162                         $cell{crit} .="Sort2" if ($i==3);
163                         push @loopfilter, \%cell;
164                 }
165         }
166         if ($status) {
167                 push @loopfilter,{crit=>"Status",filter=>$status}
168         }
169 # 1st, loop rows.
170 #problem with NULL Values.
171         my $strsth;
172         $strsth .= "select distinctrow $line from borrowers where $line is not null ";
173         $linefilter =~ s/\*/%/g;
174         if ( $linefilter ) {
175                 $strsth .= " and $line LIKE ? " ;
176         }
177         $strsth .= " and $status='1' " if ($status);
178         $strsth .=" order by $line";
179         warn "". $strsth;
180         
181         my $sth = $dbh->prepare( $strsth );
182         if ( $linefilter ) {
183                 $sth->execute($linefilter);
184         } else {
185                 $sth->execute;
186         }
187         while ( my ($celvalue) = $sth->fetchrow) {
188                 my %cell;
189                 if ($celvalue) {
190                         $cell{rowtitle} = $celvalue;
191                 } else {
192                         $cell{rowtitle} = "";
193                 }
194                 $cell{totalrow} = 0;
195                 push @looprow, \%cell;
196         }
197
198 # 2nd, loop cols.
199         my $strsth2;
200         $colfilter =~ s/\*/%/g;
201         $strsth2 .= "select distinctrow $column from borrowers where $column is not null";
202         if ( $colfilter ) {
203                 $strsth2 .= " and $column LIKE ? ";
204         } 
205         $strsth2 .= " and $status='1' " if ($status);
206         $strsth2 .= " order by $column";
207         warn "". $strsth2;
208         my $sth2 = $dbh->prepare( $strsth2 );
209         if ($colfilter) {
210                 $sth2->execute($colfilter);
211         } else {
212                 $sth2->execute;
213         }
214         while (my ($celvalue) = $sth2->fetchrow) {
215                 my %cell;
216                 my %ft;
217                 $cell{coltitle} = $celvalue;
218                 $ft{totalcol} = 0;
219                 push @loopcol, \%cell;
220                 push @loopfooter, \%ft;
221         }
222 # now, parse each category. Before filling the result array, fill it with 0 to have every itemtype column.
223         my $strcalc .= "SELECT  count( * ) FROM borrowers WHERE $line = ? and $column= ? ";
224         $strcalc .= " AND categorycode like '" . @$filters[1] ."%' " if ( @$filters[1] );
225         $strcalc .= " AND sort1 like ' " . @$filters[2] ."%'" if ( @$filters[2] );
226         $strcalc .= " AND sort2 like ' " . @$filters[3] ."%'" if ( @$filters[3] );
227         $strcalc .= " AND zipcode like ' " . @$filters[4] ."%'" if ( @$filters[4] );
228         $strcalc .= " and $status='1' " if ($status);
229         warn "". $strcalc;
230         my $dbcalc = $dbh->prepare($strcalc);
231         my $i=0;
232         my @totalcol;
233         my $hilighted=-1;
234         # for each line
235         for (my $i=0; $i<=$#looprow; $i++) {
236                 my $row = $looprow[$i]->{'rowtitle'};
237                 my @loopcell;
238                 my $totalrow=0;
239                 # for each column
240                 for (my $j=0;$j<=$#loopcol;$j++) {
241                         my $col = $loopcol[$j]->{'coltitle'};
242                         $dbcalc->execute($row,$col);
243                         my ($value) = $dbcalc->fetchrow;
244 #                       warn "$row / $col / $value";
245                         $totalrow += $value;
246                         $grantotal += $value;
247                         $loopfooter[$j]->{'totalcol'} +=$value;
248                         push @loopcell,{value => $value};
249                 }
250                 $looprow[$i]->{'totalrow'}=$totalrow;
251                 $looprow[$i]->{'loopcell'}=\@loopcell;
252                 $looprow[$i]->{'hilighted'} = 1 if $hilighted eq 1;
253                 $hilighted = -$hilighted;
254         }
255
256 #       # the header of the table
257         $globalline{loopfilter}=\@loopfilter;
258         $globalline{looprow} = \@looprow;
259 #       # the core of the table
260         $globalline{loopcol} = \@loopcol;
261 #       # the foot (totals by borrower type)
262         $globalline{loopfooter} = \@loopfooter;
263         $globalline{total}= $grantotal;
264         $globalline{line} = $line;
265         $globalline{column} = $column;
266         push @mainloop,\%globalline;
267         return \@mainloop;
268 }
269
270 1;