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