fixing some bugs in subscription add.
[koha.git] / serials / subscription-add.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2003 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 # $Id$
21
22 =head1 NAME
23
24 subscription-add.pl
25
26 =head1 DESCRIPTION
27
28 this script add a subscription into the database.
29
30 =head1 PARAMETERS
31
32 =over 4
33
34 =item op
35 op use to know the operation to do on this template.
36  * mod : to modify an existing subscription
37  * addsubscription : to add a subscription
38
39 Note that if op = mod or addsubscription there are a lot of other params.
40
41
42 =back
43
44 =cut
45
46
47 use strict;
48 use CGI;
49 use C4::Koha;
50 use C4::Auth;
51 use C4::Date;
52 use C4::Output;
53 use C4::Serials;
54 use C4::Bookfund;
55 use C4::Interface::CGI::Output;
56 use C4::Context;
57 use HTML::Template;
58 use C4::Letters;
59 use C4::Members;
60
61 my $query = new CGI;
62 my $op = $query->param('op');
63 my $dbh = C4::Context->dbh;
64 my ($subscriptionid,$auser,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
65     $dow, $numberlength, $weeklength, $monthlength,
66     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
67     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
68     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
69     $numberingmethod, $status, $biblionumber,
70     $bibliotitle, $notes, $letter);
71
72     my @budgets;
73 my ($template, $loggedinuser, $cookie)
74 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
75                 query => $query,
76                 type => "intranet",
77                 authnotrequired => 0,
78                 flagsrequired => {catalogue => 1},
79                 debug => 1,
80                 });
81
82 if ($op eq 'mod') {
83     my $subscriptionid = $query->param('subscriptionid');
84     my $subs = &GetSubscription($subscriptionid);
85     $auser = $subs->{'user'};
86     $librarian = $subs->{'librarian'};
87     $cost = $subs->{'cost'};
88     $aqbooksellerid = $subs->{'aqbooksellerid'};
89     $aqbooksellername = $subs->{'aqbooksellername'};
90     $bookfundid = $subs->{'bookfundid'};
91     $aqbudgetid = $subs->{'aqbudgetid'};
92     defined $aqbudgetid or $aqbudgetid='';
93     $startdate = $subs->{'startdate'};
94     $periodicity = $subs->{'periodicity'};
95     $dow = $subs->{'dow'};
96     $numberlength = $subs->{'numberlength'};
97     $weeklength = $subs->{'weeklength'};
98     $monthlength = $subs->{'monthlength'};
99     $add1 = $subs->{'add1'};
100     $every1 = $subs->{'every1'};
101     $whenmorethan1 = $subs->{'whenmorethan1'};
102     $setto1 = $subs->{'setto1'};
103     $lastvalue1 = $subs->{'lastvalue1'};
104     $innerloop1 = $subs->{'innerloop1'};
105     $add2 = $subs->{'add2'};
106     $every2 = $subs->{'every2'};
107     $whenmorethan2 = $subs->{'whenmorethan2'};
108     $setto2 = $subs->{'setto2'};
109     $lastvalue2 = $subs->{'lastvalue2'};
110     $innerloop2 = $subs->{'innerloop2'};
111     $add3 = $subs->{'add3'};
112     $every3 = $subs->{'every3'};
113     $whenmorethan3 = $subs->{'whenmorethan3'};
114     $setto3 = $subs->{'setto3'};
115     $lastvalue3 = $subs->{'lastvalue3'};
116     $innerloop3 = $subs->{'innerloop3'};
117     $numberingmethod = $subs->{'numberingmethod'};
118     $status = $subs->{status};
119     $biblionumber = $subs->{'biblionumber'};
120     $bibliotitle = $subs->{'bibliotitle'},
121     $notes = $subs->{'notes'};
122     $letter = $subs->{'letter'};
123     defined $letter or $letter='';
124     $template->param(
125         $op => 1,
126         user => $auser,
127         librarian => $librarian,
128         aqbooksellerid => $aqbooksellerid,
129         aqbooksellername => $aqbooksellername,
130         cost => $cost,
131         aqbudgetid => $aqbudgetid,
132         bookfundid => $bookfundid,
133         startdate => format_date($startdate),
134         periodicity => $periodicity,
135         dow => $dow,
136         numberlength => $numberlength,
137         weeklength => $weeklength,
138         monthlength => $monthlength,
139         add1 => $add1,
140         every1 => $every1,
141         whenmorethan1 => $whenmorethan1,
142         setto1 => $setto1,
143         lastvalue1 => $lastvalue1,
144         innerloop1 => $innerloop1,
145         add2 => $add2,
146         every2 => $every2,
147         whenmorethan2 => $whenmorethan2,
148         setto2 => $setto2,
149         lastvalue2 => $lastvalue2,
150         innerloop2 => $innerloop2,
151         add3 => $add3,
152         every3 => $every3,
153         whenmorethan3 => $whenmorethan3,
154         setto3 => $setto3,
155         lastvalue3 => $lastvalue3,
156         innerloop3 => $innerloop3,
157         numberingmethod => $numberingmethod,
158         status => $status,
159         biblionumber => $biblionumber,
160         bibliotitle => $bibliotitle,
161         notes => $notes,
162         letter => $letter,
163         subscriptionid => $subscriptionid,
164         "periodicity$periodicity" => 1,
165         "dow$dow" => 1,
166         );
167 }
168
169 my @letterlist = GetLetterList('serial');
170 for (my $i=0;$i<=$#letterlist;$i++) {
171     $letterlist[$i]->{'selected'} =1 if $letterlist[$i]->{'code'} eq $letter;
172 }
173 $template->param(letters => \@letterlist);
174
175 if ($op eq 'addsubscription') {
176     my $auser = $query->param('user');
177     my $aqbooksellerid = $query->param('aqbooksellerid');
178     my $cost = $query->param('cost');
179     my $aqbudgetid = $query->param('aqbudgetid');
180     my $startdate = $query->param('startdate');
181     my $periodicity = $query->param('periodicity');
182     my $dow = $query->param('dow');
183     my $numberlength = $query->param('numberlength');
184     my $weeklength = $query->param('weeklength');
185     my $monthlength = $query->param('monthlength');
186     my $add1 = $query->param('add1');
187     my $every1 = $query->param('every1');
188     my $whenmorethan1 = $query->param('whenmorethan1');
189     my $setto1 = $query->param('setto1');
190     my $lastvalue1 = $query->param('lastvalue1');
191     my $add2 = $query->param('add2');
192     my $every2 = $query->param('every2');
193     my $whenmorethan2 = $query->param('whenmorethan2');
194     my $setto2 = $query->param('setto2');
195     my $lastvalue2 = $query->param('lastvalue2');
196     my $add3 = $query->param('add3');
197     my $every3 = $query->param('every3');
198     my $whenmorethan3 = $query->param('whenmorethan3');
199     my $setto3 = $query->param('setto3');
200     my $lastvalue3 = $query->param('lastvalue3');
201     my $numberingmethod = $query->param('numberingmethod');
202     my $status = 1;
203     my $biblionumber = $query->param('biblionumber');
204     my $notes = $query->param('notes');
205     my $letter = $query->param('letter');
206     my $subscriptionid = NewSubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
207                     $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
208                     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
209                     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
210                     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
211                     $numberingmethod, $status, $notes, $letter
212                 );
213     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
214 } else {
215     output_html_with_http_headers $query, $cookie, $template->output;
216 }