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 $selected = 1 if $thisbranch eq $branch;
164 my %row =(value
=> $thisborcat,
165 # selected => $selected,
166 description
=> $labels->{$thisborcat},
168 push @borcatloop, \
%row;
174 CGIextChoice
=> $CGIextChoice,
175 CGIsepChoice
=> $CGIsepChoice,
176 branchloop
=>\
@branchloop,
177 itemtypeloop
=>\
@itemtypeloop,
178 borcatloop
=>\
@borcatloop,
180 output_html_with_http_headers
$input, $cookie, $template->output;
187 my ($line, $column, $filters) = @_;
196 my $dbh = C4
::Context
->dbh;
214 for (my $i=0;$i<=6;$i++) {
216 if ( @
$filters[$i] ) {
217 if (($i==1) and (@
$filters[$i-1])) {
218 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
220 # format the dates filters, otherwise just fill as is
222 $cell{filter
} .= @
$filters[$i];
224 $cell{filter
} .= format_date
(@
$filters[$i]);
226 defined ($cellmap[$i]) and
227 $cell{crit
} .= $cellmap[$i];
228 push @loopfilter, \
%cell;
234 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
235 $column = "biblioitems.".$column if $column=~/itemtype/;
236 $column = "borrowers.".$column if $column=~/categorycode/;
238 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
239 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
240 $colfilter[0] = @
$filters[2] if ($column =~ /returndate/ ) ;
241 $colfilter[1] = @
$filters[3] if ($column =~ /returndate/ ) ;
242 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
243 $colfilter[0] = @
$filters[5] if ($column =~ /itemtype/ ) ;
244 $colfilter[0] = @
$filters[6] if ($column =~ /category/ ) ;
245 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
246 $colfilter[0] = @
$filters[7] if ($column =~ /timestamp/ ) ;
247 $colfilter[0] = @
$filters[8] if ($column =~ /timestamp/ ) ;
248 $colfilter[0] = @
$filters[9] if ($column =~ /timestamp/ ) ;
249 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
252 if ($column eq "Day") {
254 $column = "issues.timestamp";
255 $colfield .="dayname($column)";
256 $colorder .="weekday($column)";
257 } elsif ($column eq "Month") {
259 $column = "issues.timestamp";
260 $colfield .="monthname($column)";
261 $colorder .="month($column)";
262 } elsif ($column eq "Year") {
264 $column = "issues.timestamp";
265 $colfield .="Year($column)";
266 $colorder .= $column;
268 $colfield .= $column;
269 $colorder .= $column;
273 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
274 if (($column=~/timestamp/) or ($column=~/returndate/)){
275 if ($colfilter[1] and ($colfilter[0])){
276 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
277 } elsif ($colfilter[1]) {
278 $strsth2 .= " and $column < '$colfilter[1]' " ;
279 } elsif ($colfilter[0]) {
280 $strsth2 .= " and $column > '$colfilter[0]' " ;
282 } elsif ($colfilter[0]) {
283 $colfilter[0] =~ s/\*/%/g;
284 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
286 $strsth2 .=" group by $colfield";
287 $strsth2 .=" order by $colorder";
290 my $sth2 = $dbh->prepare( $strsth2 );
293 while (my ($celvalue) = $sth2->fetchrow) {
295 $cell{'coltitle'} = ($celvalue?
$celvalue:"NULL");
296 push @loopcol, \
%cell;
298 # warn "fin des titres colonnes";
305 #Initialization of cell values.....
309 for (my $i=1;$i<=$line;$i++) {
310 foreach my $col ( @loopcol ) {
311 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
312 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"total"}->{'name'}=0;
317 # preparing calculation
320 # Processing average loanperiods
321 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
322 $strcalc .= " , $colfield " if ($colfield);
323 $strcalc .= " FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
325 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
326 $strcalc .= " AND issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
327 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
328 $strcalc .= " AND issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
329 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
330 $strcalc .= " AND issues.returndate > '" . @
$filters[2] ."'" if ( @
$filters[2] );
331 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
332 $strcalc .= " AND issues.returndate < '" . @
$filters[3] ."'" if ( @
$filters[3] );
333 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
334 $strcalc .= " AND issues.branchcode like '" . @
$filters[4] ."'" if ( @
$filters[4] );
335 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
336 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[5] ."'" if ( @
$filters[5] );
337 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
338 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[6] ."'" if ( @
$filters[6] );
339 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
340 $strcalc .= " AND dayname(issues.timestamp) like '" . @
$filters[7]."'" if (@
$filters[7]);
341 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
342 $strcalc .= " AND monthname(issues.timestamp) like '" . @
$filters[8]."'" if (@
$filters[8]);
343 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
344 $strcalc .= " AND year(issues.timestamp) like '" . @
$filters[9] ."'" if ( @
$filters[9] );
346 $strcalc .= " group by borrowers.borrowernumber";
347 $strcalc .= ", $colfield" if ($column);
348 $strcalc .= " order by RANK DESC";
349 $strcalc .= ",$colfield " if ($colfield);
352 # $max = $line*@loopcol;
353 # } else { $max=$line;}
354 # $strcalc .= " LIMIT 0,$max";
355 warn "SQL :". $strcalc;
357 my $dbcalc = $dbh->prepare($strcalc);
359 # warn "filling table";
362 while (my @data = $dbcalc->fetchrow) {
363 my ($row, $rank, $id, $col )=@data;
364 $col = "zzEMPTY" if ($col eq undef);
365 $indice{$col}=1 if (not($indice{$col}));
366 $table[$indice{$col}]->{$col}->{'name'}=$row;
367 $table[$indice{$col}]->{$col}->{'count'}=$rank;
368 $table[$indice{$col}]->{$col}->{'link'}=$id;
369 # warn " ".$i." ".$col. " ".$row;
373 push @loopcol,{coltitle
=> "Global"} if not($column);
375 for ($i=1; $i<=$line;$i++) {
378 #@loborrowersopcol ensures the order for columns is common with column titles
379 # and the number matches the number of columns
381 foreach my $col ( @loopcol ) {
382 # warn " colonne :$col->{coltitle}";
386 my $key = ((!(@loopcol)) or ($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))
387 ?
"zzEMPTY" : $col->{coltitle
};
388 $value =$table[$i]->{$key}->{'name'};
389 $count =$table[$i]->{$key}->{'count'};
390 $link =$table[$i]->{$key}->{'link'};
391 # warn " ".$i ." value:$value count:$count reference:$link";
392 push @loopcell, {value
=> $value, count
=>$count, reference
=> $link} ;
394 push @looprow,{ 'rowtitle' => $i ,
395 'loopcell' => \
@loopcell,
396 'hilighted' => ($hilighted >0),
398 $hilighted = -$hilighted;
402 # the header of the table
403 $globalline{loopfilter
}=\
@loopfilter;
404 # the core of the table
405 $globalline{looprow
} = \
@looprow;
406 $globalline{loopcol
} = \
@loopcol;
407 # # the foot (totals by borrower type)
408 $globalline{loopfooter
} = \
@loopfooter;
409 $globalline{total
}= $grantotal;
410 $globalline{line
} = $line;
411 $globalline{column
} = $column;
412 push @mainloop,\
%globalline;