Bug 8910 - silent warnings triggered in about.pl
[koha.git] / reports / bor_issues_top.pl
blob6ad34955cf33c5258e9ee0acca0b254f907f9f54
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI;
23 use C4::Auth;
24 use C4::Output;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Koha;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Reports;
31 use C4::Debug;
32 use C4::Dates qw(format_date format_date_in_iso);
34 =head1 NAME
36 plugin that shows a stats on borrowers
38 =head1 DESCRIPTION
40 =over 2
42 =cut
44 $debug = 1;
45 $debug and open DEBUG, ">/tmp/bor_issues_top.debug.log";
47 my $input = new CGI;
48 my $fullreportname = "reports/bor_issues_top.tmpl";
49 my $do_it = $input->param('do_it');
50 my $limit = $input->param("Limit");
51 my $column = $input->param("Criteria");
52 my @filters = $input->param("Filter");
53 foreach ( @filters[0..3] ) {
54 $_ and $_ = format_date_in_iso($_);
56 my $output = $input->param("output");
57 my $basename = $input->param("basename");
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
60 query => $input,
61 type => "intranet",
62 authnotrequired => 0,
63 flagsrequired => {reports => '*'},
64 debug => 1,
65 });
66 our $sep = $input->param("sep");
67 $sep = "\t" if ($sep eq 'tabulation');
68 $template->param(do_it => $do_it,
69 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
71 if ($do_it) {
72 # Displaying results
73 my $results = calculate($limit, $column, \@filters);
74 if ($output eq "screen"){
75 # Printing results to screen
76 $template->param(mainloop => $results, limit=>$limit);
77 output_html_with_http_headers $input, $cookie, $template->output;
78 } else {
79 # Printing to a csv file
80 print $input->header(-type => 'application/vnd.sun.xml.calc',
81 -encoding => 'utf-8',
82 -attachment=>"$basename.csv",
83 -filename=>"$basename.csv" );
84 my $cols = @$results[0]->{loopcol};
85 my $lines = @$results[0]->{looprow};
86 # header top-right
87 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 # Other header
89 print join($sep, map {$_->{coltitle}} @$cols);
90 print $sep . "Total\n";
91 # Table
92 foreach my $line ( @$lines ) {
93 my $x = $line->{loopcell};
94 print $line->{rowtitle}.$sep;
95 print join($sep, map {$_->{value}} @$x);
96 print $sep,$line->{totalrow};
97 print "\n";
99 # footer
100 print "TOTAL";
101 $cols = @$results[0]->{loopfooter};
102 print join($sep, map {$_->{totalcol}} @$cols);
103 print $sep.@$results[0]->{total};
105 exit;
108 my $dbh = C4::Context->dbh;
109 my @values;
111 # here each element returned by map is a hashref, get it?
112 my @mime = ( map { {type =>$_} } (split /[;:]/, 'CSV') ); # FIXME translation
113 my $delims = GetDelimiterChoices;
114 my $branches = GetBranches;
115 my @branchloop;
116 foreach (sort keys %$branches) {
117 # my $selected = 1 if $thisbranch eq $branch;
118 my %row = ( value => $_,
119 # selected => $selected,
120 branchname => $branches->{$_}->{branchname},
122 push @branchloop, \%row;
125 my $itemtypes = GetItemTypes;
126 my @itemtypeloop;
127 foreach (sort {$itemtypes->{$a}->{description} cmp $itemtypes->{$b}->{description}} keys %$itemtypes) {
128 my %row = (value => $_,
129 description => $itemtypes->{$_}->{description},
131 push @itemtypeloop, \%row;
134 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
135 my @borcatloop;
136 foreach (sort keys %$labels) {
137 my %row =(value => $_,
138 description => $labels->{$_},
140 push @borcatloop, \%row;
143 $template->param(
144 mimeloop => \@mime,
145 CGIseplist => $delims,
146 branchloop => \@branchloop,
147 itemtypeloop => \@itemtypeloop,
148 borcatloop => \@borcatloop,
150 output_html_with_http_headers $input, $cookie, $template->output;
153 sub calculate {
154 my ($limit, $column, $filters) = @_;
156 my @loopcol;
157 my @loopline;
158 my @looprow;
159 my %globalline;
160 my %columns;
161 my $grantotal =0;
162 my $dbh = C4::Context->dbh;
164 # Checking filters
165 my @loopfilter;
166 my @cellmap = (
167 "Issue From",
168 "Issue To",
169 "Return From",
170 "Return To",
171 "Branch",
172 "Doc Type",
173 "Bor Cat",
174 "Day",
175 "Month",
176 "Year"
178 for (my $i=0;$i<=6;$i++) {
179 my %cell;
180 if ( @$filters[$i] ) {
181 if (($i==1) and (@$filters[$i-1])) {
182 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
184 # format the dates filters, otherwise just fill as is
185 $cell{filter} .= ($i>=4) ? @$filters[$i] : format_date(@$filters[$i]);
186 defined ($cellmap[$i]) and
187 $cell{crit} .= $cellmap[$i];
188 push @loopfilter, \%cell;
191 my $colfield;
192 my $colorder;
193 if ($column){
194 $column = "old_issues." .$column if (($column=~/branchcode/) or ($column=~/timestamp/));
195 $column = "biblioitems.".$column if $column=~/itemtype/;
196 $column = "borrowers." .$column if $column=~/categorycode/;
197 my @colfilter ;
198 if ($column =~ /timestamp/) {
199 $colfilter[0] = @$filters[0];
200 $colfilter[1] = @$filters[1];
201 } elsif ($column =~ /returndate/) {
202 $colfilter[0] = @$filters[2];
203 $colfilter[1] = @$filters[3];
204 } elsif ($column =~ /branchcode/) {
205 $colfilter[0] = @$filters[4];
206 } elsif ($column =~ /itemtype/) {
207 $colfilter[0] = @$filters[5];
208 } elsif ($column =~ /category/) {
209 $colfilter[0] = @$filters[6];
210 } elsif ($column =~ /sort2/ ) {
211 # $colfilter[0] = @$filters[11];
214 # loop cols.
215 if ($column eq "Day") {
216 #Display by day
217 $column = "old_issues.timestamp";
218 $colfield .="dayname($column)";
219 $colorder .="weekday($column)";
220 } elsif ($column eq "Month") {
221 #Display by Month
222 $column = "old_issues.timestamp";
223 $colfield .="monthname($column)";
224 $colorder .="month($column)";
225 } elsif ($column eq "Year") {
226 #Display by Year
227 $column = "old_issues.timestamp";
228 $colfield .="Year($column)";
229 $colorder .= $column;
230 } else {
231 $colfield .= $column;
232 $colorder .= $column;
235 my $strsth2;
236 $strsth2 .= "SELECT DISTINCTROW $colfield
237 FROM `old_issues`
238 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
239 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
240 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
241 WHERE 1";
242 if (($column=~/timestamp/) or ($column=~/returndate/)){
243 if ($colfilter[1] and $colfilter[0]){
244 $strsth2 .= " AND $column between '$colfilter[0]' AND '$colfilter[1]' " ;
245 } elsif ($colfilter[1]) {
246 $strsth2 .= " AND $column < '$colfilter[1]' " ;
247 } elsif ($colfilter[0]) {
248 $strsth2 .= " AND $column > '$colfilter[0]' " ;
250 } elsif ($colfilter[0]) {
251 $colfilter[0] =~ s/\*/%/g;
252 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
254 $strsth2 .=" GROUP BY $colfield";
255 $strsth2 .=" ORDER BY $colorder";
257 $debug and print DEBUG "bor_issues_top (old_issues) SQL: $strsth2\n";
258 my $sth2 = $dbh->prepare($strsth2);
259 $sth2->execute;
260 print DEBUG "rows: ", $sth2->rows, "\n";
261 while (my @row = $sth2->fetchrow) {
262 $columns{($row[0] ||'NULL')}++;
263 push @loopcol, { coltitle => $row[0] || 'NULL' };
266 $strsth2 =~ s/old_issues/issues/g;
267 $debug and print DEBUG "bor_issues_top (issues) SQL: $strsth2\n";
268 $sth2 = $dbh->prepare($strsth2);
269 $sth2->execute;
270 $debug and print DEBUG "rows: ", $sth2->rows, "\n";
271 while (my @row = $sth2->fetchrow) {
272 $columns{($row[0] ||'NULL')}++;
273 push @loopcol, { coltitle => $row[0] || 'NULL' };
275 $debug and print DEBUG "full array: ", Dumper(\%columns), "\n";
276 }else{
277 $columns{''} = 1;
280 my $strcalc ;
282 # Processing average loanperiods
283 $strcalc .= "SELECT CONCAT(borrowers.surname , \",\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
284 $strcalc .= " , $colfield " if ($colfield);
285 $strcalc .= " FROM `old_issues`
286 LEFT JOIN borrowers USING(borrowernumber)
287 LEFT JOIN items USING(itemnumber)
288 LEFT JOIN biblioitems USING(biblioitemnumber)
289 WHERE old_issues.borrowernumber IS NOT NULL
291 my @filterterms = (
292 'old_issues.issuedate >',
293 'old_issues.issuedate <',
294 'old_issues.returndate >',
295 'old_issues.returndate <',
296 'old_issues.branchcode like',
297 'biblioitems.itemtype like',
298 'borrowers.categorycode like',
300 foreach ((@$filters)[0..9]) {
301 my $term = shift @filterterms; # go through both arrays in step
302 ($_) or next;
303 s/\*/%/g;
304 $strcalc .= " AND $term '$_' ";
306 $strcalc .= " GROUP BY borrowers.borrowernumber";
307 $strcalc .= ", $colfield" if ($column);
308 $strcalc .= " ORDER BY RANK DESC";
309 $strcalc .= ",$colfield " if ($colfield);
310 $strcalc .= " LIMIT $limit" if ($limit);
312 $debug and print DEBUG "(old_issues) SQL : $strcalc\n";
313 my $dbcalc = $dbh->prepare($strcalc);
314 $dbcalc->execute;
315 $debug and print DEBUG "rows: ", $dbcalc->rows, "\n";
316 my %patrons = ();
317 # DATA STRUCTURE is going to look like this:
318 # (2253=> {name=>"John Doe",
319 # allcols=>{MAIN=>12, MEDIA_LIB=>3}
320 # },
322 while (my @data = $dbcalc->fetchrow) {
323 my ($row, $rank, $id, $col) = @data;
324 $col = "zzEMPTY" if (!defined($col));
325 unless ($patrons{$id}) {
326 $patrons{$id} = {name=>$row, allcols=>{}, newcols=>{}, oldcols=>{}};
328 $patrons{$id}->{oldcols}->{$col} = $rank;
331 use Data::Dumper;
333 $strcalc =~ s/old_issues/issues/g;
334 $debug and print DEBUG "(issues) SQL : $strcalc\n";
335 $dbcalc = $dbh->prepare($strcalc);
336 $dbcalc->execute;
337 $debug and print DEBUG "rows: ", $dbcalc->rows, "\n";
338 while (my @data = $dbcalc->fetchrow) {
339 my ($row, $rank, $id, $col) = @data;
340 $col = "zzEMPTY" if (!defined($col));
341 unless ($patrons{$id}) {
342 $patrons{$id} = {name=>$row, allcols=>{}, newcols=>{}, oldcols=>{}};
344 $patrons{$id}->{newcols}->{$col} = $rank;
347 foreach my $id (keys %patrons) {
348 my @uniq = keys %{{ %{$patrons{$id}->{newcols}}, %{$patrons{$id}->{oldcols}} }}; # get uniq keys, see perlfaq4
349 foreach (@uniq) {
350 my $count = ($patrons{$id}->{newcols}->{$_} || 0) +
351 ($patrons{$id}->{oldcols}->{$_} || 0);
352 $patrons{$id}->{allcols}->{$_} = $count;
353 $patrons{$id}->{total} += $count;
356 $debug and print DEBUG "\n\npatrons: ", Dumper(\%patrons);
358 my $i = 1;
359 my @cols_in_order = sort keys %columns; # if you want to order the columns, do something here
360 my @ranked_ids = sort {
361 $patrons{$b}->{total} <=> $patrons{$a}->{total}
362 || $patrons{$a}->{name} cmp $patrons{$b}->{name}
363 } keys %patrons;
364 foreach my $id (@ranked_ids) {
365 my @loopcell;
366 foreach my $key (@cols_in_order) {
367 if($column){
368 push @loopcell, {
369 value => $patrons{$id}->{name},
370 reference => $id,
371 count => $patrons{$id}->{allcols}->{$key},
373 }else{
374 push @loopcell, {
375 value => $patrons{$id}->{name},
376 reference => $id,
377 count => $patrons{$id}->{total},
381 push @looprow,{ 'rowtitle' => $i++ ,
382 'loopcell' => \@loopcell,
383 'hilighted' => ($i%2),
387 # the header of the table
388 $globalline{loopfilter}=\@loopfilter;
389 # the core of the table
390 $globalline{looprow} = \@looprow;
391 $globalline{loopcol} = [ map {{coltitle=>$_}} @cols_in_order ];
392 # the foot (totals by borrower type)
393 $globalline{loopfooter} = [];
394 $globalline{total}= $grantotal; # FIXME: useless
395 $globalline{column} = $column;
396 return [\%globalline]; # reference to a 1 element array: that element is a hashref
399 $debug and close DEBUG;
401 __END__