5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
35 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/catalogue_stats.tmpl";
47 my $line = $input->param("Line");
48 my $column = $input->param("Column");
49 my @filters = $input->param("Filter");
50 my $deweydigits = $input->param("deweydigits");
51 my $lccndigits = $input->param("lccndigits");
52 my $cotedigits = $input->param("cotedigits");
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
63 flagsrequired => {editcatalogue => 1},
66 $template->param(do_it => $do_it);
68 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
69 if ($output eq "screen"){
70 $template->param(mainloop => $results);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 print $input->header(-type => 'application/vnd.sun.xml.calc', -name=>"$basename.csv" );
75 my $cols = @$results[0]->{loopcol};
76 my $lines = @$results[0]->{looprow};
78 $sep =C4::Context->preference("delimiter");
79 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
80 foreach my $col ( @$cols ) {
81 print $col->{coltitle}.$sep;
84 foreach my $line ( @$lines ) {
85 my $x = $line->{loopcell};
86 print $line->{rowtitle}.$sep;
87 foreach my $cell (@$x) {
88 print $cell->{value}.$sep;
90 print $line->{totalrow};
94 $cols = @$results[0]->{loopfooter};
95 foreach my $col ( @$cols ) {
96 print $sep.$col->{totalcol};
98 print $sep.@$results[0]->{total};
102 my $dbh = C4::Context->dbh;
106 $req = $dbh->prepare("select distinctrow left(dewey,3) from biblioitems order by dewey");
111 while (my ($value) =$req->fetchrow) {
112 $hasdewey =1 if (($value) and (! $hasdewey));
113 push @select, $value;
115 my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
121 $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems order by lccn");
127 while (my ($value) =$req->fetchrow) {
128 $hlghtlccn = !($hasdewey);
129 $haslccn =1 if (($value) and (! $haslccn));
130 push @select, $value;
132 my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
138 $req = $dbh->prepare("select distinctrow left(itemcallnumber,5) from items order by itemcallnumber");
144 while (my ($value) =$req->fetchrow) {
145 $hascote =1 if (($value) and (! $hascote));
146 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
147 push @select, $value;
149 my $CGIcote=CGI::scrolling_list( -name => 'Filter',
157 for (my $i=1950;$i<=2050;$i++) {
160 my $CGIpublicationyear=CGI::scrolling_list( -name => 'Filter',
166 $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
170 while (my ($value) =$req->fetchrow) {
171 push @select, $value;
173 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
179 $req = $dbh->prepare("select distinctrow publishercode from biblioitems order by publishercode");
183 while (my ($value) =$req->fetchrow) {
184 push @select, $value;
186 my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
194 my $branches=getbranches();
196 $select_branches{""} = "";
197 foreach my $branch (keys %$branches) {
198 push @select, $branch;
199 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
201 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
204 -labels => \%select_branches,
208 $req = $dbh->prepare("select distinctrow location from items order by location");
212 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
218 my @mime = ( C4::Context->preference("MIME") );
219 foreach my $mime (@mime){
223 my $CGIextChoice=CGI::scrolling_list(
230 my @dels = ( C4::Context->preference("delimiter") );
231 my $CGIsepChoice=CGI::scrolling_list(
238 $template->param(hasdewey=>$hasdewey,
239 CGIFromDeweyClass => $CGIdewey,
240 CGIToDeweyClass => $CGIdewey,
242 hlghtlccn => $hlghtlccn,
243 CGIFromLoCClass => $CGIlccn,
244 CGIToLoCClass => $CGIlccn,
246 hlghtcote => $hlghtcote,
247 CGIFromCoteClass => $CGIcote,
248 CGIToCoteClass => $CGIcote,
249 CGIItemType => $CGIitemtype,
250 CGIFromPublicationYear => $CGIpublicationyear,
251 CGIToPublicationYear => $CGIpublicationyear,
252 CGIPublisher => $CGIpublisher,
253 CGIBranch => $CGIbranch,
254 CGILocation => $CGIlocation,
255 CGIextChoice => $CGIextChoice,
256 CGIsepChoice => $CGIsepChoice
260 output_html_with_http_headers $input, $cookie, $template->output;
265 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
274 my $dbh = C4::Context->dbh;
280 for (my $i=0;$i<=11;$i++) {
282 if ( @$filters[$i] ) {
283 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
284 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
286 $cell{filter} .= @$filters[$i];
287 $cell{crit} .="Dewey Classification From" if ($i==0);
288 $cell{crit} .="Dewey Classification To" if ($i==1);
289 $cell{crit} .="Lccn Classification From" if ($i==2);
290 $cell{crit} .="Lccn Classification To" if ($i==3);
291 $cell{crit} .="Cote Classification From" if ($i==4);
292 $cell{crit} .="Cote Classification To" if ($i==5);
293 $cell{crit} .="Document type" if ($i==6);
294 $cell{crit} .="Publisher" if ($i==7);
295 $cell{crit} .="Publication year From" if ($i==8);
296 $cell{crit} .="Publication year To" if ($i==9);
297 $cell{crit} .="Branch :" if ($i==10);
298 $cell{crit} .="Location:" if ($i==11);
299 push @loopfilter, \%cell;
304 # warn "filtres ".@filters[0];
305 # warn "filtres ".@filters[1];
306 # warn "filtres ".@filters[2];
307 # warn "filtres ".@filters[3];
309 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
310 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
311 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
312 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
313 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
314 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
315 @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
316 @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
317 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
318 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
319 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
320 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
323 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
324 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
325 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
326 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
327 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
328 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
329 @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
330 @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
331 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
332 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
333 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
334 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
338 if (($line =~/dewey/) and ($deweydigits)) {
339 $linefield .="left($line,$deweydigits)";
340 } elsif (($line=~/lccn/) and ($lccndigits)) {
341 $linefield .="left($line,$lccndigits)";
342 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
343 $linefield .="left($line,$cotedigits)";
350 $strsth .= "select distinctrow $linefield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $line is not null ";
353 $strsth .= " and $line >= ? " ;
354 $strsth .= " and $line <= ? " ;
355 } elsif ($linefilter[0]) {
356 $linefilter[0] =~ s/\*/%/g;
357 $strsth .= " and $line LIKE ? " ;
360 $strsth .=" order by $linefield";
363 my $sth = $dbh->prepare( $strsth );
364 if (( @linefilter ) and ($linefilter[1])){
365 $sth->execute($linefilter[0],$linefilter[1]);
366 } elsif ($linefilter[0]) {
367 $sth->execute($linefilter[0]);
371 while ( my ($celvalue) = $sth->fetchrow) {
374 $cell{rowtitle} = $celvalue;
376 $cell{rowtitle} = "";
379 push @loopline, \%cell;
384 if (($column =~/dewey/) and ($deweydigits)) {
385 $colfield .="left($column,$deweydigits)";
386 }elsif (($column=~/lccn/) and ($lccndigits)) {
387 $colfield .="left($column,$lccndigits)";
388 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
389 $colfield .="left($column,$cotedigits)";
391 $colfield .= $column;
395 $strsth2 .= "select distinctrow $colfield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $column is not null ";
396 if (( @colfilter ) and ($colfilter[1])) {
397 $strsth2 .= " and $column> ? and $column< ?";
398 }elsif ($colfilter[0]){
399 $colfilter[0] =~ s/\*/%/g;
400 $strsth2 .= " and $column LIKE ? ";
402 $strsth2 .= " order by $colfield";
404 my $sth2 = $dbh->prepare( $strsth2 );
405 if ((@colfilter) and ($colfilter[1])) {
406 $sth2->execute($colfilter[0],$colfilter[1]);
407 } elsif ($colfilter[0]){
408 $sth2->execute($colfilter[0]);
412 while (my ($celvalue) = $sth2->fetchrow) {
415 $cell{coltitle} = $celvalue;
417 push @loopcol, \%cell;
425 #Initialization of cell values.....
428 foreach my $row ( @loopline ) {
429 foreach my $col ( @loopcol ) {
430 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
431 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
433 $table{$row->{rowtitle}}->{totalrow}=0;
436 # preparing calculation
437 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems, items WHERE (items.biblioitemnumber = biblioitems.biblioitemnumber) AND $line is not null AND $column is not null";
438 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
439 $strcalc .= " AND dewey >" . @$filters[0] ."" if ( @$filters[0] );
440 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
441 $strcalc .= " AND dewey <" . @$filters[1] ."" if ( @$filters[1] );
442 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
443 $strcalc .= " AND lccn >" . @$filters[2] ."" if ( @$filters[2] );
444 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
445 $strcalc .= " AND lccn <" . @$filters[3] ."" if ( @$filters[3] );
446 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
447 $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" if ( @$filters[4] );
448 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
449 $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" if ( @$filters[5] );
450 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
451 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'" if ( @$filters[6] );
452 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
453 $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'" if ( @$filters[7] );
454 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
455 $strcalc .= " AND publicationyear >" . @$filters[8] ."" if ( @$filters[8] );
456 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
457 $strcalc .= " AND publicationyear <" . @$filters[9] ."" if ( @$filters[9] );
458 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
459 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'" if ( @$filters[10] );
460 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
461 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
462 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
464 my $dbcalc = $dbh->prepare($strcalc);
466 warn "filling table";
467 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
468 warn "filling table $row / $col / $value ";
469 $table{$row}->{$col}=$value;
470 $table{$row}->{totalrow}+=$value;
471 $grantotal += $value;
474 foreach my $row ( sort keys %table ) {
476 #@loopcol ensures the order for columns is common with column titles
477 foreach my $col ( @loopcol ) {
478 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
480 push @looprow,{ 'rowtitle' => $row,
481 'loopcell' => \@loopcell,
483 'totalrow' => $table{$row}->{totalrow}
485 $hilighted = -$hilighted;
488 warn "footer processing";
489 foreach my $col ( @loopcol ) {
491 foreach my $row ( @looprow ) {
492 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
493 warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
495 warn "summ for column ".$col->{coltitle}." = ".$total;
496 push @loopfooter, {'totalcol' => $total};
500 # the header of the table
501 $globalline{loopfilter}=\@loopfilter;
502 # the core of the table
503 $globalline{looprow} = \@looprow;
504 $globalline{loopcol} = \@loopcol;
505 # # the foot (totals by borrower type)
506 $globalline{loopfooter} = \@loopfooter;
507 $globalline{total}= $grantotal;
508 $globalline{line} = $line;
509 $globalline{column} = $column;
510 push @mainloop,\%globalline;