Bug Fixing : Suggestions.pm warnings on line 132
[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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 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 => 1},
61                                 debug => 1,
62                                 });
63                                 
64                                 
65                                 
66 my $dbh = C4::Context->dbh;
67
68 if($do_it){
69         
70         my $periodicities = {
71                 16 => 'Without periodicity'
72                 , 48 => 'Unknown'
73                 , 32 => 'Irregular'
74                 , 12 => '2/day'
75                 , 1 => 'daily (n/week)'
76                 , 2 => '1/week'
77                 , 3 => '1/2 weeks'
78                 , 4 => '1/3 weeks'
79                 , 5 => '1/month'
80                 , 6 => '1/2 months (6/year)'
81                 , 7 => '1/3 months (1/quarter)'
82                 , 8 => '1/quarter (seasonal)'
83                 , 9 => '2/years'
84                 , 10 => '1/year'
85                 , 11 => '1/2 years'
86         };
87         
88     my $where = "WHERE 1 ";
89     my @args;
90     # if a specific branchcode was selected
91     if( $branchcode ne '' ){
92         $where .= "AND branchcode = ? ";
93         push @args,$branchcode;
94     }
95     
96     # if a specific bookseller was selected
97     if($bookseller ne ''){
98         $where .= "AND aqbooksellerid = ? ";
99         push @args,$bookseller;
100     }
101
102     my $sth = $dbh->prepare("SELECT * 
103                              FROM subscription 
104                                LEFT JOIN aqbooksellers 
105                                ON (aqbooksellers.id=subscription.aqbooksellerid)
106                                LEFT JOIN biblio
107                                ON (biblio.biblionumber=subscription.biblionumber)
108                                $where
109                             ");
110
111     $sth->execute(@args);
112     
113     ## hash generation of items by branchcode
114     my @datas;
115
116     while(my $row = $sth->fetchrow_hashref){
117         $row->{'periodicity'} = $periodicities->{$row->{'periodicity'}};
118         $row->{'enddate'} = format_date(GetExpirationDate($row->{'subscriptionid'}));
119         $row->{'startdate'} = format_date($row->{'startdate'});
120         push @datas, $row if ($expired || (not $expired && not HasSubscriptionExpired($row->{subscriptionid})) );
121     }
122     
123     if($output eq 'screen'){
124         $template->param(datas => \@datas,
125                          do_it => 1);
126     }else{
127         binmode STDOUT, ':utf8';
128         print $input->header(-type => 'application/vnd.sun.xml.calc',
129                          -encoding => 'utf-8',
130                              -name => "$basename.csv",
131                        -attachment => "$basename.csv");
132         print "Vendor".$sep;
133         print "Title".$sep;
134         print "Subscription id".$sep;
135         print "Branch".$sep;
136         print "Callnumber".$sep;
137         print "Periodicity".$sep;
138         print "Subscription Begin".$sep;
139         print "Subscription End\n";
140         
141         foreach my $item (@datas){
142             print $item->{name}.$sep;
143             print $item->{title}.$sep;
144             print $item->{subscriptionid}.$sep;
145             print $item->{branchcode}.$sep;
146             print $item->{callnumber}.$sep;
147             print $item->{periodicity}.$sep;
148             print $item->{startdate}.$sep;
149             print $item->{enddate}."\n";
150         }
151         exit(1);
152     }
153 }else{
154     ## We generate booksellers list
155     my @booksellers;
156     
157     my $sth = $dbh->prepare("SELECT aqbooksellerid, aqbooksellers.name 
158                                 FROM subscription 
159                                   LEFT JOIN aqbooksellers ON (subscription.aqbooksellerid=aqbooksellers.id ) 
160                                 GROUP BY aqbooksellerid");
161     $sth->execute();
162     
163     while(my $row = $sth->fetchrow_hashref){
164         push(@booksellers,$row)
165     }
166    
167     ## We generate branchlist
168     my $branches=GetBranches();
169         my @branchloop;
170         foreach (keys %$branches) {
171                 my $thisbranch = ''; # FIXME: populate $thisbranch to preselect one
172                 my %row = (branchcode => $_,
173                         selected => ($thisbranch eq $_ ? 1 : 0),
174                         branchname => $branches->{$_}->{'branchname'},
175                 );
176                 push @branchloop, \%row;
177         } 
178     
179     my @mime = ( C4::Context->preference("MIME") );
180         # warn 'MIME(s): ' . join ' ', @mime;
181         my $CGIextChoice=CGI::scrolling_list(
182                                 -name => 'MIME',
183                                 -id => 'MIME',
184                                 -values   => \@mime,
185                                 -size     => 1,
186                                 -multiple => 0 );
187         my $CGIsepChoice=GetDelimiterChoices;
188         $template->param(
189                 CGIextChoice => $CGIextChoice,
190                 CGIsepChoice => $CGIsepChoice,
191         booksellers  => \@booksellers,
192         branches     => \@branchloop);
193 }
194
195 output_html_with_http_headers $input, $cookie, $template->output;