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