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',
75 -attachment=>"$basename.csv",
76 -name=>"$basename.csv" );
77 my $cols = @$results[0]->{loopcol};
78 my $lines = @$results[0]->{looprow};
80 $sep =C4::Context->preference("delimiter");
81 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
82 foreach my $col ( @$cols ) {
83 print $col->{coltitle}.$sep;
86 foreach my $line ( @$lines ) {
87 my $x = $line->{loopcell};
88 print $line->{rowtitle}.$sep;
89 foreach my $cell (@$x) {
90 print $cell->{value}.$sep;
92 print $line->{totalrow};
96 $cols = @$results[0]->{loopfooter};
97 foreach my $col ( @$cols ) {
98 print $sep.$col->{totalcol};
100 print $sep.@$results[0]->{total};
104 my $dbh = C4::Context->dbh;
109 $req = $dbh->prepare("select distinctrow left(dewey,3) from biblioitems order by dewey");
114 while (my ($value) =$req->fetchrow) {
115 $hasdewey =1 if (($value) and (! $hasdewey));
116 $count++ if (($value) and (! $hasdewey));
117 push @select, $value;
119 my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
125 $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems order by lccn");
131 while (my ($value) =$req->fetchrow) {
132 $hlghtlccn = !($hasdewey);
133 $haslccn =1 if (($value) and (! $haslccn));
134 $count++ if (($value) and (! $haslccn));
135 push @select, $value;
137 my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
143 $req = $dbh->prepare("select distinctrow left(itemcallnumber,5) from items order by itemcallnumber");
149 while (my ($value) =$req->fetchrow) {
150 $hascote =1 if (($value) and (! $hascote));
151 $count++ if (($value) and (! $hascote));
152 $hlghtcote = (($hasdewey) and ($haslccn)) or (!($hasdewey) and !($haslccn));
153 push @select, $value;
155 my $CGIcote=CGI::scrolling_list( -name => 'Filter',
161 my $hglghtDT =$count % 2;
162 # warn "highlightDT ".$hglghtDT;
164 my $hglghtPub =$count % 2;
165 # warn "highlightPub ".$hglghtPub;
167 my $hglghtPY =$count % 2;
168 # warn "highlightPY ".$hglghtPY;
170 my $hglghtHB =$count % 2;
171 # warn "highlightHB ".$hglghtHB;
173 my $hglghtLOC =$count % 2;
174 # warn "highlightLOC ".$hglghtLOC;
178 # for (my $i=1950;$i<=2050;$i++) {
181 # my $CGIpublicationyear=CGI::scrolling_list( -name => 'Filter',
183 # -values => \@select,
187 $req = $dbh->prepare("select distinctrow itemtype from biblioitems order by itemtype");
191 while (my ($value) =$req->fetchrow) {
192 push @select, $value;
194 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
200 $req = $dbh->prepare("select distinctrow publishercode from biblioitems order by publishercode");
204 while (my ($value) =$req->fetchrow) {
205 push @select, $value;
207 my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
215 my $branches=getbranches();
217 $select_branches{""} = "";
218 foreach my $branch (keys %$branches) {
219 push @select, $branch;
220 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
222 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
225 -labels => \%select_branches,
229 $req = $dbh->prepare("select distinctrow location from items order by location");
233 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
239 my @mime = ( C4::Context->preference("MIME") );
240 foreach my $mime (@mime){
244 my $CGIextChoice=CGI::scrolling_list(
251 my @dels = ( C4::Context->preference("delimiter") );
252 my $CGIsepChoice=CGI::scrolling_list(
259 $template->param(hasdewey=>$hasdewey,
260 CGIFromDeweyClass => $CGIdewey,
261 CGIToDeweyClass => $CGIdewey,
263 hlghtlccn => $hlghtlccn,
264 CGIFromLoCClass => $CGIlccn,
265 CGIToLoCClass => $CGIlccn,
267 hlghtcote => $hlghtcote,
268 hglghtDT => $hglghtDT,
269 hglghtPub => $hglghtPub,
270 hglghtPY => $hglghtPY,
271 hglghtHB => $hglghtHB,
272 hglghtLOC => $hglghtLOC,
273 CGIFromCoteClass => $CGIcote,
274 CGIToCoteClass => $CGIcote,
275 CGIItemType => $CGIitemtype,
276 # CGIFromPublicationYear => $CGIpublicationyear,
277 # CGIToPublicationYear => $CGIpublicationyear,
278 CGIPublisher => $CGIpublisher,
279 CGIBranch => $CGIbranch,
280 CGILocation => $CGIlocation,
281 CGIextChoice => $CGIextChoice,
282 CGIsepChoice => $CGIsepChoice
286 output_html_with_http_headers $input, $cookie, $template->output;
291 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
300 my $dbh = C4::Context->dbh;
306 for (my $i=0;$i<=11;$i++) {
308 if ( @$filters[$i] ) {
309 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
310 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
312 $cell{filter} .= @$filters[$i];
313 $cell{crit} .="Dewey Classification From" if ($i==0);
314 $cell{crit} .="Dewey Classification To" if ($i==1);
315 $cell{crit} .="Lccn Classification From" if ($i==2);
316 $cell{crit} .="Lccn Classification To" if ($i==3);
317 $cell{crit} .="Cote Classification From" if ($i==4);
318 $cell{crit} .="Cote Classification To" if ($i==5);
319 $cell{crit} .="Document type" if ($i==6);
320 $cell{crit} .="Publisher" if ($i==7);
321 $cell{crit} .="Publication year From" if ($i==8);
322 $cell{crit} .="Publication year To" if ($i==9);
323 $cell{crit} .="Branch :" if ($i==10);
324 $cell{crit} .="Location:" if ($i==11);
325 push @loopfilter, \%cell;
330 # warn "filtres ".@filters[0];
331 # warn "filtres ".@filters[1];
332 # warn "filtres ".@filters[2];
333 # warn "filtres ".@filters[3];
335 $linefilter[0] = @$filters[0] if ($line =~ /dewey/ ) ;
336 $linefilter[1] = @$filters[1] if ($line =~ /dewey/ ) ;
337 $linefilter[0] = @$filters[2] if ($line =~ /lccn/ ) ;
338 $linefilter[1] = @$filters[3] if ($line =~ /lccn/ ) ;
339 $linefilter[0] = @$filters[4] if ($line =~ /items.itemcallnumber/ ) ;
340 $linefilter[1] = @$filters[5] if ($line =~ /items.itemcallnumber/ ) ;
341 @linefilter[0] = @$filters[6] if ($line =~ /itemtype/ ) ;
342 @linefilter[0] = @$filters[7] if ($line =~ /publishercode/ ) ;
343 $linefilter[0] = @$filters[8] if ($line =~ /publicationyear/ ) ;
344 $linefilter[1] = @$filters[9] if ($line =~ /publicationyear/ ) ;
345 @linefilter[0] = @$filters[10] if ($line =~ /items.homebranch/ ) ;
346 @linefilter[0] = @$filters[11] if ($line =~ /items.location/ ) ;
349 $colfilter[0] = @$filters[0] if ($column =~ /dewey/ ) ;
350 $colfilter[1] = @$filters[1] if ($column =~ /dewey/ ) ;
351 $colfilter[0] = @$filters[2] if ($column =~ /lccn/ ) ;
352 $colfilter[1] = @$filters[3] if ($column =~ /lccn/ ) ;
353 $colfilter[0] = @$filters[4] if ($column =~ /itemcallnumber/ ) ;
354 $colfilter[1] = @$filters[5] if ($column =~ /itemcallnumber/ ) ;
355 @colfilter[0] = @$filters[6] if ($column =~ /itemtype/ ) ;
356 @colfilter[0] = @$filters[7] if ($column =~ /publishercode/ ) ;
357 $colfilter[0] = @$filters[8] if ($column =~ /publicationyear/ ) ;
358 $colfilter[1] = @$filters[9] if ($column =~ /publicationyear/ ) ;
359 @colfilter[0] = @$filters[10] if ($column =~ /items.homebranch/ ) ;
360 @colfilter[0] = @$filters[11] if ($column =~ /items.location/ ) ;
364 if (($line =~/dewey/) and ($deweydigits)) {
365 $linefield .="left($line,$deweydigits)";
366 } elsif (($line=~/lccn/) and ($lccndigits)) {
367 $linefield .="left($line,$lccndigits)";
368 } elsif (($line=~/items.itemcallnumber/) and ($cotedigits)) {
369 $linefield .="left($line,$cotedigits)";
376 $strsth .= "select distinctrow $linefield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $line is not null ";
379 $strsth .= " and $line >= ? " ;
380 $strsth .= " and $line <= ? " ;
381 } elsif ($linefilter[0]) {
382 $linefilter[0] =~ s/\*/%/g;
383 $strsth .= " and $line LIKE ? " ;
386 $strsth .=" order by $linefield";
389 my $sth = $dbh->prepare( $strsth );
390 if (( @linefilter ) and ($linefilter[1])){
391 $sth->execute($linefilter[0],$linefilter[1]);
392 } elsif ($linefilter[0]) {
393 $sth->execute($linefilter[0]);
397 while ( my ($celvalue) = $sth->fetchrow) {
400 $cell{rowtitle} = $celvalue;
402 $cell{rowtitle} = "";
405 push @loopline, \%cell;
410 if (($column =~/dewey/) and ($deweydigits)) {
411 $colfield .="left($column,$deweydigits)";
412 }elsif (($column=~/lccn/) and ($lccndigits)) {
413 $colfield .="left($column,$lccndigits)";
414 }elsif (($column=~/itemcallnumber/) and ($cotedigits)) {
415 $colfield .="left($column,$cotedigits)";
417 $colfield .= $column;
421 $strsth2 .= "select distinctrow $colfield from biblioitems left join items on (items.biblioitemnumber = biblioitems.biblioitemnumber) where $column is not null ";
422 if (( @colfilter ) and ($colfilter[1])) {
423 $strsth2 .= " and $column> ? and $column< ?";
424 }elsif ($colfilter[0]){
425 $colfilter[0] =~ s/\*/%/g;
426 $strsth2 .= " and $column LIKE ? ";
428 $strsth2 .= " order by $colfield";
430 my $sth2 = $dbh->prepare( $strsth2 );
431 if ((@colfilter) and ($colfilter[1])) {
432 $sth2->execute($colfilter[0],$colfilter[1]);
433 } elsif ($colfilter[0]){
434 $sth2->execute($colfilter[0]);
438 while (my ($celvalue) = $sth2->fetchrow) {
442 $cell{coltitle} = $celvalue;
444 $cell{coltitle} = "";
447 push @loopcol, \%cell;
455 #Initialization of cell values.....
458 foreach my $row ( @loopline ) {
459 foreach my $col ( @loopcol ) {
460 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
461 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
463 $table{$row->{rowtitle}}->{totalrow}=0;
466 # preparing calculation
467 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems LEFT JOIN items ON (items.biblioitemnumber = biblioitems.biblioitemnumber)";
470 @$filters[0]=~ s/\*/%/g;
471 $strcalc .= " WHERE dewey >" . @$filters[0] ."";
475 @$filters[1]=~ s/\*/%/g ;
477 $strcalc .= " AND dewey <" . @$filters[1] ."";
479 $strcalc .= " WHERE dewey <" . @$filters[1] ."" ;
485 @$filters[2]=~ s/\*/%/g ;
487 $strcalc .= " AND lccn >" . @$filters[2] ."" ;
489 $strcalc .= " WHERE lccn > " . @$filters[2] ."" ;
494 @$filters[3]=~ s/\*/%/g;
496 $strcalc .= " AND lccn <" . @$filters[3] ."" ;
498 $strcalc .= " WHERE lccn <" . @$filters[3] ."" ;
503 @$filters[4]=~ s/\*/%/g ;
505 $strcalc .= " AND items.itemcallnumber >" . @$filters[4] ."" ;
507 $strcalc .= " WHERE items.itemcallnumber >" . @$filters[4] ."" ;
513 @$filters[5]=~ s/\*/%/g;
515 $strcalc .= " AND items.itemcallnumber <" . @$filters[5] ."" ;
517 $strcalc .= " WHERE items.itemcallnumber <" . @$filters[5] ."" ;
523 @$filters[6]=~ s/\*/%/g;
525 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'";
527 $strcalc .= " WHERE biblioitems.itemtype like '" . @$filters[6] ."'";
533 @$filters[7]=~ s/\*/%/g;
535 $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'";
537 $strcalc .= " WHERE biblioitems.publishercode like '" . @$filters[7] ."'";
542 @$filters[8]=~ s/\*/%/g;
544 $strcalc .= " AND publicationyear >" . @$filters[8] ."" ;
546 $strcalc .= " WHERE publicationyear >" . @$filters[8] ."" ;
551 @$filters[9]=~ s/\*/%/g;
553 $strcalc .= " AND publicationyear <" . @$filters[9] ."";
555 $strcalc .= " WHERE publicationyear <" . @$filters[9] ."";
560 @$filters[10]=~ s/\*/%/g;
562 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'";
564 $strcalc .= " WHERE items.homebranch like '" . @$filters[10] ."'";
569 @$filters[11]=~ s/\*/%/g;
571 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
573 $strcalc .= " WHERE items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
577 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
579 my $dbcalc = $dbh->prepare($strcalc);
581 # warn "filling table";
584 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
585 # warn "filling table $row / $col / $value ";
586 $row = "zzEMPTY" if ($row eq undef);
587 $col = "zzEMPTY" if ($col eq undef);
588 $emptycol = 1 if ($col eq undef);
590 $table{$row}->{$col}+=$value;
591 $table{$row}->{totalrow}+=$value;
592 $grantotal += $value;
595 # my %cell = {rowtitle => 'zzROWEMPTY'};
596 # push @loopline,\%cell;
599 # %cell = {coltitle => "zzEMPTY"};
600 push @loopcol,{coltitle => "zzEMPTY"} if ($emptycol);
602 foreach my $row ( sort keys %table ) {
604 #@loopcol ensures the order for columns is common with column titles
605 # and the number matches the number of columns
606 foreach my $col ( @loopcol ) {
607 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
609 push @looprow,{ 'rowtitle' => $row,
610 'loopcell' => \@loopcell,
612 'totalrow' => $table{$row}->{totalrow}
614 $hilighted = -$hilighted;
617 # warn "footer processing";
618 foreach my $col ( @loopcol ) {
620 foreach my $row ( @looprow ) {
621 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
622 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
624 # warn "summ for column ".$col->{coltitle}." = ".$total;
625 push @loopfooter, {'totalcol' => $total};
629 # the header of the table
630 $globalline{loopfilter}=\@loopfilter;
631 # the core of the table
632 $globalline{looprow} = \@looprow;
633 $globalline{loopcol} = \@loopcol;
634 # # the foot (totals by borrower type)
635 $globalline{loopfooter} = \@loopfooter;
636 $globalline{total}= $grantotal;
637 $globalline{line} = $line;
638 $globalline{column} = $column;
639 push @mainloop,\%globalline;