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>.
35 plugin that shows a stats on borrowers
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/cat_issues_top.tt";
44 my $limit = $input->param("Limit");
45 my $column = $input->param("Criteria");
46 my @filters = $input->multi_param("Filter");
47 foreach ( @filters[0..3] ) {
48 $_ and $_ = eval { output_pref
( { dt
=> dt_from_string
( $_ ), dateonly
=> 1, dateformat
=> 'iso' } ); };
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 #warn "calcul : ".$calc;
54 my ($template, $borrowernumber, $cookie)
55 = get_template_and_user
({template_name
=> $fullreportname,
59 flagsrequired
=> { reports
=> '*'},
62 our $sep = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
64 $template->param(do_it
=> $do_it,
68 my $results = calculate
($limit, $column, \
@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop
=> $results,
73 output_html_with_http_headers
$input, $cookie, $template->output;
76 # Printing to a csv file
77 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
79 -attachment
=>"$basename.csv",
80 -filename
=>"$basename.csv" );
81 my $cols = @
$results[0]->{loopcol
};
82 my $lines = @
$results[0]->{looprow
};
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 $CGIextChoice = ( 'CSV' ); # FIXME translation
118 my $CGIsepChoice=GetDelimiterChoices
;
121 my $itemtypes = Koha
::ItemTypes
->search_with_localization;
124 my $ccodes = GetAuthorisedValues
('CCODE');
126 for my $thisccode (@
$ccodes) {
127 my %row = (value
=> $thisccode->{authorised_value
},
128 description
=> $thisccode->{lib
},
130 push @ccodeloop, \
%row;
133 @ccodeloop = sort {$a->{value
} cmp $b->{value
}} @ccodeloop;
136 my $shelvinglocs = GetAuthorisedValues
('LOC');
138 for my $thisloc (@
$shelvinglocs) {
139 my %row = (value
=> $thisloc->{authorised_value
},
140 description
=> $thisloc->{lib
},
142 push @shelvinglocloop, \
%row;
145 @shelvinglocloop = sort {$a->{value
} cmp $b->{value
}} @shelvinglocloop;
147 my $patron_categories = Koha
::Patron
::Categories
->search_limited({}, {order_by
=> ['categorycode']});
150 CGIextChoice
=> $CGIextChoice,
151 CGIsepChoice
=> $CGIsepChoice,
152 itemtypes
=> $itemtypes,
153 ccodeloop
=>\
@ccodeloop,
154 shelvinglocloop
=>\
@shelvinglocloop,
155 patron_categories
=> $patron_categories,
157 output_html_with_http_headers
$input, $cookie, $template->output;
164 my ($line, $column, $filters) = @_;
173 my $dbh = C4
::Context
->dbh;
179 for (my $i=0;$i<=12;$i++) {
181 if ( @
$filters[$i] ) {
182 if (($i==1) and (@
$filters[$i-1])) {
183 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
185 # format the dates filters, otherwise just fill as is
187 $cell{filter
} .= @
$filters[$i];
189 $cell{filter
} .= eval { output_pref
( { dt
=> dt_from_string
( @
$filters[$i] ), dateonly
=> 1 }); }
190 if ( @
$filters[$i] );
192 $cell{crit
} .="Issue From" if ($i==0);
193 $cell{crit
} .="Issue To" if ($i==1);
194 $cell{crit
} .="Return From" if ($i==2);
195 $cell{crit
} .="Return To" if ($i==3);
196 $cell{crit
} .="Branch" if ($i==4);
197 $cell{crit
} .="Doc Type" if ($i==5);
198 $cell{crit
} .="Call number" if ($i==6);
199 $cell{crit
} .="Collection code" if ($i==7);
200 $cell{crit
} .="Shelving location" if ($i==8);
201 $cell{crit
} .="Bor Cat" if ($i==9);
202 $cell{crit
} .="Day" if ($i==10);
203 $cell{crit
} .="Month" if ($i==11);
204 $cell{crit
} .="Year" if ($i==12);
205 push @loopfilter, \
%cell;
211 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
212 if($column=~/itemtype/){
213 $column = C4
::Context
->preference('item-level_itypes') ?
"items.itype": "biblioitems.itemtype";
215 $column = "borrowers.".$column if $column=~/categorycode/;
217 $colfilter[0] = @
$filters[0] if ($column =~ /timestamp/ ) ;
218 $colfilter[1] = @
$filters[1] if ($column =~ /timestamp/ ) ;
219 $colfilter[0] = @
$filters[2] if ($column =~ /returndate/ ) ;
220 $colfilter[1] = @
$filters[3] if ($column =~ /returndate/ ) ;
221 $colfilter[0] = @
$filters[4] if ($column =~ /branch/ ) ;
222 $colfilter[0] = @
$filters[5] if ($column =~ /itemtype/ ) ;
223 # These limits does not currently exist, maybe later?
224 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
225 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
226 $colfilter[0] = @
$filters[8] if ($column =~ /category/ ) ;
227 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
228 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
229 $colfilter[0] = @
$filters[9] if ($column =~ /timestamp/ ) ;
230 $colfilter[0] = @
$filters[10] if ($column =~ /timestamp/ ) ;
231 $colfilter[0] = @
$filters[11] if ($column =~ /timestamp/ ) ;
232 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
235 if ($column eq "Day") {
237 $column = "old_issues.timestamp";
238 $colfield .="dayname($column)";
239 $colorder .="weekday($column)";
240 } elsif ($column eq "Month") {
242 $column = "old_issues.timestamp";
243 $colfield .="monthname($column)";
244 $colorder .="month($column)";
245 } elsif ($column eq "Year") {
247 $column = "old_issues.timestamp";
248 $colfield .="Year($column)";
249 $colorder .= $column;
251 $colfield .= $column;
252 $colorder .= $column;
256 $strsth2 .= "SELECT distinctrow $colfield
258 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
259 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
260 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
262 if (($column=~/timestamp/) or ($column=~/returndate/)){
263 if ($colfilter[1] and ($colfilter[0])){
264 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
265 } elsif ($colfilter[1]) {
266 $strsth2 .= " and $column < '$colfilter[1]' " ;
267 } elsif ($colfilter[0]) {
268 $strsth2 .= " and $column > '$colfilter[0]' " ;
270 } elsif ($colfilter[0]) {
271 $colfilter[0] =~ s/\*/%/g;
272 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
274 $strsth2 .=" group by $colfield";
275 $strsth2 .=" order by $colorder";
277 my $sth2 = $dbh->prepare( $strsth2 );
278 if (( @colfilter ) and ($colfilter[1])){
279 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
280 } elsif ($colfilter[0]) {
281 $sth2->execute($colfilter[0]);
287 while (my ($celvalue) = $sth2->fetchrow) {
289 $cell{coltitle
} = ($celvalue?
$celvalue:"NULL");
290 push @loopcol, \
%cell;
292 # warn "fin des titres colonnes";
299 #Initialization of cell values.....
303 for (my $i=1;$i<=$line;$i++) {
304 foreach my $col ( @loopcol ) {
305 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
306 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"total"}->{'name'}=0;
311 # preparing calculation
314 # Processing average loanperiods
315 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
316 $strcalc .= ", itemcallnumber as CALLNUM";
317 $strcalc .= ", ccode as CCODE";
318 $strcalc .= ", location as LOC";
319 $strcalc .= " , $colfield " if ($colfield);
320 $strcalc .= " FROM `old_issues`
321 LEFT JOIN items USING(itemnumber)
322 LEFT JOIN biblio USING(biblionumber)
323 LEFT JOIN biblioitems USING(biblionumber)
324 LEFT JOIN borrowers USING(borrowernumber)
327 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
328 $strcalc .= " AND old_issues.timestamp > '" . @
$filters[0] ."'" if ( @
$filters[0] );
329 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
330 $strcalc .= " AND old_issues.timestamp < '" . @
$filters[1] ."'" if ( @
$filters[1] );
331 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
332 $strcalc .= " AND old_issues.returndate > '" . @
$filters[2] ."'" if ( @
$filters[2] );
333 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
334 $strcalc .= " AND old_issues.returndate < '" . @
$filters[3] ."'" if ( @
$filters[3] );
335 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
336 $strcalc .= " AND old_issues.branchcode like '" . @
$filters[4] ."'" if ( @
$filters[4] );
337 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
339 if(C4
::Context
->preference('item-level_itypes') ){
340 $strcalc .= " AND items.itype like "
342 $strcalc .= " AND biblioitems.itemtype like "
344 $strcalc .= "'" . @
$filters[5] ."'" ;
346 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
347 $strcalc .= " AND itemcallnumber like '" . @
$filters[6] ."'" if ( @
$filters[6] );
348 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
349 $strcalc .= " AND ccode like '" . @
$filters[7] ."'" if ( @
$filters[7] );
350 @
$filters[8]=~ s/\*/%/g if (@
$filters[8]);
351 $strcalc .= " AND location like '" . @
$filters[8] ."'" if ( @
$filters[8] );
352 @
$filters[9]=~ s/\*/%/g if (@
$filters[9]);
353 $strcalc .= " AND borrowers.categorycode like '" . @
$filters[9] ."'" if ( @
$filters[9] );
354 @
$filters[10]=~ s/\*/%/g if (@
$filters[10]);
355 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @
$filters[10]."'" if (@
$filters[10]);
356 @
$filters[11]=~ s/\*/%/g if (@
$filters[11]);
357 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @
$filters[11]."'" if (@
$filters[11]);
358 @
$filters[12]=~ s/\*/%/g if (@
$filters[12]);
359 $strcalc .= " AND year(old_issues.timestamp) like '" . @
$filters[12] ."'" if ( @
$filters[12] );
361 $strcalc .= " group by biblio.biblionumber";
362 $strcalc .= ", $colfield" if ($column);
363 $strcalc .= " order by RANK DESC";
364 $strcalc .= ", $colfield " if ($colfield);
366 my $dbcalc = $dbh->prepare($strcalc);
370 while (my @data = $dbcalc->fetchrow) {
371 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
372 $col = "zzEMPTY" if (!defined($col));
373 $indice{$col}=1 if (not($indice{$col}));
374 $table[$indice{$col}]->{$col}->{'name'}=$row;
375 $table[$indice{$col}]->{$col}->{'count'}=$rank;
376 $table[$indice{$col}]->{$col}->{'link'}=$id;
380 push @loopcol,{coltitle
=> "Global"} if not($column);
382 for ($i=1; $i<=$line;$i++) {
384 #@loopcol ensures the order for columns is common with column titles
385 # and the number matches the number of columns
387 foreach my $col ( @loopcol ) {
392 $value =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'name'};
393 $count =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'count'};
394 $link =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'link'};
396 $value =$table[$i]->{"zzEMPTY"}->{'name'};
397 $count =$table[$i]->{"zzEMPTY"}->{'count'};
398 $link =$table[$i]->{"zzEMPTY"}->{'link'};
400 push @loopcell, {value
=> $value, count
=>$count, reference
=> $link} ;
402 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
403 push @looprow,{ 'rowtitle' => $i ,
404 'loopcell' => \
@loopcell,
405 'hilighted' => ($hilighted >0),
407 $hilighted = -$hilighted;
412 # the header of the table
413 $globalline{loopfilter
}=\
@loopfilter;
414 # the core of the table
415 $globalline{looprow
} = \
@looprow;
416 $globalline{loopcol
} = \
@loopcol;
417 # # the foot (totals by borrower type)
418 $globalline{loopfooter
} = \
@loopfooter;
419 $globalline{total
}= $grantotal;
420 $globalline{line
} = $line;
421 $globalline{column
} = $column;
422 push @mainloop,\
%globalline;