Bug 24759: Move OpacRenewalBranch code to Koha::Item
[koha.git] / serials / serials-collection.pl
blobd4b266576c6341f4e71b446d3e20be6b4edb2e0a
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
4 # Parts Copyright 2010 Biblibre
6 # This file is part of Koha.
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.
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.
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>.
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 use Koha::Serial::Items;
32 use Koha::DateUtils qw( dt_from_string );
34 use List::MoreUtils qw/uniq/;
37 my $query = new CGI;
38 my $op = $query->param('op') || q{};
39 my $nbissues=$query->param('nbissues');
40 my $date_received_today = $query->param('date_received_today') || 0;
41 my $dbh = C4::Context->dbh;
43 my ($template, $loggedinuser, $cookie)
44 = get_template_and_user({template_name => "serials/serials-collection.tt",
45 query => $query,
46 type => "intranet",
47 authnotrequired => 0,
48 flagsrequired => {serials => '*'},
49 debug => 1,
50 });
51 my $biblionumber = $query->param('biblionumber');
52 my @subscriptionid = $query->multi_param('subscriptionid');
54 @subscriptionid= uniq @subscriptionid;
55 @subscriptionid= sort @subscriptionid;
56 my $subscriptiondescs;
57 my $subscriptions;
59 if($op eq 'gennext' && @subscriptionid){
60 my $subscriptionid = $subscriptionid[0];
61 my $sth = $dbh->prepare("
62 SELECT publisheddate, publisheddatetext, serialid, serialseq,
63 planneddate, notes, routingnotes
64 FROM serial
65 WHERE status = 1 AND subscriptionid = ?
66 ");
67 my $status = defined( $nbissues ) ? 2 : 3;
68 $nbissues ||= 1;
69 for ( my $i = 0; $i < $nbissues; $i++ ){
70 $sth->execute($subscriptionid);
71 # modify actual expected issue, to generate the next
72 if ( my $issue = $sth->fetchrow_hashref ) {
73 my $planneddate = $date_received_today ? dt_from_string : $issue->{planneddate};
74 ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
75 $planneddate, $issue->{publisheddate},
76 $issue->{publisheddatetext}, $status, "" );
77 } else {
78 require C4::Serials::Numberpattern;
79 my $subscription = GetSubscription($subscriptionid);
80 my $pattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subscription->{numberpattern});
81 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequencies($subscription->{periodicity});
82 my $expected = GetNextExpected($subscriptionid);
83 my (
84 $newserialseq, $newlastvalue1, $newlastvalue2, $newlastvalue3,
85 $newinnerloop1, $newinnerloop2, $newinnerloop3
86 ) = GetNextSeq($subscription, $pattern, $frequency, $expected->{publisheddate});
88 ## We generate the next publication date
89 $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subscription->{periodicity});
90 my $nextpublisheddate = GetNextDate($subscription, $expected->{publisheddate}, $frequency, 1);
91 my $planneddate = $date_received_today ? dt_from_string : $nextpublisheddate;
92 ## Creating the new issue
93 NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
94 1, $planneddate, $nextpublisheddate, undef,
95 $issue->{notes}, $issue->{routingnotes} );
97 ## Updating the subscription seq status
98 my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
99 WHERE subscriptionid = ?";
100 my $seqsth = $dbh->prepare($squery);
101 $seqsth->execute(
102 $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
103 $newinnerloop2, $newinnerloop3, $subscriptionid
107 last if $nbissues == 1;
108 last if HasSubscriptionExpired($subscriptionid) > 0;
110 print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
111 exit;
114 my $countitems = 0;
115 my @serialsid = $query->multi_param('serialid');
116 my $subscriptionid = $subscriptionid[0];
118 if($op eq 'delete_confirm'){
119 foreach my $serialid (@serialsid){
120 $countitems += Koha::Serial::Items->search({serialid => $serialid})->count();
122 }elsif($op eq 'delete_confirmed'){
123 if($query->param('delitems') eq "Yes"){
124 foreach my $serialid (@serialsid){
125 my @itemnumbers = Koha::Serial::Items->search({serialid => $serialid})->get_column('itemnumber');
126 foreach my $itemnumber (@itemnumbers){
127 my $delcheck = C4::Items::DelItemCheck($biblionumber, $itemnumber);
128 $template->param(error_delitem => 1) if $delcheck != 1;
132 for my $serialid (@serialsid){
133 my $serial = Koha::Serials->find($serialid);
134 ModSerialStatus($serialid, $serial->serialseq, $serial->planneddate, $serial->publisheddate, $serial->publisheddatetext, 6, "");
138 my $subscriptioncount;
139 my ($location, $callnumber);
140 if (@subscriptionid){
141 my @subscriptioninformation=();
142 my $closed = 0;
143 foreach my $subscriptionid (@subscriptionid){
144 my $subs= GetSubscription($subscriptionid);
145 next unless $subs;
146 $closed = 1 if $subs->{closed};
148 ##these are display information
149 $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
150 $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
151 $subs->{'subscriptionid'} = $subscriptionid; # FIXME - why was this lost ?
152 $location = $subs->{'location'};
153 $callnumber = $subs->{callnumber};
154 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
155 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
156 $subs->{frequency} = $frequency;
157 $subs->{numberpattern} = $numberpattern;
158 $subs->{'hasRouting'} = check_routing($subscriptionid);
159 push @$subscriptiondescs,$subs;
160 my $tmpsubscription= GetFullSubscription($subscriptionid);
161 @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
164 output_and_exit( $query, $cookie, $template, 'unknown_subscription') unless @subscriptioninformation;
166 $template->param(closed => $closed);
167 $subscriptions=PrepareSerialsData(\@subscriptioninformation);
168 $subscriptioncount = CountSubscriptionFromBiblionumber($subscriptiondescs->[0]{'biblionumber'});
169 } else {
170 $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber);
171 foreach my $s (@$subscriptiondescs) {
172 my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($s->{periodicity});
173 my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($s->{numberpattern});
174 $s->{frequency} = $frequency;
175 $s->{numberpattern} = $numberpattern;
177 my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
178 $subscriptions=PrepareSerialsData($subscriptioninformation);
181 my $title = $subscriptiondescs->[0]{bibliotitle};
182 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
183 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
184 my $subscriptionidlist="";
185 foreach my $subscription (@$subscriptiondescs){
186 $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
187 $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
188 $subscription->{'hasRouting'} = check_routing($subscription->{'subscriptionid'});
191 chop $subscriptionidlist;
192 $template->param(
193 subscriptionidlist => $subscriptionidlist,
194 biblionumber => $biblionumber,
195 subscriptions => $subscriptiondescs,
196 years => $subscriptions,
197 yearmin => $yearmin,
198 yearmax =>$yearmax,
199 bibliotitle => $title,
200 suggestion => C4::Context->preference("suggestion"),
201 virtualshelves => C4::Context->preference("virtualshelves"),
202 routing => C4::Context->preference("RoutingSerials"),
203 subscr=>scalar $query->param('subscriptionid'),
204 subscriptioncount => $subscriptioncount,
205 location => $location,
206 callnumber => $callnumber,
207 uc(C4::Context->preference("marcflavour")) => 1,
208 serialsadditems => $subscriptiondescs->[0]{'serialsadditems'},
209 delete => ($op eq 'delete_confirm'),
210 subscriptionid => $subscriptionid,
211 countitems => $countitems,
212 serialnumber => scalar @serialsid,
213 serialsid => \@serialsid,
216 output_html_with_http_headers $query, $cookie, $template->output;