Split off koha-common.
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI;
24 use C4::Context;
25 use C4::Branch; # GetBranches
26 use C4::Dates qw/format_date/;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Reports;
30 use C4::Serials;
31
32 =head1 serials_out
33
34 plugin that shows a stats on serials
35
36 =head1 DESCRIPTION
37
38 =over 2
39
40 =cut
41
42 my $input      = new CGI;
43 my $templatename   = "reports/serials_stats.tmpl";
44 my $do_it      = $input->param("do_it");
45 my $bookseller = $input->param("bookseller");
46 my $branchcode = $input->param("branchcode");
47 my $expired    = $input->param("expired");
48 my $order      = $input->param("order");
49 my $output     = $input->param("output");
50 my $basename   = $input->param("basename");
51 my $mime       = $input->param("MIME");
52 our $sep       = $input->param("sep") || '';
53 $sep = "\t" if ($sep eq 'tabulation');
54
55 my ($template, $borrowernumber, $cookie)
56         = get_template_and_user({template_name => $templatename,
57                                 query => $input,
58                                 type => "intranet",
59                                 authnotrequired => 0,
60                                 flagsrequired => {reports => '*'},
61                                 debug => 1,
62                                 });
63                                 
64                                 
65                                 
66 my $dbh = C4::Context->dbh;
67
68 if($do_it){
69     my $where = "WHERE 1 ";
70     my @args;
71     # if a specific branchcode was selected
72     if( $branchcode ne '' ){
73         $where .= "AND branchcode = ? ";
74         push @args,$branchcode;
75     }
76     
77     # if a specific bookseller was selected
78     if($bookseller ne ''){
79         $where .= "AND aqbooksellerid = ? ";
80         push @args,$bookseller;
81     }
82
83     my $sth = $dbh->prepare("SELECT * 
84                              FROM subscription 
85                                LEFT JOIN aqbooksellers 
86                                ON (aqbooksellers.id=subscription.aqbooksellerid)
87                                LEFT JOIN biblio
88                                ON (biblio.biblionumber=subscription.biblionumber)
89                                $where
90                             ");
91
92     $sth->execute(@args);
93     
94     ## hash generation of items by branchcode
95     my @datas;
96
97     while(my $row = $sth->fetchrow_hashref){
98         $row->{'enddate'} = format_date(GetExpirationDate($row->{'subscriptionid'}));
99         $row->{'startdate'} = format_date($row->{'startdate'});
100         push @datas, $row if ($expired || (not $expired && not HasSubscriptionExpired($row->{subscriptionid})) );
101     }
102     
103     if($output eq 'screen'){
104         $template->param(datas => \@datas,
105                          do_it => 1);
106     }else{
107         binmode STDOUT, ':utf8';
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(1);
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                                 GROUP BY aqbooksellerid");
139     $sth->execute();
140     
141     while(my $row = $sth->fetchrow_hashref){
142         push(@booksellers,$row)
143     }
144    
145     ## We generate branchlist
146     my $branches=GetBranches();
147         my @branchloop;
148         foreach (sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches) {
149                 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
150                 my %row = (branchcode => $_,
151                         selected => ($thisbranch eq $_ ? 1 : 0),
152                         branchname => $branches->{$_}->{'branchname'},
153                 );
154                 push @branchloop, \%row;
155         } 
156     
157     my @mime = ( C4::Context->preference("MIME") );
158         # warn 'MIME(s): ' . join ' ', @mime;
159         my $CGIextChoice=CGI::scrolling_list(
160                                 -name => 'MIME',
161                                 -id => 'MIME',
162                                 -values   => \@mime,
163                                 -size     => 1,
164                                 -multiple => 0 );
165         my $CGIsepChoice=GetDelimiterChoices;
166         $template->param(
167                 CGIextChoice => $CGIextChoice,
168                 CGIsepChoice => $CGIsepChoice,
169         booksellers  => \@booksellers,
170         branches     => \@branchloop);
171 }
172
173 output_html_with_http_headers $input, $cookie, $template->output;