Markup corrections and standardizations.
[koha.git] / 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         );
62 if ($do_it) {
63 # Displaying results
64     my $results = calculate($limit, $column, \@filters);
65     if ($output eq "screen"){
66 # Printing results to screen
67         $template->param(mainloop => $results);
68         output_html_with_http_headers $input, $cookie, $template->output;
69         exit(1);
70     } else {
71 # Printing to a csv file
72         print $input->header(-type => 'application/vnd.sun.xml.calc',
73                                     -encoding    => 'utf-8',
74             -attachment=>"$basename.csv",
75             -filename=>"$basename.csv" );
76         my $cols = @$results[0]->{loopcol};
77         my $lines = @$results[0]->{looprow};
78         my $sep;
79         $sep =C4::Context->preference("delimiter");
80 # header top-right
81         print "num /". @$results[0]->{column} .$sep;
82 # Other header
83         foreach my $col ( @$cols ) {
84             print $col->{coltitle}.$sep;
85         }
86         print "Total\n";
87 # Table
88         foreach my $line ( @$lines ) {
89             my $x = $line->{loopcell};
90             print $line->{rowtitle}.$sep;
91             foreach my $cell (@$x) {
92                 print $cell->{value}.$sep;
93             }
94             print $line->{totalrow};
95             print "\n";
96         }
97 # footer
98         print "TOTAL";
99         $cols = @$results[0]->{loopfooter};
100         foreach my $col ( @$cols ) {
101             print $sep.$col->{totalcol};
102         }
103         print $sep.@$results[0]->{total};
104         exit(1);
105     }
106 # Displaying choices
107 } else {
108     my $dbh = C4::Context->dbh;
109     my @values;
110     my %labels;
111     my %select;
112     my $req;
113     
114     my @mime = ( C4::Context->preference("MIME") );
115 #       foreach my $mime (@mime){
116 #               warn "".$mime;
117 #       }
118     
119     my $CGIextChoice=CGI::scrolling_list(
120                 -name     => 'MIME',
121                 -id       => 'MIME',
122                 -values   => \@mime,
123                 -size     => 1,
124                 -multiple => 0 );
125     
126     my @dels = ( C4::Context->preference("delimiter") );
127     my $CGIsepChoice=CGI::scrolling_list(
128                 -name     => 'sep',
129                 -id       => 'sep',
130                 -values   => \@dels,
131                 -size     => 1,
132                 -multiple => 0 );
133     #doctype
134     my $itemtypes = GetItemTypes;
135     my @itemtypeloop;
136     foreach my $thisitemtype (keys %$itemtypes) {
137 #                       my $selected = 1 if $thisbranch eq $branch;
138             my %row =(value => $thisitemtype,
139 #                                                                       selected => $selected,
140                                     description => $itemtypes->{$thisitemtype}->{'description'},
141                             );
142             push @itemtypeloop, \%row;
143     }
144         
145     #branch
146     my $branches = GetBranches;
147     my @branchloop;
148     foreach my $thisbranch (keys %$branches) {
149 #                       my $selected = 1 if $thisbranch eq $branch;
150             my %row =(value => $thisbranch,
151 #                                                                       selected => $selected,
152                                     branchname => $branches->{$thisbranch}->{'branchname'},
153                             );
154             push @branchloop, \%row;
155     }
156     
157     $template->param(
158                     CGIextChoice => $CGIextChoice,
159                     CGIsepChoice => $CGIsepChoice,
160                     itemtypeloop =>\@itemtypeloop,
161                     branchloop =>\@branchloop,
162                     );
163 output_html_with_http_headers $input, $cookie, $template->output;
164 }
165
166
167
168
169 sub calculate {
170     my ($line, $column, $filters) = @_;
171     my @mainloop;
172     my @loopfooter;
173     my @loopcol;
174     my @loopline;
175     my @looprow;
176     my %globalline;
177     my $grantotal =0;
178 # extract parameters
179     my $dbh = C4::Context->dbh;
180
181 # Filters
182 # Checking filters
183 #
184     my @loopfilter;
185     for (my $i=0;$i<=6;$i++) {
186         my %cell;
187         if ( @$filters[$i] ) {
188             if (($i==1) and (@$filters[$i-1])) {
189                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
190             }
191             $cell{filter} .= @$filters[$i];
192             $cell{crit} .="Branch" if ($i==0);
193             $cell{crit} .="Doc Type" if ($i==1);
194             push @loopfilter, \%cell;
195         }
196     }
197     my $colfield;
198     my $colorder;
199     if ($column){
200         $column = "issues.".$column if (($column=~/branchcode/) or ($column=~/timestamp/));
201         $column = "biblioitems.".$column if $column=~/itemtype/;
202         $column = "borrowers.".$column if $column=~/categorycode/;
203         my @colfilter ;
204         $colfilter[0] = @$filters[0] if ($column =~ /branch/ )  ;
205         $colfilter[0] = @$filters[1] if ($column =~ /itemtype/ )  ;
206                                                 
207     # loop cols.
208         $colfield .= $column;
209         $colorder .= $column;
210         
211         my $strsth2;
212         $strsth2 .= "select distinctrow $colfield 
213           FROM `old_issues` 
214             LEFT JOIN borrowers ON borrowers.borrowernumber=old_issues.borrowernumber 
215             LEFT JOIN items ON old_issues.itemnumber=items.itemnumber 
216             LEFT JOIN biblioitems ON biblioitems.biblioitemnumber=items.biblioitemnumber  
217             WHERE old_issues.itemnumber=items.itemnumber 
218             AND old_issues.borrowernumber=borrowers.borrowernumber";
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 
266                         LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber  
267                         LEFT JOIN biblio ON biblio.biblionumber=items.biblionumber) 
268                   LEFT JOIN old_issues ON  old_issues.itemnumber=items.itemnumber 
269                   WHERE old_issues.itemnumber is null";
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 = (@loopcol) ? $line*@loopcol : $line ;
279     $strcalc .= " LIMIT 0,$max"        if ($line);
280     warn "SQL :". $strcalc;
281     
282     my $dbcalc = $dbh->prepare($strcalc);
283     $dbcalc->execute;
284 #       warn "filling table";
285     my $previous_col;
286     $i=1;
287     while (my  @data = $dbcalc->fetchrow) {
288         my ($barcode,$title,$biblionumber,$author, $col )=@data;
289         $col = "zzEMPTY" if ($col eq undef);
290         $i=1 if (($previous_col) and not($col eq $previous_col));
291         $table[$i]->{$col}->{'barcode'}=$barcode;
292         $table[$i]->{$col}->{'title'}=$title;
293         $table[$i]->{$col}->{'biblionumber'}=$biblionumber;
294         $table[$i]->{$col}->{'author'}=$author;
295 #               warn " ".$i." ".$col. " ".$row;
296         $i++;
297         $previous_col=$col;
298     }
299     
300     push @loopcol,{coltitle => "Global"} if not($column);
301     
302     $max =(($line)?$line:@table);
303     for ($i=1; $i<=$max;$i++) {
304         my @loopcell;
305         #@loopcol ensures the order for columns is common with column titles
306         # and the number matches the number of columns
307         my $colcount=0;
308         foreach my $col ( @loopcol ) {
309             my ($barcode, $author, $title, $biblionumber);
310             if (@loopcol){
311                 $barcode =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'barcode'};
312                 $title =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'title'};
313                 $author =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'author'};
314                 $biblionumber =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}}->{'biblionumber'};
315             } else {
316                 $barcode =$table[$i]->{"zzEMPTY"}->{'barcode'};
317                 $title =$table[$i]->{"zzEMPTY"}->{'title'};
318                 $author =$table[$i]->{"zzEMPTY"}->{'author'};
319                 $biblionumber =$table[$i]->{"zzEMPTY"}->{'biblionumber'};
320             }
321             push @loopcell, {author=> $author, title=>$title,biblionumber=>$biblionumber,barcode=>$barcode} ;
322         }
323         push @looprow,{ 'rowtitle' => $i ,
324                         'loopcell' => \@loopcell,
325                         'hilighted' => ($hilighted >0),
326                     };
327         $hilighted = -$hilighted;
328     }
329     
330             
331
332     # the header of the table
333     $globalline{loopfilter}=\@loopfilter;
334     # the core of the table
335     $globalline{looprow} = \@looprow;
336     $globalline{loopcol} = \@loopcol;
337 #       # the foot (totals by borrower type)
338     $globalline{loopfooter} = \@loopfooter;
339     $globalline{total}= $grantotal;
340     $globalline{line} = $line;
341     $globalline{column} = $column;
342     push @mainloop,\%globalline;
343     return \@mainloop;
344 }
345
346 1;