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;
36 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/bor_issues_top.tmpl";
48 my $limit = $input->param("Limit");
49 my $column = $input->param("Criteria");
50 my @filters = $input->param("Filter");
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 => {editcatalogue => 1},
64 $template->param(do_it => $do_it);
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results, limit=>$limit);
71 output_html_with_http_headers $input, $cookie, $template->output;
74 # Printing to a csv file
75 print $input->header(-type => 'application/vnd.sun.xml.calc',
76 -attachment=>"$basename.csv",
77 -filename=>"$basename.csv" );
78 my $cols = @$results[0]->{loopcol};
79 my $lines = @$results[0]->{looprow};
81 $sep =C4::Context->preference("delimiter");
83 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
85 foreach my $col ( @$cols ) {
86 print $col->{coltitle}.$sep;
90 foreach my $line ( @$lines ) {
91 my $x = $line->{loopcell};
92 print $line->{rowtitle}.$sep;
93 foreach my $cell (@$x) {
94 print $cell->{value}.$sep;
96 print $line->{totalrow};
101 $cols = @$results[0]->{loopfooter};
102 foreach my $col ( @$cols ) {
103 print $sep.$col->{totalcol};
105 print $sep.@$results[0]->{total};
110 my $dbh = C4::Context->dbh;
116 my @mime = ( C4::Context->preference("MIME") );
117 # foreach my $mime (@mime){
121 my $CGIextChoice=CGI::scrolling_list(
128 my @dels = ( C4::Context->preference("delimiter") );
129 my $CGIsepChoice=CGI::scrolling_list(
137 CGIextChoice => $CGIextChoice,
138 CGIsepChoice => $CGIsepChoice
140 output_html_with_http_headers $input, $cookie, $template->output;
147 my ($line, $column, $filters) = @_;
156 my $dbh = C4::Context->dbh;
162 for (my $i=0;$i<=6;$i++) {
164 if ( @$filters[$i] ) {
165 if (($i==1) and (@$filters[$i-1])) {
166 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
168 $cell{filter} .= @$filters[$i];
169 $cell{crit} .="Issue From" if ($i==0);
170 $cell{crit} .="Issue To" if ($i==1);
171 $cell{crit} .="Return From" if ($i==2);
172 $cell{crit} .="Return To" if ($i==3);
173 $cell{crit} .="Branch" if ($i==4);
174 $cell{crit} .="Doc Type" if ($i==5);
175 $cell{crit} .="Bor Cat" if ($i==6);
176 $cell{crit} .="Day" if ($i==7);
177 $cell{crit} .="Month" if ($i==8);
178 $cell{crit} .="Year" if ($i==9);
179 push @loopfilter, \%cell;
185 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
186 $column = "biblioitems.".$column if $column=~/itemtype/;
187 $column = "borrowers.".$column if $column=~/categorycode/;
189 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
190 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
191 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
192 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
193 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
194 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
195 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
196 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
197 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
198 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
199 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
200 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
203 if ($column eq "Day") {
205 $column = "issues.timestamp";
206 $colfield .="dayname($column)";
207 $colorder .="weekday($column)";
208 } elsif ($column eq "Month") {
210 $column = "issues.timestamp";
211 $colfield .="monthname($column)";
212 $colorder .="month($column)";
213 } elsif ($column eq "Year") {
215 $column = "issues.timestamp";
216 $colfield .="Year($column)";
217 $colorder .= $column;
219 $colfield .= $column;
220 $colorder .= $column;
224 $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";
225 if (($column=~/timestamp/) or ($column=~/returndate/)){
226 if ($colfilter[1] and ($colfilter[0])){
227 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
228 } elsif ($colfilter[1]) {
229 $strsth2 .= " and $column < '$colfilter[1]' " ;
230 } elsif ($colfilter[0]) {
231 $strsth2 .= " and $column > '$colfilter[0]' " ;
233 } elsif ($colfilter[0]) {
234 $colfilter[0] =~ s/\*/%/g;
235 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
237 $strsth2 .=" group by $colfield";
238 $strsth2 .=" order by $colorder";
241 my $sth2 = $dbh->prepare( $strsth2 );
244 while (my ($celvalue) = $sth2->fetchrow) {
246 $cell{coltitle} = $celvalue;
247 push @loopcol, \%cell;
249 # warn "fin des titres colonnes";
256 #Initialization of cell values.....
260 for (my $i=1;$i<=$line;$i++) {
261 foreach my $col ( @loopcol ) {
262 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
263 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
268 # preparing calculation
271 # Processing average loanperiods
272 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
273 $strcalc .= " , $colfield " if ($colfield);
274 $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";
276 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
277 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
278 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
279 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
280 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
281 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
282 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
283 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
284 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
285 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
286 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
287 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
288 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
289 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
290 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
291 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
292 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
293 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
294 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
295 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
297 $strcalc .= " group by borrowers.borrowernumber";
298 $strcalc .= ", $colfield" if ($column);
299 $strcalc .= " order by ";
300 $strcalc .= "$colfield, " if ($colfield);
301 $strcalc .= "RANK DESC ";
304 $max = $line*@loopcol;
305 } else { $max=$line;}
306 $strcalc .= " LIMIT 0,$max";
307 warn "SQL :". $strcalc;
309 my $dbcalc = $dbh->prepare($strcalc);
311 # warn "filling table";
314 while (my @data = $dbcalc->fetchrow) {
315 my ($row, $rank, $id, $col )=@data;
316 $col = "zzEMPTY" if ($col eq undef);
317 $i=1 if (($previous_col) and not($col eq $previous_col));
318 $table[$i]->{$col}->{'name'}=$row;
319 $table[$i]->{$col}->{'count'}=$rank;
320 $table[$i]->{$col}->{'link'}=$id;
321 warn " ".$i." ".$col. " ".$row;
326 push @loopcol,{coltitle => "Global"} if not($column);
328 for ($i=1; $i<=$line;$i++) {
331 #@loopcol ensures the order for columns is common with column titles
332 # and the number matches the number of columns
334 foreach my $col ( @loopcol ) {
335 # warn " colonne :$col->{coltitle}";
340 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
341 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
342 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
344 $value =$table[$i]->{"zzEMPTY"}->{'name'};
345 $count =$table[$i]->{"zzEMPTY"}->{'count'};
346 $link =$table[$i]->{"zzEMPTY"}->{'link'};
348 # warn " ".$i ." value:$value count:$count reference:$link";
349 push @loopcell, {value => $value, count =>$count, reference => $link} ;
351 push @looprow,{ 'rowtitle' => $i ,
352 'loopcell' => \@loopcell,
353 'hilighted' => ($hilighted >0),
355 $hilighted = -$hilighted;
360 # the header of the table
361 $globalline{loopfilter}=\@loopfilter;
362 # the core of the table
363 $globalline{looprow} = \@looprow;
364 $globalline{loopcol} = \@loopcol;
365 # # the foot (totals by borrower type)
366 $globalline{loopfooter} = \@loopfooter;
367 $globalline{total}= $grantotal;
368 $globalline{line} = $line;
369 $globalline{column} = $column;
370 push @mainloop,\%globalline;