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
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/catalogue_out.tmpl";
45 my $limit = $input->param("Limit");
46 my $column = $input->param("Criteria");
47 my @filters = $input->param("Filter");
48 my $output = $input->param("output");
49 my $basename = $input->param("basename");
50 my $mime = $input->param("MIME");
51 my $del = $input->param("sep");
52 #warn "calcul : ".$calc;
53 my ($template, $borrowernumber, $cookie)
54 = get_template_and_user
({template_name
=> $fullreportname,
58 flagsrequired
=> {reports
=> 1},
61 $template->param(do_it
=> $do_it,
62 intranetcolorstylesheet
=> C4
::Context
->preference("intranetcolorstylesheet"),
63 intranetstylesheet
=> C4
::Context
->preference("intranetstylesheet"),
64 IntranetNav
=> C4
::Context
->preference("IntranetNav"),
68 my $results = calculate
($limit, $column, \
@filters);
69 if ($output eq "screen"){
70 # Printing results to screen
71 $template->param(mainloop
=> $results);
72 output_html_with_http_headers
$input, $cookie, $template->output;
75 # Printing to a csv file
76 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
78 -attachment
=>"$basename.csv",
79 -filename
=>"$basename.csv" );
80 my $cols = @
$results[0]->{loopcol
};
81 my $lines = @
$results[0]->{looprow
};
83 $sep =C4
::Context
->preference("delimiter");
85 print "num /". @
$results[0]->{column
} .$sep;
87 foreach my $col ( @
$cols ) {
88 print $col->{coltitle
}.$sep;
92 foreach my $line ( @
$lines ) {
93 my $x = $line->{loopcell
};
94 print $line->{rowtitle
}.$sep;
95 foreach my $cell (@
$x) {
96 print $cell->{value
}.$sep;
98 print $line->{totalrow
};
103 $cols = @
$results[0]->{loopfooter
};
104 foreach my $col ( @
$cols ) {
105 print $sep.$col->{totalcol
};
107 print $sep.@
$results[0]->{total
};
112 my $dbh = C4
::Context
->dbh;
118 my @mime = ( C4
::Context
->preference("MIME") );
119 # foreach my $mime (@mime){
123 my $CGIextChoice=CGI
::scrolling_list
(
130 my @dels = ( C4
::Context
->preference("delimiter") );
131 my $CGIsepChoice=CGI
::scrolling_list
(
138 my $itemtypes = GetItemTypes
;
140 foreach my $thisitemtype (keys %$itemtypes) {
141 # my $selected = 1 if $thisbranch eq $branch;
142 my %row =(value
=> $thisitemtype,
143 # selected => $selected,
144 description
=> $itemtypes->{$thisitemtype}->{'description'},
146 push @itemtypeloop, \
%row;
150 my $branches = GetBranches
;
152 foreach my $thisbranch (keys %$branches) {
153 # my $selected = 1 if $thisbranch eq $branch;
154 my %row =(value
=> $thisbranch,
155 # selected => $selected,
156 branchname
=> $branches->{$thisbranch}->{'branchname'},
158 push @branchloop, \
%row;
162 CGIextChoice
=> $CGIextChoice,
163 CGIsepChoice
=> $CGIsepChoice,
164 itemtypeloop
=>\
@itemtypeloop,
165 branchloop
=>\
@branchloop,
167 output_html_with_http_headers
$input, $cookie, $template->output;
174 my ($line, $column, $filters) = @_;
183 my $dbh = C4
::Context
->dbh;
189 for (my $i=0;$i<=6;$i++) {
191 if ( @
$filters[$i] ) {
192 if (($i==1) and (@
$filters[$i-1])) {
193 $cell{err
} = 1 if (@
$filters[$i]<@
$filters[$i-1]) ;
195 $cell{filter
} .= @
$filters[$i];
196 $cell{crit
} .="Branch" if ($i==0);
197 $cell{crit
} .="Doc Type" if ($i==1);
198 push @loopfilter, \
%cell;
204 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
205 $column = "biblioitems.".$column if $column=~/itemtype/;
206 $column = "borrowers.".$column if $column=~/categorycode/;
208 $colfilter[0] = @
$filters[0] if ($column =~ /branch/ ) ;
209 $colfilter[0] = @
$filters[1] if ($column =~ /itemtype/ ) ;
212 $colfield .= $column;
213 $colorder .= $column;
216 $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";
218 $colfilter[0] =~ s/\*/%/g;
219 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
221 $strsth2 .=" group by $colfield";
222 $strsth2 .=" order by $colorder";
225 my $sth2 = $dbh->prepare( $strsth2 );
230 while (my ($celvalue) = $sth2->fetchrow) {
233 # warn "coltitle :".$celvalue;
234 $cell{coltitle
} = $celvalue;
236 push @loopcol, \
%cell;
238 # warn "fin des titres colonnes";
245 #Initialization of cell values.....
249 for (my $i=1;$i<=$line;$i++) {
250 foreach my $col ( @loopcol ) {
251 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
252 $table[$i]->{($col->{coltitle
})?
$col->{coltitle
}:"Global"}=0;
257 # preparing calculation
260 # Processing average loanperiods
261 $strcalc .= "SELECT items.barcode, biblio.title, biblio.biblionumber, biblio.author";
262 $strcalc .= " , $colfield " if ($colfield);
263 $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";
264 # @$filters[0]=~ s/\*/%/g if (@$filters[0]);
265 # $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
266 # @$filters[1]=~ s/\*/%/g if (@$filters[1]);
267 # $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
268 # @$filters[2]=~ s/\*/%/g if (@$filters[2]);
269 # $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
270 # @$filters[3]=~ s/\*/%/g if (@$filters[3]);
271 # $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
272 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
273 $strcalc .= " AND items.homebranch like '" . @
$filters[0] ."'" if ( @
$filters[0] );
274 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
275 $strcalc .= " AND biblioitems.itemtype like '" . @
$filters[1] ."'" if ( @
$filters[1] );
277 $strcalc .= " group by items.itemnumber";
278 $strcalc .= ", $colfield" if ($column);
279 $strcalc .= " order by $colfield " if ($colfield);
282 $max = $line*@loopcol;
283 } else { $max=$line;}
284 $strcalc .= " LIMIT 0,$max" if ($line);
285 warn "SQL :". $strcalc;
287 my $dbcalc = $dbh->prepare($strcalc);
289 # warn "filling table";
292 while (my @data = $dbcalc->fetchrow) {
293 my ($barcode,$title,$bibnum,$author, $col )=@data;
294 $col = "zzEMPTY" if ($col eq undef);
295 $i=1 if (($previous_col) and not($col eq $previous_col));
296 $table[$i]->{$col}->{'barcode'}=$barcode;
297 $table[$i]->{$col}->{'title'}=$title;
298 $table[$i]->{$col}->{'bibnum'}=$bibnum;
299 $table[$i]->{$col}->{'author'}=$author;
300 # warn " ".$i." ".$col. " ".$row;
305 push @loopcol,{coltitle
=> "Global"} if not($column);
307 my $max =(($line)?
$line:@table);
308 for ($i=1; $i<$max;$i++) {
310 #@loopcol ensures the order for columns is common with column titles
311 # and the number matches the number of columns
313 foreach my $col ( @loopcol ) {
314 my ($barcode, $author, $title, $bibnum);
316 $barcode =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'barcode'};
317 $title =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'title'};
318 $author =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'author'};
319 $bibnum =$table[$i]->{(($col->{coltitle
} eq "NULL") or ($col->{coltitle
} eq "Global"))?
"zzEMPTY":$col->{coltitle
}}->{'bibnum'};
321 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
322 $title =$table[$i]->{"zzEMPTY"}->{'title'};
323 $author =$table[$i]->{"zzEMPTY"}->{'author'};
324 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
326 push @loopcell, {author
=> $author, title
=>$title,bibnum
=>$bibnum,barcode
=>$barcode} ;
328 push @looprow,{ 'rowtitle' => $i ,
329 'loopcell' => \
@loopcell,
330 'hilighted' => ($hilighted >0),
332 $hilighted = -$hilighted;
337 # the header of the table
338 $globalline{loopfilter
}=\
@loopfilter;
339 # the core of the table
340 $globalline{looprow
} = \
@looprow;
341 $globalline{loopcol
} = \
@loopcol;
342 # # the foot (totals by borrower type)
343 $globalline{loopfooter
} = \
@loopfooter;
344 $globalline{total
}= $grantotal;
345 $globalline{line
} = $line;
346 $globalline{column
} = $column;
347 push @mainloop,\
%globalline;