Ukranian and Russian sql updates courtesy of Сергій Дубик
[koha.git] / serials / serials-collection.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20
21 use strict;
22 use warnings;
23 use CGI;
24 use C4::Auth;
25 use C4::Koha;
26 use C4::Dates qw/format_date/;
27 use C4::Serials;
28 use C4::Letters;
29 use C4::Output;
30 use C4::Context;
31
32
33 my $query = new CGI;
34 my $op = $query->param('op');
35 my $dbh = C4::Context->dbh;
36
37 my $sth;
38 # my $id;
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 my $subscriptiondescs ;
52 my $subscriptions;
53
54 $op ||= q{};
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, "Automatically set to late" );
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 if (@subscriptionid){
96    my @subscriptioninformation=();
97    foreach my $subscriptionid (@subscriptionid){
98     my $subs= GetSubscription($subscriptionid);
99     $subs->{opacnote}     =~ s/\n/\<br\/\>/g;
100     $subs->{missinglist}  =~ s/\n/\<br\/\>/g;
101     $subs->{recievedlist} =~ s/\n/\<br\/\>/g;
102     ##these are display information
103     $subs->{ "periodicity" . $subs->{periodicity} } = 1;
104     $subs->{ "numberpattern" . $subs->{numberpattern} } = 1;
105     $subs->{ "status" . $subs->{'status'} } = 1;
106     $subs->{startdate}     = format_date( $subs->{startdate} );
107     $subs->{histstartdate} = format_date( $subs->{histstartdate} );
108     if ( !defined $subs->{enddate} || $subs->{enddate} eq '0000-00-00' ) {
109         $subs->{enddate} = '';
110     }
111     else {
112         $subs->{enddate} = format_date( $subs->{enddate} );
113     }
114     $subs->{'abouttoexpire'}=abouttoexpire($subs->{'subscriptionid'});
115     $subs->{'subscriptionexpired'}=HasSubscriptionExpired($subs->{'subscriptionid'});
116     $subs->{'subscriptionid'} = $subscriptionid;  # FIXME - why was this lost ?
117     push @$subscriptiondescs,$subs;
118     my $tmpsubscription= GetFullSubscription($subscriptionid);
119     @subscriptioninformation=(@$tmpsubscription,@subscriptioninformation);
120   }
121   $subscriptions=PrepareSerialsData(\@subscriptioninformation);
122 } else {
123   $subscriptiondescs = GetSubscriptionsFromBiblionumber($biblionumber) ;
124   my $subscriptioninformation = GetFullSubscriptionsFromBiblionumber($biblionumber);
125   $subscriptions=PrepareSerialsData($subscriptioninformation);
126 }
127
128 my $title = $subscriptiondescs->[0]{bibliotitle};
129 my $yearmax=($subscriptions->[0]{year} eq "manage" && scalar(@$subscriptions)>1)? $subscriptions->[1]{year} :$subscriptions->[0]{year};
130 my $yearmin=$subscriptions->[scalar(@$subscriptions)-1]{year};
131 my $subscriptionidlist="";
132 foreach my $subscription (@$subscriptiondescs){
133   $subscriptionidlist.=$subscription->{'subscriptionid'}."," ;
134   $biblionumber = $subscription->{'bibnum'} unless ($biblionumber);
135 }
136
137 # warn "title : $title yearmax : $yearmax nombre d'elements dans le tableau :".scalar(@$subscriptions);
138 #  use Data::Dumper; warn Dumper($subscriptions);
139 chop $subscriptionidlist;
140 $template->param(
141           onesubscription => (scalar(@$subscriptiondescs)==1),
142           subscriptionidlist => $subscriptionidlist,
143           biblionumber => $biblionumber,
144           subscriptions => $subscriptiondescs,
145           years => $subscriptions,
146           yearmin => $yearmin,
147           yearmax =>$yearmax,
148           bibliotitle => $title,
149           suggestion => C4::Context->preference("suggestion"),
150           virtualshelves => C4::Context->preference("virtualshelves"),
151           subscr=>$query->param('subscriptionid'),
152           );
153
154 output_html_with_http_headers $query, $cookie, $template->output;