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