4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Branch; # GetBranches
30 use C4::Dates qw(format_date format_date_in_iso);
34 plugin that shows a stats on borrowers
43 my $fullreportname = "reports/bor_issues_top.tmpl";
44 my $do_it = $input->param('do_it');
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
49 $filters[$_]=format_date_in_iso($filters[$_]);
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {reports => 1},
64 $template->param(do_it => $do_it,
65 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results, limit=>$limit);
73 output_html_with_http_headers $input, $cookie, $template->output;
76 # Printing to a csv file
77 print $input->header(-type => 'application/vnd.sun.xml.calc',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
84 $sep =C4::Context->preference("delimiter");
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 foreach my $col ( @$cols ) {
89 print $col->{coltitle}.$sep;
93 foreach my $line ( @$lines ) {
94 my $x = $line->{loopcell};
95 print $line->{rowtitle}.$sep;
96 foreach my $cell (@$x) {
97 print $cell->{value}.$sep;
99 print $line->{totalrow};
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
113 my $dbh = C4::Context->dbh;
119 my @mime = ( C4::Context->preference("MIME") );
120 # foreach my $mime (@mime){
123 my $CGIextChoice=CGI::scrolling_list(
130 my @dels = ( C4::Context->preference("delimiter") );
131 my $CGIsepChoice=CGI::scrolling_list(
138 my $branches = GetBranches;
140 foreach my $thisbranch (sort keys %$branches) {
141 my %row =(value => $thisbranch,
142 branchname => $branches->{$thisbranch}->{'branchname'},
144 push @branchloop, \%row;
148 my $itemtypes = GetItemTypes;
150 foreach my $thisitemtype (keys %$itemtypes) {
151 # my $selected = 1 if $thisbranch eq $branch;
152 my %row =(value => $thisitemtype,
153 # selected => $selected,
154 description => $itemtypes->{$thisitemtype}->{'description'},
156 push @itemtypeloop, \%row;
160 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
162 foreach my $thisborcat (sort keys %$labels) {
163 my %row =(value => $thisborcat,
164 description => $labels->{$thisborcat},
166 push @borcatloop, \%row;
172 CGIextChoice => $CGIextChoice,
173 CGIsepChoice => $CGIsepChoice,
174 branchloop =>\@branchloop,
175 itemtypeloop =>\@itemtypeloop,
176 borcatloop =>\@borcatloop,
178 output_html_with_http_headers $input, $cookie, $template->output;
185 my ($line, $column, $filters) = @_;
194 my $dbh = C4::Context->dbh;
212 for (my $i=0;$i<=6;$i++) {
214 if ( @$filters[$i] ) {
215 if (($i==1) and (@$filters[$i-1])) {
216 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
218 # format the dates filters, otherwise just fill as is
220 $cell{filter} .= @$filters[$i];
222 $cell{filter} .= format_date(@$filters[$i]);
224 defined ($cellmap[$i]) and
225 $cell{crit} .= $cellmap[$i];
226 push @loopfilter, \%cell;
232 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
233 $column = "biblioitems.".$column if $column=~/itemtype/;
234 $column = "borrowers.".$column if $column=~/categorycode/;
236 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
237 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
238 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
239 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
240 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
241 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
242 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
243 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
244 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
246 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
247 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
250 if ($column eq "Day") {
252 $column = "old_issues.timestamp";
253 $colfield .="dayname($column)";
254 $colorder .="weekday($column)";
255 } elsif ($column eq "Month") {
257 $column = "old_issues.timestamp";
258 $colfield .="monthname($column)";
259 $colorder .="month($column)";
260 } elsif ($column eq "Year") {
262 $column = "old_issues.timestamp";
263 $colfield .="Year($column)";
264 $colorder .= $column;
266 $colfield .= $column;
267 $colorder .= $column;
271 $strsth2 .= "SELECT DISTINCTROW $colfield
273 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
274 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
275 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
277 if (($column=~/timestamp/) or ($column=~/returndate/)){
278 if ($colfilter[1] and ($colfilter[0])){
279 $strsth2 .= " AND $column between '$colfilter[0]' AND '$colfilter[1]' " ;
280 } elsif ($colfilter[1]) {
281 $strsth2 .= " AND $column < '$colfilter[1]' " ;
282 } elsif ($colfilter[0]) {
283 $strsth2 .= " AND $column > '$colfilter[0]' " ;
285 } elsif ($colfilter[0]) {
286 $colfilter[0] =~ s/\*/%/g;
287 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
289 $strsth2 .=" GROUP BY $colfield";
290 $strsth2 .=" ORDER BY $colorder";
293 my $sth2 = $dbh->prepare( $strsth2 );
296 while (my ($celvalue) = $sth2->fetchrow) {
298 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
299 push @loopcol, \%cell;
301 # warn "fin des titres colonnes";
308 #Initialization of cell values.....
312 for (my $i=1;$i<=$line;$i++) {
313 foreach my $col ( @loopcol ) {
314 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
315 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
320 # preparing calculation
323 # Processing average loanperiods
324 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
325 $strcalc .= " , $colfield " if ($colfield);
326 $strcalc .= " FROM `old_issues`
327 LEFT JOIN borrowers ON borrowers.borrowernumber
328 LEFT JOIN items ON items.itemnumber=old_issues.itemnumber
329 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
332 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
333 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
334 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
335 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
336 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
337 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
338 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
339 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
340 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
341 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
342 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
343 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
344 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
345 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
346 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
347 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
348 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
349 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
350 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
351 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
353 $strcalc .= " GROUP BY borrowers.borrowernumber";
354 $strcalc .= ", $colfield" if ($column);
355 $strcalc .= " ORDER BY RANK DESC";
356 $strcalc .= ",$colfield " if ($colfield);
359 # $max = $line*@loopcol;
360 # } else { $max=$line;}
361 # $strcalc .= " LIMIT 0,$max";
362 warn "SQL :". $strcalc;
364 my $dbcalc = $dbh->prepare($strcalc);
366 # warn "filling table";
369 while (my @data = $dbcalc->fetchrow) {
370 my ($row, $rank, $id, $col )=@data;
371 $col = "zzEMPTY" if ($col eq undef);
372 $indice{$col}=1 if (not($indice{$col}));
373 $table[$indice{$col}]->{$col}->{'name'}=$row;
374 $table[$indice{$col}]->{$col}->{'count'}=$rank;
375 $table[$indice{$col}]->{$col}->{'link'}=$id;
376 # warn " ".$i." ".$col. " ".$row;
380 push @loopcol,{coltitle => "Global"} if not($column);
382 for ($i=1; $i<=$line;$i++) {
385 #@loborrowersopcol ensures the order for columns is common with column titles
386 # and the number matches the number of columns
388 foreach my $col ( @loopcol ) {
389 # warn " colonne :$col->{coltitle}";
393 my $key = ((!(@loopcol)) or ($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))
394 ? "zzEMPTY" : $col->{coltitle};
395 $value =$table[$i]->{$key}->{'name'};
396 $count =$table[$i]->{$key}->{'count'};
397 $link =$table[$i]->{$key}->{'link'};
398 # warn " ".$i ." value:$value count:$count reference:$link";
399 push @loopcell, {value => $value, count =>$count, reference => $link} ;
401 push @looprow,{ 'rowtitle' => $i ,
402 'loopcell' => \@loopcell,
403 'hilighted' => ($hilighted >0),
405 $hilighted = -$hilighted;
409 # the header of the table
410 $globalline{loopfilter}=\@loopfilter;
411 # the core of the table
412 $globalline{looprow} = \@looprow;
413 $globalline{loopcol} = \@loopcol;
414 # # the foot (totals by borrower type)
415 $globalline{loopfooter} = \@loopfooter;
416 $globalline{total}= $grantotal;
417 $globalline{line} = $line;
418 $globalline{column} = $column;
419 push @mainloop,\%globalline;