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