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