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
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
36 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/catalogue_out.tmpl";
48 my $limit = $input->param("Limit");
49 my $column = $input->param("Criteria");
50 my @filters = $input->param("Filter");
51 my $output = $input->param("output");
52 my $basename = $input->param("basename");
53 my $mime = $input->param("MIME");
54 my $del = $input->param("sep");
55 #warn "calcul : ".$calc;
56 my ($template, $borrowernumber, $cookie)
57 = get_template_and_user({template_name => $fullreportname,
61 flagsrequired => {editcatalogue => 1},
64 $template->param(do_it => $do_it,
65 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
66 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
67 IntranetNav => C4::Context->preference("IntranetNav"),
71 my $results = calculate($limit, $column, \@filters);
72 if ($output eq "screen"){
73 # Printing results to screen
74 $template->param(mainloop => $results);
75 output_html_with_http_headers $input, $cookie, $template->output;
78 # Printing to a csv file
79 print $input->header(-type => 'application/vnd.sun.xml.calc',
80 -attachment=>"$basename.csv",
81 -filename=>"$basename.csv" );
82 my $cols = @$results[0]->{loopcol};
83 my $lines = @$results[0]->{looprow};
85 $sep =C4::Context->preference("delimiter");
87 print "num /". @$results[0]->{column} .$sep;
89 foreach my $col ( @$cols ) {
90 print $col->{coltitle}.$sep;
94 foreach my $line ( @$lines ) {
95 my $x = $line->{loopcell};
96 print $line->{rowtitle}.$sep;
97 foreach my $cell (@$x) {
98 print $cell->{value}.$sep;
100 print $line->{totalrow};
105 $cols = @$results[0]->{loopfooter};
106 foreach my $col ( @$cols ) {
107 print $sep.$col->{totalcol};
109 print $sep.@$results[0]->{total};
114 my $dbh = C4::Context->dbh;
120 my @mime = ( C4::Context->preference("MIME") );
121 # foreach my $mime (@mime){
125 my $CGIextChoice=CGI::scrolling_list(
132 my @dels = ( C4::Context->preference("delimiter") );
133 my $CGIsepChoice=CGI::scrolling_list(
140 my $itemtypes = getitemtypes;
142 foreach my $thisitemtype (keys %$itemtypes) {
143 # my $selected = 1 if $thisbranch eq $branch;
144 my %row =(value => $thisitemtype,
145 # selected => $selected,
146 description => $itemtypes->{$thisitemtype}->{'description'},
148 push @itemtypeloop, \%row;
152 my $branches = getallbranches;
154 foreach my $thisbranch (keys %$branches) {
155 # my $selected = 1 if $thisbranch eq $branch;
156 my %row =(value => $thisbranch,
157 # selected => $selected,
158 branchname => $branches->{$thisbranch}->{'branchname'},
160 push @branchloop, \%row;
164 CGIextChoice => $CGIextChoice,
165 CGIsepChoice => $CGIsepChoice,
166 itemtypeloop =>\@itemtypeloop,
167 branchloop =>\@branchloop,
169 output_html_with_http_headers $input, $cookie, $template->output;
176 my ($line, $column, $filters) = @_;
185 my $dbh = C4::Context->dbh;
191 for (my $i=0;$i<=6;$i++) {
193 if ( @$filters[$i] ) {
194 if (($i==1) and (@$filters[$i-1])) {
195 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
197 $cell{filter} .= @$filters[$i];
198 $cell{crit} .="Branch" if ($i==0);
199 $cell{crit} .="Doc Type" if ($i==1);
200 push @loopfilter, \%cell;
206 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
207 $column = "biblioitems.".$column if $column=~/itemtype/;
208 $column = "borrowers.".$column if $column=~/categorycode/;
210 $colfilter[0] = @$filters[0] if ($column =~ /branch/ ) ;
211 $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ ) ;
214 $colfield .= $column;
215 $colorder .= $column;
218 $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";
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 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";
266 # @$filters[0]=~ s/\*/%/g if (@$filters[0]);
267 # $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
268 # @$filters[1]=~ s/\*/%/g if (@$filters[1]);
269 # $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
270 # @$filters[2]=~ s/\*/%/g if (@$filters[2]);
271 # $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
272 # @$filters[3]=~ s/\*/%/g if (@$filters[3]);
273 # $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
274 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
275 $strcalc .= " AND items.homebranch like '" . @$filters[0] ."'" if ( @$filters[0] );
276 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
277 $strcalc .= " AND biblioitems.itemtype like '" . @$filters[1] ."'" if ( @$filters[1] );
279 $strcalc .= " group by items.itemnumber";
280 $strcalc .= ", $colfield" if ($column);
281 $strcalc .= " order by $colfield " if ($colfield);
284 $max = $line*@loopcol;
285 } else { $max=$line;}
286 $strcalc .= " LIMIT 0,$max" if ($line);
287 warn "SQL :". $strcalc;
289 my $dbcalc = $dbh->prepare($strcalc);
291 # warn "filling table";
294 while (my @data = $dbcalc->fetchrow) {
295 my ($barcode,$title,$bibnum,$author, $col )=@data;
296 $col = "zzEMPTY" if ($col eq undef);
297 $i=1 if (($previous_col) and not($col eq $previous_col));
298 $table[$i]->{$col}->{'barcode'}=$barcode;
299 $table[$i]->{$col}->{'title'}=$title;
300 $table[$i]->{$col}->{'bibnum'}=$bibnum;
301 $table[$i]->{$col}->{'author'}=$author;
302 # warn " ".$i." ".$col. " ".$row;
307 push @loopcol,{coltitle => "Global"} if not($column);
309 my $max =(($line)?$line:@table);
310 for ($i=1; $i<$max;$i++) {
312 #@loopcol ensures the order for columns is common with column titles
313 # and the number matches the number of columns
315 foreach my $col ( @loopcol ) {
316 my ($barcode, $author, $title, $bibnum);
318 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
319 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
320 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
321 $bibnum =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'bibnum'};
323 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
324 $title =$table[$i]->{"zzEMPTY"}->{'title'};
325 $author =$table[$i]->{"zzEMPTY"}->{'author'};
326 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
328 push @loopcell, {author=> $author, title=>$title,bibnum=>$bibnum,barcode=>$barcode} ;
330 push @looprow,{ 'rowtitle' => $i ,
331 'loopcell' => \@loopcell,
332 'hilighted' => ($hilighted >0),
334 $hilighted = -$hilighted;
339 # the header of the table
340 $globalline{loopfilter}=\@loopfilter;
341 # the core of the table
342 $globalline{looprow} = \@looprow;
343 $globalline{loopcol} = \@loopcol;
344 # # the foot (totals by borrower type)
345 $globalline{loopfooter} = \@loopfooter;
346 $globalline{total}= $grantotal;
347 $globalline{line} = $line;
348 $globalline{column} = $column;
349 push @mainloop,\%globalline;