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