Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / reports / serials_stats.pl
1 #!/usr/bin/perl
2
3 # Copyright 2009 SARL Biblibre
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 use C4::Auth;
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Koha;
26 use C4::Reports;
27 use C4::Serials;
28
29 =head1 serials_out
30
31 plugin that shows a stats on serials
32
33 =head1 DESCRIPTION
34
35 =cut
36
37 my $input      = CGI->new;
38 my $templatename   = "reports/serials_stats.tt";
39 my $do_it      = $input->param("do_it");
40 my $bookseller = $input->param("bookseller");
41 my $branchcode = $input->param("branchcode");
42 my $expired    = $input->param("expired");
43 my $order      = $input->param("order");
44 my $output     = $input->param("output");
45 my $basename   = $input->param("basename");
46 our $sep       = $input->param("sep") || '';
47 $sep = "\t" if ($sep eq 'tabulation');
48
49 my ($template, $borrowernumber, $cookie)
50         = get_template_and_user({template_name => $templatename,
51                                 query => $input,
52                                 type => "intranet",
53                                 flagsrequired => {reports => '*'},
54                                 });
55                                 
56                                 
57                                 
58 my $dbh = C4::Context->dbh;
59
60 if($do_it){
61     my $where = "WHERE 1 ";
62     my @args;
63     # if a specific branchcode was selected
64     if( $branchcode ne '' ){
65         $where .= "AND branchcode = ? ";
66         push @args,$branchcode;
67     }
68     
69     # if a specific bookseller was selected
70     if($bookseller ne ''){
71         $where .= "AND aqbooksellerid = ? ";
72         push @args,$bookseller;
73     }
74
75     my $sth = $dbh->prepare("SELECT * 
76                              FROM subscription 
77                                LEFT JOIN aqbooksellers 
78                                ON (aqbooksellers.id=subscription.aqbooksellerid)
79                                LEFT JOIN biblio
80                                ON (biblio.biblionumber=subscription.biblionumber)
81                                $where
82                             ");
83
84     $sth->execute(@args);
85     
86     ## hash generation of items by branchcode
87     my @datas;
88
89     while(my $row = $sth->fetchrow_hashref){
90         $row->{'enddate'} = GetExpirationDate($row->{'subscriptionid'});
91         $row->{expired} = HasSubscriptionExpired($row->{subscriptionid});
92         push @datas, $row if (
93             $expired
94             or (
95                 not $expired
96                 and (
97                     not $row->{expired}
98                     and not $row->{closed}
99                 )
100             )
101         );
102     }
103
104     if($output eq 'screen'){
105         $template->param(datas => \@datas,
106                          do_it => 1);
107     }else{
108         binmode STDOUT, ':encoding(UTF-8)';
109         print $input->header(-type => 'application/vnd.sun.xml.calc',
110                          -encoding => 'utf-8',
111                              -name => "$basename.csv",
112                        -attachment => "$basename.csv");
113         print "Vendor".$sep;
114         print "Title".$sep;
115         print "Subscription id".$sep;
116         print "Branch".$sep;
117         print "Callnumber".$sep;
118         print "Subscription Begin".$sep;
119         print "Subscription End\n";
120         
121         foreach my $item (@datas){
122             print $item->{name}.$sep;
123             print $item->{title}.$sep;
124             print $item->{subscriptionid}.$sep;
125             print $item->{branchcode}.$sep;
126             print $item->{callnumber}.$sep;
127             print $item->{startdate}.$sep;
128             print $item->{enddate}."\n";
129         }
130         exit;
131     }
132 }else{
133     ## We generate booksellers list
134     my @booksellers;
135     
136     my $sth = $dbh->prepare("SELECT aqbooksellerid, aqbooksellers.name 
137                                 FROM subscription 
138                                   LEFT JOIN aqbooksellers ON (subscription.aqbooksellerid=aqbooksellers.id ) 
139                                 ORDER BY aqbooksellers.name ASC
140                                 ");
141     $sth->execute();
142     
143     while(my $row = $sth->fetchrow_hashref){
144         push(@booksellers,$row)
145     }
146
147     my $CGIextChoice = ( 'CSV' ); # FIXME translation
148         my $CGIsepChoice=GetDelimiterChoices;
149         $template->param(
150                 CGIextChoice => $CGIextChoice,
151                 CGIsepChoice => $CGIsepChoice,
152         booksellers  => \@booksellers,
153     );
154 }
155
156 output_html_with_http_headers $input, $cookie, $template->output;