Fix authorised_value '0' problem with last commit on detail.pl
[koha.git] / serials / subscription-add.pl
blob6142a4a45975a30c849c8cd7a655a4d6e2ca6b9b
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
18 use strict;
19 use CGI;
20 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
21 use C4::Koha;
22 use C4::Biblio;
23 use C4::Auth;
24 use C4::Dates qw/format_date format_date_in_iso/;
25 use C4::Acquisition;
26 use C4::Output;
27 use C4::Context;
28 use C4::Branch; # GetBranches
29 use C4::Serials;
30 use C4::Letters;
32 #use Smart::Comments;
34 my $query = new CGI;
35 my $op = $query->param('op');
36 my $dbh = C4::Context->dbh;
37 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
38 $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
39 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
40 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
41 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
42 $numberingmethod, $status, $biblionumber,
43 $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory);
45 my @budgets;
46 my ($template, $loggedinuser, $cookie)
47 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
48 query => $query,
49 type => "intranet",
50 authnotrequired => 0,
51 flagsrequired => {serials => 1},
52 debug => 1,
53 });
56 my $weekarrayjs='';
57 my $count = 0;
58 # FIXME - This assumes first pub date of today().
59 # You can't enter past-date irregularities.
60 my ($year, $month, $day) = Today;
61 my $firstday = Day_of_Year($year,$month,$day);
62 my ($wkno,$yr) = Week_of_Year($year,$month,$day); # week starting monday
63 my $weekno = $wkno;
64 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
65 #$count = $i;
66 #if($wkno > 52){$year++; $wkno=1;}
67 #if($count>365){$count=$i-365;}
68 my ($y,$m,$d) = Add_Delta_Days($year,1,1,$i - 1);
69 #warn "$y-$m-$d";
70 #BUGFIX padding add_delta_days() date
71 my $output = sprintf("%04d-%02d-%02d",$y , $m, $d );
72 $weekarrayjs .= "'Wk $wkno: ". format_date($output) ."',";
73 $wkno++;
75 chop($weekarrayjs);
76 # warn $weekarrayjs;
78 my $sub_on;
79 my @subscription_types = (
80 'issues', 'weeks', 'months'
81 );
82 my @sub_type_data;
84 my $letters = GetLetters('serial');
85 my @letterloop;
86 foreach my $thisletter (keys %$letters) {
87 my $selected = 1 if $thisletter eq $letter;
88 my %row =(value => $thisletter,
89 selected => $selected,
90 lettername => $letters->{$thisletter},
92 push @letterloop, \%row;
94 $template->param(letterloop => \@letterloop);
96 my $onlymine=C4::Context->preference('IndependantBranches') &&
97 C4::Context->userenv &&
98 C4::Context->userenv->{flags}!=1 &&
99 C4::Context->userenv->{branch};
100 my $branches = GetBranches($onlymine);
101 my @branchloop;
102 foreach my $thisbranch (keys %$branches) {
103 my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
104 my %row =(value => $thisbranch,
105 selected => $selected,
106 branchname => $branches->{$thisbranch}->{'branchname'},
108 push @branchloop, \%row;
110 $template->param(branchloop => \@branchloop,
111 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
114 if ($op eq 'mod'||$op eq 'dup') {
116 my $subscriptionid = $query->param('subscriptionid');
117 # warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
118 my $subs = &GetSubscription($subscriptionid);
119 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
120 if ($subs->{'cannotedit'} && $op eq 'mod'){
121 warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
122 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
124 for (qw(startdate firstacquidate histstartdate enddate)) {
125 $subs->{$_} = format_date($subs->{$_}) if $subs->{$_};
127 $subs->{'letter'}='' unless($subs->{'letter'});
128 $irregularity = $subs->{'irregularity'};
129 $numberpattern = $subs->{'numberpattern'};
132 if($subs->{numberlength} > 0){
133 $sublength = $subs->{numberlength};
134 $sub_on = $subscription_types[0];
135 } elsif ($subs->{weeklength}>0){
136 $sublength = $subs->{weeklength};
137 $sub_on = $subscription_types[1];
138 } else {
139 $sublength = $subs->{monthlength};
140 $sub_on = $subscription_types[2];
142 while (@subscription_types) {
143 my $sub_type = shift @subscription_types;
144 my %row = ( 'name' => $sub_type );
145 if ( $sub_on eq $sub_type ) {
146 $row{'selected'} = ' selected';
147 } else {
148 $row{'selected'} = '';
150 push( @sub_type_data, \%row );
153 $template->param($subs);
154 $template->param(
155 $op => 1,
156 subtype => \@sub_type_data,
157 sublength =>$sublength,
158 history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
160 $template->param(
161 "periodicity".$subs->{'periodicity'} => 1,
162 "dow".$subs->{'dow'} => 1,
163 "numberpattern".$subs->{'numberpattern'} => 1,
167 if ($op eq 'addsubscription') {
168 my $auser = $query->param('user');
169 my $branchcode = $query->param('branchcode');
170 my $aqbooksellerid = $query->param('aqbooksellerid');
171 my $cost = $query->param('cost');
172 my $aqbudgetid = $query->param('aqbudgetid');
173 my $startdate = $query->param('startdate');
174 my $firstacquidate = $query->param('firstacquidate');
175 my $periodicity = $query->param('periodicity');
176 my $dow = $query->param('dow');
177 my @irregularity = $query->param('irregular');
178 my $numberlength = 0;
179 my $weeklength = 0;
180 my $monthlength = 0;
181 my $numberpattern = $query->param('numbering_pattern');
182 my $sublength = $query->param('sublength');
183 my $subtype = $query->param('subtype');
184 if ($subtype eq 'months'){
185 $monthlength = $sublength;
186 } elsif ($subtype eq 'weeks'){
187 $weeklength = $sublength;
188 } else {
189 $numberlength = $sublength;
191 my $add1 = $query->param('add1');
192 my $every1 = $query->param('every1');
193 my $whenmorethan1 = $query->param('whenmorethan1');
194 my $setto1 = $query->param('setto1');
195 my $lastvalue1 = $query->param('lastvalue1');
196 my $innerloop1 =$query->param('innerloop1');
197 my $add2 = $query->param('add2');
198 my $every2 = $query->param('every2');
199 my $whenmorethan2 = $query->param('whenmorethan2');
200 my $setto2 = $query->param('setto2');
201 my $innerloop2 =$query->param('innerloop2');
202 my $lastvalue2 = $query->param('lastvalue2');
203 my $add3 = $query->param('add3');
204 my $every3 = $query->param('every3');
205 my $whenmorethan3 = $query->param('whenmorethan3');
206 my $setto3 = $query->param('setto3');
207 my $lastvalue3 = $query->param('lastvalue3');
208 my $innerloop3 =$query->param('innerloop3');
209 my $numberingmethod = $query->param('numberingmethod');
210 my $status = 1;
211 my $biblionumber = $query->param('biblionumber');
212 my $callnumber = $query->param('callnumber');
213 my $notes = $query->param('notes');
214 my $internalnotes = $query->param('internalnotes');
215 my $hemisphere = $query->param('hemisphere') || 1;
216 my $letter = $query->param('letter');
217 # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
218 my $manualhistory = $query->param('manualhist');
219 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
220 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
221 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
222 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
223 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
224 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
225 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
228 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
229 } elsif ($op eq 'modsubscription') {
230 my $subscriptionid = $query->param('subscriptionid');
231 my @irregular = $query->param('irregular');
232 my $irregular_count = @irregular;
233 for(my $i =0;$i<$irregular_count;$i++){
234 $irregularity .=$irregular[$i].",";
235 warn "irregular : $irregular[$i] string :$irregularity";
237 $irregularity =~ s/\,$//;
239 my $auser = $query->param('user');
240 my $librarian => $query->param('librarian'),
241 my $branchcode = $query->param('branchcode');
242 my $cost = $query->param('cost');
243 my $aqbooksellerid = $query->param('aqbooksellerid');
244 my $biblionumber = $query->param('biblionumber');
245 my $aqbudgetid = $query->param('aqbudgetid');
246 my $startdate = format_date_in_iso($query->param('startdate'));
247 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
248 my $periodicity = $query->param('periodicity');
249 my $dow = $query->param('dow');
250 my $sublength = $query->param('sublength');
251 my $subtype = $query->param('subtype');
253 if($subtype eq 'months'){
254 $monthlength = $sublength;
255 } elsif ($subtype eq 'weeks'){
256 $weeklength = $sublength;
257 } else {
258 $numberlength = $sublength;
260 my $numberpattern = $query->param('numbering_pattern');
261 my $add1 = $query->param('add1');
262 my $every1 = $query->param('every1');
263 my $whenmorethan1 = $query->param('whenmorethan1');
264 my $setto1 = $query->param('setto1');
265 my $lastvalue1 = $query->param('lastvalue1');
266 my $innerloop1 = $query->param('innerloop1');
267 my $add2 = $query->param('add2');
268 my $every2 = $query->param('every2');
269 my $whenmorethan2 = $query->param('whenmorethan2');
270 my $setto2 = $query->param('setto2');
271 my $lastvalue2 = $query->param('lastvalue2');
272 my $innerloop2 = $query->param('innerloop2');
273 my $add3 = $query->param('add3');
274 my $every3 = $query->param('every3');
275 my $whenmorethan3 = $query->param('whenmorethan3');
276 my $setto3 = $query->param('setto3');
277 my $lastvalue3 = $query->param('lastvalue3');
278 my $innerloop3 = $query->param('innerloop3');
279 my $numberingmethod = $query->param('numberingmethod');
280 my $status = 1;
281 my $callnumber = $query->param('callnumber');
282 my $notes = $query->param('notes');
283 my $internalnotes = $query->param('internalnotes');
284 my $hemisphere = $query->param('hemisphere');
285 my $letter = $query->param('letter');
286 my $manualhistory = $query->param('manualhist');
287 my $enddate = $query->param('enddate');
288 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
289 my $recievedlist = $query->param('recievedlist');
290 my $missinglist = $query->param('missinglist');
291 my $opacnote = $query->param('opacnote');
292 my $librariannote = $query->param('librariannote');
293 &ModSubscription(
294 $auser, $branchcode, $aqbooksellerid, $cost,
295 $aqbudgetid, $startdate, $periodicity, $firstacquidate,
296 $dow, $irregularity, $numberpattern, $numberlength,
297 $weeklength, $monthlength, $add1, $every1,
298 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
299 $add2, $every2, $whenmorethan2, $setto2,
300 $lastvalue2, $innerloop2, $add3, $every3,
301 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
302 $numberingmethod, $status, $biblionumber, $callnumber,
303 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
304 $subscriptionid);
306 ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
307 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
308 } else {
310 while (@subscription_types) {
311 my $sub_type = shift @subscription_types;
312 my %row = ( 'name' => $sub_type );
313 if ( $sub_on eq $sub_type ) {
314 $row{'selected'} = ' selected';
315 } else {
316 $row{'selected'} = '';
318 push( @sub_type_data, \%row );
320 $template->param(subtype => \@sub_type_data,
321 weekarrayjs => $weekarrayjs,
322 weekno => $weekno,
324 output_html_with_http_headers $query, $cookie, $template->output;