Bug 20298: Fix SMS characters counter when TranslateNotices is set
[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 Modern::Perl;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26 use C4::Koha;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
30 use Koha::DateUtils;
31 use Koha::ItemTypes;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =cut
40
41 my $input = new CGI;
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' } ); };
49 }
50
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,
56                 query => $input,
57                 type => "intranet",
58                 authnotrequired => 0,
59                 flagsrequired => { reports => '*'},
60                 debug => 1,
61                 });
62 our $sep     = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
64 $template->param(do_it => $do_it,
65         );
66 if ($do_it) {
67 # Displaying results
68     my $results = calculate($limit, $column, \@filters);
69     if ($output eq "screen"){
70 # Printing results to screen
71         $template->param(mainloop => $results,
72                         limit => $limit);
73         output_html_with_http_headers $input, $cookie, $template->output;
74         exit;
75     } else {
76 # Printing to a csv file
77         print $input->header(-type => 'application/vnd.sun.xml.calc',
78                             -encoding    => 'utf-8',
79                             -attachment=>"$basename.csv",
80                             -filename=>"$basename.csv" );
81         my $cols = @$results[0]->{loopcol};
82         my $lines = @$results[0]->{looprow};
83 # header top-right
84         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
85 # Other header
86         foreach my $col ( @$cols ) {
87             print $col->{coltitle}.$sep;
88         }
89         print "Total\n";
90 # Table
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;
96             }
97             print $line->{totalrow};
98             print "\n";
99         }
100 # footer
101         print "TOTAL";
102         $cols = @$results[0]->{loopfooter};
103         foreach my $col ( @$cols ) {
104             print $sep.$col->{totalcol};
105         }
106         print $sep.@$results[0]->{total};
107         exit;
108     }
109 # Displaying choices
110 } else {
111     my $dbh = C4::Context->dbh;
112     my @values;
113     my %labels;
114     my %select;
115     my $req;
116     
117     my $CGIextChoice = ( 'CSV' ); # FIXME translation
118     my $CGIsepChoice=GetDelimiterChoices;
119
120     #doctype
121     my $itemtypes = Koha::ItemTypes->search_with_localization;
122
123     #ccode
124     my $ccodes = GetAuthorisedValues('CCODE');
125     my @ccodeloop;
126     for my $thisccode (@$ccodes) {
127             my %row = (value => $thisccode->{authorised_value},
128                        description => $thisccode->{lib},
129                             );
130             push @ccodeloop, \%row;
131     }
132
133     @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
134
135     #shelvingloc
136     my $shelvinglocs = GetAuthorisedValues('LOC');
137     my @shelvinglocloop;
138     for my $thisloc (@$shelvinglocs) {
139             my %row = (value => $thisloc->{authorised_value},
140                        description => $thisloc->{lib},
141                             );
142             push @shelvinglocloop, \%row;
143     }
144
145     @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
146
147     my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
148
149     $template->param(
150                     CGIextChoice => $CGIextChoice,
151                     CGIsepChoice => $CGIsepChoice,
152                     itemtypes => $itemtypes,
153                     ccodeloop =>\@ccodeloop,
154                     shelvinglocloop =>\@shelvinglocloop,
155                     patron_categories => $patron_categories,
156                     );
157 output_html_with_http_headers $input, $cookie, $template->output;
158 }
159
160
161
162
163 sub calculate {
164     my ($line, $column, $filters) = @_;
165     my @mainloop;
166     my @loopfooter;
167     my @loopcol;
168     my @loopline;
169     my @looprow;
170     my %globalline;
171     my $grantotal =0;
172 # extract parameters
173     my $dbh = C4::Context->dbh;
174
175 # Filters
176 # Checking filters
177 #
178     my @loopfilter;
179     for (my $i=0;$i<=12;$i++) {
180         my %cell;
181         if ( @$filters[$i] ) {
182             if (($i==1) and (@$filters[$i-1])) {
183                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
184             }
185             # format the dates filters, otherwise just fill as is
186             if ($i>=2) {
187                 $cell{filter} .= @$filters[$i];
188             } else {
189                 $cell{filter} .= eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
190                    if ( @$filters[$i] );
191             }
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;
206         }
207     }
208     my $colfield;
209     my $colorder;
210     if ($column){
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";
214         }
215         $column = "borrowers.".$column if $column=~/categorycode/;
216         my @colfilter ;
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];
233                                                 
234     # loop cols.
235         if ($column eq "Day") {
236             #Display by day
237             $column = "old_issues.timestamp";
238             $colfield .="dayname($column)";  
239             $colorder .="weekday($column)";
240         } elsif ($column eq "Month") {
241             #Display by Month
242             $column = "old_issues.timestamp";
243             $colfield .="monthname($column)";  
244             $colorder .="month($column)";  
245         } elsif ($column eq "Year") {
246             #Display by Year
247             $column = "old_issues.timestamp";
248             $colfield .="Year($column)";
249             $colorder .= $column;
250         } else {
251             $colfield .= $column;
252             $colorder .= $column;
253         }  
254         
255         my $strsth2;
256         $strsth2 .= "SELECT distinctrow $colfield 
257                      FROM `old_issues` 
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 
261                      WHERE 1";
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]' " ;
269             }
270         } elsif ($colfilter[0]) {
271             $colfilter[0] =~ s/\*/%/g;
272             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
273         }
274         $strsth2 .=" group by $colfield";
275         $strsth2 .=" order by $colorder";
276         
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]);
282         } else {
283             $sth2->execute;
284         }
285         
286     
287         while (my ($celvalue) = $sth2->fetchrow) {
288             my %cell;
289             $cell{coltitle} = ($celvalue?$celvalue:"NULL");
290             push @loopcol, \%cell;
291         }
292     #   warn "fin des titres colonnes";
293     }
294     
295     my $i=0;
296 #       my @totalcol;
297     my $hilighted=-1;
298     
299     #Initialization of cell values.....
300     my @table;
301     
302 #       warn "init table";
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;
307         }
308     }
309
310
311 # preparing calculation
312     my $strcalc ;
313     
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)
325                   WHERE 1";
326
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]);
338     if ( @$filters[5] ){
339         if(C4::Context->preference('item-level_itypes') ){
340             $strcalc .= " AND items.itype like "
341         }else{
342             $strcalc .= " AND biblioitems.itemtype like "
343         } 
344         $strcalc .= "'" . @$filters[5] ."'" ;
345     }
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] );
360     
361     $strcalc .= " group by biblio.biblionumber";
362     $strcalc .= ", $colfield" if ($column);
363     $strcalc .= " order by RANK DESC";
364     $strcalc .= ", $colfield " if ($colfield);
365     
366     my $dbcalc = $dbh->prepare($strcalc);
367     $dbcalc->execute;
368     my $previous_col;
369     my %indice;
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;
377         $indice{$col}++;
378     }
379     
380     push @loopcol,{coltitle => "Global"} if not($column);
381     
382     for ($i=1; $i<=$line;$i++) {
383         my @loopcell;
384         #@loopcol ensures the order for columns is common with column titles
385         # and the number matches the number of columns
386         my $colcount=0;
387         foreach my $col ( @loopcol ) {
388             my $value;
389             my $count=0;
390             my $link;
391             if (@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'};
395             } else {
396                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
397                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
398                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
399             }
400             push @loopcell, {value => $value, count =>$count, reference => $link} ;
401         }
402         #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
403         push @looprow,{ 'rowtitle' => $i ,
404                         'loopcell' => \@loopcell,
405                         'hilighted' => ($hilighted >0),
406                     };
407         $hilighted = -$hilighted;
408     }
409 #       
410             
411
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;
423     return \@mainloop;
424 }
425
426 1;