Bug 28772: (QA follow-up) Apply change to other dbrev too [STABLE]
[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      = new CGI;
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                                 debug => 1,
55                                 });
56                                 
57                                 
58                                 
59 my $dbh = C4::Context->dbh;
60
61 if($do_it){
62     my $where = "WHERE 1 ";
63     my @args;
64     # if a specific branchcode was selected
65     if( $branchcode ne '' ){
66         $where .= "AND branchcode = ? ";
67         push @args,$branchcode;
68     }
69     
70     # if a specific bookseller was selected
71     if($bookseller ne ''){
72         $where .= "AND aqbooksellerid = ? ";
73         push @args,$bookseller;
74     }
75
76     my $sth = $dbh->prepare("SELECT * 
77                              FROM subscription 
78                                LEFT JOIN aqbooksellers 
79                                ON (aqbooksellers.id=subscription.aqbooksellerid)
80                                LEFT JOIN biblio
81                                ON (biblio.biblionumber=subscription.biblionumber)
82                                $where
83                             ");
84
85     $sth->execute(@args);
86     
87     ## hash generation of items by branchcode
88     my @datas;
89
90     while(my $row = $sth->fetchrow_hashref){
91         $row->{'enddate'} = GetExpirationDate($row->{'subscriptionid'});
92         $row->{expired} = HasSubscriptionExpired($row->{subscriptionid});
93         push @datas, $row if (
94             $expired
95             or (
96                 not $expired
97                 and (
98                     not $row->{expired}
99                     and not $row->{closed}
100                 )
101             )
102         );
103     }
104
105     if($output eq 'screen'){
106         $template->param(datas => \@datas,
107                          do_it => 1);
108     }else{
109         binmode STDOUT, ':encoding(UTF-8)';
110         print $input->header(-type => 'application/vnd.sun.xml.calc',
111                          -encoding => 'utf-8',
112                              -name => "$basename.csv",
113                        -attachment => "$basename.csv");
114         print "Vendor".$sep;
115         print "Title".$sep;
116         print "Subscription id".$sep;
117         print "Branch".$sep;
118         print "Callnumber".$sep;
119         print "Subscription Begin".$sep;
120         print "Subscription End\n";
121         
122         foreach my $item (@datas){
123             print $item->{name}.$sep;
124             print $item->{title}.$sep;
125             print $item->{subscriptionid}.$sep;
126             print $item->{branchcode}.$sep;
127             print $item->{callnumber}.$sep;
128             print $item->{startdate}.$sep;
129             print $item->{enddate}."\n";
130         }
131         exit;
132     }
133 }else{
134     ## We generate booksellers list
135     my @booksellers;
136     
137     my $sth = $dbh->prepare("SELECT aqbooksellerid, aqbooksellers.name 
138                                 FROM subscription 
139                                   LEFT JOIN aqbooksellers ON (subscription.aqbooksellerid=aqbooksellers.id ) 
140                                 ORDER BY aqbooksellers.name ASC
141                                 ");
142     $sth->execute();
143     
144     while(my $row = $sth->fetchrow_hashref){
145         push(@booksellers,$row)
146     }
147
148     my $CGIextChoice = ( 'CSV' ); # FIXME translation
149         my $CGIsepChoice=GetDelimiterChoices;
150         $template->param(
151                 CGIextChoice => $CGIextChoice,
152                 CGIsepChoice => $CGIsepChoice,
153         booksellers  => \@booksellers,
154     );
155 }
156
157 output_html_with_http_headers $input, $cookie, $template->output;