Bug 6050 Make calls to GetItemsInfo consistent
[wip/koha-chris_n.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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21
22 use strict;
23 use warnings;
24 use CGI;
25 use C4::Auth;
26 use C4::Koha;
27 use C4::Dates qw/format_date/;
28 use C4::Serials;
29 use C4::Letters;
30 use C4::Output;
31 use C4::Context;
32 use List::MoreUtils qw/uniq/;
33
34
35 my $query = new CGI;
36 my $op = $query->param('op') || q{};
37 my $dbh = C4::Context->dbh;
38
39 my ($template, $loggedinuser, $cookie);
40 ($template, $loggedinuser, $cookie)
41   = get_template_and_user({template_name => "serials/serials-collection.tmpl",
42                             query => $query,
43                             type => "intranet",
44                             authnotrequired => 0,
45                             flagsrequired => {serials => 'receive_serials'},
46                             debug => 1,
47                             });
48 my $biblionumber = $query->param('biblionumber');
49 my @subscriptionid = $query->param('subscriptionid');
50
51 @subscriptionid= uniq @subscriptionid;
52 @subscriptionid= sort @subscriptionid;
53 my $subscriptiondescs;
54 my $subscriptions;
55
56 if($op eq 'gennext' && @subscriptionid){
57     my $subscriptionid = $subscriptionid[0];
58     my $subscription = GetSubscription($subscriptionid);
59
60         my $sth = $dbh->prepare("SELECT publisheddate, serialid, serialseq, planneddate
61                                                         FROM serial WHERE status = 1 AND subscriptionid = ?");
62         $sth->execute($subscriptionid);
63
64         # modify actual expected issue, to generate the next
65         if ( my $issue = $sth->fetchrow_hashref ) {
66                 ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
67                 $issue->{planneddate}, $issue->{publisheddate},
68                 3, "" );
69         }else{
70                 my $expected = GetNextExpected($subscriptionid);
71             my (
72                  $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
73              $newinnerloop1, $newinnerloop2, $newinnerloop3
74             ) = GetNextSeq($subscription);
75
76              ## We generate the next publication date
77              my $nextpublisheddate = GetNextDate( $expected->{planneddate}->output('iso'), $subscription );
78              ## Creating the new issue
79              NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
80                      1, $nextpublisheddate, $nextpublisheddate );
81
82              ## Updating the subscription seq status
83              my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
84                          WHERE  subscriptionid = ?";
85              $sth = $dbh->prepare($squery);
86              $sth->execute(
87                  $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
88                  $newinnerloop2, $newinnerloop3, $subscriptionid
89                  );
90
91         }
92
93     print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
94 }
95
96 my ($location, $callnumber);
97 if (@subscriptionid){
98    my @subscriptioninformation=();
99    foreach my $subscriptionid (@subscriptionid){
100     my $subs= GetSubscription($subscriptionid);
101     $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
102     $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
103     $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
104     ##these are display information
105     $subs->{ "periodicity" . $subs->{periodicity} } = 1;
106     $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
107     $subs->{ "status" . $subs->{'status'} } = 1;
108     $subs->{startdate}     = format_date( $subs->{startdate} );
109     $subs->{histstartdate} = format_date( $subs->{histstartdate} );
110     if ( !defined $subs->{enddate} || $subs->{enddate} eq '0000-00-00' ) {
111         $subs->{enddate} = '';
112     }
113     else {
114         $subs->{enddate} = format_date( $subs->{enddate} );
115     }
116     $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
117     $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
118     $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
119         $location = GetAuthorisedValues('LOC', $subs->{'location'});
120         $callnumber = $subs->{callnumber};
121     push @$subscriptiondescs,$subs;
122     my $tmpsubscription= GetFullSubscription($subscriptionid);
123     @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
124   }
125   $subscriptions=PrepareSerialsData(\@subscriptioninformation);
126 } else {
127   $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber) ;
128   my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
129   $subscriptions=PrepareSerialsData($subscriptioninformation);
130 }
131
132 my $title = $subscriptiondescs->[0]{bibliotitle};
133 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
134 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
135 my $subscriptionidlist="";
136 foreach my $subscription (@$subscriptiondescs){
137   $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
138   $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
139 }
140
141 # warn "title : $title yearmax : $yearmax nombre d'elements dans le tableau :".scalar(@$subscriptions);
142 #  use Data::Dumper; warn Dumper($subscriptions);
143 my $locationlib;
144 foreach (@$location) {
145     $locationlib = $_->{'lib'} if $_->{'selected'};
146 }
147
148 chop $subscriptionidlist;
149 $template->param(
150           onesubscription => (scalar(@$subscriptiondescs)==1),
151           subscriptionidlist => $subscriptionidlist,
152           biblionumber => $biblionumber,
153           subscriptions => $subscriptiondescs,
154           years => $subscriptions,
155           yearmin => $yearmin,
156           yearmax =>$yearmax,
157           bibliotitle => $title,
158           suggestion => C4::Context->preference("suggestion"),
159           virtualshelves => C4::Context->preference("virtualshelves"),
160           subscr=>$query->param('subscriptionid'),
161     location           => $locationlib,
162     callnumber         => $callnumber,
163           );
164
165 output_html_with_http_headers $query, $cookie, $template->output;