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