moving cataloguing scripts to a cataloguing directory (more logic than acqui.simple...
[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         #doctype
136         my $itemtypes = getitemtypes;
137         my @itemtypeloop;
138         foreach my $thisitemtype (keys %$itemtypes) {
139 #                       my $selected = 1 if $thisbranch eq $branch;
140                         my %row =(value => $thisitemtype,
141 #                                                                       selected => $selected,
142                                                                         description => $itemtypes->{$thisitemtype}->{'description'},
143                                                         );
144                         push @itemtypeloop, \%row;
145         }
146                 
147         #branch
148         my $branches = getallbranches;
149         my @branchloop;
150         foreach my $thisbranch (keys %$branches) {
151 #                       my $selected = 1 if $thisbranch eq $branch;
152                         my %row =(value => $thisbranch,
153 #                                                                       selected => $selected,
154                                                                         branchname => $branches->{$thisbranch}->{'branchname'},
155                                                         );
156                         push @branchloop, \%row;
157         }
158         
159         $template->param(
160                                         CGIextChoice => $CGIextChoice,
161                                         CGIsepChoice => $CGIsepChoice,
162                                         itemtypeloop =>\@itemtypeloop,
163                                         branchloop =>\@branchloop,
164                                         );
165 output_html_with_http_headers $input, $cookie, $template->output;
166 }
167
168
169
170
171 sub calculate {
172         my ($line, $column, $filters) = @_;
173         my @mainloop;
174         my @loopfooter;
175         my @loopcol;
176         my @loopline;
177         my @looprow;
178         my %globalline;
179         my $grantotal =0;
180 # extract parameters
181         my $dbh = C4::Context->dbh;
182
183 # Filters
184 # Checking filters
185 #
186         my @loopfilter;
187         for (my $i=0;$i<=6;$i++) {
188                 my %cell;
189                 if ( @$filters[$i] ) {
190                         if (($i==1) and (@$filters[$i-1])) {
191                                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
192                         }
193                         $cell{filter} .= @$filters[$i];
194                         $cell{crit} .="Branch" if ($i==0);
195                         $cell{crit} .="Doc Type" if ($i==1);
196                         push @loopfilter, \%cell;
197                 }
198         }
199         my $colfield;
200         my $colorder;
201         if ($column){
202                 $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
203                 $column = "biblioitems.".$column if $column=~/itemtype/;
204                 $column = "borrowers.".$column if $column=~/categorycode/;
205                 my @colfilter ;
206                 $colfilter[0] = @$filters[0] if ($column =~ /branch/ )  ;
207                 $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ )  ;
208                                                                                                 
209         # loop cols.
210                 $colfield .= $column;
211                 $colorder .= $column;
212                 
213                 my $strsth2;
214                 $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";
215                 if ($colfilter[0]) {
216                         $colfilter[0] =~ s/\*/%/g;
217                         $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
218                 }
219                 $strsth2 .=" group by $colfield";
220                 $strsth2 .=" order by $colorder";
221                 warn "". $strsth2;
222                 
223                 my $sth2 = $dbh->prepare( $strsth2 );
224                 $sth2->execute;
225
226                 
227         
228                 while (my ($celvalue) = $sth2->fetchrow) {
229                         my %cell;
230         #               my %ft;
231         #               warn "coltitle :".$celvalue;
232                         $cell{coltitle} = $celvalue;
233         #               $ft{totalcol} = 0;
234                         push @loopcol, \%cell;
235                 }
236         #       warn "fin des titres colonnes";
237         }
238         
239         my $i=0;
240 #       my @totalcol;
241         my $hilighted=-1;
242         
243         #Initialization of cell values.....
244         my @table;
245         
246 #       warn "init table";
247         for (my $i=1;$i<=$line;$i++) {
248                 foreach my $col ( @loopcol ) {
249 #                       warn " init table : $row->{rowtitle} / $col->{coltitle} ";
250                         $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
251                 }
252         }
253
254
255 # preparing calculation
256         my $strcalc ;
257         
258 # Processing average loanperiods
259         $strcalc .= "SELECT items.barcode, biblio.title, biblio.biblionumber, biblio.author";
260         $strcalc .= " , $colfield " if ($colfield);
261         $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";
262 #       @$filters[0]=~ s/\*/%/g if (@$filters[0]);
263 #       $strcalc .= " AND issues.timestamp <= '" . @$filters[0] ."'" if ( @$filters[0] );
264 #       @$filters[1]=~ s/\*/%/g if (@$filters[1]);
265 #       $strcalc .= " AND issues.timestamp >= '" . @$filters[1] ."'" if ( @$filters[1] );
266 #       @$filters[2]=~ s/\*/%/g if (@$filters[2]);
267 #       $strcalc .= " AND issues.returndate <= '" . @$filters[2] ."'" if ( @$filters[2] );
268 #       @$filters[3]=~ s/\*/%/g if (@$filters[3]);
269 #       $strcalc .= " AND issues.returndate >= '" . @$filters[3] ."'" if ( @$filters[3] );
270         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
271         $strcalc .= " AND items.homebranch like '" . @$filters[0] ."'" if ( @$filters[0] );
272         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
273         $strcalc .= " AND biblioitems.itemtype like '" . @$filters[1] ."'" if ( @$filters[1] );
274         
275         $strcalc .= " group by items.itemnumber";
276         $strcalc .= ", $colfield" if ($column);
277         $strcalc .= " order by $colfield " if ($colfield);
278         my $max;
279         if (@loopcol) {
280                 $max = $line*@loopcol;
281         } else { $max=$line;}
282         $strcalc .= " LIMIT 0,$max" if ($line);
283         warn "SQL :". $strcalc;
284         
285         my $dbcalc = $dbh->prepare($strcalc);
286         $dbcalc->execute;
287 #       warn "filling table";
288         my $previous_col;
289         my $i=1;
290         while (my  @data = $dbcalc->fetchrow) {
291                 my ($barcode,$title,$bibnum,$author, $col )=@data;
292                 $col = "zzEMPTY" if ($col eq undef);
293                 $i=1 if (($previous_col) and not($col eq $previous_col));
294                 $table[$i]->{$col}->{'barcode'}=$barcode;
295                 $table[$i]->{$col}->{'title'}=$title;
296                 $table[$i]->{$col}->{'bibnum'}=$bibnum;
297                 $table[$i]->{$col}->{'author'}=$author;
298 #               warn " ".$i." ".$col. " ".$row;
299                 $i++;
300                 $previous_col=$col;
301         }
302         
303         push @loopcol,{coltitle => "Global"} if not($column);
304         
305         my $max =(($line)?$line:@table);
306         for ($i=1; $i<$max;$i++) {
307                 my @loopcell;
308                 #@loopcol ensures the order for columns is common with column titles
309                 # and the number matches the number of columns
310                 my $colcount=0;
311                 foreach my $col ( @loopcol ) {
312                         my ($barcode, $author, $title, $bibnum);
313                         if (@loopcol){
314                                 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
315                                 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
316                                 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
317                                 $bibnum =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'bibnum'};
318                         } else {
319                                 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
320                                 $title =$table[$i]->{"zzEMPTY"}->{'title'};
321                                 $author =$table[$i]->{"zzEMPTY"}->{'author'};
322                                 $bibnum =$table[$i]->{"zzEMPTY"}->{'bibnum'};
323                         }
324                         push @loopcell, {author=> $author, title=>$title,bibnum=>$bibnum,barcode=>$barcode} ;
325                 }
326                 push @looprow,{ 'rowtitle' => $i ,
327                                                 'loopcell' => \@loopcell,
328                                                 'hilighted' => ($hilighted >0),
329                                         };
330                 $hilighted = -$hilighted;
331         }
332         
333                         
334
335         # the header of the table
336         $globalline{loopfilter}=\@loopfilter;
337         # the core of the table
338         $globalline{looprow} = \@looprow;
339         $globalline{loopcol} = \@loopcol;
340 #       # the foot (totals by borrower type)
341         $globalline{loopfooter} = \@loopfooter;
342         $globalline{total}= $grantotal;
343         $globalline{line} = $line;
344         $globalline{column} = $column;
345         push @mainloop,\%globalline;
346         return \@mainloop;
347 }
348
349 1;