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
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.
21 #use warnings; FIXME - Bug 2505
26 use C4
::Branch
; # GetBranches
32 use C4
::Dates
qw(format_date format_date_in_iso);
36 plugin that shows a stats on borrowers
45 $debug and open DEBUG
, ">/tmp/bor_issues_top.debug.log";
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,
63 flagsrequired
=> {reports
=> '*'},
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(),
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;
79 # Printing to a csv file
80 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
82 -attachment
=>"$basename.csv",
83 -filename
=>"$basename.csv" );
84 my $cols = @
$results[0]->{loopcol
};
85 my $lines = @
$results[0]->{looprow
};
87 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
89 print join($sep, map {$_->{coltitle
}} @
$cols);
90 print $sep . "Total\n";
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
};
101 $cols = @
$results[0]->{loopfooter
};
102 print join($sep, map {$_->{totalcol
}} @
$cols);
103 print $sep.@
$results[0]->{total
};
108 my $dbh = C4
::Context
->dbh;
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
;
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
;
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);
136 foreach (sort keys %$labels) {
137 my %row =(value
=> $_,
138 description
=> $labels->{$_},
140 push @borcatloop, \
%row;
145 CGIseplist
=> $delims,
146 branchloop
=> \
@branchloop,
147 itemtypeloop
=> \
@itemtypeloop,
148 borcatloop
=> \
@borcatloop,
150 output_html_with_http_headers
$input, $cookie, $template->output;
154 my ($line, $column, $filters) = @_;
161 my $dbh = C4
::Context
->dbh;
177 for (my $i=0;$i<=6;$i++) {
179 if ( @
$filters[$i] ) {
180 if (($i==1) and (@
$filters[$i-1])) {
181 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
183 # format the dates filters, otherwise just fill as is
184 $cell{filter
} .= ($i>=4) ? @
$filters[$i] : format_date
(@
$filters[$i]);
185 defined ($cellmap[$i]) and
186 $cell{crit
} .= $cellmap[$i];
187 push @loopfilter, \
%cell;
193 $column = "old_issues." .$column if (($column=~/branchcode/) or ($column=~/timestamp/));
194 $column = "biblioitems.".$column if $column=~/itemtype/;
195 $column = "borrowers." .$column if $column=~/categorycode/;
197 if ($column =~ /timestamp/) {
198 $colfilter[0] = @
$filters[0];
199 $colfilter[1] = @
$filters[1];
200 } elsif ($column =~ /returndate/) {
201 $colfilter[0] = @
$filters[2];
202 $colfilter[1] = @
$filters[3];
203 } elsif ($column =~ /branchcode/) {
204 $colfilter[0] = @
$filters[4];
205 } elsif ($column =~ /itemtype/) {
206 $colfilter[0] = @
$filters[5];
207 } elsif ($column =~ /category/) {
208 $colfilter[0] = @
$filters[6];
209 } elsif ($column =~ /sort2/ ) {
210 # $colfilter[0] = @$filters[11];
212 # $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ; FIXME This can't be right.
213 # $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ; FIXME
214 # $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ; FIXME Only this line would have effect.
216 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
219 if ($column eq "Day") {
221 $column = "old_issues.timestamp";
222 $colfield .="dayname($column)";
223 $colorder .="weekday($column)";
224 } elsif ($column eq "Month") {
226 $column = "old_issues.timestamp";
227 $colfield .="monthname($column)";
228 $colorder .="month($column)";
229 } elsif ($column eq "Year") {
231 $column = "old_issues.timestamp";
232 $colfield .="Year($column)";
233 $colorder .= $column;
235 $colfield .= $column;
236 $colorder .= $column;
240 $strsth2 .= "SELECT DISTINCTROW $colfield
242 LEFT JOIN borrowers ON old_issues.borrowernumber=borrowers.borrowernumber
243 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
244 LEFT JOIN biblioitems ON (biblioitems.biblioitemnumber=items.biblioitemnumber)
246 if (($column=~/timestamp/) or ($column=~/returndate/)){
247 if ($colfilter[1] and $colfilter[0]){
248 $strsth2 .= " AND $column between '$colfilter[0]' AND '$colfilter[1]' " ;
249 } elsif ($colfilter[1]) {
250 $strsth2 .= " AND $column < '$colfilter[1]' " ;
251 } elsif ($colfilter[0]) {
252 $strsth2 .= " AND $column > '$colfilter[0]' " ;
254 } elsif ($colfilter[0]) {
255 $colfilter[0] =~ s/\*/%/g;
256 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
258 $strsth2 .=" GROUP BY $colfield";
259 $strsth2 .=" ORDER BY $colorder";
261 $debug and print DEBUG
"bor_issues_top (old_issues) SQL: $strsth2\n";
262 my $sth2 = $dbh->prepare($strsth2);
264 print DEBUG
"rows: ", $sth2->rows, "\n";
265 while (my @row = $sth2->fetchrow) {
266 $columns{($row[0] ||'NULL')}++;
267 push @loopcol, { coltitle
=> $row[0] || 'NULL' };
270 $strsth2 =~ s/old_issues/issues/g;
271 $debug and print DEBUG
"bor_issues_top (issues) SQL: $strsth2\n";
272 $sth2 = $dbh->prepare($strsth2);
274 $debug and print DEBUG
"rows: ", $sth2->rows, "\n";
275 while (my @row = $sth2->fetchrow) {
276 $columns{($row[0] ||'NULL')}++;
277 push @loopcol, { coltitle
=> $row[0] || 'NULL' };
279 $debug and print DEBUG
"full array: ", Dumper
(\
%columns), "\n";
284 #Initialization of cell values.....
286 for (my $i=1;$i<=$line;$i++) {
287 foreach (keys %columns) {
288 # warn " init table : $row->{rowtitle} / $_ ";
289 $table[$i]->{ $_ || "total" }->{'name'}=0;
295 # Processing average loanperiods
296 $strcalc .= "SELECT CONCAT(borrowers.surname , \",\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
297 $strcalc .= " , $colfield " if ($colfield);
298 $strcalc .= " FROM `old_issues`
299 LEFT JOIN borrowers USING(borrowernumber)
300 LEFT JOIN items USING(itemnumber)
301 LEFT JOIN biblioitems USING(biblioitemnumber)
302 WHERE old_issues.borrowernumber IS NOT NULL
305 'old_issues.timestamp >',
306 'old_issues.timestamp <',
307 'old_issues.returndate >',
308 'old_issues.returndate <',
309 'old_issues.branchcode like',
310 'biblioitems.itemtype like',
311 'borrowers.categorycode like',
312 'dayname(old_issues.timestamp) like',
313 'monthname(old_issues.timestamp) like',
314 'monthname(old_issues.timestamp) like',
315 'year(old_issues.timestamp) like',
317 foreach ((@
$filters)[0..9]) {
318 my $term = shift @filterterms; # go through both arrays in step
321 $strcalc .= " AND $term '$_' ";
324 $strcalc .= " GROUP BY borrowers.borrowernumber";
325 $strcalc .= ", $colfield" if ($column);
326 $strcalc .= " ORDER BY RANK DESC";
327 $strcalc .= ",$colfield " if ($colfield);
330 # $max = $line*@loopcol;
331 # } else { $max=$line;}
332 # $strcalc .= " LIMIT 0,$max";
334 $debug and print DEBUG
"(old_issues) SQL : $strcalc\n";
335 my $dbcalc = $dbh->prepare($strcalc);
337 $debug and print DEBUG
"rows: ", $dbcalc->rows, "\n";
339 # DATA STRUCTURE is going to look like this:
340 # (2253=> {name=>"John Doe",
341 # allcols=>{MAIN=>12, MEDIA_LIB=>3}
344 while (my @data = $dbcalc->fetchrow) {
345 my ($row, $rank, $id, $col) = @data;
346 $col = "zzEMPTY" if (!defined($col));
347 unless ($patrons{$id}) {
348 $patrons{$id} = {name
=>$row, allcols
=>{}, newcols
=>{}, oldcols
=>{}};
350 $patrons{$id}->{oldcols
}->{$col} = $rank;
355 $strcalc =~ s/old_issues/issues/g;
356 $debug and print DEBUG
"(issues) SQL : $strcalc\n";
357 $dbcalc = $dbh->prepare($strcalc);
359 $debug and print DEBUG
"rows: ", $dbcalc->rows, "\n";
360 while (my @data = $dbcalc->fetchrow) {
361 my ($row, $rank, $id, $col) = @data;
362 $col = "zzEMPTY" if (!defined($col));
363 unless ($patrons{$id}) {
364 $patrons{$id} = {name
=>$row, allcols
=>{}, newcols
=>{}, oldcols
=>{}};
366 $patrons{$id}->{newcols
}->{$col} = $rank;
369 foreach my $id (keys %patrons) {
370 my @uniq = keys %{{ %{$patrons{$id}->{newcols
}}, %{$patrons{$id}->{oldcols
}} }}; # get uniq keys, see perlfaq4
372 my $count = ($patrons{$id}->{newcols
}->{$_} || 0) +
373 ($patrons{$id}->{oldcols
}->{$_} || 0);
374 $patrons{$id}->{allcols
}->{$_} = $count;
375 $patrons{$id}->{total
} += $count;
378 $debug and print DEBUG
"\n\npatrons: ", Dumper
(\
%patrons);
381 my @cols_in_order = sort keys %columns; # if you want to order the columns, do something here
382 my @ranked_ids = sort {
383 $patrons{$b}->{total
} <=> $patrons{$a}->{total
}
384 || $patrons{$a}->{name
} cmp $patrons{$b}->{name
}
386 foreach my $id (@ranked_ids) {
388 foreach my $key (@cols_in_order) {
391 value
=> $patrons{$id}->{name
},
393 count
=> $patrons{$id}->{allcols
}->{$key},
397 value
=> $patrons{$id}->{name
},
399 count
=> $patrons{$id}->{total
},
403 push @looprow,{ 'rowtitle' => $i++ ,
404 'loopcell' => \
@loopcell,
405 'hilighted' => ($i%2),
409 # the header of the table
410 $globalline{loopfilter
}=\
@loopfilter;
411 # the core of the table
412 $globalline{looprow
} = \
@looprow;
413 $globalline{loopcol
} = [ map {{coltitle
=>$_}} @cols_in_order ];
414 # the foot (totals by borrower type)
415 $globalline{loopfooter
} = [];
416 $globalline{total
}= $grantotal; # FIXME: useless
417 $globalline{line
} = $line;
418 $globalline{column
} = $column;
419 return [\
%globalline]; # reference to a 1 element array: that element is a hashref
422 $debug and close DEBUG
;