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
27 use C4::Branch; # GetBranches
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/bor_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 $filters[2]=format_date_in_iso($filters[2]);
52 $filters[3]=format_date_in_iso($filters[3]);
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 my $mime = $input->param("MIME");
56 my $del = $input->param("sep");
57 #warn "calcul : ".$calc;
58 my ($template, $borrowernumber, $cookie)
59 = get_template_and_user({template_name => $fullreportname,
63 flagsrequired => {reports => 1},
66 $template->param(do_it => $do_it,
67 DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results, limit=>$limit);
75 output_html_with_http_headers $input, $cookie, $template->output;
78 # Printing to a csv file
79 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){
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 (sort keys %$branches) {
143 my %row =(value => $thisbranch,
144 branchname => $branches->{$thisbranch}->{'branchname'},
146 push @branchloop, \%row;
150 my $itemtypes = GetItemTypes;
152 foreach my $thisitemtype (keys %$itemtypes) {
153 # my $selected = 1 if $thisbranch eq $branch;
154 my %row =(value => $thisitemtype,
155 # selected => $selected,
156 description => $itemtypes->{$thisitemtype}->{'description'},
158 push @itemtypeloop, \%row;
162 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
164 foreach my $thisborcat (sort keys %$labels) {
165 # my $selected = 1 if $thisbranch eq $branch;
166 my %row =(value => $thisborcat,
167 # selected => $selected,
168 description => $labels->{$thisborcat},
170 push @borcatloop, \%row;
176 CGIextChoice => $CGIextChoice,
177 CGIsepChoice => $CGIsepChoice,
178 branchloop =>\@branchloop,
179 itemtypeloop =>\@itemtypeloop,
180 borcatloop =>\@borcatloop,
182 output_html_with_http_headers $input, $cookie, $template->output;
189 my ($line, $column, $filters) = @_;
198 my $dbh = C4::Context->dbh;
204 for (my $i=0;$i<=6;$i++) {
206 if ( @$filters[$i] ) {
207 if (($i==1) and (@$filters[$i-1])) {
208 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
210 # format the dates filters, otherwise just fill as is
212 $cell{filter} .= @$filters[$i];
214 $cell{filter} .= format_date(@$filters[$i]);
216 $cell{crit} .="Issue From" if ($i==0);
217 $cell{crit} .="Issue To" if ($i==1);
218 $cell{crit} .="Return From" if ($i==2);
219 $cell{crit} .="Return To" if ($i==3);
220 $cell{crit} .="Branch" if ($i==4);
221 $cell{crit} .="Doc Type" if ($i==5);
222 $cell{crit} .="Bor Cat" if ($i==6);
223 $cell{crit} .="Day" if ($i==7);
224 $cell{crit} .="Month" if ($i==8);
225 $cell{crit} .="Year" if ($i==9);
226 push @loopfilter, \%cell;
232 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
233 $column = "biblioitems.".$column if $column=~/itemtype/;
234 $column = "borrowers.".$column if $column=~/categorycode/;
236 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
237 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
238 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
239 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
240 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
241 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
242 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
243 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
244 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
245 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
246 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
247 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
250 if ($column eq "Day") {
252 $column = "issues.timestamp";
253 $colfield .="dayname($column)";
254 $colorder .="weekday($column)";
255 } elsif ($column eq "Month") {
257 $column = "issues.timestamp";
258 $colfield .="monthname($column)";
259 $colorder .="month($column)";
260 } elsif ($column eq "Year") {
262 $column = "issues.timestamp";
263 $colfield .="Year($column)";
264 $colorder .= $column;
266 $colfield .= $column;
267 $colorder .= $column;
271 $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";
272 if (($column=~/timestamp/) or ($column=~/returndate/)){
273 if ($colfilter[1] and ($colfilter[0])){
274 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
275 } elsif ($colfilter[1]) {
276 $strsth2 .= " and $column < '$colfilter[1]' " ;
277 } elsif ($colfilter[0]) {
278 $strsth2 .= " and $column > '$colfilter[0]' " ;
280 } elsif ($colfilter[0]) {
281 $colfilter[0] =~ s/\*/%/g;
282 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
284 $strsth2 .=" group by $colfield";
285 $strsth2 .=" order by $colorder";
288 my $sth2 = $dbh->prepare( $strsth2 );
291 while (my ($celvalue) = $sth2->fetchrow) {
293 $cell{'coltitle'} = ($celvalue?$celvalue:"NULL");
294 push @loopcol, \%cell;
296 # warn "fin des titres colonnes";
303 #Initialization of cell values.....
307 for (my $i=1;$i<=$line;$i++) {
308 foreach my $col ( @loopcol ) {
309 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
310 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
315 # preparing calculation
318 # Processing average loanperiods
319 $strcalc .= "SELECT CONCAT(borrowers.surname , \"\\t\",borrowers.firstname), COUNT(*) AS RANK, borrowers.borrowernumber AS ID";
320 $strcalc .= " , $colfield " if ($colfield);
321 $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";
323 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
324 $strcalc .= " AND issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
325 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
326 $strcalc .= " AND issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
327 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
328 $strcalc .= " AND issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
329 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
330 $strcalc .= " AND issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
331 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
332 $strcalc .= " AND issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
333 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
334 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[5] ."'" if ( @$filters[5] );
335 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
336 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
337 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
338 $strcalc .= " AND dayname(issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
339 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
340 $strcalc .= " AND monthname(issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
341 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
342 $strcalc .= " AND year(issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
344 $strcalc .= " group by borrowers.borrowernumber";
345 $strcalc .= ", $colfield" if ($column);
346 $strcalc .= " order by RANK DESC";
347 $strcalc .= ",$colfield " if ($colfield);
350 # $max = $line*@loopcol;
351 # } else { $max=$line;}
352 # $strcalc .= " LIMIT 0,$max";
353 warn "SQL :". $strcalc;
355 my $dbcalc = $dbh->prepare($strcalc);
357 # warn "filling table";
360 while (my @data = $dbcalc->fetchrow) {
361 my ($row, $rank, $id, $col )=@data;
362 $col = "zzEMPTY" if ($col eq undef);
363 $indice{$col}=1 if (not($indice{$col}));
364 $table[$indice{$col}]->{$col}->{'name'}=$row;
365 $table[$indice{$col}]->{$col}->{'count'}=$rank;
366 $table[$indice{$col}]->{$col}->{'link'}=$id;
367 # warn " ".$i." ".$col. " ".$row;
371 push @loopcol,{coltitle => "Global"} if not($column);
373 for ($i=1; $i<=$line;$i++) {
376 #@loborrowersopcol ensures the order for columns is common with column titles
377 # and the number matches the number of columns
379 foreach my $col ( @loopcol ) {
380 # warn " colonne :$col->{coltitle}";
385 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
386 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
387 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
389 $value =$table[$i]->{"zzEMPTY"}->{'name'};
390 $count =$table[$i]->{"zzEMPTY"}->{'count'};
391 $link =$table[$i]->{"zzEMPTY"}->{'link'};
393 # warn " ".$i ." value:$value count:$count reference:$link";
394 push @loopcell, {value => $value, count =>$count, reference => $link} ;
396 push @looprow,{ 'rowtitle' => $i ,
397 'loopcell' => \@loopcell,
398 'hilighted' => ($hilighted >0),
400 $hilighted = -$hilighted;
405 # the header of the table
406 $globalline{loopfilter}=\@loopfilter;
407 # the core of the table
408 $globalline{looprow} = \@looprow;
409 $globalline{loopcol} = \@loopcol;
410 # # the foot (totals by borrower type)
411 $globalline{loopfooter} = \@loopfooter;
412 $globalline{total}= $grantotal;
413 $globalline{line} = $line;
414 $globalline{column} = $column;
415 push @mainloop,\%globalline;