Bug 18887: Make DB update idempotent
[koha.git] / reports / borrowers_stats.pl
blob30e277cc577d823ca96dfad8eef7be6486dd5bf2
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;
21 use CGI qw ( -utf8 );
22 use List::MoreUtils qw/uniq/;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Acquisition;
28 use C4::Output;
29 use C4::Reports;
30 use C4::Circulation;
31 use C4::Members::AttributeTypes;
33 use Koha::AuthorisedValues;
34 use Koha::DateUtils;
35 use Koha::Libraries;
36 use Koha::Patron::Categories;
38 use Date::Calc qw(
39 Today
40 Add_Delta_YM
43 =head1 NAME
45 plugin that shows a stats on borrowers
47 =head1 DESCRIPTION
49 =cut
51 my $input = new CGI;
52 my $do_it=$input->param('do_it');
53 my $fullreportname = "reports/borrowers_stats.tt";
54 my $line = $input->param("Line");
55 my $column = $input->param("Column");
56 my @filters = $input->multi_param("Filter");
57 $filters[3] = eval { output_pref( { dt => dt_from_string( $filters[3]), dateonly => 1, dateformat => 'iso' } ); }
58 if ( $filters[3] );
59 $filters[4] = eval { output_pref ({ dt => dt_from_string( $filters[4]), dateonly => 1, dateformat => 'iso' } ); }
60 if ( $filters[4] );
61 my $digits = $input->param("digits");
62 our $period = $input->param("period");
63 my $borstat = $input->param("status");
64 my $borstat1 = $input->param("activity");
65 my $output = $input->param("output");
66 my $basename = $input->param("basename");
67 our $sep = $input->param("sep");
68 $sep = "\t" if ($sep and $sep eq 'tabulation');
70 my ($template, $borrowernumber, $cookie)
71 = get_template_and_user({template_name => $fullreportname,
72 query => $input,
73 type => "intranet",
74 authnotrequired => 0,
75 flagsrequired => {reports => '*'},
76 debug => 1,
77 });
78 $template->param(do_it => $do_it);
79 if ($do_it) {
80 my $attributes;
81 if (C4::Context->preference('ExtendedPatronAttributes')) {
82 $attributes = parse_extended_patron_attributes($input);
84 my $results = calculate($line, $column, $digits, $borstat,$borstat1 ,\@filters, $attributes);
85 if ($output eq "screen"){
86 $template->param(mainloop => $results);
87 output_html_with_http_headers $input, $cookie, $template->output;
88 } else {
89 print $input->header(-type => 'application/vnd.sun.xml.calc',
90 -encoding => 'utf-8',
91 -name => "$basename.csv",
92 -attachment => "$basename.csv");
93 my $cols = @$results[0]->{loopcol};
94 my $lines = @$results[0]->{looprow};
95 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
96 foreach my $col ( @$cols ) {
97 print $col->{coltitle}.$sep;
99 print "Total\n";
100 foreach my $line ( @$lines ) {
101 my $x = $line->{loopcell};
102 print $line->{rowtitle}.$sep;
103 foreach my $cell (@$x) {
104 print $cell->{value}.$sep;
106 print $line->{totalrow};
107 print "\n";
109 print "TOTAL";
110 $cols = @$results[0]->{loopfooter};
111 foreach my $col ( @$cols ) {
112 print $sep.$col->{totalcol};
114 print $sep.@$results[0]->{total};
116 exit; # exit after do_it, regardless
117 } else {
118 my $dbh = C4::Context->dbh;
119 my $req;
120 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
121 $template->param( patron_categories => $patron_categories );
122 $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
123 $req->execute;
124 $template->param( ZIP_LOOP => $req->fetchall_arrayref({}));
125 $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib");
126 $req->execute;
127 $template->param( SORT1_LOOP => $req->fetchall_arrayref({}));
128 $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort2 <> '' ORDER BY sort2 LIMIT 200");
129 # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance.
130 $req->execute;
131 $template->param( SORT2_LOOP => $req->fetchall_arrayref({}));
133 my $CGIextChoice = ( 'CSV' ); # FIXME translation
134 my $CGIsepChoice=GetDelimiterChoices;
135 $template->param(
136 CGIextChoice => $CGIextChoice,
137 CGIsepChoice => $CGIsepChoice,
139 if (C4::Context->preference('ExtendedPatronAttributes')) {
140 $template->param(ExtendedPatronAttributes => 1);
141 patron_attributes_form($template);
144 output_html_with_http_headers $input, $cookie, $template->output;
146 sub calculate {
147 my ($line, $column, $digits, $status, $activity, $filters, $attr_filters) = @_;
149 my @mainloop;
150 my @loopfooter;
151 my @loopcol;
152 my @loopline;
153 my @looprow;
154 my %globalline;
155 my $grantotal =0;
156 # extract parameters
157 my $dbh = C4::Context->dbh;
159 # check parameters
160 my @valid_names = qw(categorycode zipcode branchcode sex sort1 sort2);
161 my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes;
162 if ($line =~ /^patron_attr\.(.*)/) {
163 my $attribute_type = $1;
164 return unless (grep {$attribute_type eq $_->{code}} @attribute_types);
165 } else {
166 return unless (grep /^$line$/, @valid_names);
168 if ($column =~ /^patron_attr\.(.*)/) {
169 my $attribute_type = $1;
170 return unless (grep {$attribute_type eq $_->{code}} @attribute_types);
171 } else {
172 return unless (grep /^$column$/, @valid_names);
174 return if ($digits and $digits !~ /^\d+$/);
175 return if ($status and (grep /^$status$/, qw(debarred gonenoaddress lost)) == 0);
176 return if ($activity and (grep /^$activity$/, qw(active nonactive)) == 0);
178 # Filters
179 my $linefilter;
180 if ( $line =~ /categorycode/ ) { $linefilter = @$filters[0]; }
181 elsif ( $line =~ /zipcode/ ) { $linefilter = @$filters[1]; }
182 elsif ( $line =~ /branchcode/ ) { $linefilter = @$filters[2]; }
183 elsif ( $line =~ /sex/ ) { $linefilter = @$filters[5]; }
184 elsif ( $line =~ /sort1/ ) { $linefilter = @$filters[6]; }
185 elsif ( $line =~ /sort2/ ) { $linefilter = @$filters[7]; }
186 elsif ( $line =~ /^patron_attr\.(.*)$/ ) { $linefilter = $attr_filters->{$1}; }
187 else { $linefilter = ''; }
189 my $colfilter;
190 if ( $column =~ /categorycode/ ) { $colfilter = @$filters[0]; }
191 elsif ( $column =~ /zipcode/ ) { $colfilter = @$filters[1]; }
192 elsif ( $column =~ /branchcode/) { $colfilter = @$filters[2]; }
193 elsif ( $column =~ /sex/) { $colfilter = @$filters[5]; }
194 elsif ( $column =~ /sort1/) { $colfilter = @$filters[6]; }
195 elsif ( $column =~ /sort2/) { $colfilter = @$filters[7]; }
196 elsif ( $column =~ /^patron_attr\.(.*)$/) { $colfilter = $attr_filters->{$1}; }
197 else { $colfilter = ''; }
199 my @loopfilter;
200 foreach my $i (0 .. scalar @$filters) {
201 my %cell;
202 if ( @$filters[$i] ) {
203 if ($i == 3 or $i == 4) {
204 $cell{filter} = eval { output_pref( { dt => dt_from_string( @$filters[$i] ), dateonly => 1 }); }
205 if ( @$filters[$i] );
206 } else {
207 $cell{filter} = @$filters[$i];
210 if ( $i == 0) { $cell{crit} = "Cat code"; }
211 elsif ( $i == 1 ) { $cell{crit} = "ZIP/Postal code"; }
212 elsif ( $i == 2 ) { $cell{crit} = "Branch code"; }
213 elsif ( $i == 3 ||
214 $i == 4 ) { $cell{crit} = "Date of birth"; }
215 elsif ( $i == 5 ) { $cell{crit} = "Sex"; }
216 elsif ( $i == 6 ) { $cell{crit} = "Sort1"; }
217 elsif ( $i == 7 ) { $cell{crit} = "Sort2"; }
218 else { $cell{crit} = "Unknown"; }
220 push @loopfilter, \%cell;
223 foreach my $type (keys %$attr_filters) {
224 if($attr_filters->{$type}) {
225 push @loopfilter, {
226 crit => "Attribute $type",
227 filter => $attr_filters->{$type}
232 my @branchcodes = map { $_->branchcode } Koha::Libraries->search;
233 ($status ) and push @loopfilter,{crit=>"Status", filter=>$status };
234 ($activity) and push @loopfilter,{crit=>"Activity",filter=>$activity};
235 # year of activity
236 my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
237 my $newperioddate=$period_year."-".$period_month."-".$period_day;
238 # 1st, loop rows.
239 my $linefield;
241 my $line_attribute_type;
242 if ($line =~/^patron_attr\.(.*)$/) {
243 $line_attribute_type = $1;
244 $line = 'borrower_attributes.attribute';
247 if (($line =~/zipcode/) and ($digits)) {
248 $linefield = "left($line,$digits)";
249 } else {
250 $linefield = $line;
252 my $patron_categories = Koha::Patron::Categories->search({}, {order_by => ['categorycode']});
254 my $strsth;
255 my @strparams; # bind parameters for the query
256 if ($line_attribute_type) {
257 $strsth = "SELECT distinct attribute FROM borrower_attributes
258 WHERE attribute IS NOT NULL AND code=?";
259 push @strparams, $line_attribute_type;
260 } else {
261 $strsth = "SELECT distinctrow $linefield FROM borrowers
262 WHERE $line IS NOT NULL ";
265 $linefilter =~ s/\*/%/g;
266 if ( $linefilter ) {
267 $strsth .= " AND $linefield LIKE ? " ;
268 push @strparams, $linefilter;
270 $strsth .= " AND $status='1' " if ($status);
271 $strsth .=" order by $linefield";
273 my $sth = $dbh->prepare($strsth);
274 $sth->execute(@strparams);
275 while (my ($celvalue) = $sth->fetchrow) {
276 my %cell;
277 if ($celvalue) {
278 $cell{rowtitle} = $celvalue;
279 $cell{rowtitle_display} = ($patron_categories->find($celvalue)->description || "$celvalue\*") if ($line eq 'categorycode');
281 $cell{totalrow} = 0;
282 push @loopline, \%cell;
285 # 2nd, loop cols.
286 my $colfield;
288 my $column_attribute_type;
289 if ($column =~/^patron_attr.(.*)$/) {
290 $column_attribute_type = $1;
291 $column = 'borrower_attributes.attribute';
294 if (($column =~/zipcode/) and ($digits)) {
295 $colfield = "left($column,$digits)";
296 } else {
297 $colfield = $column;
300 my $strsth2;
301 my @strparams2; # bind parameters for the query
302 if ($column_attribute_type) {
303 $strsth2 = "SELECT DISTINCT attribute FROM borrower_attributes
304 WHERE attribute IS NOT NULL AND code=?";
305 push @strparams2, $column_attribute_type;
306 } else {
307 $strsth2 = "SELECT DISTINCTROW $colfield FROM borrowers
308 WHERE $column IS NOT NULL";
311 if ($colfilter) {
312 $colfilter =~ s/\*/%/g;
313 $strsth2 .= " AND $colfield LIKE ? ";
314 push @strparams2, $colfield;
316 $strsth2 .= " AND $status='1' " if ($status);
318 $strsth2 .= " order by $colfield";
319 my $sth2 = $dbh->prepare($strsth2);
320 $sth2->execute(@strparams2);
321 while (my ($celvalue) = $sth2->fetchrow) {
322 my %cell;
323 if (defined $celvalue) {
324 $cell{coltitle} = $celvalue;
325 # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
326 $cell{coltitle_display} = $patron_categories->find($celvalue)->description if ($column eq 'categorycode');
328 push @loopcol, \%cell;
331 my $i=0;
332 #Initialization of cell values.....
333 my %table;
334 # warn "init table";
335 foreach my $row (@loopline) {
336 foreach my $col ( @loopcol ) {
337 my $rowtitle = $row->{rowtitle} // '';
338 my $coltitle = $row->{coltitle} // '';
339 $table{$rowtitle}->{$coltitle} = 0;
341 $row->{rowtitle} ||= '';
342 $table{$row->{rowtitle}}->{totalrow}=0;
343 $table{$row->{rowtitle}}->{rowtitle_display} = $row->{rowtitle_display};
346 # preparing calculation
347 my $strcalc;
348 my @calcparams;
349 $strcalc = "SELECT ";
350 if ($line_attribute_type) {
351 $strcalc .= " attribute_$line_attribute_type.attribute AS line_attribute, ";
352 } else {
353 $strcalc .= " $linefield, ";
355 if ($column_attribute_type) {
356 $strcalc .= " attribute_$column_attribute_type.attribute AS column_attribute, ";
357 } else {
358 $strcalc .= " $colfield, ";
361 $strcalc .= " COUNT(*) FROM borrowers ";
362 foreach my $type (keys %$attr_filters) {
363 if (
364 ($line_attribute_type and $line_attribute_type eq $type)
365 or ($column_attribute_type and $column_attribute_type eq $type)
366 or ($attr_filters->{$type})
368 $strcalc .= " LEFT JOIN borrower_attributes AS attribute_$type
369 ON (borrowers.borrowernumber = attribute_$type.borrowernumber
370 AND attribute_$type.code = " . $dbh->quote($type) . ") ";
373 $strcalc .= " WHERE 1 ";
375 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
376 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
377 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
378 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
379 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
380 $strcalc .= " AND branchcode like '" . @$filters[2] ."'" if ( @$filters[2] );
381 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
382 $strcalc .= " AND dateofbirth > '" . @$filters[3] ."'" if ( @$filters[3] );
383 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
384 $strcalc .= " AND dateofbirth < '" . @$filters[4] ."'" if ( @$filters[4] );
385 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
386 $strcalc .= " AND sex like '" . @$filters[5] ."'" if ( @$filters[5] );
387 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
388 $strcalc .= " AND sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
389 @$filters[7]=~ s/\*/%/g if (@$filters[7]);
390 $strcalc .= " AND sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
392 foreach my $type (keys %$attr_filters) {
393 if($attr_filters->{$type}) {
394 my $filter = $attr_filters->{$type};
395 $filter =~ s/\*/%/g;
396 $strcalc .= " AND attribute_$type.attribute LIKE '" . $filter . "' ";
399 $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
400 $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "' AND borrowernumber IS NOT NULL)" if ($activity eq 'nonactive');
401 $strcalc .= " AND $status='1' " if ($status);
403 $strcalc .= " GROUP BY ";
404 if ($line_attribute_type) {
405 $strcalc .= " line_attribute, ";
406 } else {
407 $strcalc .= " $linefield, ";
409 if ($column_attribute_type) {
410 $strcalc .= " column_attribute ";
411 } else {
412 $strcalc .= " $colfield ";
415 my $dbcalc = $dbh->prepare($strcalc);
416 (scalar(@calcparams)) ? $dbcalc->execute(@calcparams) : $dbcalc->execute();
418 my $emptycol;
419 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
420 # warn "filling table $row / $col / $value ";
421 $emptycol = 1 if (!defined($col));
422 $col = "zzEMPTY" if (!defined($col));
423 $row = "zzEMPTY" if (!defined($row));
425 $table{$row}->{$col}+=$value;
426 $table{$row}->{totalrow}+=$value;
427 $grantotal += $value;
430 push @loopcol,{coltitle => "NULL"} if ($emptycol);
432 foreach my $row (sort keys %table) {
433 my @loopcell;
434 #@loopcol ensures the order for columns is common with column titles
435 # and the number matches the number of columns
436 foreach my $col ( @loopcol ) {
437 my $coltitle = $col->{coltitle} // '';
438 $coltitle = $coltitle eq "NULL" ? "zzEMPTY" : $coltitle;
439 my $value =$table{$row}->{$coltitle};
440 push @loopcell, {value => $value};
442 push @looprow,{
443 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
444 'rowtitle_display' => $table{$row}->{rowtitle_display} || ($row eq "zzEMPTY" ? "NULL" : $row),
445 'loopcell' => \@loopcell,
446 'totalrow' => $table{$row}->{totalrow}
450 foreach my $col ( @loopcol ) {
451 my $total=0;
452 foreach my $row ( @looprow ) {
453 my $rowtitle = $row->{rowtitle} // '';
454 $rowtitle = ($rowtitle eq "NULL") ? "zzEMPTY" : $rowtitle;
455 my $coltitle = $col->{coltitle} // '';
456 $coltitle = ($coltitle eq "NULL") ? "zzEMPTY" : $coltitle;
458 $total += $table{$rowtitle}->{$coltitle} || 0;
459 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
461 # warn "summ for column ".$col->{coltitle}." = ".$total;
462 push @loopfooter, {'totalcol' => $total};
466 # the header of the table
467 $globalline{loopfilter}=\@loopfilter;
468 # the core of the table
469 $globalline{looprow} = \@looprow;
470 $globalline{loopcol} = \@loopcol;
471 # # the foot (totals by borrower type)
472 $globalline{loopfooter} = \@loopfooter;
473 $globalline{total}= $grantotal;
474 $globalline{line} = ($line_attribute_type) ? $line_attribute_type : $line;
475 $globalline{column} = ($column_attribute_type) ? $column_attribute_type : $column;
476 push @mainloop,\%globalline;
477 return \@mainloop;
480 sub parse_extended_patron_attributes {
481 my ($input) = @_;
483 my @params_names = $input->param;
484 my %attr;
485 foreach my $name (@params_names) {
486 if ($name =~ /^Filter_patron_attr\.(.*)$/) {
487 my $code = $1;
488 my $value = $input->param($name);
489 $attr{$code} = $value;
493 return \%attr;
497 sub patron_attributes_form {
498 my $template = shift;
500 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
502 my %items_by_class;
503 foreach my $type (@types) {
504 my $attr_type = C4::Members::AttributeTypes->fetch($type->{code});
505 my $entry = {
506 class => $attr_type->class(),
507 code => $attr_type->code(),
508 description => $attr_type->description(),
509 repeatable => $attr_type->repeatable(),
510 category => $attr_type->authorised_value_category(),
511 category_code => $attr_type->category_code(),
514 my $newentry = { %$entry };
515 if ($attr_type->authorised_value_category()) {
516 $newentry->{use_dropdown} = 1;
517 $newentry->{auth_val_loop} = GetAuthorisedValues(
518 $attr_type->authorised_value_category()
521 push @{ $items_by_class{ $attr_type->class() } }, $newentry;
524 my @attribute_loop;
525 foreach my $class ( sort keys %items_by_class ) {
526 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
527 my $lib = $av->count ? $av->next->lib : $class;
528 push @attribute_loop, {
529 class => $class,
530 items => $items_by_class{$class},
531 lib => $lib,
535 $template->param(patron_attributes => \@attribute_loop);