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
25 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/cat_issues_top.tmpl";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso($filters[0]);
49 $filters[1]=format_date_in_iso($filters[1]);
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user({template_name => $fullreportname,
59 flagsrequired => { reports => 1},
62 our $sep = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
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,
74 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 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
92 foreach my $line ( @$lines ) {
93 my $x = $line->{loopcell};
94 print $line->{rowtitle}.$sep;
95 foreach my $cell (@$x) {
96 print $cell->{value}.$sep;
98 print $line->{totalrow};
103 $cols = @$results[0]->{loopfooter};
104 foreach my $col ( @$cols ) {
105 print $sep.$col->{totalcol};
107 print $sep.@$results[0]->{total};
112 my $dbh = C4::Context->dbh;
118 my @mime = ( C4::Context->preference("MIME") );
119 # foreach my $mime (@mime){
123 my $CGIextChoice=CGI::scrolling_list(
130 my $CGIsepChoice=GetDelimiterChoices;
132 my $branches = GetBranches;
134 foreach my $thisbranch (keys %$branches) {
135 # my $selected = 1 if $thisbranch eq $branch;
136 my %row =(value => $thisbranch,
137 # selected => $selected,
138 branchname => $branches->{$thisbranch}->{'branchname'},
140 push @branchloop, \%row;
144 my $itemtypes = GetItemTypes;
146 foreach my $thisitemtype (keys %$itemtypes) {
147 # my $selected = 1 if $thisbranch eq $branch;
148 my %row =(value => $thisitemtype,
149 # selected => $selected,
150 description => $itemtypes->{$thisitemtype}->{'description'},
152 push @itemtypeloop, \%row;
156 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
158 foreach my $thisborcat (sort keys %$labels) {
159 # my $selected = 1 if $thisbranch eq $branch;
160 my %row =(value => $thisborcat,
161 # selected => $selected,
162 description => $labels->{$thisborcat},
164 push @borcatloop, \%row;
170 CGIextChoice => $CGIextChoice,
171 CGIsepChoice => $CGIsepChoice,
172 branchloop =>\@branchloop,
173 itemtypeloop =>\@itemtypeloop,
174 borcatloop =>\@borcatloop,
176 output_html_with_http_headers $input, $cookie, $template->output;
183 my ($line, $column, $filters) = @_;
192 my $dbh = C4::Context->dbh;
198 for (my $i=0;$i<=6;$i++) {
200 if ( @$filters[$i] ) {
201 if (($i==1) and (@$filters[$i-1])) {
202 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
204 # format the dates filters, otherwise just fill as is
206 $cell{filter} .= @$filters[$i];
208 $cell{filter} .= format_date(@$filters[$i]);
209 } $cell{crit} .="Issue From" if ($i==0);
210 $cell{crit} .="Issue To" if ($i==1);
211 $cell{crit} .="Return From" if ($i==2);
212 $cell{crit} .="Return To" if ($i==3);
213 $cell{crit} .="Branch" if ($i==4);
214 $cell{crit} .="Doc Type" if ($i==5);
215 $cell{crit} .="Bor Cat" if ($i==6);
216 $cell{crit} .="Day" if ($i==7);
217 $cell{crit} .="Month" if ($i==8);
218 $cell{crit} .="Year" if ($i==9);
219 push @loopfilter, \%cell;
225 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
226 $column = "biblioitems.".$column if $column=~/itemtype/;
227 $column = "borrowers.".$column if $column=~/categorycode/;
229 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
230 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
231 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
232 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
233 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
234 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
235 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
236 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
237 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
238 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
239 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
240 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
243 if ($column eq "Day") {
245 $column = "old_issues.timestamp";
246 $colfield .="dayname($column)";
247 $colorder .="weekday($column)";
248 } elsif ($column eq "Month") {
250 $column = "old_issues.timestamp";
251 $colfield .="monthname($column)";
252 $colorder .="month($column)";
253 } elsif ($column eq "Year") {
255 $column = "old_issues.timestamp";
256 $colfield .="Year($column)";
257 $colorder .= $column;
259 $colfield .= $column;
260 $colorder .= $column;
264 $strsth2 .= "SELECT distinctrow $colfield
266 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
267 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
268 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
270 if (($column=~/timestamp/) or ($column=~/returndate/)){
271 if ($colfilter[1] and ($colfilter[0])){
272 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
273 } elsif ($colfilter[1]) {
274 $strsth2 .= " and $column < '$colfilter[1]' " ;
275 } elsif ($colfilter[0]) {
276 $strsth2 .= " and $column > '$colfilter[0]' " ;
278 } elsif ($colfilter[0]) {
279 $colfilter[0] =~ s/\*/%/g;
280 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
282 $strsth2 .=" group by $colfield";
283 $strsth2 .=" order by $colorder";
286 my $sth2 = $dbh->prepare( $strsth2 );
287 if (( @colfilter ) and ($colfilter[1])){
288 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
289 } elsif ($colfilter[0]) {
290 $sth2->execute($colfilter[0]);
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 DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
325 $strcalc .= " , $colfield " if ($colfield);
326 $strcalc .= " FROM `old_issues`
327 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
329 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber)
330 ON items.itemnumber=old_issues.itemnumber
331 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber)
334 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
335 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
336 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
337 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
338 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
339 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
340 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
341 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
342 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
343 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
344 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
345 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
346 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
347 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
348 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
349 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
350 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
351 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
352 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
353 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
355 $strcalc .= " group by biblio.biblionumber";
356 $strcalc .= ", $colfield" if ($column);
357 $strcalc .= " order by RANK DESC";
358 $strcalc .= ", $colfield " if ($colfield);
362 # $max = $line*@loopcol;
363 # } else { $max=$line;}
364 # $strcalc .= " LIMIT 0,$max";
365 warn "SQL :". $strcalc;
367 my $dbcalc = $dbh->prepare($strcalc);
369 # warn "filling table";
372 while (my @data = $dbcalc->fetchrow) {
373 my ($row, $rank, $id, $col )=@data;
374 $col = "zzEMPTY" if (!defined($col));
375 $indice{$col}=1 if (not($indice{$col}));
376 $table[$indice{$col}]->{$col}->{'name'}=$row;
377 $table[$indice{$col}]->{$col}->{'count'}=$rank;
378 $table[$indice{$col}]->{$col}->{'link'}=$id;
379 # warn " ".$i." ".$col. " ".$row;
383 push @loopcol,{coltitle => "Global"} if not($column);
385 for ($i=1; $i<=$line;$i++) {
388 #@loopcol ensures the order for columns is common with column titles
389 # and the number matches the number of columns
391 foreach my $col ( @loopcol ) {
392 # warn " colonne :$col->{coltitle}";
397 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
398 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
399 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
401 $value =$table[$i]->{"zzEMPTY"}->{'name'};
402 $count =$table[$i]->{"zzEMPTY"}->{'count'};
403 $link =$table[$i]->{"zzEMPTY"}->{'link'};
405 # warn " ".$i ." value:$value count:$count reference:$link";
406 push @loopcell, {value => $value, count =>$count, reference => $link} ;
408 #warn "row : $row colcount:$colcount";
409 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
410 push @looprow,{ 'rowtitle' => $i ,
411 'loopcell' => \@loopcell,
412 'hilighted' => ($hilighted >0),
413 #'totalrow' => ($total)?sprintf("%.2f",$total):0
415 $hilighted = -$hilighted;
420 # the header of the table
421 $globalline{loopfilter}=\@loopfilter;
422 # the core of the table
423 $globalline{looprow} = \@looprow;
424 $globalline{loopcol} = \@loopcol;
425 # # the foot (totals by borrower type)
426 $globalline{loopfooter} = \@loopfooter;
427 $globalline{total}= $grantotal;
428 $globalline{line} = $line;
429 $globalline{column} = $column;
430 push @mainloop,\%globalline;