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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #use warnings; FIXME - Bug 2505
26 use C4
::Branch
; # GetBranches
31 use C4
::Dates qw
/format_date format_date_in_iso/;
36 plugin that shows a stats on borrowers
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/cat_issues_top.tmpl";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->param("Filter");
50 $filters[0]=format_date_in_iso
($filters[0]);
51 $filters[1]=format_date_in_iso
($filters[1]);
52 $filters[2]=format_date_in_iso
($filters[2]);
53 $filters[3]=format_date_in_iso
($filters[3]);
54 my $output = $input->param("output");
55 my $basename = $input->param("basename");
56 my $mime = $input->param("MIME");
57 #warn "calcul : ".$calc;
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user
({template_name
=> $fullreportname,
63 flagsrequired
=> { reports
=> '*'},
66 our $sep = $input->param("sep");
67 $sep = "\t" if ($sep eq 'tabulation');
68 $template->param(do_it
=> $do_it,
69 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
73 my $results = calculate
($limit, $column, \
@filters);
74 if ($output eq "screen"){
75 # Printing results to screen
76 $template->param(mainloop
=> $results,
78 output_html_with_http_headers
$input, $cookie, $template->output;
81 # Printing to a csv file
82 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
84 -attachment
=>"$basename.csv",
85 -filename
=>"$basename.csv" );
86 my $cols = @
$results[0]->{loopcol
};
87 my $lines = @
$results[0]->{looprow
};
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 $CGIsepChoice=GetDelimiterChoices
;
137 my $itemtypes = GetItemTypes
;
139 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
140 my %row =(value
=> $thisitemtype,
141 description
=> $itemtypes->{$thisitemtype}->{'description'},
143 push @itemtypeloop, \
%row;
147 my ($codes,$labels) = GetborCatFromCatType
(undef,undef);
149 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
150 my %row =(value
=> $thisborcat,
151 description
=> $labels->{$thisborcat},
153 push @borcatloop, \
%row;
159 CGIextChoice
=> $CGIextChoice,
160 CGIsepChoice
=> $CGIsepChoice,
161 branchloop
=> GetBranchesLoop
(C4
::Context
->userenv->{'branch'}),
162 itemtypeloop
=>\
@itemtypeloop,
163 borcatloop
=>\
@borcatloop,
165 output_html_with_http_headers
$input, $cookie, $template->output;
172 my ($line, $column, $filters) = @_;
181 my $dbh = C4
::Context
->dbh;
187 for (my $i=0;$i<=6;$i++) {
189 if ( @
$filters[$i] ) {
190 if (($i==1) and (@
$filters[$i-1])) {
191 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
193 # format the dates filters, otherwise just fill as is
195 $cell{filter
} .= @
$filters[$i];
197 $cell{filter
} .= format_date
(@
$filters[$i]);
198 } $cell{crit
} .="Issue From" if ($i==0);
199 $cell{crit
} .="Issue To" if ($i==1);
200 $cell{crit
} .="Return From" if ($i==2);
201 $cell{crit
} .="Return To" if ($i==3);
202 $cell{crit
} .="Branch" if ($i==4);
203 $cell{crit
} .="Doc Type" if ($i==5);
204 $cell{crit
} .="Bor Cat" if ($i==6);
205 $cell{crit
} .="Day" if ($i==7);
206 $cell{crit
} .="Month" if ($i==8);
207 $cell{crit
} .="Year" if ($i==9);
208 push @loopfilter, \
%cell;
214 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
215 if($column=~/itemtype/){
216 $column = C4
::Context
->preference('item-level_itypes') ?
"items.itype": "biblioitems.itemtype";
218 $column = "borrowers.".$column if $column=~/categorycode/;
220 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
221 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
222 $colfilter[0] = @
$filters[2] if ($column =~ /returndate/ ) ;
223 $colfilter[1] = @
$filters[3] if ($column =~ /returndate/ ) ;
224 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
225 $colfilter[0] = @
$filters[5] if ($column =~ /itemtype/ ) ;
226 $colfilter[0] = @
$filters[6] if ($column =~ /category/ ) ;
227 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
228 $colfilter[0] = @
$filters[7] if ($column =~ /timestamp/ ) ;
229 $colfilter[0] = @
$filters[8] if ($column =~ /timestamp/ ) ;
230 $colfilter[0] = @
$filters[9] if ($column =~ /timestamp/ ) ;
231 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
234 if ($column eq "Day") {
236 $column = "old_issues.timestamp";
237 $colfield .="dayname($column)";
238 $colorder .="weekday($column)";
239 } elsif ($column eq "Month") {
241 $column = "old_issues.timestamp";
242 $colfield .="monthname($column)";
243 $colorder .="month($column)";
244 } elsif ($column eq "Year") {
246 $column = "old_issues.timestamp";
247 $colfield .="Year($column)";
248 $colorder .= $column;
250 $colfield .= $column;
251 $colorder .= $column;
255 $strsth2 .= "SELECT distinctrow $colfield
257 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
258 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
259 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
261 if (($column=~/timestamp/) or ($column=~/returndate/)){
262 if ($colfilter[1] and ($colfilter[0])){
263 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
264 } elsif ($colfilter[1]) {
265 $strsth2 .= " and $column < '$colfilter[1]' " ;
266 } elsif ($colfilter[0]) {
267 $strsth2 .= " and $column > '$colfilter[0]' " ;
269 } elsif ($colfilter[0]) {
270 $colfilter[0] =~ s/\*/%/g;
271 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
273 $strsth2 .=" group by $colfield";
274 $strsth2 .=" order by $colorder";
276 my $sth2 = $dbh->prepare( $strsth2 );
277 if (( @colfilter ) and ($colfilter[1])){
278 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
279 } elsif ($colfilter[0]) {
280 $sth2->execute($colfilter[0]);
286 while (my ($celvalue) = $sth2->fetchrow) {
288 $cell{coltitle
} = ($celvalue?
$celvalue:"NULL");
289 push @loopcol, \
%cell;
291 # warn "fin des titres colonnes";
298 #Initialization of cell values.....
302 for (my $i=1;$i<=$line;$i++) {
303 foreach my $col ( @loopcol ) {
304 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
305 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"total"}->{'name'}=0;
310 # preparing calculation
313 # Processing average loanperiods
314 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
315 $strcalc .= " , $colfield " if ($colfield);
316 $strcalc .= " FROM `old_issues`
317 LEFT JOIN items USING(itemnumber)
318 LEFT JOIN biblio USING(biblionumber)
319 LEFT JOIN biblioitems USING(biblionumber)
320 LEFT JOIN borrowers USING(borrowernumber)
323 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
324 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
325 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
326 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
327 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
328 $strcalc .= " AND old_issues.returndate > '" . @
$filters[2] ."'" if ( @
$filters[2] );
329 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
330 $strcalc .= " AND old_issues.returndate < '" . @
$filters[3] ."'" if ( @
$filters[3] );
331 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
332 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[4] ."'" if ( @
$filters[4] );
333 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
335 if(C4
::Context
->preference('item-level_itypes') ){
336 $strcalc .= " AND items.itype like "
338 $strcalc .= " AND biblioitems.itemtype like "
340 $strcalc .= "'" . @
$filters[5] ."'" ;
342 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
343 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[6] ."'" if ( @
$filters[6] );
344 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
345 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @
$filters[7]."'" if (@
$filters[7]);
346 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
347 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @
$filters[8]."'" if (@
$filters[8]);
348 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
349 $strcalc .= " AND year(old_issues.timestamp) like '" . @
$filters[9] ."'" if ( @
$filters[9] );
351 $strcalc .= " group by biblio.biblionumber";
352 $strcalc .= ", $colfield" if ($column);
353 $strcalc .= " order by RANK DESC";
354 $strcalc .= ", $colfield " if ($colfield);
356 my $dbcalc = $dbh->prepare($strcalc);
360 while (my @data = $dbcalc->fetchrow) {
361 my ($row, $rank, $id, $col )=@data;
362 $col = "zzEMPTY" if (!defined($col));
363 $indice{$col}=1 if (not($indice{$col}));
364 $table[$indice{$col}]->{$col}->{'name'}=$row;
365 $table[$indice{$col}]->{$col}->{'count'}=$rank;
366 $table[$indice{$col}]->{$col}->{'link'}=$id;
370 push @loopcol,{coltitle
=> "Global"} if not($column);
372 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 ) {
382 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'name'};
383 $count =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'count'};
384 $link =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'link'};
386 $value =$table[$i]->{"zzEMPTY"}->{'name'};
387 $count =$table[$i]->{"zzEMPTY"}->{'count'};
388 $link =$table[$i]->{"zzEMPTY"}->{'link'};
390 push @loopcell, {value
=> $value, count
=>$count, reference
=> $link} ;
392 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
393 push @looprow,{ 'rowtitle' => $i ,
394 'loopcell' => \
@loopcell,
395 'hilighted' => ($hilighted >0),
397 $hilighted = -$hilighted;
402 # the header of the table
403 $globalline{loopfilter
}=\
@loopfilter;
404 # the core of the table
405 $globalline{looprow
} = \
@looprow;
406 $globalline{loopcol
} = \
@loopcol;
407 # # the foot (totals by borrower type)
408 $globalline{loopfooter
} = \
@loopfooter;
409 $globalline{total
}= $grantotal;
410 $globalline{line
} = $line;
411 $globalline{column
} = $column;
412 push @mainloop,\
%globalline;