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
36 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/cat_issues_top.tt";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->multi_param("Filter");
48 foreach ( @filters[0..3] ) {
49 $_ and $_ = eval { output_pref( { dt => dt_from_string ( $_ ), dateonly => 1, dateformat => 'iso' } ); };
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
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,
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results,
74 output_html_with_http_headers $input, $cookie, $template->output;
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 foreach my $col ( @$cols ) {
88 print $col->{coltitle}.$sep;
92 foreach my $line ( @$lines ) {
93 my $x = $line->{loopcell};
94 print $line->{rowtitle}.$sep;
95 foreach my $cell (@$x) {
96 print $cell->{value}.$sep;
97 print $cell->{count} // '';
105 my $dbh = C4::Context->dbh;
107 my $CGIextChoice = ( 'CSV' ); # FIXME translation
108 my $CGIsepChoice=GetDelimiterChoices;
111 my $itemtypes = Koha::ItemTypes->search_with_localization;
114 my $ccodes = GetAuthorisedValues('CCODE');
116 for my $thisccode (@$ccodes) {
117 my %row = (value => $thisccode->{authorised_value},
118 description => $thisccode->{lib},
120 push @ccodeloop, \%row;
123 @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
126 my $shelvinglocs = GetAuthorisedValues('LOC');
128 for my $thisloc (@$shelvinglocs) {
129 my %row = (value => $thisloc->{authorised_value},
130 description => $thisloc->{lib},
132 push @shelvinglocloop, \%row;
135 @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
137 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
140 CGIextChoice => $CGIextChoice,
141 CGIsepChoice => $CGIsepChoice,
142 itemtypes => $itemtypes,
143 ccodeloop =>\@ccodeloop,
144 shelvinglocloop =>\@shelvinglocloop,
145 patron_categories => $patron_categories,
147 output_html_with_http_headers $input, $cookie, $template->output;
154 my ($line, $column, $filters) = @_;
161 my $dbh = C4::Context->dbh;
167 for (my $i=0;$i<=12;$i++) {
169 if ( @$filters[$i] ) {
170 if (($i==1) and (@$filters[$i-1])) {
171 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
173 # format the dates filters, otherwise just fill as is
175 $cell{filter} .= @$filters[$i];
177 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
178 if ( @$filters[$i] );
180 $cell{crit} .="Issue From" if ($i==0);
181 $cell{crit} .="Issue To" if ($i==1);
182 $cell{crit} .="Return From" if ($i==2);
183 $cell{crit} .="Return To" if ($i==3);
184 $cell{crit} .="Branch" if ($i==4);
185 $cell{crit} .="Doc Type" if ($i==5);
186 $cell{crit} .="Call number" if ($i==6);
187 $cell{crit} .="Collection code" if ($i==7);
188 $cell{crit} .="Shelving location" if ($i==8);
189 $cell{crit} .="Bor Cat" if ($i==9);
190 $cell{crit} .="Day" if ($i==10);
191 $cell{crit} .="Month" if ($i==11);
192 $cell{crit} .="Year" if ($i==12);
193 push @loopfilter, \%cell;
199 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
200 if($column=~/itemtype/){
201 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
203 $column = "borrowers.".$column if $column=~/categorycode/;
205 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
206 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
207 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
208 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
209 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
210 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
211 # These limits does not currently exist, maybe later?
212 # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ ) ;
213 # $colfilter[0] = @$filters[7] if ($column =~ /location/ ) ;
214 $colfilter[0] = @$filters[8] if ($column =~ /category/ ) ;
215 # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
216 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
217 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
218 $colfilter[0] = @$filters[10] if ($column =~ /timestamp/ ) ;
219 $colfilter[0] = @$filters[11] 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 .= ", itemcallnumber as CALLNUM";
305 $strcalc .= ", ccode as CCODE";
306 $strcalc .= ", location as LOC";
307 $strcalc .= " , $colfield " if ($colfield);
308 $strcalc .= " FROM `old_issues`
309 LEFT JOIN items USING(itemnumber)
310 LEFT JOIN biblio USING(biblionumber)
311 LEFT JOIN biblioitems USING(biblionumber)
312 LEFT JOIN borrowers USING(borrowernumber)
315 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
316 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
317 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
318 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
319 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
320 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
321 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
322 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
323 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
324 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
325 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
327 if(C4::Context->preference('item-level_itypes') ){
328 $strcalc .= " AND items.itype like "
330 $strcalc .= " AND biblioitems.itemtype like "
332 $strcalc .= "'" . @$filters[5] ."'" ;
334 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
335 $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
336 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
337 $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
338 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
339 $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
340 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
341 $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
342 @$filters[10]=~ s/\*/%/g if (@$filters[10]);
343 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[10]."'" if (@$filters[10]);
344 @$filters[11]=~ s/\*/%/g if (@$filters[11]);
345 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[11]."'" if (@$filters[11]);
346 @$filters[12]=~ s/\*/%/g if (@$filters[12]);
347 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[12] ."'" if ( @$filters[12] );
349 $strcalc .= " group by biblio.biblionumber";
350 $strcalc .= ", $colfield" if ($column);
351 $strcalc .= " order by RANK DESC";
352 $strcalc .= ", $colfield " if ($colfield);
354 my $dbcalc = $dbh->prepare($strcalc);
357 while (my @data = $dbcalc->fetchrow) {
358 my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
359 $col = "zzEMPTY" if (!defined($col));
360 $indice{$col}=1 if (not($indice{$col}));
361 $table[$indice{$col}]->{$col}->{'name'}=$row;
362 $table[$indice{$col}]->{$col}->{'count'}=$rank;
363 $table[$indice{$col}]->{$col}->{'link'}=$id;
367 push @loopcol,{coltitle => "Global"} if not($column);
369 for ($i=1; $i<=$line;$i++) {
371 #@loopcol ensures the order for columns is common with column titles
372 # and the number matches the number of columns
374 foreach my $col ( @loopcol ) {
379 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
380 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
381 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
383 $value =$table[$i]->{"zzEMPTY"}->{'name'};
384 $count =$table[$i]->{"zzEMPTY"}->{'count'};
385 $link =$table[$i]->{"zzEMPTY"}->{'link'};
387 push @loopcell, {value => $value, count =>$count, reference => $link} ;
389 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
390 push @looprow,{ 'rowtitle' => $i ,
391 'loopcell' => \@loopcell,
392 'hilighted' => ($hilighted >0),
394 $hilighted = -$hilighted;
399 # the header of the table
400 $globalline{loopfilter}=\@loopfilter;
401 # the core of the table
402 $globalline{looprow} = \@looprow;
403 $globalline{loopcol} = \@loopcol;
404 # # the foot (totals by borrower type)
405 $globalline{total}= $grantotal;
406 $globalline{line} = $line;
407 $globalline{column} = $column;
408 push @mainloop,\%globalline;