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