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