New XML API
[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 HTML::Template;
27 use C4::Search;
28 use C4::Output;
29 use C4::Koha;
30 use C4::Interface::CGI::Output;
31 use C4::Circulation::Circ2;
32 use Date::Manip;
33
34 =head1 NAME
35
36 plugin that shows a stats on borrowers
37
38 =head1 DESCRIPTION
39
40
41 =over2
42
43 =cut
44
45 my $input = new CGI;
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,
58                                 query => $input,
59                                 type => "intranet",
60                                 authnotrequired => 0,
61                                 flagsrequired => {editcatalogue => 1},
62                                 debug => 1,
63                                 });
64 $template->param(do_it => $do_it);
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                         -attachment=>"$basename.csv",
77                         -filename=>"$basename.csv" );
78                 my $cols = @$results[0]->{loopcol};
79                 my $lines = @$results[0]->{looprow};
80                 my $sep;
81                 $sep =C4::Context->preference("delimiter");
82 # header top-right
83                 print "num /". @$results[0]->{column} .$sep;
84 # Other header
85                 foreach my $col ( @$cols ) {
86                         print $col->{coltitle}.$sep;
87                 }
88                 print "Total\n";
89 # Table
90                 foreach my $line ( @$lines ) {
91                         my $x = $line->{loopcell};
92                         print $line->{rowtitle}.$sep;
93                         foreach my $cell (@$x) {
94                                 print $cell->{value}.$sep;
95                         }
96                         print $line->{totalrow};
97                         print "\n";
98                 }
99 # footer
100                 print "TOTAL";
101                 $cols = @$results[0]->{loopfooter};
102                 foreach my $col ( @$cols ) {
103                         print $sep.$col->{totalcol};
104                 }
105                 print $sep.@$results[0]->{total};
106                 exit(1);
107         }
108 # Displaying choices
109 } else {
110         my $dbh = C4::Context->dbh;
111         my @values;
112         my %labels;
113         my %select;
114         my $req;
115         
116         my @mime = ( C4::Context->preference("MIME") );
117 #       foreach my $mime (@mime){
118 #               warn "".$mime;
119 #       }
120         
121         my $CGIextChoice=CGI::scrolling_list(
122                                 -name     => 'MIME',
123                                 -id       => 'MIME',
124                                 -values   => \@mime,
125                                 -size     => 1,
126                                 -multiple => 0 );
127         
128         my @dels = ( C4::Context->preference("delimiter") );
129         my $CGIsepChoice=CGI::scrolling_list(
130                                 -name     => 'sep',
131                                 -id       => 'sep',
132                                 -values   => \@dels,
133                                 -size     => 1,
134                                 -multiple => 0 );
135         
136         $template->param(
137                                         CGIextChoice => $CGIextChoice,
138                                         CGIsepChoice => $CGIsepChoice
139                                         );
140 output_html_with_http_headers $input, $cookie, $template->output;
141 }
142
143
144
145
146 sub calculate {
147         my ($line, $column, $filters) = @_;
148         my @mainloop;
149         my @loopfooter;
150         my @loopcol;
151         my @loopline;
152         my @looprow;
153         my %globalline;
154         my $grantotal =0;
155 # extract parameters
156         my $dbh = C4::Context->dbh;
157
158 # Filters
159 # Checking filters
160 #
161         my @loopfilter;
162         for (my $i=0;$i<=6;$i++) {
163                 my %cell;
164                 if ( @$filters[$i] ) {
165                         if (($i==1) and (@$filters[$i-1])) {
166                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
167                         }
168                         $cell{filter} .= @$filters[$i];
169                         $cell{crit} .="Branch" if ($i==0);
170                         $cell{crit} .="Doc Type" if ($i==1);
171                         push @loopfilter, \%cell;
172                 }
173         }
174         my $colfield;
175         my $colorder;
176         if ($column){
177                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
178                 $column = "biblioitems.".$column if $column=~/itemtype/;
179                 $column = "borrowers.".$column if $column=~/categorycode/;
180                 my @colfilter ;
181                 $colfilter[0] = @$filters[0] if ($column =~ /branch/ )  ;
182                 $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ )  ;
183                                                                                                 
184         # loop cols.
185                 $colfield .= $column;
186                 $colorder .= $column;
187                 
188                 my $strsth2;
189                 $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";
190                 if ($colfilter[0]) {
191                         $colfilter[0] =~ s/\*/%/g;
192                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
193                 }
194                 $strsth2 .=" group by $colfield";
195                 $strsth2 .=" order by $colorder";
196                 warn "". $strsth2;
197                 
198                 my $sth2 = $dbh->prepare( $strsth2 );
199                 $sth2->execute;
200
201                 
202         
203                 while (my ($celvalue) = $sth2->fetchrow) {
204                         my %cell;
205         #               my %ft;
206         #               warn "coltitle :".$celvalue;
207                         $cell{coltitle} = $celvalue;
208         #               $ft{totalcol} = 0;
209                         push @loopcol, \%cell;
210                 }
211         #       warn "fin des titres colonnes";
212         }
213         
214         my $i=0;
215 #       my @totalcol;
216         my $hilighted=-1;
217         
218         #Initialization of cell values.....
219         my @table;
220         
221 #       warn "init table";
222         for (my $i=1;$i<=$line;$i++) {
223                 foreach my $col ( @loopcol ) {
224 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
225                         $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
226                 }
227         }
228
229
230 # preparing calculation
231         my $strcalc ;
232         
233 # Processing average loanperiods
234         $strcalc .= "SELECT CONCAT( items.itemnumber, \" \",biblioitems.biblioitemnumber)";
235         $strcalc .= " , $colfield " if ($colfield);
236         $strcalc .= " FROM (items LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber)  LEFT JOIN issues ON  issues.itemnumber=items.itemnumber WHERE issues.itemnumber is null";
237 #       @$filters[0]=~ s/\*/%/g if (@$filters[0]);
238 #       $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
239 #       @$filters[1]=~ s/\*/%/g if (@$filters[1]);
240 #       $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
241 #       @$filters[2]=~ s/\*/%/g if (@$filters[2]);
242 #       $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
243 #       @$filters[3]=~ s/\*/%/g if (@$filters[3]);
244 #       $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
245         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
246         $strcalc .= " AND items.homebranch like '" . @$filters[0] ."'" if ( @$filters[0] );
247         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
248         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[1] ."'" if ( @$filters[1] );
249         
250         $strcalc .= " group by items.itemnumber";
251         $strcalc .= ", $colfield" if ($column);
252         $strcalc .= " order by $colfield " if ($colfield);
253         my $max;
254         if (@loopcol) {
255                 $max = $line*@loopcol;
256         } else { $max=$line;}
257         $strcalc .= " LIMIT 0,$max" if ($line);
258         warn "SQL :". $strcalc;
259         
260         my $dbcalc = $dbh->prepare($strcalc);
261         $dbcalc->execute;
262 #       warn "filling table";
263         my $previous_col;
264         my $i=1;
265         while (my  @data = $dbcalc->fetchrow) {
266                 my ($row, $col )=@data;
267                 $col = "zzEMPTY" if ($col eq undef);
268                 $i=1 if (($previous_col) and not($col eq $previous_col));
269                 $table[$i]->{$col}=$row;
270 #               warn " ".$i." ".$col. " ".$row;
271                 $i++;
272                 $previous_col=$col;
273         }
274         
275         push @loopcol,{coltitle => "Global"} if not($column);
276         
277         my $max =(($line)?$line:@table);
278         for ($i=1; $i<$max;$i++) {
279                 my @loopcell;
280                 #@loopcol ensures the order for columns is common with column titles
281                 # and the number matches the number of columns
282                 my $colcount=0;
283                 foreach my $col ( @loopcol ) {
284                         my $value;
285                         if (@loopcol){
286                                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
287                         } else {
288                                 $value =$table[$i]->{"zzEMPTY"};
289                         }
290                         push @loopcell, {value => $value} ;
291                 }
292                 push @looprow,{ 'rowtitle' => $i ,
293                                                 'loopcell' => \@loopcell,
294                                                 'hilighted' => ($hilighted >0),
295                                         };
296                 $hilighted = -$hilighted;
297         }
298         
299                         
300
301         # the header of the table
302         $globalline{loopfilter}=\@loopfilter;
303         # the core of the table
304         $globalline{looprow} = \@looprow;
305         $globalline{loopcol} = \@loopcol;
306 #       # the foot (totals by borrower type)
307         $globalline{loopfooter} = \@loopfooter;
308         $globalline{total}= $grantotal;
309         $globalline{line} = $line;
310         $globalline{column} = $column;
311         push @mainloop,\%globalline;
312         return \@mainloop;
313 }
314
315 1;