3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
33 use Koha::Patron::Categories;
37 plugin that shows a stats on borrowers
44 my $fullreportname = "reports/bor_issues_top.tt";
45 my $do_it = $input->param('do_it');
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->multi_param("Filter");
49 foreach ( @filters[0..3] ) {
50 $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' }); };
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
58 flagsrequired => {reports => '*'},
60 our $sep = $input->param("sep") || C4::Context->preference('CSVDelimiter') || ',';
61 $sep = "\t" if ($sep eq 'tabulation');
62 $template->param(do_it => $do_it,
66 my $results = calculate($limit, $column, \@filters);
67 if ($output eq "screen"){
68 # Printing results to screen
69 $template->param(mainloop => $results, limit=>$limit);
70 output_html_with_http_headers $input, $cookie, $template->output;
72 # Printing to a csv file
73 print $input->header(-type => 'application/vnd.sun.xml.calc',
75 -attachment=>"$basename.csv",
76 -filename=>"$basename.csv" );
77 my $cols = @$results[0]->{loopcol};
78 my $lines = @$results[0]->{looprow};
80 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82 print join($sep, map {$_->{coltitle}} @$cols);
83 print $sep . "Total\n";
85 foreach my $line ( @$lines ) {
86 my $x = $line->{loopcell};
87 print $line->{rowtitle}.$sep;
88 print join($sep, map {$_->{value}} @$x);
89 print $sep,$line->{totalrow};
94 $cols = @$results[0]->{loopfooter};
95 print join($sep, map {$_->{totalcol}} @$cols);
96 print $sep.@$results[0]->{total};
101 my $dbh = C4::Context->dbh;
103 # here each element returned by map is a hashref, get it?
104 my @mime = ( map { {type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
105 my $delims = GetDelimiterChoices;
107 my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
108 my $itemtypes = Koha::ItemTypes->search_with_localization;
111 CGIseplist => $delims,
112 itemtypes => $itemtypes,
113 patron_categories => $patron_categories,
115 output_html_with_http_headers $input, $cookie, $template->output;
119 my ($limit, $column, $filters) = @_;
126 my $dbh = C4::Context->dbh;
143 for (my $i=0;$i<=6;$i++) {
145 if ( @$filters[$i] ) {
146 if (($i==1) and (@$filters[$i-1])) {
147 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
149 # format the dates filters, otherwise just fill as is
150 $cell{filter} .= @$filters[$i];
151 defined ($cellmap[$i]) and
152 $cell{crit} .= $cellmap[$i];
153 push @loopfilter, \%cell;
159 $column = "old_issues." .$column if (($column=~/branchcode/) or ($column=~/timestamp/));
160 $column = "biblioitems.".$column if $column=~/itemtype/;
161 $column = "borrowers." .$column if $column=~/categorycode/;
163 if ($column =~ /timestamp/) {
164 $colfilter[0] = @$filters[0];
165 $colfilter[1] = @$filters[1];
166 } elsif ($column =~ /returndate/) {
167 $colfilter[0] = @$filters[2];
168 $colfilter[1] = @$filters[3];
169 } elsif ($column =~ /branchcode/) {
170 $colfilter[0] = @$filters[4];
171 } elsif ($column =~ /itemtype/) {
172 $colfilter[0] = @$filters[5];
173 } elsif ($column =~ /category/) {
174 $colfilter[0] = @$filters[6];
175 } elsif ($column =~ /sort2/ ) {
176 # $colfilter[0] = @$filters[11];
180 if ($column eq "Day") {
182 $column = "old_issues.timestamp";
183 $colfield .="dayname($column)";
184 $colorder .="weekday($column)";
185 } elsif ($column eq "Month") {
187 $column = "old_issues.timestamp";
188 $colfield .="monthname($column)";
189 $colorder .="month($column)";
190 } elsif ($column eq "Year") {
192 $column = "old_issues.timestamp";
193 $colfield .="Year($column)";
194 $colorder .= $column;
196 $colfield .= $column;
197 $colorder .= $column;
201 $strsth2 .= "SELECT DISTINCTROW $colfield
203 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
204 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
205 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
207 if (($column=~/timestamp/) or ($column=~/returndate/)){
208 if ($colfilter[1] and $colfilter[0]){
209 $strsth2 .= " AND $column between '$colfilter[0]' AND '$colfilter[1]' " ;
210 } elsif ($colfilter[1]) {
211 $strsth2 .= " AND $column < '$colfilter[1]' " ;
212 } elsif ($colfilter[0]) {
213 $strsth2 .= " AND $column > '$colfilter[0]' " ;
215 } elsif ($colfilter[0]) {
216 $colfilter[0] =~ s/\*/%/g;
217 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
219 $strsth2 .=" GROUP BY $colfield";
220 $strsth2 .=" ORDER BY $colorder";
222 my $sth2 = $dbh->prepare($strsth2);
224 while (my @row = $sth2->fetchrow) {
225 $columns{($row[0] ||'NULL')}++;
226 push @loopcol, { coltitle => $row[0] || 'NULL' };
229 $strsth2 =~ s/old_issues/issues/g;
230 $sth2 = $dbh->prepare($strsth2);
232 while (my @row = $sth2->fetchrow) {
233 $columns{($row[0] ||'NULL')}++;
234 push @loopcol, { coltitle => $row[0] || 'NULL' };
242 # Processing average loanperiods
243 $strcalc .= "SELECT CONCAT_WS('', borrowers.surname , \",\\t\", borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
244 $strcalc .= " , $colfield " if ($colfield);
245 $strcalc .= " FROM `old_issues`
246 LEFT JOIN borrowers USING(borrowernumber)
247 LEFT JOIN items USING(itemnumber)
248 LEFT JOIN biblioitems USING(biblioitemnumber)
249 WHERE old_issues.borrowernumber IS NOT NULL
252 'old_issues.issuedate >',
253 'old_issues.issuedate <',
254 'old_issues.returndate >',
255 'old_issues.returndate <',
256 'old_issues.branchcode like',
257 'biblioitems.itemtype like',
258 'borrowers.categorycode like',
260 foreach ((@$filters)[0..9]) {
261 my $term = shift @filterterms; # go through both arrays in step
264 $strcalc .= " AND $term '$_' ";
266 $strcalc .= " GROUP BY borrowers.borrowernumber";
267 $strcalc .= ", $colfield" if ($column);
268 $strcalc .= " ORDER BY RANK DESC";
269 $strcalc .= ",$colfield " if ($colfield);
270 $strcalc .= " LIMIT $limit" if ($limit);
272 my $dbcalc = $dbh->prepare($strcalc);
275 # DATA STRUCTURE is going to look like this:
276 # (2253=> {name=>"John Doe",
277 # allcols=>{MAIN=>12, MEDIA_LIB=>3}
280 while (my @data = $dbcalc->fetchrow) {
281 my ($row, $rank, $id, $col) = @data;
282 $col = "zzEMPTY" if (!defined($col));
283 unless ($patrons{$id}) {
284 $patrons{$id} = {name=>$row, allcols=>{}, newcols=>{}, oldcols=>{}};
286 $patrons{$id}->{oldcols}->{$col} = $rank;
291 $strcalc =~ s/old_issues/issues/g;
292 $dbcalc = $dbh->prepare($strcalc);
294 while (my @data = $dbcalc->fetchrow) {
295 my ($row, $rank, $id, $col) = @data;
296 $col = "zzEMPTY" if (!defined($col));
297 unless ($patrons{$id}) {
298 $patrons{$id} = {name=>$row, allcols=>{}, newcols=>{}, oldcols=>{}};
300 $patrons{$id}->{newcols}->{$col} = $rank;
303 foreach my $id (keys %patrons) {
304 my @uniq = keys %{{ %{$patrons{$id}->{newcols}}, %{$patrons{$id}->{oldcols}} }}; # get uniq keys, see perlfaq4
306 my $count = ($patrons{$id}->{newcols}->{$_} || 0) +
307 ($patrons{$id}->{oldcols}->{$_} || 0);
308 $patrons{$id}->{allcols}->{$_} = $count;
309 $patrons{$id}->{total} += $count;
314 my @cols_in_order = sort keys %columns; # if you want to order the columns, do something here
315 my @ranked_ids = sort {
316 $patrons{$b}->{total} <=> $patrons{$a}->{total}
317 || $patrons{$a}->{name} cmp $patrons{$b}->{name}
319 foreach my $id (@ranked_ids) {
322 foreach my $key (@cols_in_order) {
325 value => $patrons{$id}->{name},
327 count => $patrons{$id}->{allcols}->{$key},
331 value => $patrons{$id}->{name},
333 count => $patrons{$id}->{total},
337 push @looprow,{ 'rowtitle' => $i++ ,
338 'loopcell' => \@loopcell,
339 'hilighted' => ($i%2),
341 # use a limit, if a limit is defined
342 last if $i > $limit and $limit
345 # the header of the table
346 $globalline{loopfilter}=\@loopfilter;
347 # the core of the table
348 $globalline{looprow} = \@looprow;
349 $globalline{loopcol} = [ map {{coltitle=>$_}} @cols_in_order ];
350 # the foot (totals by borrower type)
351 $globalline{loopfooter} = [];
352 $globalline{total}= $grantotal; # FIXME: useless
353 $globalline{column} = $column;
354 return [\%globalline]; # reference to a 1 element array: that element is a hashref