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
33 plugin that shows a stats on borrowers
42 my $do_it=$input->param('do_it');
43 my $fullreportname = "reports/catalogue_out.tmpl";
44 my $limit = $input->param("Limit");
45 my $column = $input->param("Criteria");
46 my @filters = $input->param("Filter");
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 my $mime = $input->param("MIME");
50 my $del = $input->param("sep");
51 #warn "calcul : ".$calc;
52 my ($template, $borrowernumber, $cookie)
53 = get_template_and_user
({template_name
=> $fullreportname,
57 flagsrequired
=> {reports
=> 1},
60 $template->param(do_it
=> $do_it,
64 my $results = calculate
($limit, $column, \
@filters);
65 if ($output eq "screen"){
66 # Printing results to screen
67 $template->param(mainloop
=> $results);
68 output_html_with_http_headers
$input, $cookie, $template->output;
71 # Printing to a csv file
72 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
74 -attachment
=>"$basename.csv",
75 -filename
=>"$basename.csv" );
76 my $cols = @
$results[0]->{loopcol
};
77 my $lines = @
$results[0]->{looprow
};
79 $sep =C4
::Context
->preference("delimiter");
81 print "num /". @
$results[0]->{column
} .$sep;
83 foreach my $col ( @
$cols ) {
84 print $col->{coltitle
}.$sep;
88 foreach my $line ( @
$lines ) {
89 my $x = $line->{loopcell
};
90 print $line->{rowtitle
}.$sep;
91 foreach my $cell (@
$x) {
92 print $cell->{value
}.$sep;
94 print $line->{totalrow
};
99 $cols = @
$results[0]->{loopfooter
};
100 foreach my $col ( @
$cols ) {
101 print $sep.$col->{totalcol
};
103 print $sep.@
$results[0]->{total
};
108 my $dbh = C4
::Context
->dbh;
114 my @mime = ( C4
::Context
->preference("MIME") );
115 # foreach my $mime (@mime){
119 my $CGIextChoice=CGI
::scrolling_list
(
126 my @dels = ( C4
::Context
->preference("delimiter") );
127 my $CGIsepChoice=CGI
::scrolling_list
(
134 my $itemtypes = GetItemTypes
;
136 foreach my $thisitemtype (keys %$itemtypes) {
137 # my $selected = 1 if $thisbranch eq $branch;
138 my %row =(value
=> $thisitemtype,
139 # selected => $selected,
140 description
=> $itemtypes->{$thisitemtype}->{'description'},
142 push @itemtypeloop, \
%row;
146 my $branches = GetBranches
;
148 foreach my $thisbranch (keys %$branches) {
149 # my $selected = 1 if $thisbranch eq $branch;
150 my %row =(value
=> $thisbranch,
151 # selected => $selected,
152 branchname
=> $branches->{$thisbranch}->{'branchname'},
154 push @branchloop, \
%row;
158 CGIextChoice
=> $CGIextChoice,
159 CGIsepChoice
=> $CGIsepChoice,
160 itemtypeloop
=>\
@itemtypeloop,
161 branchloop
=>\
@branchloop,
163 output_html_with_http_headers
$input, $cookie, $template->output;
170 my ($line, $column, $filters) = @_;
179 my $dbh = C4
::Context
->dbh;
185 for (my $i=0;$i<=6;$i++) {
187 if ( @
$filters[$i] ) {
188 if (($i==1) and (@
$filters[$i-1])) {
189 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
191 $cell{filter
} .= @
$filters[$i];
192 $cell{crit
} .="Branch" if ($i==0);
193 $cell{crit
} .="Doc Type" if ($i==1);
194 push @loopfilter, \
%cell;
200 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
201 $column = "biblioitems.".$column if $column=~/itemtype/;
202 $column = "borrowers.".$column if $column=~/categorycode/;
204 $colfilter[0] = @
$filters[0] if ($column =~ /branch/ ) ;
205 $colfilter[0] = @
$filters[1] if ($column =~ /itemtype/ ) ;
208 $colfield .= $column;
209 $colorder .= $column;
212 $strsth2 .= "select distinctrow $colfield
214 LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber
215 LEFT JOIN items ON old_issues.itemnumber=items.itemnumber
216 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber
217 WHERE old_issues.itemnumber=items.itemnumber
218 AND old_issues.borrowernumber=borrowers.borrowernumber";
220 $colfilter[0] =~ s/\*/%/g;
221 $strsth2 .= " AND $column LIKE '$colfilter[0]' " ;
223 $strsth2 .=" GROUP BY $colfield";
224 $strsth2 .=" ORDER BY $colorder";
227 my $sth2 = $dbh->prepare( $strsth2 );
232 while (my ($celvalue) = $sth2->fetchrow) {
235 # warn "coltitle :".$celvalue;
236 $cell{coltitle
} = $celvalue;
238 push @loopcol, \
%cell;
240 # warn "fin des titres colonnes";
247 #Initialization of cell values.....
251 for (my $i=1;$i<=$line;$i++) {
252 foreach my $col ( @loopcol ) {
253 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
254 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
259 # preparing calculation
262 # Processing average loanperiods
263 $strcalc .= "SELECT items.barcode, biblio.title, biblio.biblionumber, biblio.author";
264 $strcalc .= " , $colfield " if ($colfield);
265 $strcalc .= " FROM (items
266 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
267 LEFT JOIN biblio ON biblio.biblionumber=items.biblionumber)
268 LEFT JOIN old_issues ON old_issues.itemnumber=items.itemnumber
269 WHERE old_issues.itemnumber is null";
270 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
271 $strcalc .= " AND items.homebranch like '" . @
$filters[0] ."'" if ( @
$filters[0] );
272 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
273 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[1] ."'" if ( @
$filters[1] );
275 $strcalc .= " GROUP BY items.itemnumber";
276 $strcalc .= ", $colfield" if ($column);
277 $strcalc .= " ORDER BY $colfield " if ($colfield);
278 my $max = (@loopcol) ?
$line*@loopcol : $line ;
279 $strcalc .= " LIMIT 0,$max" if ($line);
280 warn "SQL :". $strcalc;
282 my $dbcalc = $dbh->prepare($strcalc);
284 # warn "filling table";
287 while (my @data = $dbcalc->fetchrow) {
288 my ($barcode,$title,$biblionumber,$author, $col )=@data;
289 $col = "zzEMPTY" if ($col eq undef);
290 $i=1 if (($previous_col) and not($col eq $previous_col));
291 $table[$i]->{$col}->{'barcode'}=$barcode;
292 $table[$i]->{$col}->{'title'}=$title;
293 $table[$i]->{$col}->{'biblionumber'}=$biblionumber;
294 $table[$i]->{$col}->{'author'}=$author;
295 # warn " ".$i." ".$col. " ".$row;
300 push @loopcol,{coltitle
=> "Global"} if not($column);
302 $max =(($line)?
$line:@table);
303 for ($i=1; $i<=$max;$i++) {
305 #@loopcol ensures the order for columns is common with column titles
306 # and the number matches the number of columns
308 foreach my $col ( @loopcol ) {
309 my ($barcode, $author, $title, $biblionumber);
311 $barcode =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'barcode'};
312 $title =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'title'};
313 $author =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'author'};
314 $biblionumber =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'biblionumber'};
316 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
317 $title =$table[$i]->{"zzEMPTY"}->{'title'};
318 $author =$table[$i]->{"zzEMPTY"}->{'author'};
319 $biblionumber =$table[$i]->{"zzEMPTY"}->{'biblionumber'};
321 push @loopcell, {author
=> $author, title
=>$title,biblionumber
=>$biblionumber,barcode
=>$barcode} ;
323 push @looprow,{ 'rowtitle' => $i ,
324 'loopcell' => \
@loopcell,
325 'hilighted' => ($hilighted >0),
327 $hilighted = -$hilighted;
332 # the header of the table
333 $globalline{loopfilter
}=\
@loopfilter;
334 # the core of the table
335 $globalline{looprow
} = \
@looprow;
336 $globalline{loopcol
} = \
@loopcol;
337 # # the foot (totals by borrower type)
338 $globalline{loopfooter
} = \
@loopfooter;
339 $globalline{total
}= $grantotal;
340 $globalline{line
} = $line;
341 $globalline{column
} = $column;
342 push @mainloop,\
%globalline;