2 ## Will not work. Requires a complete re-write for ZEBRA
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
29 use C4::Interface::CGI::Output;
30 use C4::Circulation::Circ2;
34 plugin that shows a stats on catalogue
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/catalogue_stats.tmpl";
46 my $line = $input->param("Line");
47 my $column = $input->param("Column");
48 my @filters = $input->param("Filter");
49 my $deweydigits = $input->param("deweydigits");
50 my $lccndigits = $input->param("lccndigits");
51 my $cotedigits = $input->param("cotedigits");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {editcatalogue => 1},
65 $template->param(do_it => $do_it);
67 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
68 if ($output eq "screen"){
69 $template->param(mainloop => $results);
70 output_html_with_http_headers $input, $cookie, $template->output;
73 print $input->header(-type => 'application/vnd.sun.xml.calc',
74 -attachment=>"$basename.csv",
75 -name=>"$basename.csv" );
76 my $cols = @$results[0]->{loopcol};
77 my $lines = @$results[0]->{looprow};
79 $sep =C4::Context->preference("delimiter");
80 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
81 foreach my $col ( @$cols ) {
82 print $col->{coltitle}.$sep;
85 foreach my $line ( @$lines ) {
86 my $x = $line->{loopcell};
87 print $line->{rowtitle}.$sep;
88 foreach my $cell (@$x) {
89 print $cell->{value}.$sep;
91 print $line->{totalrow};
95 $cols = @$results[0]->{loopfooter};
96 foreach my $col ( @$cols ) {
97 print $sep.$col->{totalcol};
99 print $sep.@$results[0]->{total};
103 my $dbh = C4::Context->dbh;
108 ###Use mfield of koha_attr instead of dewey
109 my $sth = $dbh->prepare("select authorised_value from authorised_values where category='mfield' order by lib");
111 my @authorised_values;
112 #push @authorised_values,"";
113 while ((my $category) = $sth->fetchrow_array) {
114 push @authorised_values, $category;
117 my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
119 -values => \@authorised_values,
127 for my $value ("A".."Z") {
128 push @select, $value;
130 my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
139 my $hglghtDT =$count % 2;
141 my $hglghtPub =$count % 2;
143 my $hglghtPY =$count % 2;
145 my $hglghtHB =$count % 2;
147 my $hglghtLOC =$count % 2;
148 my $hglghtSTACK =$count % 2;
150 my $itemtypes = GetItemTypes;
154 $select_item{""} = "";
155 foreach my $thisitemtype (sort keys %$itemtypes) {
156 push @select,$thisitemtype;
157 $select_item{$thisitemtype}=$itemtypes->{$thisitemtype}->{'description'};
161 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
164 -labels=>\%select_item,
171 my $branches=GetBranches();
173 $select_branches{""} = "";
174 foreach my $branch (keys %$branches) {
175 push @select, $branch;
176 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
178 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
181 -labels => \%select_branches,
185 my $CGIholdingbranch=CGI::scrolling_list( -name => 'Filter',
186 -id => 'holdingbranch',
188 -labels => \%select_branches,
191 $req = $dbh->prepare("select authorised_value,lib from authorised_values where category='sections'");
197 while (my ($value,$desc) =$req->fetchrow) {
198 push @select, $value;
202 my $CGISTACK=CGI::scrolling_list( -name => 'Filter',
209 my @mime = ( C4::Context->preference("MIME") );
210 foreach my $mime (@mime){
214 my $CGIextChoice=CGI::scrolling_list(
221 my @dels = ( C4::Context->preference("delimiter") );
222 my $CGIsepChoice=CGI::scrolling_list(
229 $template->param(hasdewey=>$hasdewey,
230 CGIFromDeweyClass => $CGIdewey,
231 CGIToDeweyClass => $CGIdewey,
233 hlghtlccn => $hlghtlccn,
234 CGIFromLoCClass => $CGIlccn,
235 CGIToLoCClass => $CGIlccn,
237 hlghtcote => $hlghtcote,
238 hglghtDT => $hglghtDT,
239 hglghtPub => $hglghtPub,
240 hglghtPY => $hglghtPY,
241 hglghtHB => $hglghtHB,
242 hglghtLOC => $hglghtLOC,
243 hglghtSTACK => $hglghtSTACK,
244 CGIItemType => $CGIitemtype,
245 CGIBranch => $CGIbranch,
246 CGILocation => $CGIbranch,
247 CGISTACK => $CGISTACK,
248 CGIextChoice => $CGIextChoice,
249 CGIsepChoice => $CGIsepChoice
253 output_html_with_http_headers $input, $cookie, $template->output;
258 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
267 my $dbh = C4::Context->dbh;
273 for (my $i=0;$i<=11;$i++) {
275 if ( @$filters[$i] ) {
276 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
277 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
279 $cell{filter} .= @$filters[$i];
280 $cell{crit} .="Dewey Classification From" if ($i==0);
281 $cell{crit} .="Dewey Classification To" if ($i==1);
282 $cell{crit} .="Classification From" if ($i==2);
283 $cell{crit} .="Classification To" if ($i==3);
284 $cell{crit} .="Call Number From" if ($i==4);
285 $cell{crit} .="Call Number To" if ($i==5);
286 $cell{crit} .="Document type" if ($i==6);
287 $cell{crit} .="Publisher" if ($i==7);
288 $cell{crit} .="Publication year From" if ($i==8);
289 $cell{crit} .="Publication year To" if ($i==9);
290 $cell{crit} .="Branch :" if ($i==10);
291 $cell{crit} .="Location:" if ($i==11);
292 $cell{crit} .="Shelving:" if ($i==12);
293 push @loopfilter, \%cell;
298 # warn "filtres ".@filters[0];
299 # warn "filtres ".@filters[1];
300 # warn "filtres ".@filters[2];
301 # warn "filtres ".@filters[3];
303 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
304 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
305 $linefilter[0] = @$filters[2] if ($line =~ /classification/ ) ;
306 $linefilter[1] = @$filters[3] if ($line =~ /classification/ ) ;
307 $linefilter[0] = @$filters[4] if ($line =~ /itemcallnumber/ ) ;
308 $linefilter[1] = @$filters[5] if ($line =~ /itemcallnumber/ ) ;
309 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
310 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
311 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
312 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
313 $linefilter[0] = @$filters[10] if ($line =~ /homebranch/ ) ;
314 $linefilter[0] = @$filters[11] if ($line =~ /holdingbranch/ ) ;
315 $linefilter[0] = @$filters[12] if ($line =~ /shelf/ ) ;
318 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
319 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
320 $colfilter[0] = @$filters[2] if ($column =~ /classification/ ) ;
321 $colfilter[1] = @$filters[3] if ($column =~ /classification/ ) ;
322 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
323 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
324 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
325 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
326 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
327 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
328 $colfilter[0] = @$filters[10] if ($column =~ /homebranch/ ) ;
329 $colfilter[0] = @$filters[11] if ($column =~ /holdingbranch/ ) ;
330 $colfilter[0] = @$filters[12] if ($column =~ /shelf/ ) ;
333 if (($line =~/dewey/) and ($deweydigits)) {
334 $linefield .="left($line,$deweydigits)";
335 } elsif (($line=~/classification/) and ($lccndigits)) {
336 $linefield .="left($line,$lccndigits)";
337 } elsif (($line=~/itemcallnumber/) and ($cotedigits)) {
338 $linefield .="left($line,$cotedigits)";
343 warn $linefield,$colfilter[0],$linefilter[0],$line;
345 $strsth .= "select distinctrow $linefield from biblio left join items on (items.biblionumber = biblio.biblionumber) where $line is not null ";
348 $strsth .= " and $line >= ? " ;
349 $strsth .= " and $line <= ? " ;
350 } elsif ($linefilter[0]) {
351 $linefilter[0] =~ s/\*/%/g;
352 $strsth .= " and $line LIKE ? " ;
355 $strsth .=" order by $linefield";
358 my $sth = $dbh->prepare( $strsth );
359 if (( @linefilter ) and ($linefilter[1])){
360 $sth->execute($linefilter[0],$linefilter[1]);
361 } elsif ($linefilter[0]) {
362 $sth->execute($linefilter[0]);
366 while ( my ($celvalue) = $sth->fetchrow) {
369 $cell{rowtitle} = $celvalue;
371 # $cell{rowtitle} = "";
374 push @loopline, \%cell;
379 if (($column =~/dewey/) and ($deweydigits)) {
380 $colfield .="left($column,$deweydigits)";
381 }elsif (($column=~/classification/) and ($lccndigits)) {
382 $colfield .="left($column,$lccndigits)";
383 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
384 $colfield .="left($column,$cotedigits)";
386 $colfield .= $column;
390 $strsth2 .= "select distinctrow $colfield from biblio left join items on (items.biblionumber = biblio.biblionumber) where $column is not null ";
391 if (( @colfilter ) and ($colfilter[1])) {
392 $strsth2 .= " and $column> ? and $column< ?";
393 }elsif ($colfilter[0]){
394 $colfilter[0] =~ s/\*/%/g;
395 $strsth2 .= " and $column LIKE ? ";
397 $strsth2 .= " order by $colfield";
399 my $sth2 = $dbh->prepare( $strsth2 );
400 if ((@colfilter) and ($colfilter[1])) {
401 $sth2->execute($colfilter[0],$colfilter[1]);
402 } elsif ($colfilter[0]){
403 $sth2->execute($colfilter[0]);
407 while (my ($celvalue) = $sth2->fetchrow) {
411 $cell{coltitle} = $celvalue;
413 # $cell{coltitle} = "";
416 push @loopcol, \%cell;
424 #Initialization of cell values.....
427 foreach my $row ( @loopline ) {
428 foreach my $col ( @loopcol ) {
429 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
430 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
432 $table{$row->{rowtitle}}->{totalrow}=0;
438 # preparing calculation in zebra
439 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
441 @$filters[0]=~ s/\*/%/g;
442 $strcalc .= " AND dewey >" . @$filters[0] ."";
443 push @and_or, "\@and";
444 push @relations "\@attr 2=5";
445 push @kohafield, "dewey";
446 push @values, @$filters[0] ;
450 @$filters[1]=~ s/\*/%/g ;
451 $strcalc .= " AND dewey <" . @$filters[1] ."";
452 push @and_or, "\@and";
453 push @relations "\@attr 2=1";
454 push @kohafield, "dewey";
455 push @values, @$filters[1] ;
460 @$filters[2]=~ s/\*/%/g ;
461 $strcalc .= " AND classification >=" .$dbh->quote(@$filters[2]) ."" ;
462 push @and_or, "\@and";
463 push @relations "\@attr 2=4";
464 push @kohafield, "classification";
465 push @values, @$filters[2];
469 @$filters[3]=~ s/\*/%/g;
470 $strcalc .= " AND classification <=" . $dbh->quote(@$filters[3]) ."" ;
471 push @and_or, "\@and";
472 push @relations "\@attr 2=2";
473 push @kohafield, "classification";
474 push @values, @$filters[3] ;
477 @$filters[4]=~ s/\*/%/g ;
478 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
479 push @and_or, "\@and";
480 push @relations "\@attr 2=4";
481 push @kohafield, "itemcallnumber";
482 push @values, @$filters[4] ;
486 @$filters[5]=~ s/\*/%/g;
487 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
488 push @and_or, "\@and";
489 push @relations "\@attr 2=2";
490 push @kohafield, "itemcallnumber";
491 push @values, @$filters[5] ;
495 @$filters[6]=~ s/\*/%/g;
496 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
497 push @and_or, "\@and";
498 push @relations "\@attr 2=3";
499 push @kohafield, "itemtype";
500 push @values, @$filters[6] ;
504 @$filters[7]=~ s/\*/%/g;
505 @$filters[7].="%" unless @$filters[7]=~/%/;
506 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
507 push @and_or, "\@and";
508 push @relations "\@attr 2=3";
509 push @kohafield, "publishercode";
510 push @values, @$filters[7];
513 @$filters[8]=~ s/\*/%/g;
514 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
517 @$filters[9]=~ s/\*/%/g;
518 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
521 @$filters[10]=~ s/\*/%/g;
522 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
525 @$filters[11]=~ s/\*/%/g;
526 $strcalc .= " AND items.holdingbranch like '" . @$filters[11] ."'" if ( @$filters[11] );
529 @$filters[12]=~ s/\*/%/g;
530 $strcalc .= " AND items.stack like '" . @$filters[12] ."'" if ( @$filters[12] );
532 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
534 my $dbcalc = $dbh->prepare($strcalc);
536 # warn "filling table";
539 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
540 # warn "filling table $row / $col / $value ";
541 $emptycol = 1 if ($col eq undef);
542 $col = "zzEMPTY" if ($col eq undef);
543 $row = "zzEMPTY" if ($row eq undef);
545 $table{$row}->{$col}+=$value;
546 $table{$row}->{totalrow}+=$value;
547 $grantotal += $value;
550 push @loopcol,{coltitle => "NULL"} if ($emptycol);
552 foreach my $row ( sort keys %table ) {
554 #@loopcol ensures the order for columns is common with column titles
555 # and the number matches the number of columns
556 foreach my $col ( @loopcol ) {
557 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
558 push @loopcell, {value => $value } ;
560 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
561 'loopcell' => \@loopcell,
562 'hilighted' => ($hilighted >0),
563 'totalrow' => $table{$row}->{totalrow}
565 $hilighted = -$hilighted;
568 # warn "footer processing";
569 foreach my $col ( @loopcol ) {
571 foreach my $row ( @looprow ) {
572 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
573 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
575 # warn "summ for column ".$col->{coltitle}." = ".$total;
576 push @loopfooter, {'totalcol' => $total};
580 # the header of the table
581 $globalline{loopfilter}=\@loopfilter;
582 # the core of the table
583 $globalline{looprow} = \@looprow;
584 $globalline{loopcol} = \@loopcol;
585 # # the foot (totals by borrower type)
586 $globalline{loopfooter} = \@loopfooter;
587 $globalline{total}= $grantotal;
588 $globalline{line} = $line;
589 $globalline{column} = $column;
590 push @mainloop,\%globalline;