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 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
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.
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.
21 #use warnings; FIXME - Bug 2505
25 use C4
::Branch
; # GetBranches
32 use C4
::Dates qw
/format_date format_date_in_iso/;
40 plugin that shows a stats on borrowers
49 my $do_it=$input->param('do_it');
50 my $fullreportname = "reports/borrowers_stats.tmpl";
51 my $line = $input->param("Line");
52 my $column = $input->param("Column");
53 my @filters = $input->param("Filter");
54 $filters[3]=format_date_in_iso
($filters[3]);
55 $filters[4]=format_date_in_iso
($filters[4]);
56 my $digits = $input->param("digits");
57 my $period = $input->param("period");
58 my $borstat = $input->param("status");
59 my $borstat1 = $input->param("activity");
60 my $output = $input->param("output");
61 my $basename = $input->param("basename");
62 my $mime = $input->param("MIME");
63 our $sep = $input->param("sep");
64 $sep = "\t" if ($sep eq 'tabulation');
65 my $selected_branch; # = $input->param("?");
67 our $branches = GetBranches
;
69 my ($template, $borrowernumber, $cookie)
70 = get_template_and_user
({template_name
=> $fullreportname,
74 flagsrequired
=> {reports
=> '*'},
77 $template->param(do_it
=> $do_it);
79 my $results = calculate
($line, $column, $digits, $borstat,$borstat1 ,\
@filters);
80 if ($output eq "screen"){
81 $template->param(mainloop
=> $results);
82 output_html_with_http_headers
$input, $cookie, $template->output;
84 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
86 -name
=> "$basename.csv",
87 -attachment
=> "$basename.csv");
88 my $cols = @
$results[0]->{loopcol
};
89 my $lines = @
$results[0]->{looprow
};
90 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
91 foreach my $col ( @
$cols ) {
92 print $col->{coltitle
}.$sep;
95 foreach my $line ( @
$lines ) {
96 my $x = $line->{loopcell
};
97 print $line->{rowtitle
}.$sep;
98 foreach my $cell (@
$x) {
99 print $cell->{value
}.$sep;
101 print $line->{totalrow
};
105 $cols = @
$results[0]->{loopfooter
};
106 foreach my $col ( @
$cols ) {
107 print $sep.$col->{totalcol
};
109 print $sep.@
$results[0]->{total
};
111 exit(1); # exit after do_it, regardless
113 my $dbh = C4
::Context
->dbh;
115 $template->param( CAT_LOOP
=> &catcode_aref
);
117 foreach (sort {$branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
}} keys %$branches) {
118 my $line = {branchcode
=> $_, branchname
=> $branches->{$_}->{branchname
} || 'UNKNOWN'};
119 $line->{selected
} = 'selected' if ($selected_branch and $selected_branch eq $_);
120 push @branchloop, $line;
122 $template->param(BRANCH_LOOP
=> \
@branchloop);
123 $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
125 $template->param( ZIP_LOOP
=> $req->fetchall_arrayref({}));
126 $req = $dbh->prepare("SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' ORDER BY lib");
128 $template->param( SORT1_LOOP
=> $req->fetchall_arrayref({}));
129 $req = $dbh->prepare("SELECT DISTINCTROW sort2 AS value FROM borrowers WHERE sort2 IS NOT NULL AND sort2 <> '' ORDER BY sort2 LIMIT 200");
130 # More than 200 items in a dropdown is not going to be useful anyway, and w/ 50,000 patrons we can destory DB performance.
132 $template->param( SORT2_LOOP
=> $req->fetchall_arrayref({}));
134 my @mime = ( C4
::Context
->preference("MIME") );
135 # warn 'MIME(s): ' . join ' ', @mime;
136 my $CGIextChoice=CGI
::scrolling_list
(
142 my $CGIsepChoice=GetDelimiterChoices
;
144 CGIextChoice
=> $CGIextChoice,
145 CGIsepChoice
=> $CGIsepChoice,
146 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
150 output_html_with_http_headers
$input, $cookie, $template->output;
153 my $req = C4
::Context
->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
155 return $req->fetchall_arrayref({});
157 sub catcodes_hash
() {
159 my $catcodes = &catcode_aref
;
160 foreach (@
$catcodes) {
161 $cathash{$_->{categorycode
}} = ($_->{description
} || 'NO_DESCRIPTION') . " ($_->{categorycode})";
167 my ($line, $column, $digits, $status, $activity, $filters) = @_;
176 my $dbh = C4
::Context
->dbh;
180 # warn "filtres ".@filters[0];
181 # warn "filtres ".@filters[4];
182 # warn "filtres ".@filters[5];
183 # warn "filtres ".@filters[6];
185 $linefilter = @
$filters[0] if ($line =~ /categorycode/ ) ;
186 $linefilter = @
$filters[1] if ($line =~ /zipcode/ ) ;
187 $linefilter = @
$filters[2] if ($line =~ /branchcode/ ) ;
188 $linefilter = @
$filters[5] if ($line =~ /sex/);
189 $linefilter = @
$filters[6] if ($line =~ /sort1/ ) ;
190 $linefilter = @
$filters[7] if ($line =~ /sort2/ ) ;
193 $colfilter = @
$filters[0] if ($column =~ /categorycode/);
194 $colfilter = @
$filters[1] if ($column =~ /zipcode/);
195 $colfilter = @
$filters[2] if ($column =~ /branchcode/);
196 $colfilter = @
$filters[5] if ($column =~ /sex/);
197 $colfilter = @
$filters[6] if ($column =~ /sort1/);
198 $colfilter = @
$filters[7] if ($column =~ /sort2/);
201 for (my $i=0;$i<=7;$i++) {
203 if ( @
$filters[$i] ) {
204 if($i == 3 or $i == 4){
205 $cell{filter
} .= format_date
(@
$filters[$i]);
207 $cell{filter
} .= @
$filters[$i];
210 $cell{crit
} .="Cat Code " if ($i==0);
211 $cell{crit
} .="Zip Code" if ($i==1);
212 $cell{crit
} .="Branchcode" if ($i==2);
213 $cell{crit
} .="Date of Birth" if ($i==3);
214 $cell{crit
} .="Date of Birth" if ($i==4);
215 $cell{crit
} .="Sex" if ($i==5);
216 $cell{crit
} .="Sort1" if ($i==6);
217 $cell{crit
} .="Sort2" if ($i==7);
218 push @loopfilter, \
%cell;
221 ($status ) and push @loopfilter,{crit
=>"Status", filter
=>$status };
222 ($activity) and push @loopfilter,{crit
=>"Activity",filter
=>$activity};
223 push @loopfilter,{debug
=>1, crit
=>"Branches",filter
=>join(" ", sort keys %$branches)};
224 push @loopfilter,{debug
=>1, crit
=>"(line, column)", filter
=>"($line,$column)"};
226 my ( $period_year, $period_month, $period_day )=Add_Delta_YM
( Today
(),-$period, 0);
227 my $newperioddate=$period_year."-".$period_month."-".$period_day;
230 if (($line =~/zipcode/) and ($digits)) {
231 $linefield .="left($line,$digits)";
236 my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ?
&catcodes_hash
: ();
237 push @loopfilter, {debug
=>1, crit
=>"\%cathash", filter
=>join(", ", map {$cathash{$_}} sort keys %cathash)};
239 my $strsth = "SELECT distinctrow $linefield FROM borrowers WHERE $line IS NOT NULL ";
240 $linefilter =~ s/\*/%/g;
242 $strsth .= " AND $linefield LIKE ? " ;
244 $strsth .= " AND $status='1' " if ($status);
245 $strsth .=" order by $linefield";
247 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strsth};
248 my $sth = $dbh->prepare($strsth);
250 $sth->execute($linefilter);
254 while (my ($celvalue) = $sth->fetchrow) {
257 $cell{rowtitle
} = $celvalue;
258 # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
259 $cell{rowtitle_display
} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode');
262 push @loopline, \
%cell;
267 if (($column =~/zipcode/) and ($digits)) {
268 $colfield = "left($column,$digits)";
272 my $strsth2 = "select distinctrow $colfield from borrowers where $column is not null";
274 $colfilter =~ s/\*/%/g;
275 $strsth2 .= " AND $colfield LIKE ? ";
277 $strsth2 .= " AND $status='1' " if ($status);
278 $strsth2 .= " order by $colfield";
279 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strsth2};
280 my $sth2 = $dbh->prepare($strsth2);
282 $sth2->execute($colfilter);
286 while (my ($celvalue) = $sth2->fetchrow) {
289 $cell{coltitle
} = $celvalue;
290 # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
291 $cell{coltitle_display
} = $cathash{$celvalue} if ($column eq 'categorycode');
293 push @loopcol, \
%cell;
297 #Initialization of cell values.....
300 foreach my $row (@loopline) {
301 foreach my $col ( @loopcol ) {
302 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
303 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
305 $table{$row->{rowtitle
}}->{totalrow
}=0;
306 $table{$row->{rowtitle
}}->{rowtitle_display
} = $row->{rowtitle_display
};
309 # preparing calculation
310 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
311 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
312 $strcalc .= " AND categorycode like '" . @
$filters[0] ."'" if ( @
$filters[0] );
313 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
314 $strcalc .= " AND zipcode like '" . @
$filters[1] ."'" if ( @
$filters[1] );
315 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
316 $strcalc .= " AND branchcode like '" . @
$filters[2] ."'" if ( @
$filters[2] );
317 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
318 $strcalc .= " AND dateofbirth > '" . @
$filters[3] ."'" if ( @
$filters[3] );
319 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
320 $strcalc .= " AND dateofbirth < '" . @
$filters[4] ."'" if ( @
$filters[4] );
321 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
322 $strcalc .= " AND sex like '" . @
$filters[5] ."'" if ( @
$filters[5] );
323 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
324 $strcalc .= " AND sort1 like '" . @
$filters[6] ."'" if ( @
$filters[6] );
325 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
326 $strcalc .= " AND sort2 like '" . @
$filters[7] ."'" if ( @
$filters[7] );
327 $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
328 $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
329 $strcalc .= " AND $status='1' " if ($status);
330 $strcalc .= " group by $linefield, $colfield";
331 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strcalc};
332 my $dbcalc = $dbh->prepare($strcalc);
336 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
337 # warn "filling table $row / $col / $value ";
338 $emptycol = 1 if (!defined($col));
339 $col = "zzEMPTY" if (!defined($col));
340 $row = "zzEMPTY" if (!defined($row));
342 $table{$row}->{$col}+=$value;
343 $table{$row}->{totalrow
}+=$value;
344 $grantotal += $value;
347 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
349 foreach my $row (sort keys %table) {
351 #@loopcol ensures the order for columns is common with column titles
352 # and the number matches the number of columns
353 foreach my $col ( @loopcol ) {
354 my $value =$table{$row}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
355 push @loopcell, {value
=> $value};
358 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
359 'rowtitle_display' => $table{$row}->{rowtitle_display
} || $row,
360 'loopcell' => \
@loopcell,
361 'totalrow' => $table{$row}->{totalrow
}
365 foreach my $col ( @loopcol ) {
367 foreach my $row ( @looprow ) {
368 $total += $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
369 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
371 # warn "summ for column ".$col->{coltitle}." = ".$total;
372 push @loopfooter, {'totalcol' => $total};
376 # the header of the table
377 $globalline{loopfilter
}=\
@loopfilter;
378 # the core of the table
379 $globalline{looprow
} = \
@looprow;
380 $globalline{loopcol
} = \
@loopcol;
381 # # the foot (totals by borrower type)
382 $globalline{loopfooter
} = \
@loopfooter;
383 $globalline{total
}= $grantotal;
384 $globalline{line
} = $line;
385 $globalline{column
} = $column;
386 push @mainloop,\
%globalline;