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;
107 $req = $dbh->prepare("select distinctrow left(dewey,3) from biblioitems order by dewey");
112 while (my ($value) =$req->fetchrow) {
113 $hasdewey =1 if (($value) and (! $hasdewey));
114 $count++ if (($value) and (! $hasdewey));
115 push @select, $value;
117 my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
123 $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems order by lccn");
129 while (my ($value) =$req->fetchrow) {
130 $hlghtlccn = !($hasdewey);
131 $haslccn =1 if (($value) and (! $haslccn));
132 $count++ if (($value) and (! $haslccn));
133 push @select, $value;
135 my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
141 $req = $dbh->prepare("select distinctrow left(itemcallnumber,5) from items order by itemcallnumber");
147 while (my ($value) =$req->fetchrow) {
148 $hascote =1 if (($value) and (! $hascote));
149 $count++ if (($value) and (! $hascote));
150 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
151 push @select, $value;
153 my $CGIcote=CGI::scrolling_list( -name => 'Filter',
159 my $hglghtDT =$count % 2;
160 # warn "highlightDT ".$hglghtDT;
162 my $hglghtPub =$count % 2;
163 # warn "highlightPub ".$hglghtPub;
165 my $hglghtPY =$count % 2;
166 # warn "highlightPY ".$hglghtPY;
168 my $hglghtHB =$count % 2;
169 # warn "highlightHB ".$hglghtHB;
171 my $hglghtLOC =$count % 2;
172 # warn "highlightLOC ".$hglghtLOC;
176 # for (my $i=1950;$i<=2050;$i++) {
179 # my $CGIpublicationyear=CGI::scrolling_list( -name => 'Filter',
181 # -values => \@select,
185 $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
189 while (my ($value) =$req->fetchrow) {
190 push @select, $value;
192 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
198 $req = $dbh->prepare("select distinctrow publishercode from biblioitems order by publishercode");
202 while (my ($value) =$req->fetchrow) {
203 push @select, $value;
205 my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
213 my $branches=getbranches();
215 $select_branches{""} = "";
216 foreach my $branch (keys %$branches) {
217 push @select, $branch;
218 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
220 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
223 -labels => \%select_branches,
227 $req = $dbh->prepare("select distinctrow location from items order by location");
231 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
237 my @mime = ( C4::Context->preference("MIME") );
238 foreach my $mime (@mime){
242 my $CGIextChoice=CGI::scrolling_list(
249 my @dels = ( C4::Context->preference("delimiter") );
250 my $CGIsepChoice=CGI::scrolling_list(
257 $template->param(hasdewey=>$hasdewey,
258 CGIFromDeweyClass => $CGIdewey,
259 CGIToDeweyClass => $CGIdewey,
261 hlghtlccn => $hlghtlccn,
262 CGIFromLoCClass => $CGIlccn,
263 CGIToLoCClass => $CGIlccn,
265 hlghtcote => $hlghtcote,
266 hglghtDT => $hglghtDT,
267 hglghtPub => $hglghtPub,
268 hglghtPY => $hglghtPY,
269 hglghtHB => $hglghtHB,
270 hglghtLOC => $hglghtLOC,
271 CGIFromCoteClass => $CGIcote,
272 CGIToCoteClass => $CGIcote,
273 CGIItemType => $CGIitemtype,
274 # CGIFromPublicationYear => $CGIpublicationyear,
275 # CGIToPublicationYear => $CGIpublicationyear,
276 CGIPublisher => $CGIpublisher,
277 CGIBranch => $CGIbranch,
278 CGILocation => $CGIlocation,
279 CGIextChoice => $CGIextChoice,
280 CGIsepChoice => $CGIsepChoice
284 output_html_with_http_headers $input, $cookie, $template->output;
289 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
298 my $dbh = C4::Context->dbh;
304 for (my $i=0;$i<=11;$i++) {
306 if ( @$filters[$i] ) {
307 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
308 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
310 $cell{filter} .= @$filters[$i];
311 $cell{crit} .="Dewey Classification From" if ($i==0);
312 $cell{crit} .="Dewey Classification To" if ($i==1);
313 $cell{crit} .="Lccn Classification From" if ($i==2);
314 $cell{crit} .="Lccn Classification To" if ($i==3);
315 $cell{crit} .="Cote Classification From" if ($i==4);
316 $cell{crit} .="Cote Classification To" if ($i==5);
317 $cell{crit} .="Document type" if ($i==6);
318 $cell{crit} .="Publisher" if ($i==7);
319 $cell{crit} .="Publication year From" if ($i==8);
320 $cell{crit} .="Publication year To" if ($i==9);
321 $cell{crit} .="Branch :" if ($i==10);
322 $cell{crit} .="Location:" if ($i==11);
323 push @loopfilter, \%cell;
328 # warn "filtres ".@filters[0];
329 # warn "filtres ".@filters[1];
330 # warn "filtres ".@filters[2];
331 # warn "filtres ".@filters[3];
333 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
334 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
335 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
336 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
337 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
338 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
339 @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
340 @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
341 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
342 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
343 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
344 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
347 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
348 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
349 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
350 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
351 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
352 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
353 @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
354 @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
355 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
356 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
357 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
358 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
362 if (($line =~/dewey/) and ($deweydigits)) {
363 $linefield .="left($line,$deweydigits)";
364 } elsif (($line=~/lccn/) and ($lccndigits)) {
365 $linefield .="left($line,$lccndigits)";
366 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
367 $linefield .="left($line,$cotedigits)";
374 $strsth .= "select distinctrow $linefield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $line is not null ";
377 $strsth .= " and $line >= ? " ;
378 $strsth .= " and $line <= ? " ;
379 } elsif ($linefilter[0]) {
380 $linefilter[0] =~ s/\*/%/g;
381 $strsth .= " and $line LIKE ? " ;
384 $strsth .=" order by $linefield";
387 my $sth = $dbh->prepare( $strsth );
388 if (( @linefilter ) and ($linefilter[1])){
389 $sth->execute($linefilter[0],$linefilter[1]);
390 } elsif ($linefilter[0]) {
391 $sth->execute($linefilter[0]);
395 while ( my ($celvalue) = $sth->fetchrow) {
398 $cell{rowtitle} = $celvalue;
400 $cell{rowtitle} = "";
403 push @loopline, \%cell;
408 if (($column =~/dewey/) and ($deweydigits)) {
409 $colfield .="left($column,$deweydigits)";
410 }elsif (($column=~/lccn/) and ($lccndigits)) {
411 $colfield .="left($column,$lccndigits)";
412 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
413 $colfield .="left($column,$cotedigits)";
415 $colfield .= $column;
419 $strsth2 .= "select distinctrow $colfield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $column is not null ";
420 if (( @colfilter ) and ($colfilter[1])) {
421 $strsth2 .= " and $column> ? and $column< ?";
422 }elsif ($colfilter[0]){
423 $colfilter[0] =~ s/\*/%/g;
424 $strsth2 .= " and $column LIKE ? ";
426 $strsth2 .= " order by $colfield";
428 my $sth2 = $dbh->prepare( $strsth2 );
429 if ((@colfilter) and ($colfilter[1])) {
430 $sth2->execute($colfilter[0],$colfilter[1]);
431 } elsif ($colfilter[0]){
432 $sth2->execute($colfilter[0]);
436 while (my ($celvalue) = $sth2->fetchrow) {
439 $cell{coltitle} = $celvalue;
441 push @loopcol, \%cell;
449 #Initialization of cell values.....
452 foreach my $row ( @loopline ) {
453 foreach my $col ( @loopcol ) {
454 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
455 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
457 $table{$row->{rowtitle}}->{totalrow}=0;
460 # preparing calculation
461 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems, items WHERE (items.biblioitemnumber = biblioitems.biblioitemnumber) AND $line is not null AND $column is not null";
462 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
463 $strcalc .= " AND dewey >" . @$filters[0] ."" if ( @$filters[0] );
464 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
465 $strcalc .= " AND dewey <" . @$filters[1] ."" if ( @$filters[1] );
466 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
467 $strcalc .= " AND lccn >" . @$filters[2] ."" if ( @$filters[2] );
468 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
469 $strcalc .= " AND lccn <" . @$filters[3] ."" if ( @$filters[3] );
470 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
471 $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" if ( @$filters[4] );
472 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
473 $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" if ( @$filters[5] );
474 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
475 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'" if ( @$filters[6] );
476 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
477 $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'" if ( @$filters[7] );
478 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
479 $strcalc .= " AND publicationyear >" . @$filters[8] ."" if ( @$filters[8] );
480 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
481 $strcalc .= " AND publicationyear <" . @$filters[9] ."" if ( @$filters[9] );
482 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
483 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'" if ( @$filters[10] );
484 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
485 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
486 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
488 my $dbcalc = $dbh->prepare($strcalc);
490 # warn "filling table";
491 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
492 # warn "filling table $row / $col / $value ";
493 $table{$row}->{$col}=$value;
494 $table{$row}->{totalrow}+=$value;
495 $grantotal += $value;
498 foreach my $row ( sort keys %table ) {
500 #@loopcol ensures the order for columns is common with column titles
501 foreach my $col ( @loopcol ) {
502 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
504 push @looprow,{ 'rowtitle' => $row,
505 'loopcell' => \@loopcell,
507 'totalrow' => $table{$row}->{totalrow}
509 $hilighted = -$hilighted;
512 # warn "footer processing";
513 foreach my $col ( @loopcol ) {
515 foreach my $row ( @looprow ) {
516 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
517 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
519 # warn "summ for column ".$col->{coltitle}." = ".$total;
520 push @loopfooter, {'totalcol' => $total};
524 # the header of the table
525 $globalline{loopfilter}=\@loopfilter;
526 # the core of the table
527 $globalline{looprow} = \@looprow;
528 $globalline{loopcol} = \@loopcol;
529 # # the foot (totals by borrower type)
530 $globalline{loopfooter} = \@loopfooter;
531 $globalline{total}= $grantotal;
532 $globalline{line} = $line;
533 $globalline{column} = $column;
534 push @mainloop,\%globalline;