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;
37 plugin that shows a stats on borrowers
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/cat_issues_top.tmpl";
49 my $limit = $input->param("Limit");
50 my $column = $input->param("Criteria");
51 my @filters = $input->param("Filter");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
56 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {editcatalogue => 1},
65 $template->param(do_it => $do_it,
66 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
67 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
68 IntranetNav => C4::Context->preference("IntranetNav"),
72 my $results = calculate($limit, $column, \@filters);
73 if ($output eq "screen"){
74 # Printing results to screen
75 $template->param(mainloop => $results,
77 output_html_with_http_headers $input, $cookie, $template->output;
80 # Printing to a csv file
81 print $input->header(-type => 'application/vnd.sun.xml.calc',
82 -attachment=>"$basename.csv",
83 -filename=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
87 $sep =C4::Context->preference("delimiter");
89 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
91 foreach my $col ( @$cols ) {
92 print $col->{coltitle}.$sep;
96 foreach my $line ( @$lines ) {
97 my $x = $line->{loopcell};
98 print $line->{rowtitle}.$sep;
99 foreach my $cell (@$x) {
100 print $cell->{value}.$sep;
102 print $line->{totalrow};
107 $cols = @$results[0]->{loopfooter};
108 foreach my $col ( @$cols ) {
109 print $sep.$col->{totalcol};
111 print $sep.@$results[0]->{total};
116 my $dbh = C4::Context->dbh;
122 my @mime = ( C4::Context->preference("MIME") );
123 # foreach my $mime (@mime){
127 my $CGIextChoice=CGI::scrolling_list(
134 my @dels = ( C4::Context->preference("delimiter") );
135 my $CGIsepChoice=CGI::scrolling_list(
142 my $branches = getallbranches;
144 foreach my $thisbranch (keys %$branches) {
145 # my $selected = 1 if $thisbranch eq $branch;
146 my %row =(value => $thisbranch,
147 # selected => $selected,
148 branchname => $branches->{$thisbranch}->{'branchname'},
150 push @branchloop, \%row;
154 my $itemtypes = GetItemTypes;
156 foreach my $thisitemtype (keys %$itemtypes) {
157 # my $selected = 1 if $thisbranch eq $branch;
158 my %row =(value => $thisitemtype,
159 # selected => $selected,
160 description => $itemtypes->{$thisitemtype}->{'description'},
162 push @itemtypeloop, \%row;
166 my ($codes,$labels) = borrowercategories;
168 foreach my $thisborcat (sort keys %$labels) {
169 # my $selected = 1 if $thisbranch eq $branch;
170 my %row =(value => $thisborcat,
171 # selected => $selected,
172 description => $labels->{$thisborcat},
174 push @borcatloop, \%row;
180 CGIextChoice => $CGIextChoice,
181 CGIsepChoice => $CGIsepChoice,
182 branchloop =>\@branchloop,
183 itemtypeloop =>\@itemtypeloop,
184 borcatloop =>\@borcatloop,
186 output_html_with_http_headers $input, $cookie, $template->output;
193 my ($line, $column, $filters) = @_;
202 my $dbh = C4::Context->dbh;
208 for (my $i=0;$i<=6;$i++) {
210 if ( @$filters[$i] ) {
211 if (($i==1) and (@$filters[$i-1])) {
212 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
214 $cell{filter} .= @$filters[$i];
215 $cell{crit} .="Issue From" if ($i==0);
216 $cell{crit} .="Issue To" if ($i==1);
217 $cell{crit} .="Return From" if ($i==2);
218 $cell{crit} .="Return To" if ($i==3);
219 $cell{crit} .="Branch" if ($i==4);
220 $cell{crit} .="Doc Type" if ($i==5);
221 $cell{crit} .="Bor Cat" if ($i==6);
222 $cell{crit} .="Day" if ($i==7);
223 $cell{crit} .="Month" if ($i==8);
224 $cell{crit} .="Year" if ($i==9);
225 push @loopfilter, \%cell;
231 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
232 $column = "biblioitems.".$column if $column=~/itemtype/;
233 $column = "borrowers.".$column if $column=~/categorycode/;
235 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
236 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
237 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
238 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
239 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
240 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
241 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
242 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
243 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
244 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
246 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
249 if ($column eq "Day") {
251 $column = "issues.timestamp";
252 $colfield .="dayname($column)";
253 $colorder .="weekday($column)";
254 } elsif ($column eq "Month") {
256 $column = "issues.timestamp";
257 $colfield .="monthname($column)";
258 $colorder .="month($column)";
259 } elsif ($column eq "Year") {
261 $column = "issues.timestamp";
262 $colfield .="Year($column)";
263 $colorder .= $column;
265 $colfield .= $column;
266 $colorder .= $column;
270 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
271 if (($column=~/timestamp/) or ($column=~/returndate/)){
272 if ($colfilter[1] and ($colfilter[0])){
273 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
274 } elsif ($colfilter[1]) {
275 $strsth2 .= " and $column < '$colfilter[1]' " ;
276 } elsif ($colfilter[0]) {
277 $strsth2 .= " and $column > '$colfilter[0]' " ;
279 } elsif ($colfilter[0]) {
280 $colfilter[0] =~ s/\*/%/g;
281 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
283 $strsth2 .=" group by $colfield";
284 $strsth2 .=" order by $colorder";
287 my $sth2 = $dbh->prepare( $strsth2 );
288 if (( @colfilter ) and ($colfilter[1])){
289 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
290 } elsif ($colfilter[0]) {
291 $sth2->execute($colfilter[0]);
297 while (my ($celvalue) = $sth2->fetchrow) {
299 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
300 push @loopcol, \%cell;
302 # warn "fin des titres colonnes";
309 #Initialization of cell values.....
313 for (my $i=1;$i<=$line;$i++) {
314 foreach my $col ( @loopcol ) {
315 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
316 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
321 # preparing calculation
324 # Processing average loanperiods
325 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
326 $strcalc .= " , $colfield " if ($colfield);
327 $strcalc .= " FROM `issues`,borrowers,(items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
329 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
330 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
331 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
332 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
333 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
334 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
335 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
336 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
337 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
338 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
339 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
340 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
341 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
342 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
343 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
344 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
345 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
346 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
347 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
348 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
350 $strcalc .= " group by biblio.biblionumber";
351 $strcalc .= ", $colfield" if ($column);
352 $strcalc .= " order by RANK DESC";
353 $strcalc .= ", $colfield " if ($colfield);
356 # $max = $line*@loopcol;
357 # } else { $max=$line;}
358 # $strcalc .= " LIMIT 0,$max";
359 warn "SQL :". $strcalc;
361 my $dbcalc = $dbh->prepare($strcalc);
363 # warn "filling table";
366 while (my @data = $dbcalc->fetchrow) {
367 my ($row, $rank, $id, $col )=@data;
368 $col = "zzEMPTY" if ($col eq undef);
369 $indice{$col}=1 if (not($indice{$col}));
370 $table[$indice{$col}]->{$col}->{'name'}=$row;
371 $table[$indice{$col}]->{$col}->{'count'}=$rank;
372 $table[$indice{$col}]->{$col}->{'link'}=$id;
373 # warn " ".$i." ".$col. " ".$row;
377 push @loopcol,{coltitle => "Global"} if not($column);
379 for ($i=1; $i<=$line;$i++) {
382 #@loopcol ensures the order for columns is common with column titles
383 # and the number matches the number of columns
385 foreach my $col ( @loopcol ) {
386 # warn " colonne :$col->{coltitle}";
391 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
392 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
393 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
395 $value =$table[$i]->{"zzEMPTY"}->{'name'};
396 $count =$table[$i]->{"zzEMPTY"}->{'count'};
397 $link =$table[$i]->{"zzEMPTY"}->{'link'};
399 # warn " ".$i ." value:$value count:$count reference:$link";
400 push @loopcell, {value => $value, count =>$count, reference => $link} ;
402 #warn "row : $row colcount:$colcount";
403 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
404 push @looprow,{ 'rowtitle' => $i ,
405 'loopcell' => \@loopcell,
406 'hilighted' => ($hilighted >0),
407 #'totalrow' => ($total)?sprintf("%.2f",$total):0
409 $hilighted = -$hilighted;
414 # the header of the table
415 $globalline{loopfilter}=\@loopfilter;
416 # the core of the table
417 $globalline{looprow} = \@looprow;
418 $globalline{loopcol} = \@loopcol;
419 # # the foot (totals by borrower type)
420 $globalline{loopfooter} = \@loopfooter;
421 $globalline{total}= $grantotal;
422 $globalline{line} = $line;
423 $globalline{column} = $column;
424 push @mainloop,\%globalline;