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
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
25 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/cat_issues_top.tmpl";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 $filters[0]=format_date_in_iso($filters[0]);
49 $filters[1]=format_date_in_iso($filters[1]);
50 my $output = $input->param("output");
51 my $basename = $input->param("basename");
52 my $mime = $input->param("MIME");
53 my $del = $input->param("sep");
54 #warn "calcul : ".$calc;
55 my ($template, $borrowernumber, $cookie)
56 = get_template_and_user({template_name => $fullreportname,
60 flagsrequired => { reports => 1},
63 $template->param(do_it => $do_it,
64 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
68 my $results = calculate($limit, $column, \@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop => $results,
73 output_html_with_http_headers $input, $cookie, $template->output;
76 # Printing to a csv file
77 print $input->header(-type => 'application/vnd.sun.xml.calc',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
84 $sep =C4::Context->preference("delimiter");
86 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
88 foreach my $col ( @$cols ) {
89 print $col->{coltitle}.$sep;
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};
104 $cols = @$results[0]->{loopfooter};
105 foreach my $col ( @$cols ) {
106 print $sep.$col->{totalcol};
108 print $sep.@$results[0]->{total};
113 my $dbh = C4::Context->dbh;
119 my @mime = ( C4::Context->preference("MIME") );
120 # foreach my $mime (@mime){
124 my $CGIextChoice=CGI::scrolling_list(
131 my @dels = ( C4::Context->preference("delimiter") );
132 my $CGIsepChoice=CGI::scrolling_list(
139 my $branches = GetBranches;
141 foreach my $thisbranch (keys %$branches) {
142 # my $selected = 1 if $thisbranch eq $branch;
143 my %row =(value => $thisbranch,
144 # selected => $selected,
145 branchname => $branches->{$thisbranch}->{'branchname'},
147 push @branchloop, \%row;
151 my $itemtypes = GetItemTypes;
153 foreach my $thisitemtype (keys %$itemtypes) {
154 # my $selected = 1 if $thisbranch eq $branch;
155 my %row =(value => $thisitemtype,
156 # selected => $selected,
157 description => $itemtypes->{$thisitemtype}->{'description'},
159 push @itemtypeloop, \%row;
163 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
165 foreach my $thisborcat (sort keys %$labels) {
166 # my $selected = 1 if $thisbranch eq $branch;
167 my %row =(value => $thisborcat,
168 # selected => $selected,
169 description => $labels->{$thisborcat},
171 push @borcatloop, \%row;
177 CGIextChoice => $CGIextChoice,
178 CGIsepChoice => $CGIsepChoice,
179 branchloop =>\@branchloop,
180 itemtypeloop =>\@itemtypeloop,
181 borcatloop =>\@borcatloop,
183 output_html_with_http_headers $input, $cookie, $template->output;
190 my ($line, $column, $filters) = @_;
199 my $dbh = C4::Context->dbh;
205 for (my $i=0;$i<=6;$i++) {
207 if ( @$filters[$i] ) {
208 if (($i==1) and (@$filters[$i-1])) {
209 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
211 # format the dates filters, otherwise just fill as is
213 $cell{filter} .= @$filters[$i];
215 $cell{filter} .= format_date(@$filters[$i]);
216 } $cell{crit} .="Issue From" if ($i==0);
217 $cell{crit} .="Issue To" if ($i==1);
218 $cell{crit} .="Return From" if ($i==2);
219 $cell{crit} .="Return To" if ($i==3);
220 $cell{crit} .="Branch" if ($i==4);
221 $cell{crit} .="Doc Type" if ($i==5);
222 $cell{crit} .="Bor Cat" if ($i==6);
223 $cell{crit} .="Day" if ($i==7);
224 $cell{crit} .="Month" if ($i==8);
225 $cell{crit} .="Year" if ($i==9);
226 push @loopfilter, \%cell;
232 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
233 $column = "biblioitems.".$column if $column=~/itemtype/;
234 $column = "borrowers.".$column if $column=~/categorycode/;
236 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
237 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
238 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
239 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
240 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
241 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
242 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
243 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
244 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
246 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
247 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
250 if ($column eq "Day") {
252 $column = "issues.timestamp";
253 $colfield .="dayname($column)";
254 $colorder .="weekday($column)";
255 } elsif ($column eq "Month") {
257 $column = "issues.timestamp";
258 $colfield .="monthname($column)";
259 $colorder .="month($column)";
260 } elsif ($column eq "Year") {
262 $column = "issues.timestamp";
263 $colfield .="Year($column)";
264 $colorder .= $column;
266 $colfield .= $column;
267 $colorder .= $column;
271 $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";
272 if (($column=~/timestamp/) or ($column=~/returndate/)){
273 if ($colfilter[1] and ($colfilter[0])){
274 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
275 } elsif ($colfilter[1]) {
276 $strsth2 .= " and $column < '$colfilter[1]' " ;
277 } elsif ($colfilter[0]) {
278 $strsth2 .= " and $column > '$colfilter[0]' " ;
280 } elsif ($colfilter[0]) {
281 $colfilter[0] =~ s/\*/%/g;
282 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
284 $strsth2 .=" group by $colfield";
285 $strsth2 .=" order by $colorder";
288 my $sth2 = $dbh->prepare( $strsth2 );
289 if (( @colfilter ) and ($colfilter[1])){
290 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
291 } elsif ($colfilter[0]) {
292 $sth2->execute($colfilter[0]);
298 while (my ($celvalue) = $sth2->fetchrow) {
300 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
301 push @loopcol, \%cell;
303 # warn "fin des titres colonnes";
310 #Initialization of cell values.....
314 for (my $i=1;$i<=$line;$i++) {
315 foreach my $col ( @loopcol ) {
316 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
317 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
322 # preparing calculation
325 # Processing average loanperiods
326 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
327 $strcalc .= " , $colfield " if ($colfield);
328 $strcalc .= " FROM `issues`,borrowers,(items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber) LEFT JOIN biblio ON (biblio.biblionumber=items.biblionumber) WHERE issues.itemnumber=items.itemnumber AND issues.borrowernumber=borrowers.borrowernumber and returndate is not null";
330 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
331 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
332 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
333 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
334 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
335 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
336 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
337 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
338 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
339 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
340 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
341 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
342 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
343 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
344 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
345 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
346 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
347 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
348 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
349 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
351 $strcalc .= " group by biblio.biblionumber";
352 $strcalc .= ", $colfield" if ($column);
353 $strcalc .= " order by RANK DESC";
354 $strcalc .= ", $colfield " if ($colfield);
357 # $max = $line*@loopcol;
358 # } else { $max=$line;}
359 # $strcalc .= " LIMIT 0,$max";
360 warn "SQL :". $strcalc;
362 my $dbcalc = $dbh->prepare($strcalc);
364 # warn "filling table";
367 while (my @data = $dbcalc->fetchrow) {
368 my ($row, $rank, $id, $col )=@data;
369 $col = "zzEMPTY" if ($col eq undef);
370 $indice{$col}=1 if (not($indice{$col}));
371 $table[$indice{$col}]->{$col}->{'name'}=$row;
372 $table[$indice{$col}]->{$col}->{'count'}=$rank;
373 $table[$indice{$col}]->{$col}->{'link'}=$id;
374 # warn " ".$i." ".$col. " ".$row;
378 push @loopcol,{coltitle => "Global"} if not($column);
380 for ($i=1; $i<=$line;$i++) {
383 #@loopcol ensures the order for columns is common with column titles
384 # and the number matches the number of columns
386 foreach my $col ( @loopcol ) {
387 # warn " colonne :$col->{coltitle}";
392 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
393 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
394 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
396 $value =$table[$i]->{"zzEMPTY"}->{'name'};
397 $count =$table[$i]->{"zzEMPTY"}->{'count'};
398 $link =$table[$i]->{"zzEMPTY"}->{'link'};
400 # warn " ".$i ." value:$value count:$count reference:$link";
401 push @loopcell, {value => $value, count =>$count, reference => $link} ;
403 #warn "row : $row colcount:$colcount";
404 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
405 push @looprow,{ 'rowtitle' => $i ,
406 'loopcell' => \@loopcell,
407 'hilighted' => ($hilighted >0),
408 #'totalrow' => ($total)?sprintf("%.2f",$total):0
410 $hilighted = -$hilighted;
415 # the header of the table
416 $globalline{loopfilter}=\@loopfilter;
417 # the core of the table
418 $globalline{looprow} = \@looprow;
419 $globalline{loopcol} = \@loopcol;
420 # # the foot (totals by borrower type)
421 $globalline{loopfooter} = \@loopfooter;
422 $globalline{total}= $grantotal;
423 $globalline{line} = $line;
424 $globalline{column} = $column;
425 push @mainloop,\%globalline;