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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
24 use C4::Branch; # GetBranches
37 plugin that shows a stats on borrowers
46 my $do_it=$input->param('do_it');
47 my $fullreportname = "reports/borrowers_stats.tmpl";
48 my $line = $input->param("Line");
49 my $column = $input->param("Column");
50 my @filters = $input->param("Filter");
51 my $digits = $input->param("digits");
52 my $period = $input->param("period");
53 my $borstat = $input->param("status");
54 my $borstat1 = $input->param("activity");
55 my $output = $input->param("output");
56 my $basename = $input->param("basename");
57 my $mime = $input->param("MIME");
58 my $del = $input->param("sep");
60 my ($template, $borrowernumber, $cookie)
61 = get_template_and_user({template_name => $fullreportname,
65 flagsrequired => {reports=> 1},
68 $template->param(do_it => $do_it);
70 my $results = calculate($line, $column, $digits, $borstat,$borstat1 ,\@filters);
71 if ($output eq "screen"){
72 $template->param(mainloop => $results);
73 output_html_with_http_headers $input, $cookie, $template->output;
76 print $input->header(-type => 'application/vnd.sun.xml.calc',
78 -name=>"$basename.csv",
79 -attachment=>"$basename.csv");
80 my $cols = @$results[0]->{loopcol};
81 my $lines = @$results[0]->{looprow};
83 $sep =C4::Context->preference("delimiter");
84 print @$results[0]->{line} ."/". @$results[0]->{column} .$sep;
85 foreach my $col ( @$cols ) {
86 print $col->{coltitle}.$sep;
89 foreach my $line ( @$lines ) {
90 my $x = $line->{loopcell};
91 print $line->{rowtitle}.$sep;
92 foreach my $cell (@$x) {
93 print $cell->{value}.$sep;
95 print $line->{totalrow};
99 $cols = @$results[0]->{loopfooter};
100 foreach my $col ( @$cols ) {
101 print $sep.$col->{totalcol};
103 print $sep.@$results[0]->{total};
107 my $dbh = C4::Context->dbh;
111 $req = $dbh->prepare( "select categorycode, description from categories order by description");
115 push @select_catcode,"";
116 $select_catcode{""} ="";
117 while (my ($catcode, $description) =$req->fetchrow) {
118 push @select_catcode, $catcode;
119 $select_catcode{$catcode} = $description;
121 my $CGICatCode=CGI::scrolling_list( -name => 'Filter',
123 -values => \@select_catcode,
124 -labels => \%select_catcode,
129 my $branches = GetBranches;
132 #my %select_branches;
133 push @select_branch,"";
134 #$select_branches{""}="";
135 foreach my $thisbranch (sort keys %$branches) {
136 push @select_branch,$thisbranch;
137 # my $selected = 1 if $thisbranch eq $branch;
138 my %row =(value => $thisbranch,
139 # selected => $selected,
140 branchname => $branches->{$thisbranch}->{'branchname'},
142 push @branchloop, \%row;
144 my $CGIBranch=CGI::scrolling_list( -name => 'Filter',
146 -values => \@select_branch,
147 # -labels => \%select_branches,
152 $req = $dbh->prepare( "select distinctrow zipcode from borrowers order by zipcode");
155 push @select_zipcode,"";
156 while (my ($value) =$req->fetchrow) {
158 push @select_zipcode, $value;
162 my $CGIZipCode=CGI::scrolling_list( -name => 'Filter',
164 -values => \@select_zipcode,
169 $req = $dbh->prepare( "SELECT authorised_value,lib FROM authorised_values WHERE category='Bsort1' order by lib");
173 push @select_sort1,"";
174 $select_sort1{""}="";
176 while (my ($auth_value,$lib) =$req->fetchrow) {
179 push @select_sort1, $auth_value;
180 $select_sort1{$auth_value}=$lib
184 my $CGIsort1=CGI::scrolling_list( -name => 'Filter',
186 -values => \@select_sort1,
187 -labels =>\%select_sort1,
191 $req = $dbh->prepare( "select distinctrow sort2 from borrowers order by sort2");
194 push @select_sort2,"";
196 while (my ($value) =$req->fetchrow) {
199 push @select_sort2, $value;
202 my $CGIsort2=CGI::scrolling_list( -name => 'Filter',
204 -values => \@select_sort2,
208 my @mime = ( C4::Context->preference("MIME") );
209 # warn 'MIME(s): ' . join ' ', @mime;
210 my $CGIextChoice=CGI::scrolling_list(
217 my @dels = ( C4::Context->preference("delimiter") );
218 my $CGIsepChoice=CGI::scrolling_list(
225 $template->param( CGICatCode => $CGICatCode,
226 CGIZipCode => $CGIZipCode,
227 CGISort1 => $CGIsort1,
228 hassort1 => $hassort1,
229 CGISort2 => $CGIsort2,
230 hassort2 => $hassort2,
231 CGIextChoice => $CGIextChoice,
232 CGIsepChoice => $CGIsepChoice,
233 CGIBranch => $CGIBranch,
234 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
238 output_html_with_http_headers $input, $cookie, $template->output;
243 my ($line, $column, $digits, $status, $activity, $filters) = @_;
252 my $dbh = C4::Context->dbh;
256 # warn "filtres ".@filters[0];
257 # warn "filtres ".@filters[4];
258 # warn "filtres ".@filters[5];
259 # warn "filtres ".@filters[6];
262 $linefilter = @$filters[0] if ($line =~ /categorycode/ ) ;
263 $linefilter = @$filters[1] if ($line =~ /zipcode/ ) ;
264 $linefilter = @$filters[2] if ($line =~ /branchcode/ ) ;
265 $linefilter = @$filters[5] if ($line =~ /sort1/ ) ;
266 $linefilter = @$filters[6] if ($line =~ /sort2/ ) ;
269 $colfilter = @$filters[0] if ($column =~ /categorycode/);
270 $colfilter = @$filters[1] if ($column =~ /zipcode/);
271 $colfilter = @$filters[2] if ($column =~ /branchcode/);
272 $colfilter = @$filters[5] if ($column =~ /sort1/);
273 $colfilter = @$filters[6] if ($column =~ /sort2/);
276 for (my $i=0;$i<=6;$i++) {
278 if ( @$filters[$i] ) {
279 $cell{filter} .= @$filters[$i];
280 $cell{crit} .="Cat Code " if ($i==0);
281 $cell{crit} .="Zip Code" if ($i==1);
282 $cell{crit} .="Branchcode" if ($i==2);
283 $cell{crit} .="Date of Birth" if ($i==3);
284 $cell{crit} .="Date of Birth" if ($i==4);
285 $cell{crit} .="Sort1" if ($i==5);
286 $cell{crit} .="Sort2" if ($i==6);
287 push @loopfilter, \%cell;
291 push @loopfilter,{crit=>"Status",filter=>$status}
295 push @loopfilter,{crit=>"Activity",filter=>$activity};
298 my ( $period_year, $period_month, $period_day )=Add_Delta_YM( Today(),-$period, 0);
299 my $newperioddate=$period_year."-".$period_month."-".$period_day;
300 # warn "PERIOD".$period;
303 if (($line =~/zipcode/) and ($digits)) {
304 $linefield .="left($line,$digits)";
310 $strsth .= "select distinctrow $linefield from borrowers where $line is not null ";
311 $linefilter =~ s/\*/%/g;
313 $strsth .= " and $linefield LIKE ? " ;
315 $strsth .= " and $status='1' " if ($status);
316 $strsth .=" order by $linefield";
319 my $sth = $dbh->prepare( $strsth );
321 $sth->execute($linefilter);
325 while ( my ($celvalue) = $sth->fetchrow) {
328 $cell{rowtitle} = $celvalue;
330 # $cell{rowtitle} = "";
333 push @loopline, \%cell;
338 if (($column =~/zipcode/) and ($digits)) {
339 $colfield .= "left($column,$digits)";
341 $colfield .= $column;
344 $colfilter =~ s/\*/%/g;
345 $strsth2 .= "select distinctrow $colfield from borrowers where $column is not null";
347 $strsth2 .= " and $colfield LIKE ? ";
349 $strsth2 .= " and $status='1' " if ($status);
350 $strsth2 .= " order by $colfield";
352 my $sth2 = $dbh->prepare( $strsth2 );
354 $sth2->execute($colfilter);
358 while (my ($celvalue) = $sth2->fetchrow) {
362 $cell{coltitle} = $celvalue;
364 push @loopcol, \%cell;
372 #Initialization of cell values.....
375 foreach my $row ( @loopline ) {
376 foreach my $col ( @loopcol ) {
377 # warn " init table : $row->{rowtitle} / $col->{coltitle} ";
378 $table{$row->{rowtitle}}->{$col->{coltitle}}=0;
380 $table{$row->{rowtitle}}->{totalrow}=0;
383 # preparing calculation
384 my $strcalc .= "SELECT $linefield, $colfield, count( * ) FROM borrowers WHERE 1 ";
385 @$filters[0]=~ s/\*/%/g if (@$filters[0]);
386 $strcalc .= " AND categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
387 @$filters[1]=~ s/\*/%/g if (@$filters[1]);
388 $strcalc .= " AND zipcode like '" . @$filters[1] ."'" if ( @$filters[1] );
389 @$filters[2]=~ s/\*/%/g if (@$filters[2]);
390 $strcalc .= " AND branchcode like '" . @$filters[2] ."'" if ( @$filters[2] );
391 @$filters[3]=~ s/\*/%/g if (@$filters[3]);
392 $strcalc .= " AND dateofbirth > '" . @$filters[3] ."'" if ( @$filters[3] );
393 @$filters[4]=~ s/\*/%/g if (@$filters[4]);
394 $strcalc .= " AND dateofbirth < '" . @$filters[4] ."'" if ( @$filters[4] );
395 @$filters[5]=~ s/\*/%/g if (@$filters[5]);
396 $strcalc .= " AND sort1 like '" . @$filters[5] ."'" if ( @$filters[5] );
397 @$filters[6]=~ s/\*/%/g if (@$filters[6]);
398 $strcalc .= " AND sort2 like '" . @$filters[6] ."'" if ( @$filters[6] );
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 . "')" if ($activity eq 'nonactive');
401 $strcalc .= " AND $status='1' " if ($status);
402 $strcalc .= " group by $linefield, $colfield";
404 my $dbcalc = $dbh->prepare($strcalc);
406 # warn "filling table";
409 while (my ($row, $col, $value) = $dbcalc->fetchrow) {
410 # warn "filling table $row / $col / $value ";
411 $emptycol = 1 if ($col eq undef);
412 $col = "zzEMPTY" if ($col eq undef);
413 $row = "zzEMPTY" if ($row eq undef);
415 $table{$row}->{$col}+=$value;
416 $table{$row}->{totalrow}+=$value;
417 $grantotal += $value;
420 push @loopcol,{coltitle => "NULL"} if ($emptycol);
422 foreach my $row ( sort keys %table ) {
424 #@loopcol ensures the order for columns is common with column titles
425 # and the number matches the number of columns
426 foreach my $col ( @loopcol ) {
427 my $value =$table{$row}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
428 push @loopcell, {value => $value } ;
430 push @looprow,{ 'rowtitle' => ($row eq "zzEMPTY")?"NULL":$row,
431 'loopcell' => \@loopcell,
432 'hilighted' => ($hilighted >0),
433 'totalrow' => $table{$row}->{totalrow}
435 $hilighted = -$hilighted;
438 foreach my $col ( @loopcol ) {
440 foreach my $row ( @looprow ) {
441 $total += $table{($row->{rowtitle} eq "NULL")?"zzEMPTY":$row->{rowtitle}}->{($col->{coltitle} eq "NULL")?"zzEMPTY":$col->{coltitle}};
442 # warn "value added ".$table{$row->{rowtitle}}->{$col->{coltitle}}. "for line ".$row->{rowtitle};
444 # warn "summ for column ".$col->{coltitle}." = ".$total;
445 push @loopfooter, {'totalcol' => $total};
449 # the header of the table
450 $globalline{loopfilter}=\@loopfilter;
451 # the core of the table
452 $globalline{looprow} = \@looprow;
453 $globalline{loopcol} = \@loopcol;
454 # # the foot (totals by borrower type)
455 $globalline{loopfooter} = \@loopfooter;
456 $globalline{total}= $grantotal;
457 $globalline{line} = $line;
458 $globalline{column} = $column;
459 push @mainloop,\%globalline;