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);
68 my $results = calculate($limit, $column, \@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop => $results, limit=>$limit);
72 output_html_with_http_headers $input, $cookie, $template->output;
75 # Printing to a csv file
76 print $input->header(-type => 'application/vnd.sun.xml.calc',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
82 $sep =C4::Context->preference("delimiter");
84 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
111 my $dbh = C4::Context->dbh;
117 my @mime = ( C4::Context->preference("MIME") );
118 # foreach my $mime (@mime){
122 my $CGIextChoice=CGI::scrolling_list(
129 my @dels = ( C4::Context->preference("delimiter") );
130 my $CGIsepChoice=CGI::scrolling_list(
137 my $branches = getallbranches;
139 foreach my $thisbranch (keys %$branches) {
140 # my $selected = 1 if $thisbranch eq $branch;
141 my %row =(value => $thisbranch,
142 # selected => $selected,
143 branchname => $branches->{$thisbranch}->{'branchname'},
145 push @branchloop, \%row;
149 my $itemtypes = getitemtypes;
151 foreach my $thisitemtype (keys %$itemtypes) {
152 # my $selected = 1 if $thisbranch eq $branch;
153 my %row =(value => $thisitemtype,
154 # selected => $selected,
155 description => $itemtypes->{$thisitemtype}->{'description'},
157 push @itemtypeloop, \%row;
161 my ($codes,$labels) = borrowercategories;
163 foreach my $thisborcat (sort keys %$labels) {
164 # my $selected = 1 if $thisbranch eq $branch;
165 my %row =(value => $thisborcat,
166 # selected => $selected,
167 description => $labels->{$thisborcat},
169 push @borcatloop, \%row;
175 CGIextChoice => $CGIextChoice,
176 CGIsepChoice => $CGIsepChoice,
177 branchloop =>\@branchloop,
178 itemtypeloop =>\@itemtypeloop,
179 borcatloop =>\@borcatloop,
181 output_html_with_http_headers $input, $cookie, $template->output;
188 my ($line, $column, $filters) = @_;
197 my $dbh = C4::Context->dbh;
203 for (my $i=0;$i<=6;$i++) {
205 if ( @$filters[$i] ) {
206 if (($i==1) and (@$filters[$i-1])) {
207 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
209 $cell{filter} .= @$filters[$i];
210 $cell{crit} .="Issue From" if ($i==0);
211 $cell{crit} .="Issue To" if ($i==1);
212 $cell{crit} .="Return From" if ($i==2);
213 $cell{crit} .="Return To" if ($i==3);
214 $cell{crit} .="Branch" if ($i==4);
215 $cell{crit} .="Doc Type" if ($i==5);
216 $cell{crit} .="Bor Cat" if ($i==6);
217 $cell{crit} .="Day" if ($i==7);
218 $cell{crit} .="Month" if ($i==8);
219 $cell{crit} .="Year" if ($i==9);
220 push @loopfilter, \%cell;
226 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
227 $column = "biblioitems.".$column if $column=~/itemtype/;
228 $column = "borrowers.".$column if $column=~/categorycode/;
230 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
231 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
232 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
233 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
234 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
235 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
236 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
237 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
238 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
239 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
240 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
241 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
244 if ($column eq "Day") {
246 $column = "issues.timestamp";
247 $colfield .="dayname($column)";
248 $colorder .="weekday($column)";
249 } elsif ($column eq "Month") {
251 $column = "issues.timestamp";
252 $colfield .="monthname($column)";
253 $colorder .="month($column)";
254 } elsif ($column eq "Year") {
256 $column = "issues.timestamp";
257 $colfield .="Year($column)";
258 $colorder .= $column;
260 $colfield .= $column;
261 $colorder .= $column;
265 $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";
266 if (($column=~/timestamp/) or ($column=~/returndate/)){
267 if ($colfilter[1] and ($colfilter[0])){
268 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
269 } elsif ($colfilter[1]) {
270 $strsth2 .= " and $column < '$colfilter[1]' " ;
271 } elsif ($colfilter[0]) {
272 $strsth2 .= " and $column > '$colfilter[0]' " ;
274 } elsif ($colfilter[0]) {
275 $colfilter[0] =~ s/\*/%/g;
276 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
278 $strsth2 .=" group by $colfield";
279 $strsth2 .=" order by $colorder";
282 my $sth2 = $dbh->prepare( $strsth2 );
285 while (my ($celvalue) = $sth2->fetchrow) {
287 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
288 push @loopcol, \%cell;
290 # warn "fin des titres colonnes";
297 #Initialization of cell values.....
301 for (my $i=1;$i<=$line;$i++) {
302 foreach my $col ( @loopcol ) {
303 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
304 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
309 # preparing calculation
312 # Processing average loanperiods
313 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
314 $strcalc .= " , $colfield " if ($colfield);
315 $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";
317 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
318 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
319 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
320 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
321 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
322 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
323 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
324 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
325 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
326 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
327 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
328 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
329 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
330 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
331 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
332 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
333 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
334 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
335 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
336 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
338 $strcalc .= " group by borrowers.borrowernumber";
339 $strcalc .= ", $colfield" if ($column);
340 $strcalc .= " order by RANK DESC";
341 $strcalc .= ",$colfield " if ($colfield);
344 # $max = $line*@loopcol;
345 # } else { $max=$line;}
346 # $strcalc .= " LIMIT 0,$max";
347 warn "SQL :". $strcalc;
349 my $dbcalc = $dbh->prepare($strcalc);
351 # warn "filling table";
354 while (my @data = $dbcalc->fetchrow) {
355 my ($row, $rank, $id, $col )=@data;
356 $col = "zzEMPTY" if ($col eq undef);
357 $indice{$col}=1 if (not($indice{$col}));
358 $table[$indice{$col}]->{$col}->{'name'}=$row;
359 $table[$indice{$col}]->{$col}->{'count'}=$rank;
360 $table[$indice{$col}]->{$col}->{'link'}=$id;
361 # warn " ".$i." ".$col. " ".$row;
365 push @loopcol,{coltitle => "Global"} if not($column);
367 for ($i=1; $i<=$line;$i++) {
370 #@loborrowersopcol ensures the order for columns is common with column titles
371 # and the number matches the number of columns
373 foreach my $col ( @loopcol ) {
374 # warn " colonne :$col->{coltitle}";
379 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
380 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
381 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
383 $value =$table[$i]->{"zzEMPTY"}->{'name'};
384 $count =$table[$i]->{"zzEMPTY"}->{'count'};
385 $link =$table[$i]->{"zzEMPTY"}->{'link'};
387 # warn " ".$i ." value:$value count:$count reference:$link";
388 push @loopcell, {value => $value, count =>$count, reference => $link} ;
390 push @looprow,{ 'rowtitle' => $i ,
391 'loopcell' => \@loopcell,
392 'hilighted' => ($hilighted >0),
394 $hilighted = -$hilighted;
399 # the header of the table
400 $globalline{loopfilter}=\@loopfilter;
401 # the core of the table
402 $globalline{looprow} = \@looprow;
403 $globalline{loopcol} = \@loopcol;
404 # # the foot (totals by borrower type)
405 $globalline{loopfooter} = \@loopfooter;
406 $globalline{total}= $grantotal;
407 $globalline{line} = $line;
408 $globalline{column} = $column;
409 push @mainloop,\%globalline;