Bug 20793: Don't show a holds link in staff for users without permission
[koha.git] / reports / cat_issues_top.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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.
12 #
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.
17 #
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>.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Members;
31 use Koha::DateUtils;
32 use Koha::ItemTypes;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 my $input = new CGI;
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' } ); };
50 }
51
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,
57                 query => $input,
58                 type => "intranet",
59                 authnotrequired => 0,
60                 flagsrequired => { reports => '*'},
61                 debug => 1,
62                 });
63 our $sep     = $input->param("sep");
64 $sep = "\t" if ($sep eq 'tabulation');
65 $template->param(do_it => $do_it,
66         );
67 if ($do_it) {
68 # Displaying results
69     my $results = calculate($limit, $column, \@filters);
70     if ($output eq "screen"){
71 # Printing results to screen
72         $template->param(mainloop => $results,
73                         limit => $limit);
74         output_html_with_http_headers $input, $cookie, $template->output;
75         exit;
76     } else {
77 # Printing to a csv file
78         print $input->header(-type => 'application/vnd.sun.xml.calc',
79                             -encoding    => 'utf-8',
80                             -attachment=>"$basename.csv",
81                             -filename=>"$basename.csv" );
82         my $cols = @$results[0]->{loopcol};
83         my $lines = @$results[0]->{looprow};
84 # header top-right
85         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
86 # Other header
87         foreach my $col ( @$cols ) {
88             print $col->{coltitle}.$sep;
89         }
90         print "Total\n";
91 # Table
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} // '';
98             }
99             print "\n";
100         }
101         exit;
102     }
103 # Displaying choices
104 } else {
105     my $dbh = C4::Context->dbh;
106     
107     my $CGIextChoice = ( 'CSV' ); # FIXME translation
108     my $CGIsepChoice=GetDelimiterChoices;
109
110     #doctype
111     my $itemtypes = Koha::ItemTypes->search_with_localization;
112
113     #ccode
114     my $ccodes = GetAuthorisedValues('CCODE');
115     my @ccodeloop;
116     for my $thisccode (@$ccodes) {
117             my %row = (value => $thisccode->{authorised_value},
118                        description => $thisccode->{lib},
119                             );
120             push @ccodeloop, \%row;
121     }
122
123     @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
124
125     #shelvingloc
126     my $shelvinglocs = GetAuthorisedValues('LOC');
127     my @shelvinglocloop;
128     for my $thisloc (@$shelvinglocs) {
129             my %row = (value => $thisloc->{authorised_value},
130                        description => $thisloc->{lib},
131                             );
132             push @shelvinglocloop, \%row;
133     }
134
135     @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
136
137     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
138
139     $template->param(
140                     CGIextChoice => $CGIextChoice,
141                     CGIsepChoice => $CGIsepChoice,
142                     itemtypes => $itemtypes,
143                     ccodeloop =>\@ccodeloop,
144                     shelvinglocloop =>\@shelvinglocloop,
145                     patron_categories => $patron_categories,
146                     );
147 output_html_with_http_headers $input, $cookie, $template->output;
148 }
149
150
151
152
153 sub calculate {
154     my ($line, $column, $filters) = @_;
155     my @mainloop;
156     my @loopcol;
157     my @looprow;
158     my %globalline;
159     my $grantotal =0;
160 # extract parameters
161     my $dbh = C4::Context->dbh;
162
163 # Filters
164 # Checking filters
165 #
166     my @loopfilter;
167     for (my $i=0;$i<=12;$i++) {
168         my %cell;
169         if ( @$filters[$i] ) {
170             if (($i==1) and (@$filters[$i-1])) {
171                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
172             }
173             # format the dates filters, otherwise just fill as is
174             if ($i>=2) {
175                 $cell{filter} .= @$filters[$i];
176             } else {
177                 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
178                    if ( @$filters[$i] );
179             }
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;
194         }
195     }
196     my $colfield;
197     my $colorder;
198     if ($column){
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";
202         }
203         $column = "borrowers.".$column if $column=~/categorycode/;
204         my @colfilter ;
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];
221                                                 
222     # loop cols.
223         if ($column eq "Day") {
224             #Display by day
225             $column = "old_issues.timestamp";
226             $colfield .="dayname($column)";  
227             $colorder .="weekday($column)";
228         } elsif ($column eq "Month") {
229             #Display by Month
230             $column = "old_issues.timestamp";
231             $colfield .="monthname($column)";  
232             $colorder .="month($column)";  
233         } elsif ($column eq "Year") {
234             #Display by Year
235             $column = "old_issues.timestamp";
236             $colfield .="Year($column)";
237             $colorder .= $column;
238         } else {
239             $colfield .= $column;
240             $colorder .= $column;
241         }  
242         
243         my $strsth2;
244         $strsth2 .= "SELECT distinctrow $colfield 
245                      FROM `old_issues` 
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 
249                      WHERE 1";
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]' " ;
257             }
258         } elsif ($colfilter[0]) {
259             $colfilter[0] =~ s/\*/%/g;
260             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
261         }
262         $strsth2 .=" group by $colfield";
263         $strsth2 .=" order by $colorder";
264         
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]);
270         } else {
271             $sth2->execute;
272         }
273         
274     
275         while (my ($celvalue) = $sth2->fetchrow) {
276             my %cell;
277             $cell{coltitle} = ($celvalue?$celvalue:"NULL");
278             push @loopcol, \%cell;
279         }
280     #   warn "fin des titres colonnes";
281     }
282     
283     my $i=0;
284 #       my @totalcol;
285     my $hilighted=-1;
286     
287     #Initialization of cell values.....
288     my @table;
289     
290 #       warn "init table";
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;
295         }
296     }
297
298
299 # preparing calculation
300     my $strcalc ;
301     
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)
313                   WHERE 1";
314
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]);
326     if ( @$filters[5] ){
327         if(C4::Context->preference('item-level_itypes') ){
328             $strcalc .= " AND items.itype like "
329         }else{
330             $strcalc .= " AND biblioitems.itemtype like "
331         } 
332         $strcalc .= "'" . @$filters[5] ."'" ;
333     }
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] );
348     
349     $strcalc .= " group by biblio.biblionumber";
350     $strcalc .= ", $colfield" if ($column);
351     $strcalc .= " order by RANK DESC";
352     $strcalc .= ", $colfield " if ($colfield);
353     
354     my $dbcalc = $dbh->prepare($strcalc);
355     $dbcalc->execute;
356     my %indice;
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;
364         $indice{$col}++;
365     }
366     
367     push @loopcol,{coltitle => "Global"} if not($column);
368     
369     for ($i=1; $i<=$line;$i++) {
370         my @loopcell;
371         #@loopcol ensures the order for columns is common with column titles
372         # and the number matches the number of columns
373         my $colcount=0;
374         foreach my $col ( @loopcol ) {
375             my $value;
376             my $count=0;
377             my $link;
378             if (@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'};
382             } else {
383                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
384                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
385                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
386             }
387             push @loopcell, {value => $value, count =>$count, reference => $link} ;
388         }
389         #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
390         push @looprow,{ 'rowtitle' => $i ,
391                         'loopcell' => \@loopcell,
392                         'hilighted' => ($hilighted >0),
393                     };
394         $hilighted = -$hilighted;
395     }
396 #       
397             
398
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;
409     return \@mainloop;
410 }
411
412 1;