Bug 22216: Make GET /patrons/{patron_id} staff only
[koha.git] / serials / serials-collection.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Serials;
27 use C4::Letters;
28 use C4::Output;
29 use C4::Context;
30
31 use Koha::DateUtils qw( dt_from_string );
32
33 use List::MoreUtils qw/uniq/;
34
35
36 my $query = new CGI;
37 my $op = $query->param('op') || q{};
38 my $nbissues=$query->param('nbissues');
39 my $date_received_today = $query->param('date_received_today') || 0;
40 my $dbh = C4::Context->dbh;
41
42 my ($template, $loggedinuser, $cookie)
43   = get_template_and_user({template_name => "serials/serials-collection.tt",
44                             query => $query,
45                             type => "intranet",
46                             authnotrequired => 0,
47                             flagsrequired => {serials => '*'},
48                             debug => 1,
49                             });
50 my $biblionumber = $query->param('biblionumber');
51 my @subscriptionid = $query->multi_param('subscriptionid');
52
53 @subscriptionid= uniq @subscriptionid;
54 @subscriptionid= sort @subscriptionid;
55 my $subscriptiondescs;
56 my $subscriptions;
57
58 if($op eq 'gennext' && @subscriptionid){
59     my $subscriptionid = $subscriptionid[0];
60     my $sth = $dbh->prepare("
61         SELECT publisheddate, publisheddatetext, serialid, serialseq,
62             planneddate
63         FROM serial
64         WHERE status = 1 AND subscriptionid = ?
65     ");
66     my $status = defined( $nbissues ) ? 2 : 3;
67     $nbissues ||= 1;
68     for ( my $i = 0; $i < $nbissues; $i++ ){
69         $sth->execute($subscriptionid);
70         # modify actual expected issue, to generate the next
71         if ( my $issue = $sth->fetchrow_hashref ) {
72             my $planneddate = $date_received_today ? dt_from_string : $issue->{planneddate};
73             ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
74                     $planneddate, $issue->{publisheddate},
75                     $issue->{publisheddatetext}, $status, "" );
76         } else {
77             require C4::Serials::Numberpattern;
78             my $subscription = GetSubscription($subscriptionid);
79             my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
80             my $frequency = C4::Serials::Frequency::GetSubscriptionFrequencies($subscription->{periodicity});
81             my $expected = GetNextExpected($subscriptionid);
82             my (
83                  $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
84                  $newinnerloop1, $newinnerloop2, $newinnerloop3
85             ) = GetNextSeq($subscription, $pattern, $frequency, $expected->{publisheddate});
86
87              ## We generate the next publication date
88              $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
89              my $nextpublisheddate = GetNextDate($subscription, $expected->{publisheddate}, $frequency, 1);
90              my $planneddate = $date_received_today ? dt_from_string : $nextpublisheddate;
91              ## Creating the new issue
92              NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
93                      1, $planneddate, $nextpublisheddate );
94
95              ## Updating the subscription seq status
96              my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
97                          WHERE  subscriptionid = ?";
98              my $seqsth = $dbh->prepare($squery);
99              $seqsth->execute(
100                  $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
101                  $newinnerloop2, $newinnerloop3, $subscriptionid
102                  );
103
104         }
105         last if $nbissues == 1;
106         last if HasSubscriptionExpired($subscriptionid) > 0;
107     }
108     print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
109     exit;
110 }
111
112 my $subscriptioncount;
113 my ($location, $callnumber);
114 if (@subscriptionid){
115    my @subscriptioninformation=();
116    my $closed = 0;
117    foreach my $subscriptionid (@subscriptionid){
118     my $subs= GetSubscription($subscriptionid);
119     next unless $subs;
120     $closed = 1 if $subs->{closed};
121
122     $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
123     $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
124     $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
125
126     ##these are display information
127     $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
128     $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
129     $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
130     $location = $subs->{'location'};
131         $callnumber = $subs->{callnumber};
132     my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
133     my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
134     $subs->{frequency} = $frequency;
135     $subs->{numberpattern} = $numberpattern;
136     $subs->{'hasRouting'} = check_routing($subscriptionid);
137     push @$subscriptiondescs,$subs;
138     my $tmpsubscription= GetFullSubscription($subscriptionid);
139     @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
140   }
141
142   output_and_exit( $query, $cookie, $template, 'unknown_subscription') unless @subscriptioninformation;
143
144   $template->param(closed => $closed);
145   $subscriptions=PrepareSerialsData(\@subscriptioninformation);
146   $subscriptioncount = CountSubscriptionFromBiblionumber($subscriptiondescs->[0]{'biblionumber'});
147 } else {
148   $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber);
149   foreach my $s (@$subscriptiondescs) {
150     my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($s->{periodicity});
151     my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($s->{numberpattern});
152     $s->{frequency} = $frequency;
153     $s->{numberpattern} = $numberpattern;
154   }
155   my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
156   $subscriptions=PrepareSerialsData($subscriptioninformation);
157 }
158
159 my $title = $subscriptiondescs->[0]{bibliotitle};
160 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
161 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
162 my $subscriptionidlist="";
163 foreach my $subscription (@$subscriptiondescs){
164   $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
165   $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
166   $subscription->{'hasRouting'} = check_routing($subscription->{'subscriptionid'});
167 }
168
169 chop $subscriptionidlist;
170 $template->param(
171           subscriptionidlist => $subscriptionidlist,
172           biblionumber => $biblionumber,
173           subscriptions => $subscriptiondescs,
174           years => $subscriptions,
175           yearmin => $yearmin,
176           yearmax =>$yearmax,
177           bibliotitle => $title,
178           suggestion => C4::Context->preference("suggestion"),
179           virtualshelves => C4::Context->preference("virtualshelves"),
180           routing => C4::Context->preference("RoutingSerials"),
181           subscr=>scalar $query->param('subscriptionid'),
182           subscriptioncount => $subscriptioncount,
183           location => $location,
184           callnumber           => $callnumber,
185           uc(C4::Context->preference("marcflavour")) => 1,
186           serialsadditems   => $subscriptiondescs->[0]{'serialsadditems'},
187           );
188
189 output_html_with_http_headers $query, $cookie, $template->output;