22f71c9924341b6addb47155bd1548189e832895
[srvgit] / 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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use C4::Auth;
24 use C4::Context;
25 use C4::Koha;
26 use C4::Output;
27 use C4::Circulation;
28 use C4::Reports;
29 use C4::Members;
30
31 use Koha::DateUtils;
32 use Koha::Patron::Categories;
33
34 =head1 NAME
35
36 reports/borrowers_out.pl
37
38 =head1 DESCRIPTION
39
40 Plugin that shows a stats on borrowers
41
42 =cut
43
44 my $input = CGI->new;
45 my $do_it=$input->param('do_it');
46 my $fullreportname = "reports/borrowers_out.tt";
47 my $limit = $input->param("Limit");
48 my $column = $input->param("Criteria");
49 my @filters = $input->multi_param("Filter");
50 $filters[1] = eval { output_pref( { dt => dt_from_string( $filters[1]), dateonly => 1, dateformat => 'iso' } ); }
51     if ( $filters[1] );
52
53 my $output = $input->param("output");
54 my $basename = $input->param("basename");
55 our $sep     = $input->param("sep") || '';
56 $sep = "\t" if ($sep eq 'tabulation');
57 my ($template, $borrowernumber, $cookie)
58     = get_template_and_user({template_name => $fullreportname,
59                 query => $input,
60                 type => "intranet",
61                 flagsrequired => {reports => '*'},
62                 });
63 $template->param(do_it => $do_it,
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;
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;
107     }
108 # Displaying choices
109 } else {
110     my $dbh = C4::Context->dbh;
111
112     my $CGIextChoice = ( 'CSV' ); # FIXME translation
113         my $CGIsepChoice = GetDelimiterChoices;
114
115     my $patron_categories = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['categorycode']});
116     $template->param(
117                     CGIextChoice => $CGIextChoice,
118                     CGIsepChoice => $CGIsepChoice,
119                     patron_categories => $patron_categories,
120                     );
121 output_html_with_http_headers $input, $cookie, $template->output;
122 }
123
124
125 sub calculate {
126     my ($line, $column, $filters) = @_;
127     my @mainloop;
128     my @loopfooter;
129     my @loopcol;
130     my @looprow;
131     my %globalline;
132     my $grantotal =0;
133 # extract parameters
134     my $dbh = C4::Context->dbh;
135
136 # Filters
137 # Checking filters
138 #
139     my @loopfilter;
140     for (my $i=0;$i<=2;$i++) {
141         my %cell;
142         if ( @$filters[$i] ) {
143             if (($i==1) and (@$filters[$i-1])) {
144                 $cell{err} = 1 if (@$filters[$i]<@$filters[$i-1]) ;
145             }
146             $cell{filter} .= @$filters[$i];
147             $cell{crit} .="Bor Cat" if ($i==0);
148             $cell{crit} .="Without issues since" if ($i==1);
149             push @loopfilter, \%cell;
150         }
151     }
152     my $colfield;
153     my $colorder;
154     if ($column){
155         $column = "borrowers.".$column if $column=~/categorycode/ || $column=~/branchcode/;
156         my @colfilter ;
157         $colfilter[0] = @$filters[0] if ($column =~ /category/ )  ;
158     #   $colfilter[0] = @$filters[11] if ($column =~ /sort2/ ) ;
159     #warn "filtre col ".$colfilter[0]." ".$colfilter[1];
160                                                 
161     # loop cols.
162         $colfield .= $column;
163         $colorder .= $column;
164         
165         my $strsth2;
166         $strsth2 .= "select distinct " . $dbh->quote($colfield) . " FROM borrowers WHERE 1";
167         my @query_args;
168         if ( $colfilter[0] ) {
169             $colfilter[0] =~ s/\*/%/g;
170             $strsth2 .= " and " . $dbh->quote($column) . "LIKE ?" ;
171             push @query_args, $colfilter[0];
172         }
173         $strsth2 .=" group by " . $dbh->quote($colfield);
174         $strsth2 .=" order by " . $dbh->quote($colorder);
175         # warn "". $strsth2;
176         
177         my $sth2 = $dbh->prepare( $strsth2 );
178         $sth2->execute( @query_args );
179         while (my ($celvalue) = $sth2->fetchrow) {
180             my %cell;
181     #           my %ft;
182     #           warn "coltitle :".$celvalue;
183             $cell{coltitle} = $celvalue;
184     #           $ft{totalcol} = 0;
185             push @loopcol, \%cell;
186         }
187     #   warn "fin des titres colonnes";
188     }
189     
190     my $i=0;
191 #       my @totalcol;
192     
193     #Initialization of cell values.....
194     my @table;
195     
196 #       warn "init table";
197     if($line) {
198         for (my $i=1;$i<=$line;$i++) {
199             foreach my $col ( @loopcol ) {
200                 $table[$i]->{($col->{coltitle})?$col->{coltitle}:"Global"}=0;
201             }
202         }
203     }
204
205
206 # preparing calculation
207     my $strcalc ;
208     
209 # Processing calculation
210     $strcalc .= "SELECT CONCAT( borrowers.surname , \"\\t\",borrowers.firstname, \"\\t\", borrowers.cardnumber)";
211     $strcalc .= " , " . $dbh->quote($colfield) if ($colfield);
212     $strcalc .= " FROM borrowers ";
213     $strcalc .= "WHERE 1 ";
214     my @query_args;
215     if ( @$filters[0] ) {
216         @$filters[0]=~ s/\*/%/g;
217         $strcalc .= " AND borrowers.categorycode like ?";
218         push @query_args, @$filters[0];
219     }
220     $strcalc .= " AND NOT EXISTS (SELECT * FROM issues WHERE issues.borrowernumber=borrowers.borrowernumber ";
221     if ( @$filters[1] ) {
222         $strcalc .= " AND issues.timestamp > ?";
223         push @query_args, @$filters[1];
224     }
225     $strcalc .= ") ";
226     $strcalc .= " AND NOT EXISTS (SELECT * FROM old_issues WHERE old_issues.borrowernumber=borrowers.borrowernumber ";
227     if ( @$filters[1] ) {
228         $strcalc .= " AND old_issues.timestamp > ?";
229         push @query_args, @$filters[1];
230     }
231     $strcalc .= ") ";
232     $strcalc .= " group by borrowers.borrowernumber";
233     $strcalc .= ", " . $dbh->quote($colfield) if ($column);
234     $strcalc .= " order by " . $dbh->quote($colfield) if ($colfield);
235     my $max;
236     if ($line) {
237         if (@loopcol) {
238             $max = $line*@loopcol;
239         } else { $max=$line;}
240         $strcalc .= " LIMIT 0,$max";
241      } 
242     
243     my $dbcalc = $dbh->prepare($strcalc);
244     $dbcalc->execute( @query_args );
245 #       warn "filling table";
246     my $previous_col;
247     $i=1;
248     while (my  @data = $dbcalc->fetchrow) {
249         my ($row, $col )=@data;
250         $col = "zzEMPTY" if (!defined($col));
251         $i=1 if (($previous_col) and not($col eq $previous_col));
252         $table[$i]->{$col}=$row;
253 #               warn " $i $col $row";
254         $i++;
255         $previous_col=$col;
256     }
257     
258     push @loopcol,{coltitle => "Global"} if not($column);
259     
260     $max =(($line)?$line:@table -1);
261     for ($i=1; $i<=$max;$i++) {
262         my @loopcell;
263         #@loopcol ensures the order for columns is common with column titles
264         # and the number matches the number of columns
265         my $colcount=0;
266         foreach my $col ( @loopcol ) {
267             my $value;
268             if (@loopcol){
269                 $value =$table[$i]->{(($col->{coltitle} eq "NULL") or ($col->{coltitle} eq "Global"))?"zzEMPTY":$col->{coltitle}};
270             } else {
271                 $value =$table[$i]->{"zzEMPTY"};
272             }
273             push @loopcell, {value => $value} ;
274         }
275         push @looprow,{ 'rowtitle' => $i ,
276                         'loopcell' => \@loopcell,
277                     };
278     }
279     
280             
281
282     # the header of the table
283     $globalline{loopfilter}=\@loopfilter;
284     # the core of the table
285     $globalline{looprow} = \@looprow;
286     $globalline{loopcol} = \@loopcol;
287 #       # the foot (totals by borrower type)
288     $globalline{loopfooter} = \@loopfooter;
289     $globalline{total}= $grantotal;
290     $globalline{line} = $line;
291     $globalline{column} = $column;
292     push @mainloop,\%globalline;
293     return \@mainloop;
294 }
295
296 1;
297 __END__