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
25 use C4::Branch; # GetBranches
30 use C4::Dates qw/format_date format_date_in_iso/;
35 plugin that shows a stats on borrowers
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/cat_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 #warn "calcul : ".$calc;
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
62 flagsrequired => { reports => '*'},
65 our $sep = $input->param("sep");
66 $sep = "\t" if ($sep eq 'tabulation');
67 $template->param(do_it => $do_it,
68 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
72 my $results = calculate($limit, $column, \@filters);
73 if ($output eq "screen"){
74 # Printing results to screen
75 $template->param(mainloop => $results,
77 output_html_with_http_headers $input, $cookie, $template->output;
80 # Printing to a csv file
81 print $input->header(-type => 'application/vnd.sun.xml.calc',
83 -attachment=>"$basename.csv",
84 -filename=>"$basename.csv" );
85 my $cols = @$results[0]->{loopcol};
86 my $lines = @$results[0]->{looprow};
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 $CGIsepChoice=GetDelimiterChoices;
136 my $itemtypes = GetItemTypes;
138 foreach my $thisitemtype ( sort {$itemtypes->{$a}->{'description'} cmp $itemtypes->{$b}->{'description'}} keys %$itemtypes) {
139 my %row =(value => $thisitemtype,
140 description => $itemtypes->{$thisitemtype}->{'description'},
142 push @itemtypeloop, \%row;
146 my ($codes,$labels) = GetborCatFromCatType(undef,undef);
148 foreach my $thisborcat (sort {$labels->{$a} cmp $labels->{$b}} keys %$labels) {
149 my %row =(value => $thisborcat,
150 description => $labels->{$thisborcat},
152 push @borcatloop, \%row;
158 CGIextChoice => $CGIextChoice,
159 CGIsepChoice => $CGIsepChoice,
160 branchloop => GetBranchesLoop(C4::Context->userenv->{'branch'}),
161 itemtypeloop =>\@itemtypeloop,
162 borcatloop =>\@borcatloop,
164 output_html_with_http_headers $input, $cookie, $template->output;
171 my ($line, $column, $filters) = @_;
180 my $dbh = C4::Context->dbh;
186 for (my $i=0;$i<=6;$i++) {
188 if ( @$filters[$i] ) {
189 if (($i==1) and (@$filters[$i-1])) {
190 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
192 # format the dates filters, otherwise just fill as is
194 $cell{filter} .= @$filters[$i];
196 $cell{filter} .= format_date(@$filters[$i]);
197 } $cell{crit} .="Issue From" if ($i==0);
198 $cell{crit} .="Issue To" if ($i==1);
199 $cell{crit} .="Return From" if ($i==2);
200 $cell{crit} .="Return To" if ($i==3);
201 $cell{crit} .="Branch" if ($i==4);
202 $cell{crit} .="Doc Type" if ($i==5);
203 $cell{crit} .="Bor Cat" if ($i==6);
204 $cell{crit} .="Day" if ($i==7);
205 $cell{crit} .="Month" if ($i==8);
206 $cell{crit} .="Year" if ($i==9);
207 push @loopfilter, \%cell;
213 $column = "old_issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
214 if($column=~/itemtype/){
215 $column = C4::Context->preference('item-level_itypes') ? "items.itype": "biblioitems.itemtype";
217 $column = "borrowers.".$column if $column=~/categorycode/;
219 $colfilter[0] = @$filters[0] if ($column =~ /timestamp/ ) ;
220 $colfilter[1] = @$filters[1] if ($column =~ /timestamp/ ) ;
221 $colfilter[0] = @$filters[2] if ($column =~ /returndate/ ) ;
222 $colfilter[1] = @$filters[3] if ($column =~ /returndate/ ) ;
223 $colfilter[0] = @$filters[4] if ($column =~ /branch/ ) ;
224 $colfilter[0] = @$filters[5] if ($column =~ /itemtype/ ) ;
225 $colfilter[0] = @$filters[6] if ($column =~ /category/ ) ;
226 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
227 $colfilter[0] = @$filters[7] if ($column =~ /timestamp/ ) ;
228 $colfilter[0] = @$filters[8] if ($column =~ /timestamp/ ) ;
229 $colfilter[0] = @$filters[9] if ($column =~ /timestamp/ ) ;
230 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
233 if ($column eq "Day") {
235 $column = "old_issues.timestamp";
236 $colfield .="dayname($column)";
237 $colorder .="weekday($column)";
238 } elsif ($column eq "Month") {
240 $column = "old_issues.timestamp";
241 $colfield .="monthname($column)";
242 $colorder .="month($column)";
243 } elsif ($column eq "Year") {
245 $column = "old_issues.timestamp";
246 $colfield .="Year($column)";
247 $colorder .= $column;
249 $colfield .= $column;
250 $colorder .= $column;
254 $strsth2 .= "SELECT distinctrow $colfield
256 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
257 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
258 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
260 if (($column=~/timestamp/) or ($column=~/returndate/)){
261 if ($colfilter[1] and ($colfilter[0])){
262 $strsth2 .= " and $column between '$colfilter[0]' and '$colfilter[1]' " ;
263 } elsif ($colfilter[1]) {
264 $strsth2 .= " and $column < '$colfilter[1]' " ;
265 } elsif ($colfilter[0]) {
266 $strsth2 .= " and $column > '$colfilter[0]' " ;
268 } elsif ($colfilter[0]) {
269 $colfilter[0] =~ s/\*/%/g;
270 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
272 $strsth2 .=" group by $colfield";
273 $strsth2 .=" order by $colorder";
276 my $sth2 = $dbh->prepare( $strsth2 );
277 if (( @colfilter ) and ($colfilter[1])){
278 $sth2->execute("'".$colfilter[0]."'","'".$colfilter[1]."'");
279 } elsif ($colfilter[0]) {
280 $sth2->execute($colfilter[0]);
286 while (my ($celvalue) = $sth2->fetchrow) {
288 $cell{coltitle} = ($celvalue?$celvalue:"NULL");
289 push @loopcol, \%cell;
291 # warn "fin des titres colonnes";
298 #Initialization of cell values.....
302 for (my $i=1;$i<=$line;$i++) {
303 foreach my $col ( @loopcol ) {
304 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
305 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"total"}->{'name'}=0;
310 # preparing calculation
313 # Processing average loanperiods
314 $strcalc .= "SELECT DISTINCT biblio.title, COUNT(biblio.biblionumber) AS RANK, biblio.biblionumber AS ID";
315 $strcalc .= " , $colfield " if ($colfield);
316 $strcalc .= " FROM `old_issues`
317 LEFT JOIN items USING(itemnumber)
318 LEFT JOIN biblio USING(biblionumber)
319 LEFT JOIN biblioitems USING(biblionumber)
320 LEFT JOIN borrowers USING(borrowernumber)
323 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
324 $strcalc .= " AND old_issues.timestamp > '" . @$filters[0] ."'" if ( @$filters[0] );
325 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
326 $strcalc .= " AND old_issues.timestamp < '" . @$filters[1] ."'" if ( @$filters[1] );
327 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
328 $strcalc .= " AND old_issues.returndate > '" . @$filters[2] ."'" if ( @$filters[2] );
329 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
330 $strcalc .= " AND old_issues.returndate < '" . @$filters[3] ."'" if ( @$filters[3] );
331 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
332 $strcalc .= " AND old_issues.branchcode like '" . @$filters[4] ."'" if ( @$filters[4] );
333 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
335 if(C4::Context->preference('item-level_itypes') ){
336 $strcalc .= " AND items.itype like "
338 $strcalc .= " AND biblioitems.itemtype like "
340 $strcalc .= "'" . @$filters[5] ."'" ;
342 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
343 $strcalc .= " AND borrowers.categorycode like '" . @$filters[6] ."'" if ( @$filters[6] );
344 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
345 $strcalc .= " AND dayname(old_issues.timestamp) like '" . @$filters[7]."'" if (@$filters[7]);
346 @$filters[8]=~ s/\*/%/g if (@$filters[8]);
347 $strcalc .= " AND monthname(old_issues.timestamp) like '" . @$filters[8]."'" if (@$filters[8]);
348 @$filters[9]=~ s/\*/%/g if (@$filters[9]);
349 $strcalc .= " AND year(old_issues.timestamp) like '" . @$filters[9] ."'" if ( @$filters[9] );
351 $strcalc .= " group by biblio.biblionumber";
352 $strcalc .= ", $colfield" if ($column);
353 $strcalc .= " order by RANK DESC";
354 $strcalc .= ", $colfield " if ($colfield);
356 my $dbcalc = $dbh->prepare($strcalc);
360 while (my @data = $dbcalc->fetchrow) {
361 my ($row, $rank, $id, $col )=@data;
362 $col = "zzEMPTY" if (!defined($col));
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;
370 push @loopcol,{coltitle => "Global"} if not($column);
372 for ($i=1; $i<=$line;$i++) {
374 #@loopcol ensures the order for columns is common with column titles
375 # and the number matches the number of columns
377 foreach my $col ( @loopcol ) {
382 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'name'};
383 $count =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'count'};
384 $link =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'link'};
386 $value =$table[$i]->{"zzEMPTY"}->{'name'};
387 $count =$table[$i]->{"zzEMPTY"}->{'count'};
388 $link =$table[$i]->{"zzEMPTY"}->{'link'};
390 push @loopcell, {value => $value, count =>$count, reference => $link} ;
392 #my $total = $table[$i]->{totalrow}/$colcount if ($colcount>0);
393 push @looprow,{ 'rowtitle' => $i ,
394 'loopcell' => \@loopcell,
395 'hilighted' => ($hilighted >0),
397 $hilighted = -$hilighted;
402 # the header of the table
403 $globalline{loopfilter}=\@loopfilter;
404 # the core of the table
405 $globalline{looprow} = \@looprow;
406 $globalline{loopcol} = \@loopcol;
407 # # the foot (totals by borrower type)
408 $globalline{loopfooter} = \@loopfooter;
409 $globalline{total}= $grantotal;
410 $globalline{line} = $line;
411 $globalline{column} = $column;
412 push @mainloop,\%globalline;