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 our $sep = $input->param("sep");
63 $sep = "\t" if ($sep eq 'tabulation');
64 my $selected_branch; # = $input->param("?");
66 our $branches = GetBranches
;
68 my ($template, $borrowernumber, $cookie)
69 = get_template_and_user
({template_name
=> $fullreportname,
73 flagsrequired
=> {reports
=> '*'},
76 $template->param(do_it
=> $do_it);
78 my $results = calculate
($line, $column, $digits, $borstat,$borstat1 ,\
@filters);
79 if ($output eq "screen"){
80 $template->param(mainloop
=> $results);
81 output_html_with_http_headers
$input, $cookie, $template->output;
83 print $input->header(-type
=> 'application/vnd.sun.xml.calc',
85 -name
=> "$basename.csv",
86 -attachment
=> "$basename.csv");
87 my $cols = @
$results[0]->{loopcol
};
88 my $lines = @
$results[0]->{looprow
};
89 print @
$results[0]->{line
} ."/". @
$results[0]->{column
} .$sep;
90 foreach my $col ( @
$cols ) {
91 print $col->{coltitle
}.$sep;
94 foreach my $line ( @
$lines ) {
95 my $x = $line->{loopcell
};
96 print $line->{rowtitle
}.$sep;
97 foreach my $cell (@
$x) {
98 print $cell->{value
}.$sep;
100 print $line->{totalrow
};
104 $cols = @
$results[0]->{loopfooter
};
105 foreach my $col ( @
$cols ) {
106 print $sep.$col->{totalcol
};
108 print $sep.@
$results[0]->{total
};
110 exit(1); # exit after do_it, regardless
112 my $dbh = C4
::Context
->dbh;
114 $template->param( CAT_LOOP
=> &catcode_aref
);
116 foreach (sort {$branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
}} keys %$branches) {
117 my $line = {branchcode
=> $_, branchname
=> $branches->{$_}->{branchname
} || 'UNKNOWN'};
118 $line->{selected
} = 'selected' if ($selected_branch and $selected_branch eq $_);
119 push @branchloop, $line;
121 $template->param(BRANCH_LOOP
=> \
@branchloop);
122 $req = $dbh->prepare("SELECT DISTINCTROW zipcode FROM borrowers WHERE zipcode IS NOT NULL AND zipcode <> '' ORDER BY zipcode");
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");
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.
131 $template->param( SORT2_LOOP
=> $req->fetchall_arrayref({}));
133 my $CGIextChoice=CGI
::scrolling_list
(
136 -values => ['CSV'], # FIXME translation
139 my $CGIsepChoice=GetDelimiterChoices
;
141 CGIextChoice
=> $CGIextChoice,
142 CGIsepChoice
=> $CGIsepChoice,
143 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
147 output_html_with_http_headers
$input, $cookie, $template->output;
150 my $req = C4
::Context
->dbh->prepare("SELECT categorycode, description FROM categories ORDER BY description");
152 return $req->fetchall_arrayref({});
154 sub catcodes_hash
() {
156 my $catcodes = &catcode_aref
;
157 foreach (@
$catcodes) {
158 $cathash{$_->{categorycode
}} = ($_->{description
} || 'NO_DESCRIPTION') . " ($_->{categorycode})";
164 my ($line, $column, $digits, $status, $activity, $filters) = @_;
173 my $dbh = C4
::Context
->dbh;
177 # warn "filtres ".@filters[0];
178 # warn "filtres ".@filters[4];
179 # warn "filtres ".@filters[5];
180 # warn "filtres ".@filters[6];
182 $linefilter = @
$filters[0] if ($line =~ /categorycode/ ) ;
183 $linefilter = @
$filters[1] if ($line =~ /zipcode/ ) ;
184 $linefilter = @
$filters[2] if ($line =~ /branchcode/ ) ;
185 $linefilter = @
$filters[5] if ($line =~ /sex/);
186 $linefilter = @
$filters[6] if ($line =~ /sort1/ ) ;
187 $linefilter = @
$filters[7] if ($line =~ /sort2/ ) ;
190 $colfilter = @
$filters[0] if ($column =~ /categorycode/);
191 $colfilter = @
$filters[1] if ($column =~ /zipcode/);
192 $colfilter = @
$filters[2] if ($column =~ /branchcode/);
193 $colfilter = @
$filters[5] if ($column =~ /sex/);
194 $colfilter = @
$filters[6] if ($column =~ /sort1/);
195 $colfilter = @
$filters[7] if ($column =~ /sort2/);
198 for (my $i=0;$i<=7;$i++) {
200 if ( @
$filters[$i] ) {
201 if($i == 3 or $i == 4){
202 $cell{filter
} .= format_date
(@
$filters[$i]);
204 $cell{filter
} .= @
$filters[$i];
207 $cell{crit
} .="Cat Code " if ($i==0);
208 $cell{crit
} .="Zip Code" if ($i==1);
209 $cell{crit
} .="Branchcode" if ($i==2);
210 $cell{crit
} .="Date of Birth" if ($i==3);
211 $cell{crit
} .="Date of Birth" if ($i==4);
212 $cell{crit
} .="Sex" if ($i==5);
213 $cell{crit
} .="Sort1" if ($i==6);
214 $cell{crit
} .="Sort2" if ($i==7);
215 push @loopfilter, \
%cell;
218 ($status ) and push @loopfilter,{crit
=>"Status", filter
=>$status };
219 ($activity) and push @loopfilter,{crit
=>"Activity",filter
=>$activity};
220 push @loopfilter,{debug
=>1, crit
=>"Branches",filter
=>join(" ", sort keys %$branches)};
221 push @loopfilter,{debug
=>1, crit
=>"(line, column)", filter
=>"($line,$column)"};
223 my ( $period_year, $period_month, $period_day )=Add_Delta_YM
( Today
(),-$period, 0);
224 my $newperioddate=$period_year."-".$period_month."-".$period_day;
227 if (($line =~/zipcode/) and ($digits)) {
228 $linefield .="left($line,$digits)";
233 my %cathash = ($line eq 'categorycode' or $column eq 'categorycode') ?
&catcodes_hash
: ();
234 push @loopfilter, {debug
=>1, crit
=>"\%cathash", filter
=>join(", ", map {$cathash{$_}} sort keys %cathash)};
236 my $strsth = "SELECT distinctrow $linefield FROM borrowers WHERE $line IS NOT NULL ";
237 $linefilter =~ s/\*/%/g;
239 $strsth .= " AND $linefield LIKE ? " ;
241 $strsth .= " AND $status='1' " if ($status);
242 $strsth .=" order by $linefield";
244 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strsth};
245 my $sth = $dbh->prepare($strsth);
247 $sth->execute($linefilter);
251 while (my ($celvalue) = $sth->fetchrow) {
254 $cell{rowtitle
} = $celvalue;
255 # $cell{rowtitle_display} = ($linefield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
256 $cell{rowtitle_display
} = ($cathash{$celvalue} || "$celvalue\*") if ($line eq 'categorycode');
259 push @loopline, \
%cell;
264 if (($column =~/zipcode/) and ($digits)) {
265 $colfield = "left($column,$digits)";
269 my $strsth2 = "select distinctrow $colfield from borrowers where $column is not null";
271 $colfilter =~ s/\*/%/g;
272 $strsth2 .= " AND $colfield LIKE ? ";
274 $strsth2 .= " AND $status='1' " if ($status);
275 $strsth2 .= " order by $colfield";
276 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strsth2};
277 my $sth2 = $dbh->prepare($strsth2);
279 $sth2->execute($colfilter);
283 while (my ($celvalue) = $sth2->fetchrow) {
286 $cell{coltitle
} = $celvalue;
287 # $cell{coltitle_display} = ($colfield eq 'branchcode') ? $branches->{$celvalue}->{branchname} : $celvalue;
288 $cell{coltitle_display
} = $cathash{$celvalue} if ($column eq 'categorycode');
290 push @loopcol, \
%cell;
294 #Initialization of cell values.....
297 foreach my $row (@loopline) {
298 foreach my $col ( @loopcol ) {
299 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
300 $table{$row->{rowtitle
}}->{$col->{coltitle
}}=0;
302 $table{$row->{rowtitle
}}->{totalrow
}=0;
303 $table{$row->{rowtitle
}}->{rowtitle_display
} = $row->{rowtitle_display
};
306 # preparing calculation
307 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
308 @
$filters[0]=~ s/\*/%/g if (@
$filters[0]);
309 $strcalc .= " AND categorycode like '" . @
$filters[0] ."'" if ( @
$filters[0] );
310 @
$filters[1]=~ s/\*/%/g if (@
$filters[1]);
311 $strcalc .= " AND zipcode like '" . @
$filters[1] ."'" if ( @
$filters[1] );
312 @
$filters[2]=~ s/\*/%/g if (@
$filters[2]);
313 $strcalc .= " AND branchcode like '" . @
$filters[2] ."'" if ( @
$filters[2] );
314 @
$filters[3]=~ s/\*/%/g if (@
$filters[3]);
315 $strcalc .= " AND dateofbirth > '" . @
$filters[3] ."'" if ( @
$filters[3] );
316 @
$filters[4]=~ s/\*/%/g if (@
$filters[4]);
317 $strcalc .= " AND dateofbirth < '" . @
$filters[4] ."'" if ( @
$filters[4] );
318 @
$filters[5]=~ s/\*/%/g if (@
$filters[5]);
319 $strcalc .= " AND sex like '" . @
$filters[5] ."'" if ( @
$filters[5] );
320 @
$filters[6]=~ s/\*/%/g if (@
$filters[6]);
321 $strcalc .= " AND sort1 like '" . @
$filters[6] ."'" if ( @
$filters[6] );
322 @
$filters[7]=~ s/\*/%/g if (@
$filters[7]);
323 $strcalc .= " AND sort2 like '" . @
$filters[7] ."'" if ( @
$filters[7] );
324 $strcalc .= " AND borrowernumber in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'active');
325 $strcalc .= " AND borrowernumber not in (select distinct(borrowernumber) from old_issues where issuedate > '" . $newperioddate . "')" if ($activity eq 'nonactive');
326 $strcalc .= " AND $status='1' " if ($status);
327 $strcalc .= " group by $linefield, $colfield";
328 push @loopfilter, {sql
=>1, crit
=>"Query", filter
=>$strcalc};
329 my $dbcalc = $dbh->prepare($strcalc);
333 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
334 # warn "filling table $row / $col / $value ";
335 $emptycol = 1 if (!defined($col));
336 $col = "zzEMPTY" if (!defined($col));
337 $row = "zzEMPTY" if (!defined($row));
339 $table{$row}->{$col}+=$value;
340 $table{$row}->{totalrow
}+=$value;
341 $grantotal += $value;
344 push @loopcol,{coltitle
=> "NULL"} if ($emptycol);
346 foreach my $row (sort keys %table) {
348 #@loopcol ensures the order for columns is common with column titles
349 # and the number matches the number of columns
350 foreach my $col ( @loopcol ) {
351 my $value =$table{$row}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
352 push @loopcell, {value
=> $value};
355 'rowtitle' => ($row eq "zzEMPTY")?
"NULL":$row,
356 'rowtitle_display' => $table{$row}->{rowtitle_display
} || $row,
357 'loopcell' => \
@loopcell,
358 'totalrow' => $table{$row}->{totalrow
}
362 foreach my $col ( @loopcol ) {
364 foreach my $row ( @looprow ) {
365 $total += $table{($row->{rowtitle
} eq "NULL")?
"zzEMPTY":$row->{rowtitle
}}->{($col->{coltitle
} eq "NULL")?
"zzEMPTY":$col->{coltitle
}};
366 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
368 # warn "summ for column ".$col->{coltitle}." = ".$total;
369 push @loopfooter, {'totalcol' => $total};
373 # the header of the table
374 $globalline{loopfilter
}=\
@loopfilter;
375 # the core of the table
376 $globalline{looprow
} = \
@looprow;
377 $globalline{loopcol
} = \
@loopcol;
378 # # the foot (totals by borrower type)
379 $globalline{loopfooter
} = \
@loopfooter;
380 $globalline{total
}= $grantotal;
381 $globalline{line
} = $line;
382 $globalline{column
} = $column;
383 push @mainloop,\
%globalline;