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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
37 plugin that shows a stats on borrowers
47 my $do_it=$input->param('do_it');
48 my $fullreportname = "reports/bor_issues_top.tmpl";
49 my $limit = $input->param("Limit");
50 my $column = $input->param("Criteria");
51 my @filters = $input->param("Filter");
52 my $output = $input->param("output");
53 my $basename = $input->param("basename");
54 my $mime = $input->param("MIME");
55 my $del = $input->param("sep");
56 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => {editcatalogue => 1},
65 $template->param(do_it => $do_it,
66 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
67 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
68 IntranetNav => C4::Context->preference("IntranetNav"),
72 my $results = calculate($limit, $column, \@filters);
73 if ($output eq "screen"){
74 # Printing results to screen
75 $template->param(mainloop => $results, limit=>$limit);
76 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',
81 -attachment=>"$basename.csv",
82 -filename=>"$basename.csv" );
83 my $cols = @$results[0]->{loopcol};
84 my $lines = @$results[0]->{looprow};
86 $sep =C4::Context->preference("delimiter");
88 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
90 foreach my $col ( @$cols ) {
91 print $col->{coltitle}.$sep;
95 foreach my $line ( @$lines ) {
96 my $x = $line->{loopcell};
97 print $line->{rowtitle}.$sep;
98 foreach my $cell (@$x) {
99 print $cell->{value}.$sep;
101 print $line->{totalrow};
106 $cols = @$results[0]->{loopfooter};
107 foreach my $col ( @$cols ) {
108 print $sep.$col->{totalcol};
110 print $sep.@$results[0]->{total};
115 my $dbh = C4::Context->dbh;
121 my @mime = ( C4::Context->preference("MIME") );
122 # foreach my $mime (@mime){
126 my $CGIextChoice=CGI::scrolling_list(
133 my @dels = ( C4::Context->preference("delimiter") );
134 my $CGIsepChoice=CGI::scrolling_list(
141 my $branches = getallbranches;
143 foreach my $thisbranch (keys %$branches) {
144 # my $selected = 1 if $thisbranch eq $branch;
145 my %row =(value => $thisbranch,
146 # selected => $selected,
147 branchname => $branches->{$thisbranch}->{'branchname'},
149 push @branchloop, \%row;
153 my $itemtypes = GetItemTypes;
155 foreach my $thisitemtype (keys %$itemtypes) {
156 # my $selected = 1 if $thisbranch eq $branch;
157 my %row =(value => $thisitemtype,
158 # selected => $selected,
159 description => $itemtypes->{$thisitemtype}->{'description'},
161 push @itemtypeloop, \%row;
165 my ($codes,$labels) = borrowercategories;
167 foreach my $thisborcat (sort keys %$labels) {
168 # my $selected = 1 if $thisbranch eq $branch;
169 my %row =(value => $thisborcat,
170 # selected => $selected,
171 description => $labels->{$thisborcat},
173 push @borcatloop, \%row;
179 CGIextChoice => $CGIextChoice,
180 CGIsepChoice => $CGIsepChoice,
181 branchloop =>\@branchloop,
182 itemtypeloop =>\@itemtypeloop,
183 borcatloop =>\@borcatloop,
185 output_html_with_http_headers $input, $cookie, $template->output;
192 my ($line, $column, $filters) = @_;
201 my $dbh = C4::Context->dbh;
207 for (my $i=0;$i<=6;$i++) {
209 if ( @$filters[$i] ) {
210 if (($i==1) and (@$filters[$i-1])) {
211 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
213 $cell{filter} .= @$filters[$i];
214 $cell{crit} .="Issue From" if ($i==0);
215 $cell{crit} .="Issue To" if ($i==1);
216 $cell{crit} .="Return From" if ($i==2);
217 $cell{crit} .="Return To" if ($i==3);
218 $cell{crit} .="Branch" if ($i==4);
219 $cell{crit} .="Doc Type" if ($i==5);
220 $cell{crit} .="Bor Cat" if ($i==6);
221 $cell{crit} .="Day" if ($i==7);
222 $cell{crit} .="Month" if ($i==8);
223 $cell{crit} .="Year" if ($i==9);
224 push @loopfilter, \%cell;
230 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
231 $column = "biblioitems.".$column if $column=~/itemtype/;
232 $column = "borrowers.".$column if $column=~/categorycode/;
234 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
235 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
236 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
237 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
238 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
239 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
240 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
241 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
242 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
243 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
244 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
245 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
248 if ($column eq "Day") {
250 $column = "issues.timestamp";
251 $colfield .="dayname($column)";
252 $colorder .="weekday($column)";
253 } elsif ($column eq "Month") {
255 $column = "issues.timestamp";
256 $colfield .="monthname($column)";
257 $colorder .="month($column)";
258 } elsif ($column eq "Year") {
260 $column = "issues.timestamp";
261 $colfield .="Year($column)";
262 $colorder .= $column;
264 $colfield .= $column;
265 $colorder .= $column;
269 $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";
270 if (($column=~/timestamp/) or ($column=~/returndate/)){
271 if ($colfilter[1] and ($colfilter[0])){
272 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
273 } elsif ($colfilter[1]) {
274 $strsth2 .= " and $column < '$colfilter[1]' " ;
275 } elsif ($colfilter[0]) {
276 $strsth2 .= " and $column > '$colfilter[0]' " ;
278 } elsif ($colfilter[0]) {
279 $colfilter[0] =~ s/\*/%/g;
280 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
282 $strsth2 .=" group by $colfield";
283 $strsth2 .=" order by $colorder";
286 my $sth2 = $dbh->prepare( $strsth2 );
289 while (my ($celvalue) = $sth2->fetchrow) {
291 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
292 push @loopcol, \%cell;
294 # warn "fin des titres colonnes";
301 #Initialization of cell values.....
305 for (my $i=1;$i<=$line;$i++) {
306 foreach my $col ( @loopcol ) {
307 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
308 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
313 # preparing calculation
316 # Processing average loanperiods
317 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
318 $strcalc .= " , $colfield " if ($colfield);
319 $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";
321 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
322 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
323 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
324 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
325 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
326 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
327 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
328 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
329 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
330 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
331 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
332 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
333 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
334 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
335 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
336 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
337 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
338 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
339 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
340 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
342 $strcalc .= " group by borrowers.borrowernumber";
343 $strcalc .= ", $colfield" if ($column);
344 $strcalc .= " order by RANK DESC";
345 $strcalc .= ",$colfield " if ($colfield);
348 # $max = $line*@loopcol;
349 # } else { $max=$line;}
350 # $strcalc .= " LIMIT 0,$max";
351 warn "SQL :". $strcalc;
353 my $dbcalc = $dbh->prepare($strcalc);
355 # warn "filling table";
358 while (my @data = $dbcalc->fetchrow) {
359 my ($row, $rank, $id, $col )=@data;
360 $col = "zzEMPTY" if ($col eq undef);
361 $indice{$col}=1 if (not($indice{$col}));
362 $table[$indice{$col}]->{$col}->{'name'}=$row;
363 $table[$indice{$col}]->{$col}->{'count'}=$rank;
364 $table[$indice{$col}]->{$col}->{'link'}=$id;
365 # warn " ".$i." ".$col. " ".$row;
369 push @loopcol,{coltitle => "Global"} if not($column);
371 for ($i=1; $i<=$line;$i++) {
374 #@loborrowersopcol ensures the order for columns is common with column titles
375 # and the number matches the number of columns
377 foreach my $col ( @loopcol ) {
378 # warn " colonne :$col->{coltitle}";
383 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
384 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
385 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
387 $value =$table[$i]->{"zzEMPTY"}->{'name'};
388 $count =$table[$i]->{"zzEMPTY"}->{'count'};
389 $link =$table[$i]->{"zzEMPTY"}->{'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;
403 # the header of the table
404 $globalline{loopfilter}=\@loopfilter;
405 # the core of the table
406 $globalline{looprow} = \@looprow;
407 $globalline{loopcol} = \@loopcol;
408 # # the foot (totals by borrower type)
409 $globalline{loopfooter} = \@loopfooter;
410 $globalline{total}= $grantotal;
411 $globalline{line} = $line;
412 $globalline{column} = $column;
413 push @mainloop,\%globalline;