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
26 use C4::Branch; # GetBranches
33 plugin that shows a stats on borrowers
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/catalogue_stats.tmpl";
44 my $line = $input->param("Line");
45 my $column = $input->param("Column");
46 my @filters = $input->param("Filter");
47 my $deweydigits = $input->param("deweydigits");
48 my $lccndigits = $input->param("lccndigits");
49 my $cotedigits = $input->param("cotedigits");
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => {reports => 1},
63 $template->param(do_it => $do_it);
65 my $results = calculate($line, $column, $deweydigits, $lccndigits, $cotedigits, \@filters);
66 if ($output eq "screen"){
67 $template->param(mainloop => $results);
68 output_html_with_http_headers $input, $cookie, $template->output;
71 print $input->header(-type => 'application/vnd.sun.xml.calc',
73 -attachment=>"$basename.csv",
74 -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 count(dewey) from biblioitems ");
112 while (my ($value) =$req->fetchrow) {
113 $hasdewey =1 if (($value>2) and (! $hasdewey));
114 $count++ if (($value>2) and (! $hasdewey));
115 # push @select, $value;
117 # my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
119 # -values => \@select,
123 $req = $dbh->prepare( "select count(lccn) from biblioitems ");
129 while (my ($value) =$req->fetchrow) {
130 $hlghtlccn = !($hasdewey);
131 $haslccn =1 if (($value>2) and (! $haslccn));
132 $count++ if (($value) and (! $haslccn));
133 # push @select, $value;
135 # my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
137 # -values => \@select,
141 $req = $dbh->prepare("select count(itemcallnumber) from items");
147 while (my ($value) =$req->fetchrow) {
148 $hascote =1 if (($value>2) 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',
155 # -values => \@select,
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;
175 $req = $dbh->prepare("select itemtype from itemtypes order by itemtype");
179 while (my ($value) =$req->fetchrow) {
180 push @select, $value;
182 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
188 # $req = $dbh->prepare("select distinctrow left(publishercode,75) from biblioitems order by publishercode");
192 # while (my ($value) =$req->fetchrow) {
193 # push @select, $value;
195 # my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
197 # -values => \@select,
203 my $branches=GetBranches();
205 $select_branches{""} = "";
206 foreach my $branch (keys %$branches) {
207 push @select, $branch;
208 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
210 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
213 -labels => \%select_branches,
217 $req = $dbh->prepare("select distinctrow location from items order by location");
221 while (my ($value) =$req->fetchrow) {
222 push @select, $value;
224 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
230 my @mime = ( C4::Context->preference("MIME") );
231 foreach my $mime (@mime){
235 my $CGIextChoice=CGI::scrolling_list(
242 my @dels = ( C4::Context->preference("delimiter") );
243 my $CGIsepChoice=CGI::scrolling_list(
250 $template->param(hasdewey=>$hasdewey,
251 # CGIFromDeweyClass => $CGIdewey,
252 # CGIToDeweyClass => $CGIdewey,
254 hlghtlccn => $hlghtlccn,
255 # CGIFromLoCClass => $CGIlccn,
256 # CGIToLoCClass => $CGIlccn,
258 hlghtcote => $hlghtcote,
259 hglghtDT => $hglghtDT,
260 hglghtPub => $hglghtPub,
261 hglghtPY => $hglghtPY,
262 hglghtHB => $hglghtHB,
263 hglghtLOC => $hglghtLOC,
264 # CGIFromCoteClass => $CGIcote,
265 # CGIToCoteClass => $CGIcote,
266 CGIItemType => $CGIitemtype,
267 # CGIFromPublicationYear => $CGIpublicationyear,
268 # CGIToPublicationYear => $CGIpublicationyear,
269 # CGIPublisher => $CGIpublisher,
270 CGIBranch => $CGIbranch,
271 CGILocation => $CGIlocation,
272 CGIextChoice => $CGIextChoice,
273 CGIsepChoice => $CGIsepChoice
277 output_html_with_http_headers $input, $cookie, $template->output;
282 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
291 my $dbh = C4::Context->dbh;
297 for (my $i=0;$i<=11;$i++) {
299 if ( @$filters[$i] ) {
300 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
301 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
303 $cell{filter} .= @$filters[$i];
304 $cell{crit} .="Dewey Classification From" if ($i==0);
305 $cell{crit} .="Dewey Classification To" if ($i==1);
306 $cell{crit} .="Lccn Classification From" if ($i==2);
307 $cell{crit} .="Lccn Classification To" if ($i==3);
308 $cell{crit} .="Cote Classification From" if ($i==4);
309 $cell{crit} .="Cote Classification To" if ($i==5);
310 $cell{crit} .="Document type" if ($i==6);
311 $cell{crit} .="Publisher" if ($i==7);
312 $cell{crit} .="Publication year From" if ($i==8);
313 $cell{crit} .="Publication year To" if ($i==9);
314 $cell{crit} .="Branch :" if ($i==10);
315 $cell{crit} .="Location:" if ($i==11);
316 push @loopfilter, \%cell;
321 # warn "filtres ".@filters[0];
322 # warn "filtres ".@filters[1];
323 # warn "filtres ".@filters[2];
324 # warn "filtres ".@filters[3];
326 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
327 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
328 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
329 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
330 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
331 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
332 $linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
333 $linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
334 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
335 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
336 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
337 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
340 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
341 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
342 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
343 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
344 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
345 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
346 $colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
347 $colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
348 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
349 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
350 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
351 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
355 if (($line =~/dewey/) and ($deweydigits)) {
356 $linefield .="left($line,$deweydigits)";
357 } elsif (($line=~/lccn/) and ($lccndigits)) {
358 $linefield .="left($line,$lccndigits)";
359 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
360 $linefield .="left($line,$cotedigits)";
367 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
370 $strsth .= " and $line >= ? " ;
371 $strsth .= " and $line <= ? " ;
372 } elsif ($linefilter[0]) {
373 $linefilter[0] =~ s/\*/%/g;
374 $strsth .= " and $line LIKE ? " ;
377 $strsth .=" order by $linefield";
380 my $sth = $dbh->prepare( $strsth );
381 if (( @linefilter ) and ($linefilter[1])){
382 $sth->execute($linefilter[0],$linefilter[1]);
383 } elsif ($linefilter[0]) {
384 $sth->execute($linefilter[0]);
388 while ( my ($celvalue) = $sth->fetchrow) {
391 $cell{rowtitle} = $celvalue;
393 # $cell{rowtitle} = "";
396 push @loopline, \%cell;
401 if (($column =~/dewey/) and ($deweydigits)) {
402 $colfield .="left($column,$deweydigits)";
403 }elsif (($column=~/lccn/) and ($lccndigits)) {
404 $colfield .="left($column,$lccndigits)";
405 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
406 $colfield .="left($column,$cotedigits)";
408 $colfield .= $column;
412 $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
413 if (( @colfilter ) and ($colfilter[1])) {
414 $strsth2 .= " and $column> ? and $column< ?";
415 }elsif ($colfilter[0]){
416 $colfilter[0] =~ s/\*/%/g;
417 $strsth2 .= " and $column LIKE ? ";
419 $strsth2 .= " order by $colfield";
421 my $sth2 = $dbh->prepare( $strsth2 );
422 if ((@colfilter) and ($colfilter[1])) {
423 $sth2->execute($colfilter[0],$colfilter[1]);
424 } elsif ($colfilter[0]){
425 $sth2->execute($colfilter[0]);
429 while (my ($celvalue) = $sth2->fetchrow) {
433 $cell{coltitle} = $celvalue;
435 # $cell{coltitle} = "";
438 push @loopcol, \%cell;
446 #Initialization of cell values.....
449 foreach my $row ( @loopline ) {
450 foreach my $col ( @loopcol ) {
451 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
452 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
454 $table{$row->{rowtitle}}->{totalrow}=0;
457 # preparing calculation
458 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber) WHERE 1";
460 @$filters[0]=~ s/\*/%/g;
461 $strcalc .= " AND dewey >" . @$filters[0] ."";
464 @$filters[1]=~ s/\*/%/g ;
465 $strcalc .= " AND dewey <" . @$filters[1] ."";
469 @$filters[2]=~ s/\*/%/g ;
470 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
473 @$filters[3]=~ s/\*/%/g;
474 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
477 @$filters[4]=~ s/\*/%/g ;
478 $strcalc .= " AND items.itemcallnumber >=" . $dbh->quote(@$filters[4]) ."" ;
482 @$filters[5]=~ s/\*/%/g;
483 $strcalc .= " AND items.itemcallnumber <=" . $dbh->quote(@$filters[5]) ."" ;
487 @$filters[6]=~ s/\*/%/g;
488 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
492 @$filters[7]=~ s/\*/%/g;
493 @$filters[7].="%" unless @$filters[7]=~/%/;
494 $strcalc .= " AND biblioitems.publishercode like \"" . @$filters[7] ."\"";
497 @$filters[8]=~ s/\*/%/g;
498 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
501 @$filters[9]=~ s/\*/%/g;
502 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
505 @$filters[10]=~ s/\*/%/g;
506 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
509 @$filters[11]=~ s/\*/%/g;
510 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
513 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
515 my $dbcalc = $dbh->prepare($strcalc);
517 # warn "filling table";
520 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
521 # warn "filling table $row / $col / $value ";
522 $emptycol = 1 if ($col eq undef);
523 $col = "zzEMPTY" if ($col eq undef);
524 $row = "zzEMPTY" if ($row eq undef);
526 $table{$row}->{$col}+=$value;
527 $table{$row}->{totalrow}+=$value;
528 $grantotal += $value;
531 # my %cell = {rowtitle => 'zzROWEMPTY'};
532 # push @loopline,\%cell;
535 # %cell = {coltitle => "zzEMPTY"};
536 push @loopcol,{coltitle => "NULL"} if ($emptycol);
538 foreach my $row ( sort keys %table ) {
540 #@loopcol ensures the order for columns is common with column titles
541 # and the number matches the number of columns
542 foreach my $col ( @loopcol ) {
543 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
544 push @loopcell, {value => $value } ;
546 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
547 'loopcell' => \@loopcell,
548 'hilighted' => ($hilighted >0),
549 'totalrow' => $table{$row}->{totalrow}
551 $hilighted = -$hilighted;
554 # warn "footer processing";
555 foreach my $col ( @loopcol ) {
557 foreach my $row ( @looprow ) {
558 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
559 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
561 # warn "summ for column ".$col->{coltitle}." = ".$total;
562 push @loopfooter, {'totalcol' => $total};
566 # the header of the table
567 $globalline{loopfilter}=\@loopfilter;
568 # the core of the table
569 $globalline{looprow} = \@looprow;
570 $globalline{loopcol} = \@loopcol;
571 # # the foot (totals by borrower type)
572 $globalline{loopfooter} = \@loopfooter;
573 $globalline{total}= $grantotal;
574 $globalline{line} = $line;
575 $globalline{column} = $column;
576 push @mainloop,\%globalline;