Merge remote branch 'kc/new/enh/bug_2965' into kcmaster
[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 my $subscriptiondescs;
53 my $subscriptions;
54
55 if($op eq 'gennext' && @subscriptionid){
56     my $subscriptionid = $subscriptionid[0];
57     my $subscription = GetSubscription($subscriptionid);
58
59         my $sth = $dbh->prepare("SELECT publisheddate, serialid, serialseq, planneddate
60                                                         FROM serial WHERE status = 1 AND subscriptionid = ?");
61         $sth->execute($subscriptionid);
62
63         # modify actual expected issue, to generate the next
64         if ( my $issue = $sth->fetchrow_hashref ) {
65                 ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
66                 $issue->{planneddate}, $issue->{publisheddate},
67                 3, "" );
68         }else{
69                 my $expected = GetNextExpected($subscriptionid);
70             my (
71                  $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
72              $newinnerloop1, $newinnerloop2, $newinnerloop3
73             ) = GetNextSeq($subscription);
74
75              ## We generate the next publication date
76              my $nextpublisheddate = GetNextDate( $expected->{planneddate}->output('iso'), $subscription );
77              ## Creating the new issue
78              NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
79                      1, $nextpublisheddate, $nextpublisheddate );
80
81              ## Updating the subscription seq status
82              my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
83                          WHERE  subscriptionid = ?";
84              $sth = $dbh->prepare($squery);
85              $sth->execute(
86                  $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
87                  $newinnerloop2, $newinnerloop3, $subscriptionid
88                  );
89
90         }
91
92     print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
93 }
94
95 my ($location, $callnumber);
96 if (@subscriptionid){
97    my @subscriptioninformation=();
98    foreach my $subscriptionid (@subscriptionid){
99     my $subs= GetSubscription($subscriptionid);
100     $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
101     $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
102     $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
103     ##these are display information
104     $subs->{ "periodicity" . $subs->{periodicity} } = 1;
105     $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
106     $subs->{ "status" . $subs->{'status'} } = 1;
107     $subs->{startdate}     = format_date( $subs->{startdate} );
108     $subs->{histstartdate} = format_date( $subs->{histstartdate} );
109     if ( !defined $subs->{enddate} || $subs->{enddate} eq '0000-00-00' ) {
110         $subs->{enddate} = '';
111     }
112     else {
113         $subs->{enddate} = format_date( $subs->{enddate} );
114     }
115     $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
116     $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
117     $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
118         $location = GetAuthorisedValues('LOC', $subs->{'location'});
119         $callnumber = $subs->{callnumber};
120     push @$subscriptiondescs,$subs;
121     my $tmpsubscription= GetFullSubscription($subscriptionid);
122     @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
123   }
124   $subscriptions=PrepareSerialsData(\@subscriptioninformation);
125 } else {
126   $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber) ;
127   my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
128   $subscriptions=PrepareSerialsData($subscriptioninformation);
129 }
130
131 my $title = $subscriptiondescs->[0]{bibliotitle};
132 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
133 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
134 my $subscriptionidlist="";
135 foreach my $subscription (@$subscriptiondescs){
136   $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
137   $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
138 }
139
140 # warn "title : $title yearmax : $yearmax nombre d'elements dans le tableau :".scalar(@$subscriptions);
141 #  use Data::Dumper; warn Dumper($subscriptions);
142 my $locationlib;
143 foreach (@$location) {
144     $locationlib = $_->{'lib'} if $_->{'selected'};
145 }
146
147 chop $subscriptionidlist;
148 $template->param(
149           onesubscription => (scalar(@$subscriptiondescs)==1),
150           subscriptionidlist => $subscriptionidlist,
151           biblionumber => $biblionumber,
152           subscriptions => $subscriptiondescs,
153           years => $subscriptions,
154           yearmin => $yearmin,
155           yearmax =>$yearmax,
156           bibliotitle => $title,
157           suggestion => C4::Context->preference("suggestion"),
158           virtualshelves => C4::Context->preference("virtualshelves"),
159           subscr=>$query->param('subscriptionid'),
160     location           => $locationlib,
161     callnumber         => $callnumber,
162           );
163
164 output_html_with_http_headers $query, $cookie, $template->output;