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,
61 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
62 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
63 IntranetNav => C4::Context->preference("IntranetNav"),
67 my $results = calculate($limit, $column, \@filters);
68 if ($output eq "screen"){
69 # Printing results to screen
70 $template->param(mainloop => $results);
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',
77 -attachment=>"$basename.csv",
78 -filename=>"$basename.csv" );
79 my $cols = @$results[0]->{loopcol};
80 my $lines = @$results[0]->{looprow};
82 $sep =C4::Context->preference("delimiter");
84 print "num /". @$results[0]->{column} .$sep;
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 print $cell->{value}.$sep;
97 print $line->{totalrow};
102 $cols = @$results[0]->{loopfooter};
103 foreach my $col ( @$cols ) {
104 print $sep.$col->{totalcol};
106 print $sep.@$results[0]->{total};
111 my $dbh = C4::Context->dbh;
117 my @mime = ( C4::Context->preference("MIME") );
118 # foreach my $mime (@mime){
122 my $CGIextChoice=CGI::scrolling_list(
129 my @dels = ( C4::Context->preference("delimiter") );
130 my $CGIsepChoice=CGI::scrolling_list(
137 my $itemtypes = GetItemTypes;
139 foreach my $thisitemtype (keys %$itemtypes) {
140 # my $selected = 1 if $thisbranch eq $branch;
141 my %row =(value => $thisitemtype,
142 # selected => $selected,
143 description => $itemtypes->{$thisitemtype}->{'description'},
145 push @itemtypeloop, \%row;
149 my $branches = GetBranches;
151 foreach my $thisbranch (keys %$branches) {
152 # my $selected = 1 if $thisbranch eq $branch;
153 my %row =(value => $thisbranch,
154 # selected => $selected,
155 branchname => $branches->{$thisbranch}->{'branchname'},
157 push @branchloop, \%row;
161 CGIextChoice => $CGIextChoice,
162 CGIsepChoice => $CGIsepChoice,
163 itemtypeloop =>\@itemtypeloop,
164 branchloop =>\@branchloop,
166 output_html_with_http_headers $input, $cookie, $template->output;
173 my ($line, $column, $filters) = @_;
182 my $dbh = C4::Context->dbh;
188 for (my $i=0;$i<=6;$i++) {
190 if ( @$filters[$i] ) {
191 if (($i==1) and (@$filters[$i-1])) {
192 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
194 $cell{filter} .= @$filters[$i];
195 $cell{crit} .="Branch" if ($i==0);
196 $cell{crit} .="Doc Type" if ($i==1);
197 push @loopfilter, \%cell;
203 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
204 $column = "biblioitems.".$column if $column=~/itemtype/;
205 $column = "borrowers.".$column if $column=~/categorycode/;
207 $colfilter[0] = @$filters[0] if ($column =~ /branch/ ) ;
208 $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ ) ;
211 $colfield .= $column;
212 $colorder .= $column;
215 $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";
217 $colfilter[0] =~ s/\*/%/g;
218 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
220 $strsth2 .=" group by $colfield";
221 $strsth2 .=" order by $colorder";
224 my $sth2 = $dbh->prepare( $strsth2 );
229 while (my ($celvalue) = $sth2->fetchrow) {
232 # warn "coltitle :".$celvalue;
233 $cell{coltitle} = $celvalue;
235 push @loopcol, \%cell;
237 # warn "fin des titres colonnes";
244 #Initialization of cell values.....
248 for (my $i=1;$i<=$line;$i++) {
249 foreach my $col ( @loopcol ) {
250 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
251 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
256 # preparing calculation
259 # Processing average loanperiods
260 $strcalc .= "SELECT items.barcode, biblio.title, biblio.biblionumber, biblio.author";
261 $strcalc .= " , $colfield " if ($colfield);
262 $strcalc .= " FROM (items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber LEFT JOIN biblio ON biblio.biblionumber=items.biblionumber) LEFT JOIN issues ON issues.itemnumber=items.itemnumber WHERE issues.itemnumber is null";
263 # @$filters[0]=~ s/\*/%/g if (@$filters[0]);
264 # $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
265 # @$filters[1]=~ s/\*/%/g if (@$filters[1]);
266 # $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
267 # @$filters[2]=~ s/\*/%/g if (@$filters[2]);
268 # $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
269 # @$filters[3]=~ s/\*/%/g if (@$filters[3]);
270 # $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
271 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
272 $strcalc .= " AND items.homebranch like '" . @$filters[0] ."'" if ( @$filters[0] );
273 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
274 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[1] ."'" if ( @$filters[1] );
276 $strcalc .= " group by items.itemnumber";
277 $strcalc .= ", $colfield" if ($column);
278 $strcalc .= " order by $colfield " if ($colfield);
281 $max = $line*@loopcol;
282 } else { $max=$line;}
283 $strcalc .= " LIMIT 0,$max" if ($line);
284 warn "SQL :". $strcalc;
286 my $dbcalc = $dbh->prepare($strcalc);
288 # warn "filling table";
291 while (my @data = $dbcalc->fetchrow) {
292 my ($barcode,$title,$bibnum,$author, $col )=@data;
293 $col = "zzEMPTY" if ($col eq undef);
294 $i=1 if (($previous_col) and not($col eq $previous_col));
295 $table[$i]->{$col}->{'barcode'}=$barcode;
296 $table[$i]->{$col}->{'title'}=$title;
297 $table[$i]->{$col}->{'bibnum'}=$bibnum;
298 $table[$i]->{$col}->{'author'}=$author;
299 # warn " ".$i." ".$col. " ".$row;
304 push @loopcol,{coltitle => "Global"} if not($column);
306 my $max =(($line)?$line:@table);
307 for ($i=1; $i<$max;$i++) {
309 #@loopcol ensures the order for columns is common with column titles
310 # and the number matches the number of columns
312 foreach my $col ( @loopcol ) {
313 my ($barcode, $author, $title, $bibnum);
315 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
316 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
317 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
318 $bibnum =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'bibnum'};
320 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
321 $title =$table[$i]->{"zzEMPTY"}->{'title'};
322 $author =$table[$i]->{"zzEMPTY"}->{'author'};
323 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
325 push @loopcell, {author=> $author, title=>$title,bibnum=>$bibnum,barcode=>$barcode} ;
327 push @looprow,{ 'rowtitle' => $i ,
328 'loopcell' => \@loopcell,
329 'hilighted' => ($hilighted >0),
331 $hilighted = -$hilighted;
336 # the header of the table
337 $globalline{loopfilter}=\@loopfilter;
338 # the core of the table
339 $globalline{looprow} = \@looprow;
340 $globalline{loopcol} = \@loopcol;
341 # # the foot (totals by borrower type)
342 $globalline{loopfooter} = \@loopfooter;
343 $globalline{total}= $grantotal;
344 $globalline{line} = $line;
345 $globalline{column} = $column;
346 push @mainloop,\%globalline;