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 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";
214 $colfilter[0] =~ s/\*/%/g;
215 $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
217 $strsth2 .=" group by $colfield";
218 $strsth2 .=" order by $colorder";
221 my $sth2 = $dbh->prepare( $strsth2 );
226 while (my ($celvalue) = $sth2->fetchrow) {
229 # warn "coltitle :".$celvalue;
230 $cell{coltitle} = $celvalue;
232 push @loopcol, \%cell;
234 # warn "fin des titres colonnes";
241 #Initialization of cell values.....
245 for (my $i=1;$i<=$line;$i++) {
246 foreach my $col ( @loopcol ) {
247 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
248 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
253 # preparing calculation
256 # Processing average loanperiods
257 $strcalc .= "SELECT items.barcode, biblio.title, biblio.biblionumber, biblio.author";
258 $strcalc .= " , $colfield " if ($colfield);
259 $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";
260 # @$filters[0]=~ s/\*/%/g if (@$filters[0]);
261 # $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
262 # @$filters[1]=~ s/\*/%/g if (@$filters[1]);
263 # $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
264 # @$filters[2]=~ s/\*/%/g if (@$filters[2]);
265 # $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
266 # @$filters[3]=~ s/\*/%/g if (@$filters[3]);
267 # $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
268 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
269 $strcalc .= " AND items.homebranch like '" . @$filters[0] ."'" if ( @$filters[0] );
270 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
271 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[1] ."'" if ( @$filters[1] );
273 $strcalc .= " group by items.itemnumber";
274 $strcalc .= ", $colfield" if ($column);
275 $strcalc .= " order by $colfield " if ($colfield);
278 $max = $line*@loopcol;
279 } else { $max=$line;}
280 $strcalc .= " LIMIT 0,$max" if ($line);
281 warn "SQL :". $strcalc;
283 my $dbcalc = $dbh->prepare($strcalc);
285 # warn "filling table";
288 while (my @data = $dbcalc->fetchrow) {
289 my ($barcode,$title,$bibnum,$author, $col )=@data;
290 $col = "zzEMPTY" if ($col eq undef);
291 $i=1 if (($previous_col) and not($col eq $previous_col));
292 $table[$i]->{$col}->{'barcode'}=$barcode;
293 $table[$i]->{$col}->{'title'}=$title;
294 $table[$i]->{$col}->{'bibnum'}=$bibnum;
295 $table[$i]->{$col}->{'author'}=$author;
296 # warn " ".$i." ".$col. " ".$row;
301 push @loopcol,{coltitle => "Global"} if not($column);
303 my $max =(($line)?$line:@table);
304 for ($i=1; $i<$max;$i++) {
306 #@loopcol ensures the order for columns is common with column titles
307 # and the number matches the number of columns
309 foreach my $col ( @loopcol ) {
310 my ($barcode, $author, $title, $bibnum);
312 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
313 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
314 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
315 $bibnum =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'bibnum'};
317 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
318 $title =$table[$i]->{"zzEMPTY"}->{'title'};
319 $author =$table[$i]->{"zzEMPTY"}->{'author'};
320 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
322 push @loopcell, {author=> $author, title=>$title,bibnum=>$bibnum,barcode=>$barcode} ;
324 push @looprow,{ 'rowtitle' => $i ,
325 'loopcell' => \@loopcell,
326 'hilighted' => ($hilighted >0),
328 $hilighted = -$hilighted;
333 # the header of the table
334 $globalline{loopfilter}=\@loopfilter;
335 # the core of the table
336 $globalline{looprow} = \@looprow;
337 $globalline{loopcol} = \@loopcol;
338 # # the foot (totals by borrower type)
339 $globalline{loopfooter} = \@loopfooter;
340 $globalline{total}= $grantotal;
341 $globalline{line} = $line;
342 $globalline{column} = $column;
343 push @mainloop,\%globalline;