Bug 35453: Fix wrong 'Laserdisc)' string on 007 builder (MARC21)
[koha.git] / reports / borrowers_out.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth qw( get_template_and_user );
24 use C4::Context;
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Reports qw( GetDelimiterChoices );
27
28 use Koha::Patron::Categories;
29
30 =head1 NAME
31
32 reports/borrowers_out.pl
33
34 =head1 DESCRIPTION
35
36 Plugin that shows a stats on borrowers
37
38 =cut
39
40 my $input = CGI->new;
41 my $do_it=$input->param('do_it');
42 my $fullreportname = "reports/borrowers_out.tt";
43 my $limit = $input->param("Limit");
44 my $column = $input->param("Criteria");
45 my @filters = $input->multi_param("Filter");
46
47 my $output = $input->param("output");
48 my $basename = $input->param("basename");
49 our $sep     = C4::Context->csv_delimiter(scalar $input->param("sep"));
50 my ($template, $borrowernumber, $cookie)
51     = get_template_and_user({template_name => $fullreportname,
52                 query => $input,
53                 type => "intranet",
54                 flagsrequired => {reports => '*'},
55                 });
56 $template->param(do_it => $do_it,
57         );
58 if ($do_it) {
59 # Displaying results
60     my $results = calculate($limit, $column, \@filters);
61     if ($output eq "screen"){
62 # Printing results to screen
63         $template->param(mainloop => $results);
64         output_html_with_http_headers $input, $cookie, $template->output;
65         exit;
66     } else {
67 # Printing to a csv file
68         print $input->header(-type => 'application/vnd.sun.xml.calc',
69                             -encoding    => 'utf-8',
70                             -attachment=>"$basename.csv",
71                             -filename=>"$basename.csv" );
72         my $cols = @$results[0]->{loopcol};
73         my $lines = @$results[0]->{looprow};
74 # header top-right
75         print "num /". @$results[0]->{column} .$sep;
76 # Other header
77         foreach my $col ( @$cols ) {
78             print $col->{coltitle}.$sep;
79         }
80         print "Total\n";
81 # Table
82         foreach my $line ( @$lines ) {
83             my $x = $line->{loopcell};
84             print $line->{rowtitle}.$sep;
85             foreach my $cell (@$x) {
86                 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
87                 print $cellvalue;
88             }
89 #            print $line->{totalrow};
90             print "\n";
91         }
92 # footer
93         print "TOTAL";
94         $cols = @$results[0]->{loopfooter};
95         foreach my $col ( @$cols ) {
96             print $sep.$col->{totalcol};
97         }
98         print $sep.@$results[0]->{total};
99         exit;
100     }
101 # Displaying choices
102 } else {
103     my $dbh = C4::Context->dbh;
104
105     my $CGIextChoice = ( 'CSV' ); # FIXME translation
106         my $CGIsepChoice = GetDelimiterChoices;
107
108     my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
109     $template->param(
110                     CGIextChoice => $CGIextChoice,
111                     CGIsepChoice => $CGIsepChoice,
112                     patron_categories => $patron_categories,
113                     );
114 output_html_with_http_headers $input, $cookie, $template->output;
115 }
116
117
118 sub calculate {
119     my ($line, $column, $filters) = @_;
120     my @mainloop;
121     my @loopfooter;
122     my @loopcol;
123     my @looprow;
124     my %globalline;
125     my $grantotal =0;
126 # extract parameters
127     my $dbh = C4::Context->dbh;
128
129 # Filters
130 # Checking filters
131 #
132     my @loopfilter;
133     for (my $i=0;$i<=2;$i++) {
134         my %cell;
135         if ( @$filters[$i] ) {
136             if (($i==1) and (@$filters[$i-1])) {
137                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
138             }
139             $cell{filter} .= @$filters[$i];
140             $cell{crit} .="Bor Cat" if ($i==0);
141             $cell{crit} .="Without issues since" if ($i==1);
142             push @loopfilter, \%cell;
143         }
144     }
145     my $colfield;
146     my $colorder;
147     if ($column){
148         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
149         my @colfilter ;
150         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
151     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
152     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
153                                                 
154     # loop cols.
155         $colfield .= $column;
156         $colorder .= $column;
157         
158         my $strsth2;
159         $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
160         my @query_args;
161         if ( $colfilter[0] ) {
162             $colfilter[0] =~ s/\*/%/g;
163             $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
164             push @query_args, $colfilter[0];
165         }
166         $strsth2 .=" group by " . $dbh->quote($colfield);
167         $strsth2 .=" order by " . $dbh->quote($colorder);
168         # warn "". $strsth2;
169         
170         my $sth2 = $dbh->prepare( $strsth2 );
171         $sth2->execute( @query_args );
172         while (my ($celvalue) = $sth2->fetchrow) {
173             my %cell;
174     #           my %ft;
175     #           warn "coltitle :".$celvalue;
176             $cell{coltitle} = $celvalue;
177     #           $ft{totalcol} = 0;
178             push @loopcol, \%cell;
179         }
180     #   warn "fin des titres colonnes";
181     }
182     
183     my $i=0;
184 #       my @totalcol;
185     
186     #Initialization of cell values.....
187     my @table;
188     
189 #       warn "init table";
190     if($line) {
191         for (my $i=1;$i<=$line;$i++) {
192             foreach my $col ( @loopcol ) {
193                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
194             }
195         }
196     }
197
198
199 # preparing calculation
200     my $strcalc ;
201     
202 # Processing calculation
203     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
204     $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
205     $strcalc .= " FROM borrowers ";
206     $strcalc .= "WHERE 1 ";
207     if(C4::Context->preference('IndependentBranches') && !C4::Context->IsSuperLibrarian()){
208       $strcalc .= "AND branchcode = '".C4::Context->userenv->{branch}."' ";
209     }
210
211
212     my @query_args;
213     if ( @$filters[0] ) {
214         @$filters[0]=~ s/\*/%/g;
215         $strcalc .= " AND borrowers.categorycode like ?";
216         push @query_args, @$filters[0];
217     }
218     $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
219     if ( @$filters[1] ) {
220         $strcalc .= " AND issues.timestamp > ?";
221         push @query_args, @$filters[1];
222     }
223     $strcalc .= ") ";
224     $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
225     if ( @$filters[1] ) {
226         $strcalc .= " AND old_issues.timestamp > ?";
227         push @query_args, @$filters[1];
228     }
229     $strcalc .= ") ";
230     $strcalc .= " group by borrowers.borrowernumber";
231     $strcalc .= ", " . $dbh->quote($colfield) if ($column);
232     $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
233     my $max;
234     if ($line) {
235         if (@loopcol) {
236             $max = $line*@loopcol;
237         } else { $max=$line;}
238         $strcalc .= " LIMIT 0,$max";
239      } 
240     
241     my $dbcalc = $dbh->prepare($strcalc);
242     $dbcalc->execute( @query_args );
243 #       warn "filling table";
244     my $previous_col;
245     $i=1;
246     while (my  @data = $dbcalc->fetchrow) {
247         my ($row, $col )=@data;
248         $col = "zzEMPTY" if (!defined($col));
249         $i=1 if (($previous_col) and not($col eq $previous_col));
250         $table[$i]->{$col}=$row;
251 #               warn " $i $col $row";
252         $i++;
253         $previous_col=$col;
254     }
255     
256     push @loopcol,{coltitle => "Global"} if not($column);
257     
258     $max =(($line)?$line:@table -1);
259     for ($i=1; $i<=$max;$i++) {
260         my @loopcell;
261         #@loopcol ensures the order for columns is common with column titles
262         # and the number matches the number of columns
263         my $colcount=0;
264         foreach my $col ( @loopcol ) {
265             my $value;
266             if (@loopcol){
267                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
268             } else {
269                 $value =$table[$i]->{"zzEMPTY"};
270             }
271             push @loopcell, {value => $value} ;
272         }
273         push @looprow,{ 'rowtitle' => $i ,
274                         'loopcell' => \@loopcell,
275                     };
276     }
277     
278             
279
280     # the header of the table
281     $globalline{loopfilter}=\@loopfilter;
282     # the core of the table
283     $globalline{looprow} = \@looprow;
284     $globalline{loopcol} = \@loopcol;
285 #       # the foot (totals by borrower type)
286     $globalline{loopfooter} = \@loopfooter;
287     $globalline{total}= $grantotal;
288     $globalline{line} = $line;
289     $globalline{column} = $column;
290     push @mainloop,\%globalline;
291     return \@mainloop;
292 }
293
294 1;
295 __END__