NewsChannels - format_date for news "Posted on" date
[koha.git] / reports / bor_issues_top.pl
blobce98071fdde377fd3d35a792555b3b87b0e535f5
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
21 use strict;
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::Dates qw(format_date format_date_in_iso);
32 =head1 NAME
34 plugin that shows a stats on borrowers
36 =head1 DESCRIPTION
38 =over 2
40 =cut
42 my $input = new CGI;
43 my $fullreportname = "reports/bor_issues_top.tmpl";
44 my $do_it = $input->param('do_it');
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 for (0..3) {
49 $filters[$_]=format_date_in_iso($filters[$_]);
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
58 query => $input,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => {reports => 1},
62 debug => 1,
63 });
64 $template->param(do_it => $do_it,
65 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
67 if ($do_it) {
68 # Displaying results
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results, limit=>$limit);
73 output_html_with_http_headers $input, $cookie, $template->output;
74 exit(1);
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 my $sep;
84 $sep =C4::Context->preference("delimiter");
85 # header top-right
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
87 # Other header
88 foreach my $col ( @$cols ) {
89 print $col->{coltitle}.$sep;
91 print "Total\n";
92 # Table
93 foreach my $line ( @$lines ) {
94 my $x = $line->{loopcell};
95 print $line->{rowtitle}.$sep;
96 foreach my $cell (@$x) {
97 print $cell->{value}.$sep;
99 print $line->{totalrow};
100 print "\n";
102 # footer
103 print "TOTAL";
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
109 exit(1);
111 # Displaying choices
112 } else {
113 my $dbh = C4::Context->dbh;
114 my @values;
115 my %labels;
116 my %select;
117 my $req;
119 my @mime = ( C4::Context->preference("MIME") );
120 # foreach my $mime (@mime){
121 # warn "".$mime;
123 my $CGIextChoice=CGI::scrolling_list(
124 -name => 'MIME',
125 -id => 'MIME',
126 -values => \@mime,
127 -size => 1,
128 -multiple => 0 );
130 my @dels = ( C4::Context->preference("delimiter") );
131 my $CGIsepChoice=CGI::scrolling_list(
132 -name => 'sep',
133 -id => 'sep',
134 -values => \@dels,
135 -size => 1,
136 -multiple => 0 );
137 #branch
138 my $branches = GetBranches;
139 my @branchloop;
140 foreach my $thisbranch (sort keys %$branches) {
141 my %row =(value => $thisbranch,
142 branchname => $branches->{$thisbranch}->{'branchname'},
144 push @branchloop, \%row;
147 #doctype
148 my $itemtypes = GetItemTypes;
149 my @itemtypeloop;
150 foreach my $thisitemtype (keys %$itemtypes) {
151 # my $selected = 1 if $thisbranch eq $branch;
152 my %row =(value => $thisitemtype,
153 # selected => $selected,
154 description => $itemtypes->{$thisitemtype}->{'description'},
156 push @itemtypeloop, \%row;
159 #borcat
160 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
161 my @borcatloop;
162 foreach my $thisborcat (sort keys %$labels) {
163 # my $selected = 1 if $thisbranch eq $branch;
164 my %row =(value => $thisborcat,
165 # selected => $selected,
166 description => $labels->{$thisborcat},
168 push @borcatloop, \%row;
171 #Day
172 #Month
173 $template->param(
174 CGIextChoice => $CGIextChoice,
175 CGIsepChoice => $CGIsepChoice,
176 branchloop =>\@branchloop,
177 itemtypeloop =>\@itemtypeloop,
178 borcatloop =>\@borcatloop,
180 output_html_with_http_headers $input, $cookie, $template->output;
186 sub calculate {
187 my ($line, $column, $filters) = @_;
188 my @mainloop;
189 my @loopfooter;
190 my @loopcol;
191 my @loopline;
192 my @looprow;
193 my %globalline;
194 my $grantotal =0;
195 # extract parameters
196 my $dbh = C4::Context->dbh;
198 # Filters
199 # Checking filters
201 my @loopfilter;
202 my @cellmap = (
203 "Issue From",
204 "Issue To",
205 "Return From",
206 "Return To",
207 "Branch",
208 "Doc Type",
209 "Bor Cat",
210 "Day",
211 "Month",
212 "Year"
214 for (my $i=0;$i<=6;$i++) {
215 my %cell;
216 if ( @$filters[$i] ) {
217 if (($i==1) and (@$filters[$i-1])) {
218 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
220 # format the dates filters, otherwise just fill as is
221 if ($i>=4) {
222 $cell{filter} .= @$filters[$i];
223 } else {
224 $cell{filter} .= format_date(@$filters[$i]);
226 defined ($cellmap[$i]) and
227 $cell{crit} .= $cellmap[$i];
228 push @loopfilter, \%cell;
231 my $colfield;
232 my $colorder;
233 if ($column){
234 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
235 $column = "biblioitems.".$column if $column=~/itemtype/;
236 $column = "borrowers.".$column if $column=~/categorycode/;
237 my @colfilter ;
238 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
239 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
240 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
241 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
242 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
243 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
244 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
245 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
246 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
247 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
248 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
249 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
251 # loop cols.
252 if ($column eq "Day") {
253 #Display by day
254 $column = "issues.timestamp";
255 $colfield .="dayname($column)";
256 $colorder .="weekday($column)";
257 } elsif ($column eq "Month") {
258 #Display by Month
259 $column = "issues.timestamp";
260 $colfield .="monthname($column)";
261 $colorder .="month($column)";
262 } elsif ($column eq "Year") {
263 #Display by Year
264 $column = "issues.timestamp";
265 $colfield .="Year($column)";
266 $colorder .= $column;
267 } else {
268 $colfield .= $column;
269 $colorder .= $column;
272 my $strsth2;
273 $strsth2 .= "select distinctrow $colfield FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
274 if (($column=~/timestamp/) or ($column=~/returndate/)){
275 if ($colfilter[1] and ($colfilter[0])){
276 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
277 } elsif ($colfilter[1]) {
278 $strsth2 .= " and $column < '$colfilter[1]' " ;
279 } elsif ($colfilter[0]) {
280 $strsth2 .= " and $column > '$colfilter[0]' " ;
282 } elsif ($colfilter[0]) {
283 $colfilter[0] =~ s/\*/%/g;
284 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
286 $strsth2 .=" group by $colfield";
287 $strsth2 .=" order by $colorder";
288 warn "". $strsth2;
290 my $sth2 = $dbh->prepare( $strsth2 );
291 $sth2->execute;
293 while (my ($celvalue) = $sth2->fetchrow) {
294 my %cell;
295 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
296 push @loopcol, \%cell;
298 # warn "fin des titres colonnes";
301 my $i=0;
302 # my @totalcol;
303 my $hilighted=-1;
305 #Initialization of cell values.....
306 my @table;
308 # warn "init table";
309 for (my $i=1;$i<=$line;$i++) {
310 foreach my $col ( @loopcol ) {
311 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
312 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
317 # preparing calculation
318 my $strcalc ;
320 # Processing average loanperiods
321 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
322 $strcalc .= " , $colfield " if ($colfield);
323 $strcalc .= " FROM `issues`,borrowers,biblioitems LEFT JOIN items ON (biblioitems.biblioitemnumber=items.biblioitemnumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
325 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
326 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
327 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
328 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
329 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
330 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
331 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
332 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
333 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
334 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
335 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
336 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
337 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
338 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
339 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
340 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
341 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
342 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
343 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
344 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
346 $strcalc .= " group by borrowers.borrowernumber";
347 $strcalc .= ", $colfield" if ($column);
348 $strcalc .= " order by RANK DESC";
349 $strcalc .= ",$colfield " if ($colfield);
350 # my $max;
351 # if (@loopcol) {
352 # $max = $line*@loopcol;
353 # } else { $max=$line;}
354 # $strcalc .= " LIMIT 0,$max";
355 warn "SQL :". $strcalc;
357 my $dbcalc = $dbh->prepare($strcalc);
358 $dbcalc->execute;
359 # warn "filling table";
360 my $previous_col;
361 my %indice;
362 while (my @data = $dbcalc->fetchrow) {
363 my ($row, $rank, $id, $col )=@data;
364 $col = "zzEMPTY" if ($col eq undef);
365 $indice{$col}=1 if (not($indice{$col}));
366 $table[$indice{$col}]->{$col}->{'name'}=$row;
367 $table[$indice{$col}]->{$col}->{'count'}=$rank;
368 $table[$indice{$col}]->{$col}->{'link'}=$id;
369 # warn " ".$i." ".$col. " ".$row;
370 $indice{$col}++;
373 push @loopcol,{coltitle => "Global"} if not($column);
375 for ($i=1; $i<=$line;$i++) {
376 my @loopcell;
377 warn " $i";
378 #@loborrowersopcol ensures the order for columns is common with column titles
379 # and the number matches the number of columns
380 my $colcount=0;
381 foreach my $col ( @loopcol ) {
382 # warn " colonne :$col->{coltitle}";
383 my $value;
384 my $count=0;
385 my $link;
386 my $key = ((!(@loopcol)) or ($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))
387 ? "zzEMPTY" : $col->{coltitle};
388 $value =$table[$i]->{$key}->{'name'};
389 $count =$table[$i]->{$key}->{'count'};
390 $link =$table[$i]->{$key}->{'link'};
391 # warn " ".$i ." value:$value count:$count reference:$link";
392 push @loopcell, {value => $value, count =>$count, reference => $link} ;
394 push @looprow,{ 'rowtitle' => $i ,
395 'loopcell' => \@loopcell,
396 'hilighted' => ($hilighted >0),
398 $hilighted = -$hilighted;
402 # the header of the table
403 $globalline{loopfilter}=\@loopfilter;
404 # the core of the table
405 $globalline{looprow} = \@looprow;
406 $globalline{loopcol} = \@loopcol;
407 # # the foot (totals by borrower type)
408 $globalline{loopfooter} = \@loopfooter;
409 $globalline{total}= $grantotal;
410 $globalline{line} = $line;
411 $globalline{column} = $column;
412 push @mainloop,\%globalline;
413 return \@mainloop;