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
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 #use warnings; FIXME - Bug 2505
26 use C4
::Branch
; # GetBranches
36 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 foreach ( @filters[0..3] ) {
51 $_ and $_ = eval { output_pref
( { dt
=> dt_from_string
( $_ ), dateonly
=> 1, dateformat
=> 'iso' } ); };
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user
({template_name
=> $fullreportname,
62 flagsrequired
=> { reports
=> '*'},
65 our $sep = $input->param("sep");
66 $sep = "\t" if ($sep eq 'tabulation');
67 $template->param(do_it
=> $do_it,
71 my $results = calculate
($limit, $column, \
@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop
=> $results,
76 output_html_with_http_headers
$input, $cookie, $template->output;
79 # Printing to a csv file
80 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 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
89 foreach my $col ( @
$cols ) {
90 print $col->{coltitle
}.$sep;
94 foreach my $line ( @
$lines ) {
95 my $x = $line->{loopcell
};
96 print $line->{rowtitle
}.$sep;
97 foreach my $cell (@
$x) {
98 print $cell->{value
}.$sep;
100 print $line->{totalrow
};
105 $cols = @
$results[0]->{loopfooter
};
106 foreach my $col ( @
$cols ) {
107 print $sep.$col->{totalcol
};
109 print $sep.@
$results[0]->{total
};
114 my $dbh = C4
::Context
->dbh;
120 my $CGIextChoice = ( 'CSV' ); # FIXME translation
121 my $CGIsepChoice=GetDelimiterChoices
;
124 my $itemtypes = GetItemTypes
;
126 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{translated_description
} cmp $itemtypes->{$b}->{translated_description
}} keys %$itemtypes) {
127 my %row =(value
=> $thisitemtype,
128 description
=> $itemtypes->{$thisitemtype}->{translated_description
},
130 push @itemtypeloop, \
%row;
134 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
136 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
137 my %row =(value
=> $thisborcat,
138 description
=> $labels->{$thisborcat},
140 push @borcatloop, \
%row;
146 CGIextChoice
=> $CGIextChoice,
147 CGIsepChoice
=> $CGIsepChoice,
148 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
149 itemtypeloop
=>\
@itemtypeloop,
150 borcatloop
=>\
@borcatloop,
152 output_html_with_http_headers
$input, $cookie, $template->output;
159 my ($line, $column, $filters) = @_;
168 my $dbh = C4
::Context
->dbh;
174 for (my $i=0;$i<=6;$i++) {
176 if ( @
$filters[$i] ) {
177 if (($i==1) and (@
$filters[$i-1])) {
178 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
180 # format the dates filters, otherwise just fill as is
182 $cell{filter
} .= @
$filters[$i];
184 $cell{filter
} .= eval { output_pref
( { dt
=> dt_from_string
( @
$filters[$i] ), dateonly
=> 1 }); }
185 if ( @
$filters[$i] );
187 $cell{crit
} .="Issue From" if ($i==0);
188 $cell{crit
} .="Issue To" if ($i==1);
189 $cell{crit
} .="Return From" if ($i==2);
190 $cell{crit
} .="Return To" if ($i==3);
191 $cell{crit
} .="Branch" if ($i==4);
192 $cell{crit
} .="Doc Type" if ($i==5);
193 $cell{crit
} .="Bor Cat" if ($i==6);
194 $cell{crit
} .="Day" if ($i==7);
195 $cell{crit
} .="Month" if ($i==8);
196 $cell{crit
} .="Year" if ($i==9);
197 push @loopfilter, \
%cell;
203 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
204 if($column=~/itemtype/){
205 $column = C4
::Context
->preference('item-level_itypes') ?
"items.itype": "biblioitems.itemtype";
207 $column = "borrowers.".$column if $column=~/categorycode/;
209 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
210 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
211 $colfilter[0] = @
$filters[2] if ($column =~ /returndate/ ) ;
212 $colfilter[1] = @
$filters[3] if ($column =~ /returndate/ ) ;
213 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
214 $colfilter[0] = @
$filters[5] if ($column =~ /itemtype/ ) ;
215 $colfilter[0] = @
$filters[6] if ($column =~ /category/ ) ;
216 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
217 $colfilter[0] = @
$filters[7] if ($column =~ /timestamp/ ) ;
218 $colfilter[0] = @
$filters[8] if ($column =~ /timestamp/ ) ;
219 $colfilter[0] = @
$filters[9] if ($column =~ /timestamp/ ) ;
220 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
223 if ($column eq "Day") {
225 $column = "old_issues.timestamp";
226 $colfield .="dayname($column)";
227 $colorder .="weekday($column)";
228 } elsif ($column eq "Month") {
230 $column = "old_issues.timestamp";
231 $colfield .="monthname($column)";
232 $colorder .="month($column)";
233 } elsif ($column eq "Year") {
235 $column = "old_issues.timestamp";
236 $colfield .="Year($column)";
237 $colorder .= $column;
239 $colfield .= $column;
240 $colorder .= $column;
244 $strsth2 .= "SELECT distinctrow $colfield
246 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
247 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
248 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
250 if (($column=~/timestamp/) or ($column=~/returndate/)){
251 if ($colfilter[1] and ($colfilter[0])){
252 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
253 } elsif ($colfilter[1]) {
254 $strsth2 .= " and $column < '$colfilter[1]' " ;
255 } elsif ($colfilter[0]) {
256 $strsth2 .= " and $column > '$colfilter[0]' " ;
258 } elsif ($colfilter[0]) {
259 $colfilter[0] =~ s/\*/%/g;
260 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
262 $strsth2 .=" group by $colfield";
263 $strsth2 .=" order by $colorder";
265 my $sth2 = $dbh->prepare( $strsth2 );
266 if (( @colfilter ) and ($colfilter[1])){
267 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
268 } elsif ($colfilter[0]) {
269 $sth2->execute($colfilter[0]);
275 while (my ($celvalue) = $sth2->fetchrow) {
277 $cell{coltitle
} = ($celvalue?
$celvalue:"NULL");
278 push @loopcol, \
%cell;
280 # warn "fin des titres colonnes";
287 #Initialization of cell values.....
291 for (my $i=1;$i<=$line;$i++) {
292 foreach my $col ( @loopcol ) {
293 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
294 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"total"}->{'name'}=0;
299 # preparing calculation
302 # Processing average loanperiods
303 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
304 $strcalc .= " , $colfield " if ($colfield);
305 $strcalc .= " FROM `old_issues`
306 LEFT JOIN items USING(itemnumber)
307 LEFT JOIN biblio USING(biblionumber)
308 LEFT JOIN biblioitems USING(biblionumber)
309 LEFT JOIN borrowers USING(borrowernumber)
312 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
313 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
314 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
315 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
316 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
317 $strcalc .= " AND old_issues.returndate > '" . @
$filters[2] ."'" if ( @
$filters[2] );
318 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
319 $strcalc .= " AND old_issues.returndate < '" . @
$filters[3] ."'" if ( @
$filters[3] );
320 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
321 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[4] ."'" if ( @
$filters[4] );
322 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
324 if(C4
::Context
->preference('item-level_itypes') ){
325 $strcalc .= " AND items.itype like "
327 $strcalc .= " AND biblioitems.itemtype like "
329 $strcalc .= "'" . @
$filters[5] ."'" ;
331 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
332 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[6] ."'" if ( @
$filters[6] );
333 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
334 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @
$filters[7]."'" if (@
$filters[7]);
335 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
336 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @
$filters[8]."'" if (@
$filters[8]);
337 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
338 $strcalc .= " AND year(old_issues.timestamp) like '" . @
$filters[9] ."'" if ( @
$filters[9] );
340 $strcalc .= " group by biblio.biblionumber";
341 $strcalc .= ", $colfield" if ($column);
342 $strcalc .= " order by RANK DESC";
343 $strcalc .= ", $colfield " if ($colfield);
345 my $dbcalc = $dbh->prepare($strcalc);
349 while (my @data = $dbcalc->fetchrow) {
350 my ($row, $rank, $id, $col )=@data;
351 $col = "zzEMPTY" if (!defined($col));
352 $indice{$col}=1 if (not($indice{$col}));
353 $table[$indice{$col}]->{$col}->{'name'}=$row;
354 $table[$indice{$col}]->{$col}->{'count'}=$rank;
355 $table[$indice{$col}]->{$col}->{'link'}=$id;
359 push @loopcol,{coltitle
=> "Global"} if not($column);
361 for ($i=1; $i<=$line;$i++) {
363 #@loopcol ensures the order for columns is common with column titles
364 # and the number matches the number of columns
366 foreach my $col ( @loopcol ) {
371 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'name'};
372 $count =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'count'};
373 $link =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'link'};
375 $value =$table[$i]->{"zzEMPTY"}->{'name'};
376 $count =$table[$i]->{"zzEMPTY"}->{'count'};
377 $link =$table[$i]->{"zzEMPTY"}->{'link'};
379 push @loopcell, {value
=> $value, count
=>$count, reference
=> $link} ;
381 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
382 push @looprow,{ 'rowtitle' => $i ,
383 'loopcell' => \
@loopcell,
384 'hilighted' => ($hilighted >0),
386 $hilighted = -$hilighted;
391 # the header of the table
392 $globalline{loopfilter
}=\
@loopfilter;
393 # the core of the table
394 $globalline{looprow
} = \
@looprow;
395 $globalline{loopcol
} = \
@loopcol;
396 # # the foot (totals by borrower type)
397 $globalline{loopfooter
} = \
@loopfooter;
398 $globalline{total
}= $grantotal;
399 $globalline{line
} = $line;
400 $globalline{column
} = $column;
401 push @mainloop,\
%globalline;