9245 Fixes visual bux with fines tab not highlighting
[koha.git] / reports / borrowers_stats.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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use CGI;
22 use List::MoreUtils qw/uniq/;
23
24 use C4::Auth;
25 use C4::Context;
26 use C4::Branch; # GetBranches
27 use C4::Koha;
28 use C4::Dates;
29 use C4::Acquisition;
30 use C4::Output;
31 use C4::Reports;
32 use C4::Circulation;
33 use C4::Members::AttributeTypes;
34 use C4::Dates qw/format_date format_date_in_iso/;
35 use Date::Calc qw(
36   Today
37   Add_Delta_YM
38   );
39
40 =head1 NAME
41
42 plugin that shows a stats on borrowers
43
44 =head1 DESCRIPTION
45
46 =over 2
47
48 =cut
49
50 my $input = new CGI;
51 my $do_it=$input->param('do_it');
52 my $fullreportname = "reports/borrowers_stats.tmpl";
53 my $line = $input->param("Line");
54 my $column = $input->param("Column");
55 my @filters = $input->param("Filter");
56 $filters[3]=format_date_in_iso($filters[3]);
57 $filters[4]=format_date_in_iso($filters[4]);
58 my $digits = $input->param("digits");
59 our $period = $input->param("period");
60 my $borstat = $input->param("status");
61 my $borstat1 = $input->param("activity");
62 my $output = $input->param("output");
63 my $basename = $input->param("basename");
64 our $sep     = $input->param("sep");
65 $sep = "\t" if ($sep and $sep eq 'tabulation');
66 my $selected_branch; # = $input->param("?");
67
68 our $branches = GetBranches;
69
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);
83         }
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;
98                 }
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;
105                         }
106                         print $line->{totalrow};
107                         print "\n";
108                 }
109                 print "TOTAL";
110                 $cols = @$results[0]->{loopfooter};
111                 foreach my $col ( @$cols ) {
112                         print $sep.$col->{totalcol};
113                 }
114                 print $sep.@$results[0]->{total};
115         }
116         exit;   # exit after do_it, regardless
117 } else {
118         my $dbh = C4::Context->dbh;
119         my $req;
120         $template->param(  CAT_LOOP => &catcode_aref);
121         my @branchloop;
122         foreach (sort {$branches->{$a}->{branchname} cmp $branches->{$b}->{branchname}} keys %$branches) {
123                 my $line = {branchcode => $_, branchname => $branches->{$_}->{branchname} || 'UNKNOWN'};
124                 $line->{selected} = 'selected' if ($selected_branch and $selected_branch eq $_);
125                 push @branchloop, $line;
126         }
127         $template->param(BRANCH_LOOP => \@branchloop);
128         $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
129         $req->execute;
130         $template->param(   ZIP_LOOP => $req->fetchall_arrayref({}));
131         $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib");
132         $req->execute;
133         $template->param( SORT1_LOOP => $req->fetchall_arrayref({}));
134         $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort2 <> '' ORDER BY sort2 LIMIT 200");
135                 # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance.
136         $req->execute;
137         $template->param( SORT2_LOOP => $req->fetchall_arrayref({}));
138         
139         my $CGIextChoice=CGI::scrolling_list(
140                                 -name => 'MIME',
141                                 -id => 'MIME',
142                                 -values   => ['CSV'], # FIXME translation
143                                 -size     => 1,
144                                 -multiple => 0 );
145         my $CGIsepChoice=GetDelimiterChoices;
146         $template->param(
147                 CGIextChoice => $CGIextChoice,
148                 CGIsepChoice => $CGIsepChoice,
149     );
150     if (C4::Context->preference('ExtendedPatronAttributes')) {
151         $template->param(ExtendedPatronAttributes => 1);
152         patron_attributes_form($template);
153     }
154 }
155 output_html_with_http_headers $input, $cookie, $template->output;
156
157 sub catcode_aref {
158         my $req = C4::Context->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
159         $req->execute;
160         return $req->fetchall_arrayref({});
161 }
162 sub catcodes_hash {
163         my %cathash;
164         my $catcodes = &catcode_aref;
165         foreach (@$catcodes) {
166                 $cathash{$_->{categorycode}} = ($_->{description} || 'NO_DESCRIPTION') . " ($_->{categorycode})";
167         }
168         return %cathash;
169 }
170
171 sub calculate {
172         my ($line, $column, $digits, $status, $activity, $filters, $attr_filters) = @_;
173
174         my @mainloop;
175         my @loopfooter;
176         my @loopcol;
177         my @loopline;
178         my @looprow;
179         my %globalline;
180         my $grantotal =0;
181 # extract parameters
182         my $dbh = C4::Context->dbh;
183
184     # check parameters
185     my @valid_names = qw(categorycode zipcode branchcode sex sort1 sort2);
186     my @attribute_types = C4::Members::AttributeTypes::GetAttributeTypes;
187     if ($line =~ /^patron_attr\.(.*)/) {
188         my $attribute_type = $1;
189         return unless (grep {$attribute_type eq $_->{code}} @attribute_types);
190     } else {
191         return unless (grep /^$line$/, @valid_names);
192     }
193     if ($column =~ /^patron_attr\.(.*)/) {
194         my $attribute_type = $1;
195         return unless (grep {$attribute_type eq $_->{code}} @attribute_types);
196     } else {
197         return unless (grep /^$column$/, @valid_names);
198     }
199     return if ($digits and $digits !~ /^\d+$/);
200     return if ($status and (grep /^$status$/, qw(debarred gonenoaddress lost)) == 0);
201     return if ($activity and (grep /^$activity$/, qw(active nonactive)) == 0);
202
203     # Filters
204     my $linefilter;
205     if    ( $line =~ /categorycode/ ) { $linefilter = @$filters[0]; }
206     elsif ( $line =~ /zipcode/ )      { $linefilter = @$filters[1]; }
207     elsif ( $line =~ /branchcode/ )   { $linefilter = @$filters[2]; }
208     elsif ( $line =~ /sex/ )          { $linefilter = @$filters[5]; }
209     elsif ( $line =~ /sort1/ )        { $linefilter = @$filters[6]; }
210     elsif ( $line =~ /sort2/ )        { $linefilter = @$filters[7]; }
211     elsif ( $line =~ /^patron_attr\.(.*)$/ ) { $linefilter = $attr_filters->{$1}; }
212     else  { $linefilter = ''; }
213
214     my $colfilter;
215     if    ( $column =~ /categorycode/ ) { $colfilter = @$filters[0]; }
216     elsif ( $column =~ /zipcode/ )      { $colfilter = @$filters[1]; }
217     elsif ( $column =~ /branchcode/)    { $colfilter = @$filters[2]; }
218     elsif ( $column =~ /sex/)           { $colfilter = @$filters[5]; }
219     elsif ( $column =~ /sort1/)         { $colfilter = @$filters[6]; }
220     elsif ( $column =~ /sort2/)         { $colfilter = @$filters[7]; }
221     elsif ( $column =~ /^patron_attr\.(.*)$/) { $colfilter = $attr_filters->{$1}; }
222     else  { $colfilter = ''; }
223
224     my @loopfilter;
225     foreach my $i (0 .. scalar @$filters) {
226         my %cell;
227         if ( @$filters[$i] ) {
228             if ($i == 3 or $i == 4) {
229                 $cell{filter} = format_date(@$filters[$i]);
230             } else {
231                 $cell{filter} = @$filters[$i];
232             }
233
234             if    ( $i == 0)  { $cell{crit} = "Cat code"; }
235             elsif ( $i == 1 ) { $cell{crit} = "Zip code"; }
236             elsif ( $i == 2 ) { $cell{crit} = "Branch code"; }
237             elsif ( $i == 3 ||
238                     $i == 4 ) { $cell{crit} = "Date of birth"; }
239             elsif ( $i == 5 ) { $cell{crit} = "Sex"; }
240             elsif ( $i == 6 ) { $cell{crit} = "Sort1"; }
241             elsif ( $i == 7 ) { $cell{crit} = "Sort2"; }
242             else { $cell{crit} = "Unknown"; }
243
244             push @loopfilter, \%cell;
245         }
246     }
247     foreach my $type (keys %$attr_filters) {
248         if($attr_filters->{$type}) {
249             push @loopfilter, {
250                 crit => "Attribute $type",
251                 filter => $attr_filters->{$type}
252             }
253         }
254     }
255
256         ($status  ) and push @loopfilter,{crit=>"Status",  filter=>$status  };
257         ($activity) and push @loopfilter,{crit=>"Activity",filter=>$activity};
258         push @loopfilter,{debug=>1, crit=>"Branches",filter=>join(" ", sort keys %$branches)};
259         push @loopfilter,{debug=>1, crit=>"(line, column)", filter=>"($line,$column)"};
260 # year of activity
261         my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
262         my $newperioddate=$period_year."-".$period_month."-".$period_day;
263 # 1st, loop rows.
264         my $linefield;
265
266     my $line_attribute_type;
267     if ($line  =~/^patron_attr\.(.*)$/) {
268         $line_attribute_type = $1;
269         $line = 'borrower_attributes.attribute';
270     }
271
272     if (($line =~/zipcode/) and ($digits)) {
273         $linefield = "left($line,$digits)";
274     } else {
275         $linefield = $line;
276     }
277
278         my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ? &catcodes_hash : ();
279         push @loopfilter, {debug=>1, crit=>"\%cathash", filter=>join(", ", map {$cathash{$_}} sort keys %cathash)};
280
281     my $strsth;
282     my @strparams; # bind parameters for the query
283     if ($line_attribute_type) {
284         $strsth = "SELECT distinct attribute FROM borrower_attributes
285             WHERE attribute IS NOT NULL AND code=?";
286         push @strparams, $line_attribute_type;
287     } else {
288         $strsth = "SELECT distinctrow $linefield FROM borrowers
289             WHERE $line IS NOT NULL ";
290     }
291
292         $linefilter =~ s/\*/%/g;
293         if ( $linefilter ) {
294                 $strsth .= " AND $linefield LIKE ? " ;
295                 push @strparams, $linefilter;
296         }
297         $strsth .= " AND $status='1' " if ($status);
298     $strsth .=" order by $linefield";
299         
300         push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth};
301         my $sth = $dbh->prepare($strsth);
302     $sth->execute(@strparams);
303         while (my ($celvalue) = $sth->fetchrow) {
304                 my %cell;
305                 if ($celvalue) {
306                         $cell{rowtitle} = $celvalue;
307                         # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
308                         $cell{rowtitle_display} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode');
309                 }
310                 $cell{totalrow} = 0;
311                 push @loopline, \%cell;
312         }
313
314 # 2nd, loop cols.
315         my $colfield;
316
317     my $column_attribute_type;
318     if ($column  =~/^patron_attr.(.*)$/) {
319         $column_attribute_type = $1;
320         $column = 'borrower_attributes.attribute';
321     }
322
323     if (($column =~/zipcode/) and ($digits)) {
324         $colfield = "left($column,$digits)";
325     } else {
326         $colfield = $column;
327     }
328
329     my $strsth2;
330     my @strparams2; # bind parameters for the query
331     if ($column_attribute_type) {
332         $strsth2 = "SELECT DISTINCT attribute FROM borrower_attributes
333             WHERE attribute IS NOT NULL AND code=?";
334         push @strparams2, $column_attribute_type;
335     } else {
336         $strsth2 = "SELECT DISTINCTROW $colfield FROM borrowers
337             WHERE $column IS NOT NULL";
338     }
339
340         if ($colfilter) {
341                 $colfilter =~ s/\*/%/g;
342                 $strsth2 .= " AND $colfield LIKE ? ";
343         push @strparams2, $colfield;
344         }
345         $strsth2 .= " AND $status='1' " if ($status);
346
347     $strsth2 .= " order by $colfield";
348         push @loopfilter, {sql=>1, crit=>"Query", filter=>$strsth2};
349         my $sth2 = $dbh->prepare($strsth2);
350     $sth2->execute(@strparams2);
351         while (my ($celvalue) = $sth2->fetchrow) {
352                 my %cell;
353              if (defined $celvalue) {
354                         $cell{coltitle} = $celvalue;
355                         # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
356                         $cell{coltitle_display} = $cathash{$celvalue} if ($column eq 'categorycode');
357                 }
358                 push @loopcol, \%cell;
359         }
360
361         my $i=0;
362         #Initialization of cell values.....
363         my %table;
364 #       warn "init table";
365         foreach my $row (@loopline) {
366                 foreach my $col ( @loopcol ) {
367             my $rowtitle = $row->{rowtitle} // '';
368             my $coltitle = $row->{coltitle} // '';
369             $table{$rowtitle}->{$coltitle} = 0;
370                 }
371         $row->{rowtitle} ||= '';
372                 $table{$row->{rowtitle}}->{totalrow}=0;
373                 $table{$row->{rowtitle}}->{rowtitle_display} = $row->{rowtitle_display};
374         }
375
376     # preparing calculation
377     my $strcalc;
378     my @calcparams;
379     $strcalc = "SELECT ";
380     if ($line_attribute_type) {
381         $strcalc .= " attribute_$line_attribute_type.attribute AS line_attribute, ";
382     } else {
383         $strcalc .= " $linefield, ";
384     }
385     if ($column_attribute_type) {
386         $strcalc .= " attribute_$column_attribute_type.attribute AS column_attribute, ";
387     } else {
388         $strcalc .= " $colfield, ";
389     }
390
391     $strcalc .= " COUNT(*) FROM borrowers ";
392     foreach my $type (keys %$attr_filters) {
393         if (
394             ($line_attribute_type and $line_attribute_type eq $type)
395          or ($column_attribute_type and $column_attribute_type eq $type)
396          or ($attr_filters->{$type})
397         ) {
398             $strcalc .= " LEFT JOIN borrower_attributes AS attribute_$type
399                 ON (borrowers.borrowernumber = attribute_$type.borrowernumber
400                     AND attribute_$type.code = " . $dbh->quote($type) . ") ";
401         }
402     }
403     $strcalc .= " WHERE 1 ";
404
405         @$filters[0]=~ s/\*/%/g if (@$filters[0]);
406         $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
407         @$filters[1]=~ s/\*/%/g if (@$filters[1]);
408         $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
409         @$filters[2]=~ s/\*/%/g if (@$filters[2]);
410         $strcalc .= " AND branchcode like '" . @$filters[2] ."'" if ( @$filters[2] );
411         @$filters[3]=~ s/\*/%/g if (@$filters[3]);
412         $strcalc .= " AND dateofbirth > '" . @$filters[3] ."'" if ( @$filters[3] );
413         @$filters[4]=~ s/\*/%/g if (@$filters[4]);
414         $strcalc .= " AND dateofbirth < '" . @$filters[4] ."'" if ( @$filters[4] );
415     @$filters[5]=~ s/\*/%/g if (@$filters[5]);
416     $strcalc .= " AND sex like '" . @$filters[5] ."'" if ( @$filters[5] );
417     @$filters[6]=~ s/\*/%/g if (@$filters[6]);
418         $strcalc .= " AND sort1 like '" . @$filters[6] ."'" if ( @$filters[6] );
419         @$filters[7]=~ s/\*/%/g if (@$filters[7]);
420         $strcalc .= " AND sort2 like '" . @$filters[7] ."'" if ( @$filters[7] );
421
422     foreach my $type (keys %$attr_filters) {
423         if($attr_filters->{$type}) {
424             my $filter = $attr_filters->{$type};
425             $filter =~ s/\*/%/g;
426             $strcalc .= " AND attribute_$type.attribute LIKE '" . $filter . "' ";
427         }
428     }
429         $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
430         $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
431         $strcalc .= " AND $status='1' " if ($status);
432
433     $strcalc .= " GROUP BY ";
434     if ($line_attribute_type) {
435         $strcalc .= " line_attribute, ";
436     } else {
437         $strcalc .= " $linefield, ";
438     }
439     if ($column_attribute_type) {
440         $strcalc .= " column_attribute ";
441     } else {
442         $strcalc .= " $colfield ";
443     }
444
445         push @loopfilter, {sql=>1, crit=>"Query", filter=>$strcalc};
446         my $dbcalc = $dbh->prepare($strcalc);
447         (scalar(@calcparams)) ? $dbcalc->execute(@calcparams) : $dbcalc->execute();
448         
449         my $emptycol; 
450         while (my ($row, $col, $value) = $dbcalc->fetchrow) {
451 #               warn "filling table $row / $col / $value ";
452                 $emptycol = 1 if (!defined($col));
453                 $col = "zzEMPTY" if (!defined($col));
454                 $row = "zzEMPTY" if (!defined($row));
455                 
456                 $table{$row}->{$col}+=$value;
457                 $table{$row}->{totalrow}+=$value;
458                 $grantotal += $value;
459         }
460         
461         push @loopcol,{coltitle => "NULL"} if ($emptycol);
462         
463         foreach my $row (sort keys %table) {
464                 my @loopcell;
465                 #@loopcol ensures the order for columns is common with column titles
466                 # and the number matches the number of columns
467                 foreach my $col ( @loopcol ) {
468             my $coltitle = $col->{coltitle} // '';
469             $coltitle = $coltitle eq "NULL" ? "zzEMPTY" : $coltitle;
470                         my $value =$table{$row}->{$coltitle};
471                         push @loopcell, {value => $value};
472                 }
473                 push @looprow,{
474                         'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
475                         'rowtitle_display' => $table{$row}->{rowtitle_display} || ($row eq "zzEMPTY" ? "NULL" : $row),
476                         'loopcell' => \@loopcell,
477                         'totalrow' => $table{$row}->{totalrow}
478                 };
479         }
480         
481         foreach my $col ( @loopcol ) {
482                 my $total=0;
483                 foreach my $row ( @looprow ) {
484             my $rowtitle = $row->{rowtitle} // '';
485             $rowtitle = ($rowtitle eq "NULL") ? "zzEMPTY" : $rowtitle;
486             my $coltitle = $col->{coltitle} // '';
487             $coltitle = ($coltitle eq "NULL") ? "zzEMPTY" : $coltitle;
488
489             $total += $table{$rowtitle}->{$coltitle} || 0;
490 #                       warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
491                 }
492 #               warn "summ for column ".$col->{coltitle}."  = ".$total;
493                 push @loopfooter, {'totalcol' => $total};
494         }
495                         
496
497         # the header of the table
498         $globalline{loopfilter}=\@loopfilter;
499         # the core of the table
500         $globalline{looprow} = \@looprow;
501         $globalline{loopcol} = \@loopcol;
502 #       # the foot (totals by borrower type)
503         $globalline{loopfooter} = \@loopfooter;
504         $globalline{total}= $grantotal;
505         $globalline{line} = ($line_attribute_type) ? $line_attribute_type : $line;
506         $globalline{column} = ($column_attribute_type) ? $column_attribute_type : $column;
507         push @mainloop,\%globalline;
508         return \@mainloop;
509 }
510
511 sub parse_extended_patron_attributes {
512     my ($input) = @_;
513
514     my @params_names = $input->param;
515     my %attr;
516     foreach my $name (@params_names) {
517         if ($name =~ /^Filter_patron_attr\.(.*)$/) {
518             my $code = $1;
519             my $value = $input->param($name);
520             $attr{$code} = $value;
521         }
522     }
523
524     return \%attr;
525 }
526
527
528 sub patron_attributes_form {
529     my $template = shift;
530
531     my @types = C4::Members::AttributeTypes::GetAttributeTypes();
532
533     my %items_by_class;
534     foreach my $type (@types) {
535         my $attr_type = C4::Members::AttributeTypes->fetch($type->{code});
536         my $entry = {
537             class             => $attr_type->class(),
538             code              => $attr_type->code(),
539             description       => $attr_type->description(),
540             repeatable        => $attr_type->repeatable(),
541             category          => $attr_type->authorised_value_category(),
542             category_code     => $attr_type->category_code(),
543         };
544
545         my $newentry = { %$entry };
546         if ($attr_type->authorised_value_category()) {
547             $newentry->{use_dropdown} = 1;
548             $newentry->{auth_val_loop} = GetAuthorisedValues(
549                 $attr_type->authorised_value_category()
550             );
551         }
552         push @{ $items_by_class{ $attr_type->class() } }, $newentry;
553     }
554
555     my @attribute_loop;
556     foreach my $class ( sort keys %items_by_class ) {
557         my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
558         push @attribute_loop, {
559             class => $class,
560             items => $items_by_class{$class},
561             lib   => $lib,
562         };
563     }
564
565     $template->param(patron_attributes => \@attribute_loop);
566
567 }