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;
36 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/cat_issues_top.tmpl";
48 my $limit = $input->param("Limit");
49 my $column = $input->param("Criteria");
50 my @filters = $input->param("Filter");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {editcatalogue => 1},
64 $template->param(do_it => $do_it);
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results,
72 output_html_with_http_headers $input, $cookie, $template->output;
75 # Printing to a csv file
76 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
82 $sep =C4::Context->preference("delimiter");
84 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
111 my $dbh = C4::Context->dbh;
117 my @mime = ( C4::Context->preference("MIME") );
118 # foreach my $mime (@mime){
122 my $CGIextChoice=CGI::scrolling_list(
129 my @dels = ( C4::Context->preference("delimiter") );
130 my $CGIsepChoice=CGI::scrolling_list(
137 my $branches = getallbranches;
139 foreach my $thisbranch (keys %$branches) {
140 # my $selected = 1 if $thisbranch eq $branch;
141 my %row =(value => $thisbranch,
142 # selected => $selected,
143 branchname => $branches->{$thisbranch}->{'branchname'},
145 push @branchloop, \%row;
149 my $itemtypes = getitemtypes;
151 foreach my $thisitemtype (keys %$itemtypes) {
152 # my $selected = 1 if $thisbranch eq $branch;
153 my %row =(value => $thisitemtype,
154 # selected => $selected,
155 description => $itemtypes->{$thisitemtype}->{'description'},
157 push @itemtypeloop, \%row;
161 my ($codes,$labels) = borrowercategories;
163 foreach my $thisborcat (sort keys %$labels) {
164 # my $selected = 1 if $thisbranch eq $branch;
165 my %row =(value => $thisborcat,
166 # selected => $selected,
167 description => $labels->{$thisborcat},
169 push @borcatloop, \%row;
175 CGIextChoice => $CGIextChoice,
176 CGIsepChoice => $CGIsepChoice,
177 branchloop =>\@branchloop,
178 itemtypeloop =>\@itemtypeloop,
179 borcatloop =>\@borcatloop,
181 output_html_with_http_headers $input, $cookie, $template->output;
188 my ($line, $column, $filters) = @_;
197 my $dbh = C4::Context->dbh;
203 for (my $i=0;$i<=6;$i++) {
205 if ( @$filters[$i] ) {
206 if (($i==1) and (@$filters[$i-1])) {
207 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
209 $cell{filter} .= @$filters[$i];
210 $cell{crit} .="Issue From" if ($i==0);
211 $cell{crit} .="Issue To" if ($i==1);
212 $cell{crit} .="Return From" if ($i==2);
213 $cell{crit} .="Return To" if ($i==3);
214 $cell{crit} .="Branch" if ($i==4);
215 $cell{crit} .="Doc Type" if ($i==5);
216 $cell{crit} .="Bor Cat" if ($i==6);
217 $cell{crit} .="Day" if ($i==7);
218 $cell{crit} .="Month" if ($i==8);
219 $cell{crit} .="Year" if ($i==9);
220 push @loopfilter, \%cell;
226 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
227 $column = "biblioitems.".$column if $column=~/itemtype/;
228 $column = "borrowers.".$column if $column=~/categorycode/;
230 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
231 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
232 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
233 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
234 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
235 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
236 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
237 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
238 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
239 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
240 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
241 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
244 if ($column eq "Day") {
246 $column = "issues.timestamp";
247 $colfield .="dayname($column)";
248 $colorder .="weekday($column)";
249 } elsif ($column eq "Month") {
251 $column = "issues.timestamp";
252 $colfield .="monthname($column)";
253 $colorder .="month($column)";
254 } elsif ($column eq "Year") {
256 $column = "issues.timestamp";
257 $colfield .="Year($column)";
258 $colorder .= $column;
260 $colfield .= $column;
261 $colorder .= $column;
265 $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";
266 if (($column=~/timestamp/) or ($column=~/returndate/)){
267 if ($colfilter[1] and ($colfilter[0])){
268 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
269 } elsif ($colfilter[1]) {
270 $strsth2 .= " and $column < '$colfilter[1]' " ;
271 } elsif ($colfilter[0]) {
272 $strsth2 .= " and $column > '$colfilter[0]' " ;
274 } elsif ($colfilter[0]) {
275 $colfilter[0] =~ s/\*/%/g;
276 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
278 $strsth2 .=" group by $colfield";
279 $strsth2 .=" order by $colorder";
282 my $sth2 = $dbh->prepare( $strsth2 );
283 if (( @colfilter ) and ($colfilter[1])){
284 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
285 } elsif ($colfilter[0]) {
286 $sth2->execute($colfilter[0]);
292 while (my ($celvalue) = $sth2->fetchrow) {
294 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
295 push @loopcol, \%cell;
297 # warn "fin des titres colonnes";
304 #Initialization of cell values.....
308 for (my $i=1;$i<=$line;$i++) {
309 foreach my $col ( @loopcol ) {
310 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
311 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
316 # preparing calculation
319 # Processing average loanperiods
320 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
321 $strcalc .= " , $colfield " if ($colfield);
322 $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";
324 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
325 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
326 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
327 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
328 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
329 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
330 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
331 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
332 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
333 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
334 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
335 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
336 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
337 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
338 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
339 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
340 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
341 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
342 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
343 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
345 $strcalc .= " group by biblio.biblionumber";
346 $strcalc .= ", $colfield" if ($column);
347 $strcalc .= " order by RANK DESC";
348 $strcalc .= ", $colfield " if ($colfield);
351 # $max = $line*@loopcol;
352 # } else { $max=$line;}
353 # $strcalc .= " LIMIT 0,$max";
354 warn "SQL :". $strcalc;
356 my $dbcalc = $dbh->prepare($strcalc);
358 # warn "filling table";
361 while (my @data = $dbcalc->fetchrow) {
362 my ($row, $rank, $id, $col )=@data;
363 $col = "zzEMPTY" if ($col eq undef);
364 $indice{$col}=1 if (not($indice{$col}));
365 $table[$indice{$col}]->{$col}->{'name'}=$row;
366 $table[$indice{$col}]->{$col}->{'count'}=$rank;
367 $table[$indice{$col}]->{$col}->{'link'}=$id;
368 # warn " ".$i." ".$col. " ".$row;
372 push @loopcol,{coltitle => "Global"} if not($column);
374 for ($i=1; $i<=$line;$i++) {
377 #@loopcol ensures the order for columns is common with column titles
378 # and the number matches the number of columns
380 foreach my $col ( @loopcol ) {
381 # warn " colonne :$col->{coltitle}";
386 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
387 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
388 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
390 $value =$table[$i]->{"zzEMPTY"}->{'name'};
391 $count =$table[$i]->{"zzEMPTY"}->{'count'};
392 $link =$table[$i]->{"zzEMPTY"}->{'link'};
394 # warn " ".$i ." value:$value count:$count reference:$link";
395 push @loopcell, {value => $value, count =>$count, reference => $link} ;
397 #warn "row : $row colcount:$colcount";
398 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
399 push @looprow,{ 'rowtitle' => $i ,
400 'loopcell' => \@loopcell,
401 'hilighted' => ($hilighted >0),
402 #'totalrow' => ($total)?sprintf("%.2f",$total):0
404 $hilighted = -$hilighted;
409 # the header of the table
410 $globalline{loopfilter}=\@loopfilter;
411 # the core of the table
412 $globalline{looprow} = \@looprow;
413 $globalline{loopcol} = \@loopcol;
414 # # the foot (totals by borrower type)
415 $globalline{loopfooter} = \@loopfooter;
416 $globalline{total}= $grantotal;
417 $globalline{line} = $line;
418 $globalline{column} = $column;
419 push @mainloop,\%globalline;