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