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>.
34 use Koha::Patron::Categories;
38 plugin that shows a stats on borrowers
44 $debug and open DEBUG, ">/tmp/bor_issues_top.debug.log";
47 my $fullreportname = "reports/bor_issues_top.tt";
48 my $do_it = $input->param('do_it');
49 my $limit = $input->param("Limit");
50 my $column = $input->param("Criteria");
51 my @filters = $input->multi_param("Filter");
52 foreach ( @filters[0..3] ) {
53 $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' }); };
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {reports => '*'},
65 our $sep = $input->param("sep") || C4::Context->preference('delimiter') || ',';
66 $sep = "\t" if ($sep eq 'tabulation');
67 $template->param(do_it => $do_it,
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results, limit=>$limit);
75 output_html_with_http_headers $input, $cookie, $template->output;
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 print join($sep, map {$_->{coltitle}} @$cols);
88 print $sep . "Total\n";
90 foreach my $line ( @$lines ) {
91 my $x = $line->{loopcell};
92 print $line->{rowtitle}.$sep;
93 print join($sep, map {$_->{value}} @$x);
94 print $sep,$line->{totalrow};
99 $cols = @$results[0]->{loopfooter};
100 print join($sep, map {$_->{totalcol}} @$cols);
101 print $sep.@$results[0]->{total};
106 my $dbh = C4::Context->dbh;
109 # here each element returned by map is a hashref, get it?
110 my @mime = ( map { {type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
111 my $delims = GetDelimiterChoices;
113 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
114 my $itemtypes = Koha::ItemTypes->search_with_localization;
117 CGIseplist => $delims,
118 itemtypes => $itemtypes,
119 patron_categories => $patron_categories,
121 output_html_with_http_headers $input, $cookie, $template->output;
125 my ($limit, $column, $filters) = @_;
133 my $dbh = C4::Context->dbh;
150 for (my $i=0;$i<=6;$i++) {
152 if ( @$filters[$i] ) {
153 if (($i==1) and (@$filters[$i-1])) {
154 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
156 # format the dates filters, otherwise just fill as is
157 $cell{filter} .= @$filters[$i];
158 defined ($cellmap[$i]) and
159 $cell{crit} .= $cellmap[$i];
160 push @loopfilter, \%cell;
166 $column = "old_issues." .$column if (($column=~/branchcode/) or ($column=~/timestamp/));
167 $column = "biblioitems.".$column if $column=~/itemtype/;
168 $column = "borrowers." .$column if $column=~/categorycode/;
170 if ($column =~ /timestamp/) {
171 $colfilter[0] = @$filters[0];
172 $colfilter[1] = @$filters[1];
173 } elsif ($column =~ /returndate/) {
174 $colfilter[0] = @$filters[2];
175 $colfilter[1] = @$filters[3];
176 } elsif ($column =~ /branchcode/) {
177 $colfilter[0] = @$filters[4];
178 } elsif ($column =~ /itemtype/) {
179 $colfilter[0] = @$filters[5];
180 } elsif ($column =~ /category/) {
181 $colfilter[0] = @$filters[6];
182 } elsif ($column =~ /sort2/ ) {
183 # $colfilter[0] = @$filters[11];
187 if ($column eq "Day") {
189 $column = "old_issues.timestamp";
190 $colfield .="dayname($column)";
191 $colorder .="weekday($column)";
192 } elsif ($column eq "Month") {
194 $column = "old_issues.timestamp";
195 $colfield .="monthname($column)";
196 $colorder .="month($column)";
197 } elsif ($column eq "Year") {
199 $column = "old_issues.timestamp";
200 $colfield .="Year($column)";
201 $colorder .= $column;
203 $colfield .= $column;
204 $colorder .= $column;
208 $strsth2 .= "SELECT DISTINCTROW $colfield
210 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
211 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
212 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
214 if (($column=~/timestamp/) or ($column=~/returndate/)){
215 if ($colfilter[1] and $colfilter[0]){
216 $strsth2 .= " AND $column between '$colfilter[0]' AND '$colfilter[1]' " ;
217 } elsif ($colfilter[1]) {
218 $strsth2 .= " AND $column < '$colfilter[1]' " ;
219 } elsif ($colfilter[0]) {
220 $strsth2 .= " AND $column > '$colfilter[0]' " ;
222 } elsif ($colfilter[0]) {
223 $colfilter[0] =~ s/\*/%/g;
224 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
226 $strsth2 .=" GROUP BY $colfield";
227 $strsth2 .=" ORDER BY $colorder";
229 $debug and print DEBUG "bor_issues_top (old_issues) SQL: $strsth2\n";
230 my $sth2 = $dbh->prepare($strsth2);
232 print DEBUG "rows: ", $sth2->rows, "\n";
233 while (my @row = $sth2->fetchrow) {
234 $columns{($row[0] ||'NULL')}++;
235 push @loopcol, { coltitle => $row[0] || 'NULL' };
238 $strsth2 =~ s/old_issues/issues/g;
239 $debug and print DEBUG "bor_issues_top (issues) SQL: $strsth2\n";
240 $sth2 = $dbh->prepare($strsth2);
242 $debug and print DEBUG "rows: ", $sth2->rows, "\n";
243 while (my @row = $sth2->fetchrow) {
244 $columns{($row[0] ||'NULL')}++;
245 push @loopcol, { coltitle => $row[0] || 'NULL' };
247 $debug and print DEBUG "full array: ", Dumper(\%columns), "\n";
254 # Processing average loanperiods
255 $strcalc .= "SELECT CONCAT_WS('', borrowers.surname , \",\\t\", borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
256 $strcalc .= " , $colfield " if ($colfield);
257 $strcalc .= " FROM `old_issues`
258 LEFT JOIN borrowers USING(borrowernumber)
259 LEFT JOIN items USING(itemnumber)
260 LEFT JOIN biblioitems USING(biblioitemnumber)
261 WHERE old_issues.borrowernumber IS NOT NULL
264 'old_issues.issuedate >',
265 'old_issues.issuedate <',
266 'old_issues.returndate >',
267 'old_issues.returndate <',
268 'old_issues.branchcode like',
269 'biblioitems.itemtype like',
270 'borrowers.categorycode like',
272 foreach ((@$filters)[0..9]) {
273 my $term = shift @filterterms; # go through both arrays in step
276 $strcalc .= " AND $term '$_' ";
278 $strcalc .= " GROUP BY borrowers.borrowernumber";
279 $strcalc .= ", $colfield" if ($column);
280 $strcalc .= " ORDER BY RANK DESC";
281 $strcalc .= ",$colfield " if ($colfield);
282 $strcalc .= " LIMIT $limit" if ($limit);
284 $debug and print DEBUG "(old_issues) SQL : $strcalc\n";
285 my $dbcalc = $dbh->prepare($strcalc);
287 $debug and print DEBUG "rows: ", $dbcalc->rows, "\n";
289 # DATA STRUCTURE is going to look like this:
290 # (2253=> {name=>"John Doe",
291 # allcols=>{MAIN=>12, MEDIA_LIB=>3}
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}->{oldcols}->{$col} = $rank;
305 $strcalc =~ s/old_issues/issues/g;
306 $debug and print DEBUG "(issues) SQL : $strcalc\n";
307 $dbcalc = $dbh->prepare($strcalc);
309 $debug and print DEBUG "rows: ", $dbcalc->rows, "\n";
310 while (my @data = $dbcalc->fetchrow) {
311 my ($row, $rank, $id, $col) = @data;
312 $col = "zzEMPTY" if (!defined($col));
313 unless ($patrons{$id}) {
314 $patrons{$id} = {name=>$row, allcols=>{}, newcols=>{}, oldcols=>{}};
316 $patrons{$id}->{newcols}->{$col} = $rank;
319 foreach my $id (keys %patrons) {
320 my @uniq = keys %{{ %{$patrons{$id}->{newcols}}, %{$patrons{$id}->{oldcols}} }}; # get uniq keys, see perlfaq4
322 my $count = ($patrons{$id}->{newcols}->{$_} || 0) +
323 ($patrons{$id}->{oldcols}->{$_} || 0);
324 $patrons{$id}->{allcols}->{$_} = $count;
325 $patrons{$id}->{total} += $count;
328 $debug and print DEBUG "\n\npatrons: ", Dumper(\%patrons);
331 my @cols_in_order = sort keys %columns; # if you want to order the columns, do something here
332 my @ranked_ids = sort {
333 $patrons{$b}->{total} <=> $patrons{$a}->{total}
334 || $patrons{$a}->{name} cmp $patrons{$b}->{name}
336 foreach my $id (@ranked_ids) {
339 foreach my $key (@cols_in_order) {
342 value => $patrons{$id}->{name},
344 count => $patrons{$id}->{allcols}->{$key},
348 value => $patrons{$id}->{name},
350 count => $patrons{$id}->{total},
354 push @looprow,{ 'rowtitle' => $i++ ,
355 'loopcell' => \@loopcell,
356 'hilighted' => ($i%2),
358 # use a limit, if a limit is defined
359 last if $i > $limit and $limit
362 # the header of the table
363 $globalline{loopfilter}=\@loopfilter;
364 # the core of the table
365 $globalline{looprow} = \@looprow;
366 $globalline{loopcol} = [ map {{coltitle=>$_}} @cols_in_order ];
367 # the foot (totals by borrower type)
368 $globalline{loopfooter} = [];
369 $globalline{total}= $grantotal; # FIXME: useless
370 $globalline{column} = $column;
371 return [\%globalline]; # reference to a 1 element array: that element is a hashref
374 $debug and close DEBUG;