4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use C4
::Branch
; # GetBranches
30 use C4
::Dates qw
/format_date format_date_in_iso/;
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/cat_issues_top.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 $filters[0]=format_date_in_iso
($filters[0]);
50 $filters[1]=format_date_in_iso
($filters[1]);
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 #warn "calcul : ".$calc;
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user
({template_name
=> $fullreportname,
60 flagsrequired
=> { reports
=> '*'},
63 our $sep = $input->param("sep");
64 $sep = "\t" if ($sep eq 'tabulation');
65 $template->param(do_it
=> $do_it,
66 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
70 my $results = calculate
($limit, $column, \
@filters);
71 if ($output eq "screen"){
72 # Printing results to screen
73 $template->param(mainloop
=> $results,
75 output_html_with_http_headers
$input, $cookie, $template->output;
78 # Printing to a csv file
79 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
81 -attachment
=>"$basename.csv",
82 -filename
=>"$basename.csv" );
83 my $cols = @
$results[0]->{loopcol
};
84 my $lines = @
$results[0]->{looprow
};
86 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
88 foreach my $col ( @
$cols ) {
89 print $col->{coltitle
}.$sep;
93 foreach my $line ( @
$lines ) {
94 my $x = $line->{loopcell
};
95 print $line->{rowtitle
}.$sep;
96 foreach my $cell (@
$x) {
97 print $cell->{value
}.$sep;
99 print $line->{totalrow
};
104 $cols = @
$results[0]->{loopfooter
};
105 foreach my $col ( @
$cols ) {
106 print $sep.$col->{totalcol
};
108 print $sep.@
$results[0]->{total
};
113 my $dbh = C4
::Context
->dbh;
119 my @mime = ( C4
::Context
->preference("MIME") );
120 # foreach my $mime (@mime){
124 my $CGIextChoice=CGI
::scrolling_list
(
131 my $CGIsepChoice=GetDelimiterChoices
;
134 my $itemtypes = GetItemTypes
;
136 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
137 my %row =(value
=> $thisitemtype,
138 description
=> $itemtypes->{$thisitemtype}->{'description'},
140 push @itemtypeloop, \
%row;
144 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
146 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
147 my %row =(value
=> $thisborcat,
148 description
=> $labels->{$thisborcat},
150 push @borcatloop, \
%row;
156 CGIextChoice
=> $CGIextChoice,
157 CGIsepChoice
=> $CGIsepChoice,
158 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
159 itemtypeloop
=>\
@itemtypeloop,
160 borcatloop
=>\
@borcatloop,
162 output_html_with_http_headers
$input, $cookie, $template->output;
169 my ($line, $column, $filters) = @_;
178 my $dbh = C4
::Context
->dbh;
184 for (my $i=0;$i<=6;$i++) {
186 if ( @
$filters[$i] ) {
187 if (($i==1) and (@
$filters[$i-1])) {
188 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
190 # format the dates filters, otherwise just fill as is
192 $cell{filter
} .= @
$filters[$i];
194 $cell{filter
} .= format_date
(@
$filters[$i]);
195 } $cell{crit
} .="Issue From" if ($i==0);
196 $cell{crit
} .="Issue To" if ($i==1);
197 $cell{crit
} .="Return From" if ($i==2);
198 $cell{crit
} .="Return To" if ($i==3);
199 $cell{crit
} .="Branch" if ($i==4);
200 $cell{crit
} .="Doc Type" if ($i==5);
201 $cell{crit
} .="Bor Cat" if ($i==6);
202 $cell{crit
} .="Day" if ($i==7);
203 $cell{crit
} .="Month" if ($i==8);
204 $cell{crit
} .="Year" if ($i==9);
205 push @loopfilter, \
%cell;
211 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
212 $column = "biblioitems.".$column if $column=~/itemtype/;
213 $column = "borrowers.".$column if $column=~/categorycode/;
215 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
216 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
217 $colfilter[0] = @
$filters[2] if ($column =~ /returndate/ ) ;
218 $colfilter[1] = @
$filters[3] if ($column =~ /returndate/ ) ;
219 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
220 $colfilter[0] = @
$filters[5] if ($column =~ /itemtype/ ) ;
221 $colfilter[0] = @
$filters[6] if ($column =~ /category/ ) ;
222 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
223 $colfilter[0] = @
$filters[7] if ($column =~ /timestamp/ ) ;
224 $colfilter[0] = @
$filters[8] if ($column =~ /timestamp/ ) ;
225 $colfilter[0] = @
$filters[9] if ($column =~ /timestamp/ ) ;
226 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
229 if ($column eq "Day") {
231 $column = "old_issues.timestamp";
232 $colfield .="dayname($column)";
233 $colorder .="weekday($column)";
234 } elsif ($column eq "Month") {
236 $column = "old_issues.timestamp";
237 $colfield .="monthname($column)";
238 $colorder .="month($column)";
239 } elsif ($column eq "Year") {
241 $column = "old_issues.timestamp";
242 $colfield .="Year($column)";
243 $colorder .= $column;
245 $colfield .= $column;
246 $colorder .= $column;
250 $strsth2 .= "SELECT distinctrow $colfield
252 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
253 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
254 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
256 if (($column=~/timestamp/) or ($column=~/returndate/)){
257 if ($colfilter[1] and ($colfilter[0])){
258 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
259 } elsif ($colfilter[1]) {
260 $strsth2 .= " and $column < '$colfilter[1]' " ;
261 } elsif ($colfilter[0]) {
262 $strsth2 .= " and $column > '$colfilter[0]' " ;
264 } elsif ($colfilter[0]) {
265 $colfilter[0] =~ s/\*/%/g;
266 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
268 $strsth2 .=" group by $colfield";
269 $strsth2 .=" order by $colorder";
272 my $sth2 = $dbh->prepare( $strsth2 );
273 if (( @colfilter ) and ($colfilter[1])){
274 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
275 } elsif ($colfilter[0]) {
276 $sth2->execute($colfilter[0]);
282 while (my ($celvalue) = $sth2->fetchrow) {
284 $cell{coltitle
} = ($celvalue?
$celvalue:"NULL");
285 push @loopcol, \
%cell;
287 # warn "fin des titres colonnes";
294 #Initialization of cell values.....
298 for (my $i=1;$i<=$line;$i++) {
299 foreach my $col ( @loopcol ) {
300 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
301 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"total"}->{'name'}=0;
306 # preparing calculation
309 # Processing average loanperiods
310 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
311 $strcalc .= " , $colfield " if ($colfield);
312 $strcalc .= " FROM `old_issues`
313 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
315 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber)
316 ON items.itemnumber=old_issues.itemnumber
317 LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber)
320 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
321 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
322 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
323 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
324 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
325 $strcalc .= " AND old_issues.returndate > '" . @
$filters[2] ."'" if ( @
$filters[2] );
326 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
327 $strcalc .= " AND old_issues.returndate < '" . @
$filters[3] ."'" if ( @
$filters[3] );
328 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
329 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[4] ."'" if ( @
$filters[4] );
330 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
331 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[5] ."'" if ( @
$filters[5] );
332 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
333 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[6] ."'" if ( @
$filters[6] );
334 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
335 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @
$filters[7]."'" if (@
$filters[7]);
336 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
337 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @
$filters[8]."'" if (@
$filters[8]);
338 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
339 $strcalc .= " AND year(old_issues.timestamp) like '" . @
$filters[9] ."'" if ( @
$filters[9] );
341 $strcalc .= " group by biblio.biblionumber";
342 $strcalc .= ", $colfield" if ($column);
343 $strcalc .= " order by RANK DESC";
344 $strcalc .= ", $colfield " if ($colfield);
348 # $max = $line*@loopcol;
349 # } else { $max=$line;}
350 # $strcalc .= " LIMIT 0,$max";
351 warn "SQL :". $strcalc;
353 my $dbcalc = $dbh->prepare($strcalc);
355 # warn "filling table";
358 while (my @data = $dbcalc->fetchrow) {
359 my ($row, $rank, $id, $col )=@data;
360 $col = "zzEMPTY" if (!defined($col));
361 $indice{$col}=1 if (not($indice{$col}));
362 $table[$indice{$col}]->{$col}->{'name'}=$row;
363 $table[$indice{$col}]->{$col}->{'count'}=$rank;
364 $table[$indice{$col}]->{$col}->{'link'}=$id;
365 # warn " ".$i." ".$col. " ".$row;
369 push @loopcol,{coltitle
=> "Global"} if not($column);
371 for ($i=1; $i<=$line;$i++) {
374 #@loopcol ensures the order for columns is common with column titles
375 # and the number matches the number of columns
377 foreach my $col ( @loopcol ) {
378 # warn " colonne :$col->{coltitle}";
383 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'name'};
384 $count =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'count'};
385 $link =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'link'};
387 $value =$table[$i]->{"zzEMPTY"}->{'name'};
388 $count =$table[$i]->{"zzEMPTY"}->{'count'};
389 $link =$table[$i]->{"zzEMPTY"}->{'link'};
391 # warn " ".$i ." value:$value count:$count reference:$link";
392 push @loopcell, {value
=> $value, count
=>$count, reference
=> $link} ;
394 #warn "row : $row colcount:$colcount";
395 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
396 push @looprow,{ 'rowtitle' => $i ,
397 'loopcell' => \
@loopcell,
398 'hilighted' => ($hilighted >0),
399 #'totalrow' => ($total)?sprintf("%.2f",$total):0
401 $hilighted = -$hilighted;
406 # the header of the table
407 $globalline{loopfilter
}=\
@loopfilter;
408 # the core of the table
409 $globalline{looprow
} = \
@looprow;
410 $globalline{loopcol
} = \
@loopcol;
411 # # the foot (totals by borrower type)
412 $globalline{loopfooter
} = \
@loopfooter;
413 $globalline{total
}= $grantotal;
414 $globalline{line
} = $line;
415 $globalline{column
} = $column;
416 push @mainloop,\
%globalline;