Bug 22814: Make patron names on holds modal consistent (second check-in)
[koha.git] / reports / borrowers_out.pl
blob762a0d4d09f721c41e5b6a4799bc7a9e2fae8451
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
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.
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.
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>.
20 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
34 =head1 NAME
36 reports/borrowers_out.pl
38 =head1 DESCRIPTION
40 Plugin that shows a stats on borrowers
42 =cut
44 my $input = new CGI;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_out.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
51 if ( $filters[1] );
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 our $sep = $input->param("sep") || '';
56 $sep = "\t" if ($sep eq 'tabulation');
57 my ($template, $borrowernumber, $cookie)
58 = get_template_and_user({template_name => $fullreportname,
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {reports => '*'},
63 debug => 1,
64 });
65 $template->param(do_it => $do_it,
67 if ($do_it) {
68 # Displaying results
69 my $results = calculate($limit, $column, \@filters);
70 if ($output eq "screen"){
71 # Printing results to screen
72 $template->param(mainloop => $results);
73 output_html_with_http_headers $input, $cookie, $template->output;
74 exit;
75 } else {
76 # Printing to a csv file
77 print $input->header(-type => 'application/vnd.sun.xml.calc',
78 -encoding => 'utf-8',
79 -attachment=>"$basename.csv",
80 -filename=>"$basename.csv" );
81 my $cols = @$results[0]->{loopcol};
82 my $lines = @$results[0]->{looprow};
83 # header top-right
84 print "num /". @$results[0]->{column} .$sep;
85 # Other header
86 foreach my $col ( @$cols ) {
87 print $col->{coltitle}.$sep;
89 print "Total\n";
90 # Table
91 foreach my $line ( @$lines ) {
92 my $x = $line->{loopcell};
93 print $line->{rowtitle}.$sep;
94 foreach my $cell (@$x) {
95 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
96 print $cellvalue;
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;
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 $CGIextChoice = ( 'CSV' ); # FIXME translation
119 my $CGIsepChoice = GetDelimiterChoices;
121 my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['categorycode']});
122 $template->param(
123 CGIextChoice => $CGIextChoice,
124 CGIsepChoice => $CGIsepChoice,
125 patron_categories => $patron_categories,
127 output_html_with_http_headers $input, $cookie, $template->output;
131 sub calculate {
132 my ($line, $column, $filters) = @_;
133 my @mainloop;
134 my @loopfooter;
135 my @loopcol;
136 my @loopline;
137 my @looprow;
138 my %globalline;
139 my $grantotal =0;
140 # extract parameters
141 my $dbh = C4::Context->dbh;
143 # Filters
144 # Checking filters
146 my @loopfilter;
147 for (my $i=0;$i<=2;$i++) {
148 my %cell;
149 if ( @$filters[$i] ) {
150 if (($i==1) and (@$filters[$i-1])) {
151 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
153 $cell{filter} .= @$filters[$i];
154 $cell{crit} .="Bor Cat" if ($i==0);
155 $cell{crit} .="Without issues since" if ($i==1);
156 push @loopfilter, \%cell;
159 my $colfield;
160 my $colorder;
161 if ($column){
162 $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
163 my @colfilter ;
164 $colfilter[0] = @$filters[0] if ($column =~ /category/ ) ;
165 # $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
166 #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
168 # loop cols.
169 $colfield .= $column;
170 $colorder .= $column;
172 my $strsth2;
173 $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
174 my @query_args;
175 if ( $colfilter[0] ) {
176 $colfilter[0] =~ s/\*/%/g;
177 $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
178 push @query_args, $colfilter[0];
180 $strsth2 .=" group by " . $dbh->quote($colfield);
181 $strsth2 .=" order by " . $dbh->quote($colorder);
182 # warn "". $strsth2;
184 my $sth2 = $dbh->prepare( $strsth2 );
185 $sth2->execute( @query_args );
186 while (my ($celvalue) = $sth2->fetchrow) {
187 my %cell;
188 # my %ft;
189 # warn "coltitle :".$celvalue;
190 $cell{coltitle} = $celvalue;
191 # $ft{totalcol} = 0;
192 push @loopcol, \%cell;
194 # warn "fin des titres colonnes";
197 my $i=0;
198 # my @totalcol;
200 #Initialization of cell values.....
201 my @table;
203 # warn "init table";
204 if($line) {
205 for (my $i=1;$i<=$line;$i++) {
206 foreach my $col ( @loopcol ) {
207 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
213 # preparing calculation
214 my $strcalc ;
216 # Processing calculation
217 $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
218 $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
219 $strcalc .= " FROM borrowers ";
220 $strcalc .= "WHERE 1 ";
221 my @query_args;
222 if ( @$filters[0] ) {
223 @$filters[0]=~ s/\*/%/g;
224 $strcalc .= " AND borrowers.categorycode like ?";
225 push @query_args, @$filters[0];
227 $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
228 if ( @$filters[1] ) {
229 $strcalc .= " AND issues.timestamp > ?";
230 push @query_args, @$filters[1];
232 $strcalc .= ") ";
233 $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
234 if ( @$filters[1] ) {
235 $strcalc .= " AND old_issues.timestamp > ?";
236 push @query_args, @$filters[1];
238 $strcalc .= ") ";
239 $strcalc .= " group by borrowers.borrowernumber";
240 $strcalc .= ", " . $dbh->quote($colfield) if ($column);
241 $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
242 my $max;
243 if ($line) {
244 if (@loopcol) {
245 $max = $line*@loopcol;
246 } else { $max=$line;}
247 $strcalc .= " LIMIT 0,$max";
250 my $dbcalc = $dbh->prepare($strcalc);
251 $dbcalc->execute( @query_args );
252 # warn "filling table";
253 my $previous_col;
254 $i=1;
255 while (my @data = $dbcalc->fetchrow) {
256 my ($row, $col )=@data;
257 $col = "zzEMPTY" if (!defined($col));
258 $i=1 if (($previous_col) and not($col eq $previous_col));
259 $table[$i]->{$col}=$row;
260 # warn " $i $col $row";
261 $i++;
262 $previous_col=$col;
265 push @loopcol,{coltitle => "Global"} if not($column);
267 $max =(($line)?$line:@table -1);
268 for ($i=1; $i<=$max;$i++) {
269 my @loopcell;
270 #@loopcol ensures the order for columns is common with column titles
271 # and the number matches the number of columns
272 my $colcount=0;
273 foreach my $col ( @loopcol ) {
274 my $value;
275 if (@loopcol){
276 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
277 } else {
278 $value =$table[$i]->{"zzEMPTY"};
280 push @loopcell, {value => $value} ;
282 push @looprow,{ 'rowtitle' => $i ,
283 'loopcell' => \@loopcell,
289 # the header of the table
290 $globalline{loopfilter}=\@loopfilter;
291 # the core of the table
292 $globalline{looprow} = \@looprow;
293 $globalline{loopcol} = \@loopcol;
294 # # the foot (totals by borrower type)
295 $globalline{loopfooter} = \@loopfooter;
296 $globalline{total}= $grantotal;
297 $globalline{line} = $line;
298 $globalline{column} = $column;
299 push @mainloop,\%globalline;
300 return \@mainloop;
304 __END__