Bug 29955: Move populate_order_with_prices to Koha namespace
[koha-ffzg.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 qw( get_template_and_user );
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Koha qw( GetAuthorisedValues );
27 use C4::Reports qw( GetDelimiterChoices );
28 use Koha::ItemTypes;
29
30 =head1 NAME
31
32 plugin that shows a stats on borrowers
33
34 =head1 DESCRIPTION
35
36 =cut
37
38 my $input = CGI->new;
39 my $do_it=$input->param('do_it');
40 my $fullreportname = "reports/cat_issues_top.tt";
41 my $limit = $input->param("Limit");
42 my $column = $input->param("Criteria");
43 my @filters = $input->multi_param("Filter");
44
45 my $output = $input->param("output");
46 my $basename = $input->param("basename");
47 #warn "calcul : ".$calc;
48 my ($template, $borrowernumber, $cookie)
49     = get_template_and_user({template_name => $fullreportname,
50                 query => $input,
51                 type => "intranet",
52                 flagsrequired => { reports => '*'},
53                 });
54 our $sep     = $input->param("sep");
55 $sep = "\t" if ($sep eq 'tabulation');
56 $template->param(do_it => $do_it,
57         );
58 if ($do_it) {
59 # Displaying results
60     my $results = calculate($limit, $column, \@filters);
61     if ($output eq "screen"){
62 # Printing results to screen
63         $template->param(mainloop => $results,
64                         limit => $limit);
65         output_html_with_http_headers $input, $cookie, $template->output;
66         exit;
67     } else {
68 # Printing to a csv file
69         print $input->header(-type => 'application/vnd.sun.xml.calc',
70                             -encoding    => 'utf-8',
71                             -attachment=>"$basename.csv",
72                             -filename=>"$basename.csv" );
73         my $cols = @$results[0]->{loopcol};
74         my $lines = @$results[0]->{looprow};
75 # header top-right
76         print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
77 # Other header
78         foreach my $col ( @$cols ) {
79             print $col->{coltitle}.$sep;
80         }
81         print "Total\n";
82 # Table
83         foreach my $line ( @$lines ) {
84             my $x = $line->{loopcell};
85             print $line->{rowtitle}.$sep;
86             foreach my $cell (@$x) {
87                 print $cell->{value}.$sep;
88                 print $cell->{count} // '';
89             }
90             print "\n";
91         }
92         exit;
93     }
94 # Displaying choices
95 } else {
96     my $dbh = C4::Context->dbh;
97     
98     my $CGIextChoice = ( 'CSV' ); # FIXME translation
99     my $CGIsepChoice=GetDelimiterChoices;
100
101     #doctype
102     my $itemtypes = Koha::ItemTypes->search_with_localization;
103
104     #ccode
105     my $ccodes = GetAuthorisedValues('CCODE');
106     my @ccodeloop;
107     for my $thisccode (@$ccodes) {
108             my %row = (value => $thisccode->{authorised_value},
109                        description => $thisccode->{lib},
110                             );
111             push @ccodeloop, \%row;
112     }
113
114     @ccodeloop = sort {$a->{value} cmp $b->{value}} @ccodeloop;
115
116     #shelvingloc
117     my $shelvinglocs = GetAuthorisedValues('LOC');
118     my @shelvinglocloop;
119     for my $thisloc (@$shelvinglocs) {
120             my %row = (value => $thisloc->{authorised_value},
121                        description => $thisloc->{lib},
122                             );
123             push @shelvinglocloop, \%row;
124     }
125
126     @shelvinglocloop = sort {$a->{value} cmp $b->{value}} @shelvinglocloop;
127
128     my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
129
130     $template->param(
131                     CGIextChoice => $CGIextChoice,
132                     CGIsepChoice => $CGIsepChoice,
133                     itemtypes => $itemtypes,
134                     ccodeloop =>\@ccodeloop,
135                     shelvinglocloop =>\@shelvinglocloop,
136                     patron_categories => $patron_categories,
137                     );
138 output_html_with_http_headers $input, $cookie, $template->output;
139 }
140
141
142
143
144 sub calculate {
145     my ($line, $column, $filters) = @_;
146     my @mainloop;
147     my @loopcol;
148     my @looprow;
149     my %globalline;
150     my $grantotal =0;
151 # extract parameters
152     my $dbh = C4::Context->dbh;
153
154 # Filters
155 # Checking filters
156 #
157     my @loopfilter;
158     for (my $i=0;$i<=12;$i++) {
159         my %cell;
160         if ( @$filters[$i] ) {
161             if (($i==1) and (@$filters[$i-1])) {
162                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
163             }
164             $cell{filter} .= @$filters[$i];
165             $cell{crit} .="Issue From" if ($i==0);
166             $cell{crit} .="Issue To" if ($i==1);
167             $cell{crit} .="Return From" if ($i==2);
168             $cell{crit} .="Return To" if ($i==3);
169             $cell{crit} .="Branch" if ($i==4);
170             $cell{crit} .="Doc Type" if ($i==5);
171             $cell{crit} .="Call number" if ($i==6);
172             $cell{crit} .="Collection code" if ($i==7);
173             $cell{crit} .="Shelving location" if ($i==8);
174             $cell{crit} .="Bor Cat" if ($i==9);
175             $cell{crit} .="Day" if ($i==10);
176             $cell{crit} .="Month" if ($i==11);
177             $cell{crit} .="Year" if ($i==12);
178             push @loopfilter, \%cell;
179         }
180     }
181     my $colfield;
182     my $colorder;
183     if ($column){
184         $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/issuedate/));
185         if($column=~/itemtype/){
186             $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
187         }
188         $column = "borrowers.".$column if $column=~/categorycode/;
189         my @colfilter ;
190         $colfilter[0] = @$filters[0] if ($column =~ /issuedate/ )  ;
191         $colfilter[1] = @$filters[1] if ($column =~ /issuedate/ )  ;
192         $colfilter[0] = @$filters[2] if ($column =~ /returndate/ )  ;
193         $colfilter[1] = @$filters[3] if ($column =~ /returndate/ )  ;
194         $colfilter[0] = @$filters[4] if ($column =~ /branch/ )  ;
195         $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ )  ;
196       # These limits does not currently exist, maybe later?
197       # $colfilter[0] = @$filters[6] if ($column =~ /ccode/ )  ;
198       # $colfilter[0] = @$filters[7] if ($column =~ /location/ )  ;
199         $colfilter[0] = @$filters[8] if ($column =~ /category/ )  ;
200       # This commented out row (sort2) was not removed when adding new filters for ccode, shelving location and call number
201       # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
202         $colfilter[0] = @$filters[9] if ($column =~ /issuedate/ ) ;
203         $colfilter[0] = @$filters[10] if ($column =~ /issuedate/ ) ;
204         $colfilter[0] = @$filters[11] if ($column =~ /issuedate/ ) ;
205     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
206                                                 
207     # loop cols.
208         if ($column eq "Day") {
209             #Display by day
210             $column = "old_issues.issuedate";
211             $colfield .="dayname($column)";  
212             $colorder .="weekday($column)";
213         } elsif ($column eq "Month") {
214             #Display by Month
215             $column = "old_issues.issuedate";
216             $colfield .="monthname($column)";  
217             $colorder .="month($column)";  
218         } elsif ($column eq "Year") {
219             #Display by Year
220             $column = "old_issues.issuedate";
221             $colfield .="Year($column)";
222             $colorder .= $column;
223         } else {
224             $colfield .= $column;
225             $colorder .= $column;
226         }  
227         
228         my $strsth2;
229         $strsth2 .= "SELECT distinctrow $colfield 
230                      FROM `old_issues` 
231                      LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber 
232                      LEFT JOIN items ON old_issues.itemnumber=items.itemnumber 
233                      LEFT JOIN biblioitems  ON biblioitems.biblioitemnumber=items.biblioitemnumber 
234                      WHERE 1";
235         if (($column=~/issuedate/) or ($column=~/returndate/)){
236             if ($colfilter[1] and ($colfilter[0])){
237                 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
238             } elsif ($colfilter[1]) {
239                     $strsth2 .= " and $column < '$colfilter[1]' " ;
240             } elsif ($colfilter[0]) {
241                 $strsth2 .= " and $column > '$colfilter[0]' " ;
242             }
243         } elsif ($colfilter[0]) {
244             $colfilter[0] =~ s/\*/%/g;
245             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
246         }
247         $strsth2 .=" group by $colfield";
248         $strsth2 .=" order by $colorder";
249         
250         my $sth2 = $dbh->prepare( $strsth2 );
251         if (( @colfilter ) and ($colfilter[1])){
252             $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
253         } elsif ($colfilter[0]) {
254             $sth2->execute($colfilter[0]);
255         } else {
256             $sth2->execute;
257         }
258         
259     
260         while (my ($celvalue) = $sth2->fetchrow) {
261             my %cell;
262             $cell{coltitle} = ($celvalue?$celvalue:"NULL");
263             push @loopcol, \%cell;
264         }
265     #   warn "fin des titres colonnes";
266     }
267     
268     my $i=0;
269 #       my @totalcol;
270     my $hilighted=-1;
271     
272     #Initialization of cell values.....
273     my @table;
274     
275 #       warn "init table";
276     for (my $i=1;$i<=$line;$i++) {
277         foreach my $col ( @loopcol ) {
278 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
279             $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
280         }
281     }
282
283
284 # preparing calculation
285     my $strcalc ;
286     
287 # Processing average loanperiods
288     $strcalc .= "SELECT biblio.title, COUNT(biblio.biblionumber) AS `RANK`, biblio.biblionumber AS ID";
289     $strcalc .= " , $colfield " if ($colfield);
290     $strcalc .= " FROM `old_issues` 
291                   LEFT JOIN items USING(itemnumber) 
292                   LEFT JOIN biblio USING(biblionumber) 
293                   LEFT JOIN biblioitems USING(biblionumber)
294                   LEFT JOIN borrowers USING(borrowernumber)
295                   WHERE 1";
296
297     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
298     $strcalc .= " AND old_issues.issuedate > '" . @$filters[0] ."'" if ( @$filters[0] );
299     @$filters[1]=~ s/\*/%/g if (@$filters[1]);
300     $strcalc .= " AND old_issues.issuedate < '" . @$filters[1] ."'" if ( @$filters[1] );
301     @$filters[2]=~ s/\*/%/g if (@$filters[2]);
302     $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
303     @$filters[3]=~ s/\*/%/g if (@$filters[3]);
304     $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
305     @$filters[4]=~ s/\*/%/g if (@$filters[4]);
306     $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
307     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
308     if ( @$filters[5] ){
309         if(C4::Context->preference('item-level_itypes') ){
310             $strcalc .= " AND items.itype like "
311         }else{
312             $strcalc .= " AND biblioitems.itemtype like "
313         } 
314         $strcalc .= "'" . @$filters[5] ."'" ;
315     }
316     @$filters[6]=~ s/\*/%/g if (@$filters[6]);
317     $strcalc .= " AND itemcallnumber like '" . @$filters[6] ."'" if ( @$filters[6] );
318     @$filters[7]=~ s/\*/%/g if (@$filters[7]);
319     $strcalc .= " AND ccode like '" . @$filters[7] ."'" if ( @$filters[7] );
320     @$filters[8]=~ s/\*/%/g if (@$filters[8]);
321     $strcalc .= " AND location like '" . @$filters[8] ."'" if ( @$filters[8] );
322     @$filters[9]=~ s/\*/%/g if (@$filters[9]);
323     $strcalc .= " AND borrowers.categorycode like '" . @$filters[9] ."'" if ( @$filters[9] );
324     @$filters[10]=~ s/\*/%/g if (@$filters[10]);
325     $strcalc .= " AND dayname(old_issues.issuedate) like '" . @$filters[10]."'" if (@$filters[10]);
326     @$filters[11]=~ s/\*/%/g if (@$filters[11]);
327     $strcalc .= " AND monthname(old_issues.issuedate) like '" . @$filters[11]."'" if (@$filters[11]);
328     @$filters[12]=~ s/\*/%/g if (@$filters[12]);
329     $strcalc .= " AND year(old_issues.issuedate) like '" . @$filters[12] ."'" if ( @$filters[12] );
330     
331     $strcalc .= " group by biblio.biblionumber, biblio.title";
332     $strcalc .= ", $colfield" if ($column);
333     $strcalc .= " order by `RANK` DESC";
334     $strcalc .= ", $colfield " if ($colfield);
335     
336     my $dbcalc = $dbh->prepare($strcalc);
337     $dbcalc->execute;
338     my %indice;
339     while (my  @data = $dbcalc->fetchrow) {
340         my ($row, $rank, $id, $callnum, $ccode, $loc, $col )=@data;
341         $col = "zzEMPTY" if (!defined($col));
342         $indice{$col}=1 if (not($indice{$col}));
343         $table[$indice{$col}]->{$col}->{'name'}=$row;
344         $table[$indice{$col}]->{$col}->{'count'}=$rank;
345         $table[$indice{$col}]->{$col}->{'link'}=$id;
346         $indice{$col}++;
347     }
348     
349     push @loopcol,{coltitle => "Global"} if not($column);
350     
351     for ($i=1; $i<=$line;$i++) {
352         my @loopcell;
353         #@loopcol ensures the order for columns is common with column titles
354         # and the number matches the number of columns
355         my $colcount=0;
356         foreach my $col ( @loopcol ) {
357             my $value;
358             my $count=0;
359             my $link;
360             if (@loopcol){
361                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
362                 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
363                 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
364             } else {
365                 $value =$table[$i]->{"zzEMPTY"}->{'name'};
366                 $count =$table[$i]->{"zzEMPTY"}->{'count'};
367                 $link =$table[$i]->{"zzEMPTY"}->{'link'};
368             }
369             push @loopcell, {value => $value, count =>$count, reference => $link} ;
370         }
371         #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
372         push @looprow,{ 'rowtitle' => $i ,
373                         'loopcell' => \@loopcell,
374                         'hilighted' => ($hilighted >0),
375                     };
376         $hilighted = -$hilighted;
377     }
378 #       
379             
380
381     # the header of the table
382     $globalline{loopfilter}=\@loopfilter;
383     # the core of the table
384     $globalline{looprow} = \@looprow;
385     $globalline{loopcol} = \@loopcol;
386 #       # the foot (totals by borrower type)
387     $globalline{total}= $grantotal;
388     $globalline{line} = $line;
389     $globalline{column} = $column;
390     push @mainloop,\%globalline;
391     return \@mainloop;
392 }
393
394 1;