Updates to Slovak staff interface for 3.0.4
[koha.git] / reports / borrowers_out.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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use C4::Auth;
25 use C4::Context;
26 use C4::Koha;
27 use C4::Output;
28 use C4::Circulation;
29 use C4::Reports;
30 use C4::Members;
31 use C4::Dates qw/format_date_in_iso/;
32
33 =head1 NAME
34
35 plugin that shows a stats on borrowers
36
37 =head1 DESCRIPTION
38
39 =over 2
40
41 =cut
42
43 my $input = new CGI;
44 my $do_it=$input->param('do_it');
45 my $fullreportname = "reports/borrowers_out.tmpl";
46 my $limit = $input->param("Limit");
47 my $column = $input->param("Criteria");
48 my @filters = $input->param("Filter");
49 my $output = $input->param("output");
50 my $basename = $input->param("basename");
51 my $mime = $input->param("MIME");
52 our $sep     = $input->param("sep") || '';
53 $sep = "\t" if ($sep eq 'tabulation');
54 my ($template, $borrowernumber, $cookie)
55     = get_template_and_user({template_name => $fullreportname,
56                 query => $input,
57                 type => "intranet",
58                 authnotrequired => 0,
59                 flagsrequired => {reports => 1},
60                 debug => 1,
61                 });
62 $template->param(do_it => $do_it,
63         DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
64         );
65 if ($do_it) {
66 # Displaying results
67     my $results = calculate($limit, $column, \@filters);
68     if ($output eq "screen"){
69 # Printing results to screen
70         $template->param(mainloop => $results);
71         output_html_with_http_headers $input, $cookie, $template->output;
72         exit(1);
73     } else {
74 # Printing to a csv file
75         print $input->header(-type => 'application/vnd.sun.xml.calc',
76                             -encoding    => 'utf-8',
77                             -attachment=>"$basename.csv",
78                             -filename=>"$basename.csv" );
79         my $cols = @$results[0]->{loopcol};
80         my $lines = @$results[0]->{looprow};
81 # header top-right
82         print "num /". @$results[0]->{column} .$sep;
83 # Other header
84         foreach my $col ( @$cols ) {
85             print $col->{coltitle}.$sep;
86         }
87         print "Total\n";
88 # Table
89         foreach my $line ( @$lines ) {
90             my $x = $line->{loopcell};
91             print $line->{rowtitle}.$sep;
92             foreach my $cell (@$x) {
93                 my $cellvalue = defined $cell->{value} ? $cell->{value}.$sep : ''.$sep;
94                 print $cellvalue;
95             }
96 #            print $line->{totalrow};
97             print "\n";
98         }
99 # footer
100         print "TOTAL";
101         $cols = @$results[0]->{loopfooter};
102         foreach my $col ( @$cols ) {
103             print $sep.$col->{totalcol};
104         }
105         print $sep.@$results[0]->{total};
106         exit(1);
107     }
108 # Displaying choices
109 } else {
110     my $dbh = C4::Context->dbh;
111     my @values;
112     my %labels;
113     my %select;
114     my $req;
115     
116     my @mime = ( C4::Context->preference("MIME") );
117 #       foreach my $mime (@mime){
118 #               warn "".$mime;
119 #       }
120     
121     my $CGIextChoice=CGI::scrolling_list(
122                 -name     => 'MIME',
123                 -id       => 'MIME',
124                 -values   => \@mime,
125                 -size     => 1,
126                 -multiple => 0 );
127     
128         my $CGIsepChoice = GetDelimiterChoices;
129     
130     my ($codes,$labels) = GetborCatFromCatType(undef,undef);
131     my @borcatloop;
132     foreach my $thisborcat (sort keys %$labels) {
133             my %row =(value => $thisborcat,
134                                     description => $labels->{$thisborcat},
135                             );
136             push @borcatloop, \%row;
137     }
138     $template->param(
139                     CGIextChoice => $CGIextChoice,
140                     CGIsepChoice => $CGIsepChoice,
141                     borcatloop =>\@borcatloop,
142                     );
143 output_html_with_http_headers $input, $cookie, $template->output;
144 }
145
146
147 sub calculate {
148     my ($line, $column, $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;
158
159 # Filters
160 # Checking filters
161 #
162     my @loopfilter;
163     for (my $i=0;$i<=2;$i++) {
164         my %cell;
165         if ( @$filters[$i] ) {
166             if (($i==1) and (@$filters[$i-1])) {
167                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
168             }
169             $cell{filter} .= @$filters[$i];
170             $cell{crit} .="Bor Cat" if ($i==0);
171             $cell{crit} .="Without issues since" if ($i==1);
172             push @loopfilter, \%cell;
173         }
174     }
175     my $colfield;
176     my $colorder;
177     if ($column){
178         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
179         my @colfilter ;
180         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
181     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
182     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
183                                                 
184     # loop cols.
185         $colfield .= $column;
186         $colorder .= $column;
187         
188         my $strsth2;
189         $strsth2 .= "select distinctrow $colfield FROM borrowers LEFT JOIN `old_issues` ON old_issues.borrowernumber=borrowers.borrowernumber";
190         if ($colfilter[0]) {
191             $colfilter[0] =~ s/\*/%/g;
192             $strsth2 .= " and $column LIKE '$colfilter[0]' " ;
193         }
194         $strsth2 .=" group by $colfield";
195         $strsth2 .=" order by $colorder";
196         # warn "". $strsth2;
197         
198         my $sth2 = $dbh->prepare( $strsth2 );
199         $sth2->execute;
200
201         while (my ($celvalue) = $sth2->fetchrow) {
202             my %cell;
203     #           my %ft;
204     #           warn "coltitle :".$celvalue;
205             $cell{coltitle} = $celvalue;
206     #           $ft{totalcol} = 0;
207             push @loopcol, \%cell;
208         }
209     #   warn "fin des titres colonnes";
210     }
211     
212     my $i=0;
213 #       my @totalcol;
214     
215     #Initialization of cell values.....
216     my @table;
217     
218 #       warn "init table";
219     if($line) {
220         for (my $i=1;$i<=$line;$i++) {
221             foreach my $col ( @loopcol ) {
222                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
223             }
224         }
225     }
226
227
228 # preparing calculation
229     my $strcalc ;
230     
231 # Processing calculation
232     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
233     $strcalc .= " , $colfield " if ($colfield);
234     $strcalc .= " FROM borrowers ";
235     $strcalc .= "WHERE 1 ";
236     @$filters[0]=~ s/\*/%/g if (@$filters[0]);
237     $strcalc .= " AND borrowers.categorycode like '" . @$filters[0] ."'" if ( @$filters[0] );
238     if (@$filters[1]){
239         my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> @$filters[1] ";
240 #        my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues where old_issues.timestamp> ".format_date_in_iso(@$filters[1]));
241         $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
242         
243 #               $queryfilter->execute(@$filters[1]);
244 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
245 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
246 #               }
247     } else {
248         my $strqueryfilter="SELECT DISTINCT borrowernumber FROM old_issues ";
249 #        my $queryfilter = $dbh->prepare("SELECT DISTINCT borrowernumber FROM old_issues ");
250 #        $queryfilter->execute;
251         $strcalc .= " AND borrowers.borrowernumber not in ($strqueryfilter)";
252 #               while (my ($borrowernumber)=$queryfilter->fetchrow){
253 #                       $strcalc .= " AND borrowers.borrowernumber <> $borrowernumber ";
254 #               }
255     }
256     $strcalc .= " group by borrowers.borrowernumber";
257     $strcalc .= ", $colfield" if ($column);
258     $strcalc .= " order by $colfield " if ($colfield);
259     my $max;
260     if ($line) {
261         if (@loopcol) {
262             $max = $line*@loopcol;
263         } else { $max=$line;}
264         $strcalc .= " LIMIT 0,$max";
265      } 
266 #    warn "SQL :". $strcalc;
267     
268     my $dbcalc = $dbh->prepare($strcalc);
269     $dbcalc->execute;
270 #       warn "filling table";
271     my $previous_col;
272     $i=1;
273     while (my  @data = $dbcalc->fetchrow) {
274         my ($row, $col )=@data;
275         $col = "zzEMPTY" if (!defined($col));
276         $i=1 if (($previous_col) and not($col eq $previous_col));
277         $table[$i]->{$col}=$row;
278 #               warn " $i $col $row";
279         $i++;
280         $previous_col=$col;
281     }
282     
283     push @loopcol,{coltitle => "Global"} if not($column);
284     
285     $max =(($line)?$line:@table -1);
286     for ($i=1; $i<=$max;$i++) {
287         my @loopcell;
288         #@loopcol ensures the order for columns is common with column titles
289         # and the number matches the number of columns
290         my $colcount=0;
291         foreach my $col ( @loopcol ) {
292             my $value;
293             if (@loopcol){
294                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
295             } else {
296                 $value =$table[$i]->{"zzEMPTY"};
297             }
298             push @loopcell, {value => $value} ;
299         }
300         push @looprow,{ 'rowtitle' => $i ,
301                         'loopcell' => \@loopcell,
302                     };
303     }
304     
305             
306
307     # the header of the table
308     $globalline{loopfilter}=\@loopfilter;
309     # the core of the table
310     $globalline{looprow} = \@looprow;
311     $globalline{loopcol} = \@loopcol;
312 #       # the foot (totals by borrower type)
313     $globalline{loopfooter} = \@loopfooter;
314     $globalline{total}= $grantotal;
315     $globalline{line} = $line;
316     $globalline{column} = $column;
317     push @mainloop,\%globalline;
318     return \@mainloop;
319 }
320
321 1;
322 __END__