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
26 use C4::Branch; # GetBranches
34 plugin that shows a stats on borrowers
43 my $do_it=$input->param('do_it');
44 my $fullreportname = "reports/bor_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 $filters[2]=format_date_in_iso($filters[2]);
51 $filters[3]=format_date_in_iso($filters[3]);
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 => {reports => 1},
65 $template->param(do_it => $do_it,
66 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
70 my $results = calculate($limit, $column, \@filters);
71 if ($output eq "screen"){
72 # Printing results to screen
73 $template->param(mainloop => $results, limit=>$limit);
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){
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 (sort keys %$branches) {
142 my %row =(value => $thisbranch,
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) = GetborCatFromCatType(undef,undef);
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 # format the dates filters, otherwise just fill as is
211 $cell{filter} .= @$filters[$i];
213 $cell{filter} .= format_date(@$filters[$i]);
215 $cell{crit} .="Issue From" if ($i==0);
216 $cell{crit} .="Issue To" if ($i==1);
217 $cell{crit} .="Return From" if ($i==2);
218 $cell{crit} .="Return To" if ($i==3);
219 $cell{crit} .="Branch" if ($i==4);
220 $cell{crit} .="Doc Type" if ($i==5);
221 $cell{crit} .="Bor Cat" if ($i==6);
222 $cell{crit} .="Day" if ($i==7);
223 $cell{crit} .="Month" if ($i==8);
224 $cell{crit} .="Year" if ($i==9);
225 push @loopfilter, \%cell;
231 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
232 $column = "biblioitems.".$column if $column=~/itemtype/;
233 $column = "borrowers.".$column if $column=~/categorycode/;
235 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
236 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
237 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
238 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
239 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
240 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
241 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
242 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
243 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
244 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
246 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
249 if ($column eq "Day") {
251 $column = "issues.timestamp";
252 $colfield .="dayname($column)";
253 $colorder .="weekday($column)";
254 } elsif ($column eq "Month") {
256 $column = "issues.timestamp";
257 $colfield .="monthname($column)";
258 $colorder .="month($column)";
259 } elsif ($column eq "Year") {
261 $column = "issues.timestamp";
262 $colfield .="Year($column)";
263 $colorder .= $column;
265 $colfield .= $column;
266 $colorder .= $column;
270 $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";
271 if (($column=~/timestamp/) or ($column=~/returndate/)){
272 if ($colfilter[1] and ($colfilter[0])){
273 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
274 } elsif ($colfilter[1]) {
275 $strsth2 .= " and $column < '$colfilter[1]' " ;
276 } elsif ($colfilter[0]) {
277 $strsth2 .= " and $column > '$colfilter[0]' " ;
279 } elsif ($colfilter[0]) {
280 $colfilter[0] =~ s/\*/%/g;
281 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
283 $strsth2 .=" group by $colfield";
284 $strsth2 .=" order by $colorder";
287 my $sth2 = $dbh->prepare( $strsth2 );
290 while (my ($celvalue) = $sth2->fetchrow) {
292 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
293 push @loopcol, \%cell;
295 # warn "fin des titres colonnes";
302 #Initialization of cell values.....
306 for (my $i=1;$i<=$line;$i++) {
307 foreach my $col ( @loopcol ) {
308 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
309 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
314 # preparing calculation
317 # Processing average loanperiods
318 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
319 $strcalc .= " , $colfield " if ($colfield);
320 $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";
322 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
323 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
324 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
325 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
326 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
327 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
328 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
329 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
330 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
331 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
332 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
333 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
334 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
335 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
336 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
337 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
338 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
339 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
340 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
341 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
343 $strcalc .= " group by borrowers.borrowernumber";
344 $strcalc .= ", $colfield" if ($column);
345 $strcalc .= " order by RANK DESC";
346 $strcalc .= ",$colfield " if ($colfield);
349 # $max = $line*@loopcol;
350 # } else { $max=$line;}
351 # $strcalc .= " LIMIT 0,$max";
352 warn "SQL :". $strcalc;
354 my $dbcalc = $dbh->prepare($strcalc);
356 # warn "filling table";
359 while (my @data = $dbcalc->fetchrow) {
360 my ($row, $rank, $id, $col )=@data;
361 $col = "zzEMPTY" if ($col eq undef);
362 $indice{$col}=1 if (not($indice{$col}));
363 $table[$indice{$col}]->{$col}->{'name'}=$row;
364 $table[$indice{$col}]->{$col}->{'count'}=$rank;
365 $table[$indice{$col}]->{$col}->{'link'}=$id;
366 # warn " ".$i." ".$col. " ".$row;
370 push @loopcol,{coltitle => "Global"} if not($column);
372 for ($i=1; $i<=$line;$i++) {
375 #@loborrowersopcol ensures the order for columns is common with column titles
376 # and the number matches the number of columns
378 foreach my $col ( @loopcol ) {
379 # warn " colonne :$col->{coltitle}";
384 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
385 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
386 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
388 $value =$table[$i]->{"zzEMPTY"}->{'name'};
389 $count =$table[$i]->{"zzEMPTY"}->{'count'};
390 $link =$table[$i]->{"zzEMPTY"}->{'link'};
392 # warn " ".$i ." value:$value count:$count reference:$link";
393 push @loopcell, {value => $value, count =>$count, reference => $link} ;
395 push @looprow,{ 'rowtitle' => $i ,
396 'loopcell' => \@loopcell,
397 'hilighted' => ($hilighted >0),
399 $hilighted = -$hilighted;
404 # the header of the table
405 $globalline{loopfilter}=\@loopfilter;
406 # the core of the table
407 $globalline{looprow} = \@looprow;
408 $globalline{loopcol} = \@loopcol;
409 # # the foot (totals by borrower type)
410 $globalline{loopfooter} = \@loopfooter;
411 $globalline{total}= $grantotal;
412 $globalline{line} = $line;
413 $globalline{column} = $column;
414 push @mainloop,\%globalline;