Bug 7688: Change subscription numbering pattern and frequencies
[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 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 $nbissues=$query->param('nbissues');
38 my $dbh = C4::Context->dbh;
39
40 my ($template, $loggedinuser, $cookie);
41 ($template, $loggedinuser, $cookie)
42   = get_template_and_user({template_name => "serials/serials-collection.tmpl",
43                             query => $query,
44                             type => "intranet",
45                             authnotrequired => 0,
46                             flagsrequired => {serials => '*'},
47                             debug => 1,
48                             });
49 my $biblionumber = $query->param('biblionumber');
50 my @subscriptionid = $query->param('subscriptionid');
51
52 @subscriptionid= uniq @subscriptionid;
53 @subscriptionid= sort @subscriptionid;
54 my $subscriptiondescs;
55 my $subscriptions;
56
57 if($op eq 'gennext' && @subscriptionid){
58     my $subscriptionid = $subscriptionid[0];
59     my $sth = $dbh->prepare("SELECT publisheddate, serialid, serialseq, planneddate
60                                                         FROM serial WHERE status = 1 AND subscriptionid = ?");
61     my $status = defined( $nbissues ) ? 2 : 3;
62     $nbissues ||= 1;
63     for ( my $i = 0; $i < $nbissues; $i++ ){
64         $sth->execute($subscriptionid);
65         # modify actual expected issue, to generate the next
66         if ( my $issue = $sth->fetchrow_hashref ) {
67                 ModSerialStatus( $issue->{serialid}, $issue->{serialseq},
68                 $issue->{planneddate}, $issue->{publisheddate},
69                 $status, "" );
70         }else{
71             my $subscription = GetSubscription($subscriptionid);
72             my $expected = GetNextExpected($subscriptionid);
73             my (
74                  $newserialseq,  $newlastvalue1, $newlastvalue2, $newlastvalue3,
75              $newinnerloop1, $newinnerloop2, $newinnerloop3
76             ) = GetNextSeq($subscription);
77
78              ## We generate the next publication date
79              my $nextpublisheddate = GetNextDate( $expected->{planneddate}->output('iso'), $subscription );
80              ## Creating the new issue
81              NewIssue( $newserialseq, $subscriptionid, $subscription->{'biblionumber'},
82                      1, $nextpublisheddate, $nextpublisheddate );
83
84              ## Updating the subscription seq status
85              my $squery = "UPDATE subscription SET lastvalue1=?, lastvalue2=?, lastvalue3=?, innerloop1=?, innerloop2=?, innerloop3=?
86                          WHERE  subscriptionid = ?";
87              my $seqsth = $dbh->prepare($squery);
88              $seqsth->execute(
89                  $newlastvalue1, $newlastvalue2, $newlastvalue3, $newinnerloop1,
90                  $newinnerloop2, $newinnerloop3, $subscriptionid
91                  );
92
93         }
94         last if $nbissues == 1;
95         last if HasSubscriptionExpired($subscriptionid) > 0;
96     }
97     print $query->redirect('/cgi-bin/koha/serials/serials-collection.pl?subscriptionid='.$subscriptionid);
98 }
99
100 my $subscriptioncount;
101 my ($location, $callnumber);
102 if (@subscriptionid){
103    my @subscriptioninformation=();
104    my $closed = 0;
105    foreach my $subscriptionid (@subscriptionid){
106     my $subs= GetSubscription($subscriptionid);
107     $closed = 1 if $subs->{closed};
108     $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
109     $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
110     $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
111     ##these are display information
112     $subs->{startdate}     = format_date( $subs->{startdate} );
113     $subs->{histstartdate} = format_date( $subs->{histstartdate} );
114     if ( !defined $subs->{enddate} || $subs->{enddate} eq '0000-00-00' ) {
115         $subs->{enddate} = '';
116     }
117     else {
118         $subs->{enddate} = format_date( $subs->{enddate} );
119     }
120     $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
121     $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
122     $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
123         $location = GetAuthorisedValues('LOC', $subs->{'location'});
124         $callnumber = $subs->{callnumber};
125     my $frequency = C4::Serials::Frequency::GetSubscriptionFrequency($subs->{periodicity});
126     my $numberpattern = C4::Serials::Numberpattern::GetSubscriptionNumberpattern($subs->{numberpattern});
127     $subs->{frequency} = $frequency;
128     $subs->{numberpattern} = $numberpattern;
129     push @$subscriptiondescs,$subs;
130     my $tmpsubscription= GetFullSubscription($subscriptionid);
131     @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
132   }
133   $template->param(closed => $closed);
134   $subscriptions=PrepareSerialsData(\@subscriptioninformation);
135   $subscriptioncount = CountSubscriptionFromBiblionumber($subscriptiondescs->[0]{'biblionumber'});
136 } else {
137   $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber) ;
138   my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
139   $subscriptions=PrepareSerialsData($subscriptioninformation);
140 }
141
142 my $title = $subscriptiondescs->[0]{bibliotitle};
143 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
144 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
145 my $subscriptionidlist="";
146 foreach my $subscription (@$subscriptiondescs){
147   $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
148   $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
149 }
150
151 # warn "title : $title yearmax : $yearmax nombre d'elements dans le tableau :".scalar(@$subscriptions);
152 #  use Data::Dumper; warn Dumper($subscriptions);
153 my $locationlib;
154 foreach (@$location) {
155     $locationlib = $_->{'lib'} if $_->{'selected'};
156 }
157
158
159 chop $subscriptionidlist;
160 $template->param(
161           subscriptionidlist => $subscriptionidlist,
162           biblionumber => $biblionumber,
163           subscriptions => $subscriptiondescs,
164           years => $subscriptions,
165           yearmin => $yearmin,
166           yearmax =>$yearmax,
167           bibliotitle => $title,
168           suggestion => C4::Context->preference("suggestion"),
169           virtualshelves => C4::Context->preference("virtualshelves"),
170           routing => C4::Context->preference("RoutingSerials"),
171           subscr=>$query->param('subscriptionid'),
172           subscriptioncount => $subscriptioncount,
173           location             => $locationlib,
174           callnumber           => $callnumber,
175           uc(C4::Context->preference("marcflavour")) => 1,
176           serialsadditems   => $subscriptiondescs->[0]{'serialsadditems'},
177           dateformatmetric   => C4::Context->preference("dateformat") eq "metric" ? 1 : 0,
178           );
179
180 output_html_with_http_headers $query, $cookie, $template->output;