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");
110 while (my ($value) =$req->fetchrow) {
111 push @select, $value;
113 my $CGIdewey=CGI::scrolling_list( -name => 'Filter',
119 $req = $dbh->prepare( "select distinctrow left(lccn,3) from biblioitems");
123 while (my ($value) =$req->fetchrow) {
124 push @select, $value;
126 my $CGIlccn=CGI::scrolling_list( -name => 'Filter',
132 $req = $dbh->prepare("select distinctrow left(itemcolnumber,5) from items");
136 while (my ($value) =$req->fetchrow) {
137 push @select, $value;
139 my $CGIcote=CGI::scrolling_list( -name => 'Filter',
147 for (my $i=1950;$i<=2050;$i++) {
150 my $CGIpublicationyear=CGI::scrolling_list( -name => 'Filter',
156 $req = $dbh->prepare("select distinctrow itemtype from biblioitems");
160 while (my ($value) =$req->fetchrow) {
161 push @select, $value;
163 my $CGIitemtype=CGI::scrolling_list( -name => 'Filter',
169 $req = $dbh->prepare("select distinctrow publishercode from biblioitems");
173 while (my ($value) =$req->fetchrow) {
174 push @select, $value;
176 my $CGIpublisher=CGI::scrolling_list( -name => 'Filter',
184 my $branches=getbranches();
186 $select_branches{""} = "";
187 foreach my $branch (keys %$branches) {
188 push @select, $branch;
189 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
191 my $CGIbranch=CGI::scrolling_list( -name => 'Filter',
194 -labels => \%select_branches,
198 $req = $dbh->prepare("select distinctrow location from items");
202 my $CGIlocation=CGI::scrolling_list( -name => 'Filter',
208 my @mime = ( C4::Context->preference("MIME") );
209 foreach my $mime (@mime){
213 my $CGIextChoice=CGI::scrolling_list(
220 my @dels = ( C4::Context->preference("delimiter") );
221 my $CGIsepChoice=CGI::scrolling_list(
228 $template->param(CGIFromDeweyClass => $CGIdewey,
229 CGIToDeweyClass => $CGIdewey,
230 CGIFromLoCClass => $CGIlccn,
231 CGIToLoCClass => $CGIlccn,
232 CGIFromCoteClass => $CGIcote,
233 CGIToCoteClass => $CGIcote,
234 CGIItemType => $CGIitemtype,
235 CGIFromPublicationYear => $CGIpublicationyear,
236 CGIToPublicationYear => $CGIpublicationyear,
237 CGIPublisher => $CGIpublisher,
238 CGIBranch => $CGIbranch,
239 CGILocation => $CGIlocation,
240 CGIextChoice => $CGIextChoice,
241 CGIsepChoice => $CGIsepChoice
245 output_html_with_http_headers $input, $cookie, $template->output;
250 my ($line, $column, $deweydigits, $lccndigits, $cotedigits, $filters) = @_;
259 my $dbh = C4::Context->dbh;
265 for (my $i=0;$i<=11;$i++) {
267 if ( @$filters[$i] ) {
268 if ((($i==1) or ($i==3) or ($i==5) or ($i==9)) and (@$filters[$i-1])) {
269 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
271 $cell{filter} .= @$filters[$i];
272 $cell{crit} .="Dewey Classification From :" if ($i==0);
273 $cell{crit} .="Dewey Classification To :" if ($i==1);
274 $cell{crit} .="Lccn Classification From :" if ($i==2);
275 $cell{crit} .="Lccn Classification To :" if ($i==3);
276 $cell{crit} .="Cote Classification From :" if ($i==4);
277 $cell{crit} .="Cote Classification To :" if ($i==5);
278 $cell{crit} .="Document type :" if ($i==6);
279 $cell{crit} .="Publisher :" if ($i==7);
280 $cell{crit} .="Publication year From :" if ($i==8);
281 $cell{crit} .="Publication year To :" if ($i==9);
282 $cell{crit} .="Branch :" if ($i==10);
283 $cell{crit} .="Location:" if ($i==11);
284 push @loopfilter, \%cell;
289 # warn "filtres ".@filters[0];
290 # warn "filtres ".@filters[1];
291 # warn "filtres ".@filters[2];
292 # warn "filtres ".@filters[3];
294 $linefilter = @$filters[0] if ($line =~ /dewey/ ) ;
295 $linefilter = @$filters[1] if ($line =~ /dewey/ ) ;
296 $linefilter = @$filters[2] if ($line =~ /lccn/ ) ;
297 $linefilter = @$filters[3] if ($line =~ /lccn/ ) ;
298 $linefilter = @$filters[4] if ($line =~ /itemcolnumber/ ) ;
299 $linefilter = @$filters[5] if ($line =~ /itemcolnumber/ ) ;
300 $linefilter = @$filters[6] if ($line =~ /itemtype/ ) ;
301 $linefilter = @$filters[7] if ($line =~ /publishercode/ ) ;
302 $linefilter = @$filters[8] if ($line =~ /publicationyear/ ) ;
303 $linefilter = @$filters[9] if ($line =~ /publicationyear/ ) ;
304 $linefilter = @$filters[10] if ($line =~ /items.homebranch/ ) ;
305 $linefilter = @$filters[11] if ($line =~ /items.location/ ) ;
308 $colfilter = @$filters[0] if ($column =~ /dewey/ ) ;
309 $colfilter = @$filters[1] if ($column =~ /dewey/ ) ;
310 $colfilter = @$filters[2] if ($column =~ /lccn/ ) ;
311 $colfilter = @$filters[3] if ($column =~ /lccn/ ) ;
312 $colfilter = @$filters[4] if ($column =~ /itemcolnumber/ ) ;
313 $colfilter = @$filters[5] if ($column =~ /itemcolnumber/ ) ;
314 $colfilter = @$filters[6] if ($column =~ /itemtype/ ) ;
315 $colfilter = @$filters[7] if ($column =~ /publishercode/ ) ;
316 $colfilter = @$filters[8] if ($column =~ /publicationyear/ ) ;
317 $colfilter = @$filters[9] if ($column =~ /publicationyear/ ) ;
318 $colfilter = @$filters[10] if ($column =~ /items.homebranch/ ) ;
319 $colfilter = @$filters[11] if ($column =~ /items.location/ ) ;
323 if (($line =~/dewey/) and ($deweydigits)) {
324 $linefield .="left($line,$deweydigits)";
325 } elsif (($line=~/lccn/) and ($lccndigits)) {
326 $linefield .="left($line,$lccndigits)";
327 } elsif (($line=~/itemcolnumber/) and ($cotedigits)) {
328 $linefield .="left($line,$cotedigits)";
335 $strsth .= "select distinctrow $linefield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $line is not null ";
336 $linefilter =~ s/\*/%/g;
338 $strsth .= " and $linefield LIKE ? " ;
340 $strsth .=" order by $linefield";
343 my $sth = $dbh->prepare( $strsth );
345 $sth->execute($linefilter);
349 while ( my ($celvalue) = $sth->fetchrow) {
352 $cell{rowtitle} = $celvalue;
354 $cell{rowtitle} = "";
357 push @loopline, \%cell;
362 if (($column =~/dewey/) and ($deweydigits)) {
363 $colfield .="left($column,$deweydigits)";
364 }elsif (($column=~/lccn/) and ($lccndigits)) {
365 $colfield .="left($column,$lccndigits)";
366 }elsif (($column=~/itemcolnumber/) and ($cotedigits)) {
367 $colfield .="left($column,$cotedigits)";
369 $colfield .= $column;
373 $colfilter =~ s/\*/%/g;
374 $strsth2 .= "select distinctrow $colfield from biblioitems, items where (items.biblioitemnumber = biblioitems.biblioitemnumber) and $column is not null ";
376 $strsth2 .= " and $colfield LIKE ? ";
378 $strsth2 .= " order by $colfield";
380 my $sth2 = $dbh->prepare( $strsth2 );
382 $sth2->execute($colfilter);
386 while (my ($celvalue) = $sth2->fetchrow) {
389 $cell{coltitle} = $celvalue;
391 push @loopcol, \%cell;
399 #Initialization of cell values.....
402 foreach my $row ( @loopline ) {
403 foreach my $col ( @loopcol ) {
404 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
405 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
407 $table{$row->{rowtitle}}->{totalrow}=0;
410 # preparing calculation
411 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM biblioitems, items WHERE (items.biblioitemnumber = biblioitems.biblioitemnumber) AND $line is not null AND $column is not null";
412 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
413 $strcalc .= " AND dewey >" . @$filters[0] ."" if ( @$filters[0] );
414 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
415 $strcalc .= " AND dewey <" . @$filters[1] ."" if ( @$filters[1] );
416 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
417 $strcalc .= " AND lccn >" . @$filters[2] ."" if ( @$filters[2] );
418 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
419 $strcalc .= " AND lccn <" . @$filters[3] ."" if ( @$filters[3] );
420 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
421 $strcalc .= " AND items.itemcolnumber >" . @$filters[4] ."" if ( @$filters[4] );
422 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
423 $strcalc .= " AND items.itemcolnumber <" . @$filters[5] ."" if ( @$filters[5] );
424 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
425 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[6] ."'" if ( @$filters[6] );
426 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
427 $strcalc .= " AND biblioitems.publishercode like '" . @$filters[7] ."'" if ( @$filters[7] );
428 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
429 $strcalc .= " AND publicationyear >" . @$filters[8] ."" if ( @$filters[8] );
430 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
431 $strcalc .= " AND publicationyear <" . @$filters[9] ."" if ( @$filters[9] );
432 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
433 $strcalc .= " AND items.homebranch like '" . @$filters[10] ."'" if ( @$filters[10] );
434 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
435 $strcalc .= " AND items.location like '" . @$filters[11] ."'" if ( @$filters[11] );
436 $strcalc .= " group by $linefield, $colfield order by $linefield,$colfield";
438 my $dbcalc = $dbh->prepare($strcalc);
440 # warn "filling table";
441 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
442 # warn "filling table $row / $col / $value ";
443 $table{$row}->{$col}=$value;
444 $table{$row}->{totalrow}+=$value;
445 $grantotal += $value;
448 foreach my $row ( sort keys %table ) {
450 #@loopcol ensures the order for columns is common with column titles
451 foreach my $col ( @loopcol ) {
452 push @loopcell, {value => $table{$row}->{$col->{coltitle}}} ;
454 push @looprow,{ 'rowtitle' => $row,
455 'loopcell' => \@loopcell,
457 'totalrow' => $table{$row}->{totalrow}
459 $hilighted = -$hilighted;
462 foreach my $col ( @loopcol ) {
464 foreach my $row ( @loopline ) {
465 $total += $table{$row->{rowtitle}}->{$col->{coltitle}};
467 push @loopfooter, {'totalcol' => $total};
471 # the header of the table
472 $globalline{loopfilter}=\@loopfilter;
473 # the core of the table
474 $globalline{looprow} = \@looprow;
475 $globalline{loopcol} = \@loopcol;
476 # # the foot (totals by borrower type)
477 $globalline{loopfooter} = \@loopfooter;
478 $globalline{total}= $grantotal;
479 $globalline{line} = $line;
480 $globalline{column} = $column;
481 push @mainloop,\%globalline;