opac.css - button background fix for input.icon
[koha.git] / reports / catalogue_out.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 use strict;
22 use CGI;
23 use C4::Auth;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Output;
27 use C4::Koha;
28 use C4::Circulation;
29 use Date::Manip;
30
31 =head1 NAME
32
33 plugin that shows a stats on borrowers
34
35 =head1 DESCRIPTION
36
37 =over 2
38
39 =cut
40
41 my $input = new CGI;
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,
54                                 query => $input,
55                                 type => "intranet",
56                                 authnotrequired => 0,
57                                 flagsrequired => {reports => 1},
58                                 debug => 1,
59                                 });
60 $template->param(do_it => $do_it,
61                 );
62 if ($do_it) {
63 # Displaying results
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;
69                 exit(1);
70         } else {
71 # Printing to a csv file
72                 print $input->header(-type => 'application/vnd.sun.xml.calc',
73                                      -encoding    => 'utf-8',
74                         -attachment=>"$basename.csv",
75                         -filename=>"$basename.csv" );
76                 my $cols = @$results[0]->{loopcol};
77                 my $lines = @$results[0]->{looprow};
78                 my $sep;
79                 $sep =C4::Context->preference("delimiter");
80 # header top-right
81                 print "num /". @$results[0]->{column} .$sep;
82 # Other header
83                 foreach my $col ( @$cols ) {
84                         print $col->{coltitle}.$sep;
85                 }
86                 print "Total\n";
87 # Table
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;
93                         }
94                         print $line->{totalrow};
95                         print "\n";
96                 }
97 # footer
98                 print "TOTAL";
99                 $cols = @$results[0]->{loopfooter};
100                 foreach my $col ( @$cols ) {
101                         print $sep.$col->{totalcol};
102                 }
103                 print $sep.@$results[0]->{total};
104                 exit(1);
105         }
106 # Displaying choices
107 } else {
108         my $dbh = C4::Context->dbh;
109         my @values;
110         my %labels;
111         my %select;
112         my $req;
113         
114         my @mime = ( C4::Context->preference("MIME") );
115 #       foreach my $mime (@mime){
116 #               warn "".$mime;
117 #       }
118         
119         my $CGIextChoice=CGI::scrolling_list(
120                                 -name     => 'MIME',
121                                 -id       => 'MIME',
122                                 -values   => \@mime,
123                                 -size     => 1,
124                                 -multiple => 0 );
125         
126         my @dels = ( C4::Context->preference("delimiter") );
127         my $CGIsepChoice=CGI::scrolling_list(
128                                 -name     => 'sep',
129                                 -id       => 'sep',
130                                 -values   => \@dels,
131                                 -size     => 1,
132                                 -multiple => 0 );
133         #doctype
134         my $itemtypes = GetItemTypes;
135         my @itemtypeloop;
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'},
141                                                         );
142                         push @itemtypeloop, \%row;
143         }
144                 
145         #branch
146         my $branches = GetBranches;
147         my @branchloop;
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'},
153                                                         );
154                         push @branchloop, \%row;
155         }
156         
157         $template->param(
158                                         CGIextChoice => $CGIextChoice,
159                                         CGIsepChoice => $CGIsepChoice,
160                                         itemtypeloop =>\@itemtypeloop,
161                                         branchloop =>\@branchloop,
162                                         );
163 output_html_with_http_headers $input, $cookie, $template->output;
164 }
165
166
167
168
169 sub calculate {
170         my ($line, $column, $filters) = @_;
171         my @mainloop;
172         my @loopfooter;
173         my @loopcol;
174         my @loopline;
175         my @looprow;
176         my %globalline;
177         my $grantotal =0;
178 # extract parameters
179         my $dbh = C4::Context->dbh;
180
181 # Filters
182 # Checking filters
183 #
184         my @loopfilter;
185         for (my $i=0;$i<=6;$i++) {
186                 my %cell;
187                 if ( @$filters[$i] ) {
188                         if (($i==1) and (@$filters[$i-1])) {
189                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
190                         }
191                         $cell{filter} .= @$filters[$i];
192                         $cell{crit} .="Branch" if ($i==0);
193                         $cell{crit} .="Doc Type" if ($i==1);
194                         push @loopfilter, \%cell;
195                 }
196         }
197         my $colfield;
198         my $colorder;
199         if ($column){
200                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
201                 $column = "biblioitems.".$column if $column=~/itemtype/;
202                 $column = "borrowers.".$column if $column=~/categorycode/;
203                 my @colfilter ;
204                 $colfilter[0] = @$filters[0] if ($column =~ /branch/ )  ;
205                 $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ )  ;
206                                                                                                 
207         # loop cols.
208                 $colfield .= $column;
209                 $colorder .= $column;
210                 
211                 my $strsth2;
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";
213                 if ($colfilter[0]) {
214                         $colfilter[0] =~ s/\*/%/g;
215                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
216                 }
217                 $strsth2 .=" group by $colfield";
218                 $strsth2 .=" order by $colorder";
219                 warn "". $strsth2;
220                 
221                 my $sth2 = $dbh->prepare( $strsth2 );
222                 $sth2->execute;
223
224                 
225         
226                 while (my ($celvalue) = $sth2->fetchrow) {
227                         my %cell;
228         #               my %ft;
229         #               warn "coltitle :".$celvalue;
230                         $cell{coltitle} = $celvalue;
231         #               $ft{totalcol} = 0;
232                         push @loopcol, \%cell;
233                 }
234         #       warn "fin des titres colonnes";
235         }
236         
237         my $i=0;
238 #       my @totalcol;
239         my $hilighted=-1;
240         
241         #Initialization of cell values.....
242         my @table;
243         
244 #       warn "init table";
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;
249                 }
250         }
251
252
253 # preparing calculation
254         my $strcalc ;
255         
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] );
272         
273         $strcalc .= " group by items.itemnumber";
274         $strcalc .= ", $colfield"          if ($column);
275         $strcalc .= " order by $colfield " if ($colfield);
276         my $max = (@loopcol) ? $line*@loopcol : $line ;
277         $strcalc .= " LIMIT 0,$max"        if ($line);
278         warn "SQL :". $strcalc;
279         
280         my $dbcalc = $dbh->prepare($strcalc);
281         $dbcalc->execute;
282 #       warn "filling table";
283         my $previous_col;
284         $i=1;
285         while (my  @data = $dbcalc->fetchrow) {
286                 my ($barcode,$title,$bibnum,$author, $col )=@data;
287                 $col = "zzEMPTY" if ($col eq undef);
288                 $i=1 if (($previous_col) and not($col eq $previous_col));
289                 $table[$i]->{$col}->{'barcode'}=$barcode;
290                 $table[$i]->{$col}->{'title'}=$title;
291                 $table[$i]->{$col}->{'bibnum'}=$bibnum;
292                 $table[$i]->{$col}->{'author'}=$author;
293 #               warn " ".$i." ".$col. " ".$row;
294                 $i++;
295                 $previous_col=$col;
296         }
297         
298         push @loopcol,{coltitle => "Global"} if not($column);
299         
300         $max =(($line)?$line:@table);
301         for ($i=1; $i<$max;$i++) {
302                 my @loopcell;
303                 #@loopcol ensures the order for columns is common with column titles
304                 # and the number matches the number of columns
305                 my $colcount=0;
306                 foreach my $col ( @loopcol ) {
307                         my ($barcode, $author, $title, $bibnum);
308                         if (@loopcol){
309                                 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
310                                 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
311                                 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
312                                 $bibnum =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'bibnum'};
313                         } else {
314                                 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
315                                 $title =$table[$i]->{"zzEMPTY"}->{'title'};
316                                 $author =$table[$i]->{"zzEMPTY"}->{'author'};
317                                 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
318                         }
319                         push @loopcell, {author=> $author, title=>$title,bibnum=>$bibnum,barcode=>$barcode} ;
320                 }
321                 push @looprow,{ 'rowtitle' => $i ,
322                                                 'loopcell' => \@loopcell,
323                                                 'hilighted' => ($hilighted >0),
324                                         };
325                 $hilighted = -$hilighted;
326         }
327         
328                         
329
330         # the header of the table
331         $globalline{loopfilter}=\@loopfilter;
332         # the core of the table
333         $globalline{looprow} = \@looprow;
334         $globalline{loopcol} = \@loopcol;
335 #       # the foot (totals by borrower type)
336         $globalline{loopfooter} = \@loopfooter;
337         $globalline{total}= $grantotal;
338         $globalline{line} = $line;
339         $globalline{column} = $column;
340         push @mainloop,\%globalline;
341         return \@mainloop;
342 }
343
344 1;