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