5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
26 use C4::Branch; # GetBranches
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/cat_issues_top.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 $filters[0]=format_date_in_iso($filters[0]);
50 $filters[1]=format_date_in_iso($filters[1]);
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,
61 flagsrequired => { reports => 1},
64 $template->param(do_it => $do_it,
65 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results,
74 output_html_with_http_headers $input, $cookie, $template->output;
77 # Printing to a csv file
78 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
87 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
89 foreach my $col ( @$cols ) {
90 print $col->{coltitle}.$sep;
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
114 my $dbh = C4::Context->dbh;
120 my @mime = ( C4::Context->preference("MIME") );
121 # foreach my $mime (@mime){
125 my $CGIextChoice=CGI::scrolling_list(
132 my @dels = ( C4::Context->preference("delimiter") );
133 my $CGIsepChoice=CGI::scrolling_list(
140 my $branches = GetBranches;
142 foreach my $thisbranch (keys %$branches) {
143 # my $selected = 1 if $thisbranch eq $branch;
144 my %row =(value => $thisbranch,
145 # selected => $selected,
146 branchname => $branches->{$thisbranch}->{'branchname'},
148 push @branchloop, \%row;
152 my $itemtypes = GetItemTypes;
154 foreach my $thisitemtype (keys %$itemtypes) {
155 # my $selected = 1 if $thisbranch eq $branch;
156 my %row =(value => $thisitemtype,
157 # selected => $selected,
158 description => $itemtypes->{$thisitemtype}->{'description'},
160 push @itemtypeloop, \%row;
164 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
166 foreach my $thisborcat (sort keys %$labels) {
167 # my $selected = 1 if $thisbranch eq $branch;
168 my %row =(value => $thisborcat,
169 # selected => $selected,
170 description => $labels->{$thisborcat},
172 push @borcatloop, \%row;
178 CGIextChoice => $CGIextChoice,
179 CGIsepChoice => $CGIsepChoice,
180 branchloop =>\@branchloop,
181 itemtypeloop =>\@itemtypeloop,
182 borcatloop =>\@borcatloop,
184 output_html_with_http_headers $input, $cookie, $template->output;
191 my ($line, $column, $filters) = @_;
200 my $dbh = C4::Context->dbh;
206 for (my $i=0;$i<=6;$i++) {
208 if ( @$filters[$i] ) {
209 if (($i==1) and (@$filters[$i-1])) {
210 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
212 # format the dates filters, otherwise just fill as is
214 $cell{filter} .= @$filters[$i];
216 $cell{filter} .= format_date(@$filters[$i]);
217 } $cell{crit} .="Issue From" if ($i==0);
218 $cell{crit} .="Issue To" if ($i==1);
219 $cell{crit} .="Return From" if ($i==2);
220 $cell{crit} .="Return To" if ($i==3);
221 $cell{crit} .="Branch" if ($i==4);
222 $cell{crit} .="Doc Type" if ($i==5);
223 $cell{crit} .="Bor Cat" if ($i==6);
224 $cell{crit} .="Day" if ($i==7);
225 $cell{crit} .="Month" if ($i==8);
226 $cell{crit} .="Year" if ($i==9);
227 push @loopfilter, \%cell;
233 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
234 $column = "biblioitems.".$column if $column=~/itemtype/;
235 $column = "borrowers.".$column if $column=~/categorycode/;
237 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
238 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
239 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
240 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
241 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
242 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
243 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
244 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
245 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
246 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
247 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
248 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
251 if ($column eq "Day") {
253 $column = "issues.timestamp";
254 $colfield .="dayname($column)";
255 $colorder .="weekday($column)";
256 } elsif ($column eq "Month") {
258 $column = "issues.timestamp";
259 $colfield .="monthname($column)";
260 $colorder .="month($column)";
261 } elsif ($column eq "Year") {
263 $column = "issues.timestamp";
264 $colfield .="Year($column)";
265 $colorder .= $column;
267 $colfield .= $column;
268 $colorder .= $column;
272 $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";
273 if (($column=~/timestamp/) or ($column=~/returndate/)){
274 if ($colfilter[1] and ($colfilter[0])){
275 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
276 } elsif ($colfilter[1]) {
277 $strsth2 .= " and $column < '$colfilter[1]' " ;
278 } elsif ($colfilter[0]) {
279 $strsth2 .= " and $column > '$colfilter[0]' " ;
281 } elsif ($colfilter[0]) {
282 $colfilter[0] =~ s/\*/%/g;
283 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
285 $strsth2 .=" group by $colfield";
286 $strsth2 .=" order by $colorder";
289 my $sth2 = $dbh->prepare( $strsth2 );
290 if (( @colfilter ) and ($colfilter[1])){
291 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
292 } elsif ($colfilter[0]) {
293 $sth2->execute($colfilter[0]);
299 while (my ($celvalue) = $sth2->fetchrow) {
301 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
302 push @loopcol, \%cell;
304 # warn "fin des titres colonnes";
311 #Initialization of cell values.....
315 for (my $i=1;$i<=$line;$i++) {
316 foreach my $col ( @loopcol ) {
317 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
318 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
323 # preparing calculation
326 # Processing average loanperiods
327 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
328 $strcalc .= " , $colfield " if ($colfield);
329 $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";
331 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
332 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
333 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
334 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
335 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
336 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
337 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
338 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
339 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
340 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
341 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
342 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
343 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
344 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
345 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
346 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
347 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
348 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
349 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
350 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
352 $strcalc .= " group by biblio.biblionumber";
353 $strcalc .= ", $colfield" if ($column);
354 $strcalc .= " order by RANK DESC";
355 $strcalc .= ", $colfield " if ($colfield);
358 # $max = $line*@loopcol;
359 # } else { $max=$line;}
360 # $strcalc .= " LIMIT 0,$max";
361 warn "SQL :". $strcalc;
363 my $dbcalc = $dbh->prepare($strcalc);
365 # warn "filling table";
368 while (my @data = $dbcalc->fetchrow) {
369 my ($row, $rank, $id, $col )=@data;
370 $col = "zzEMPTY" if ($col eq undef);
371 $indice{$col}=1 if (not($indice{$col}));
372 $table[$indice{$col}]->{$col}->{'name'}=$row;
373 $table[$indice{$col}]->{$col}->{'count'}=$rank;
374 $table[$indice{$col}]->{$col}->{'link'}=$id;
375 # warn " ".$i." ".$col. " ".$row;
379 push @loopcol,{coltitle => "Global"} if not($column);
381 for ($i=1; $i<=$line;$i++) {
384 #@loopcol ensures the order for columns is common with column titles
385 # and the number matches the number of columns
387 foreach my $col ( @loopcol ) {
388 # warn " colonne :$col->{coltitle}";
393 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
394 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
395 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
397 $value =$table[$i]->{"zzEMPTY"}->{'name'};
398 $count =$table[$i]->{"zzEMPTY"}->{'count'};
399 $link =$table[$i]->{"zzEMPTY"}->{'link'};
401 # warn " ".$i ." value:$value count:$count reference:$link";
402 push @loopcell, {value => $value, count =>$count, reference => $link} ;
404 #warn "row : $row colcount:$colcount";
405 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
406 push @looprow,{ 'rowtitle' => $i ,
407 'loopcell' => \@loopcell,
408 'hilighted' => ($hilighted >0),
409 #'totalrow' => ($total)?sprintf("%.2f",$total):0
411 $hilighted = -$hilighted;
416 # the header of the table
417 $globalline{loopfilter}=\@loopfilter;
418 # the core of the table
419 $globalline{looprow} = \@looprow;
420 $globalline{loopcol} = \@loopcol;
421 # # the foot (totals by borrower type)
422 $globalline{loopfooter} = \@loopfooter;
423 $globalline{total}= $grantotal;
424 $globalline{line} = $line;
425 $globalline{column} = $column;
426 push @mainloop,\%globalline;