Bug 5919: Display MARC21 fields 583, 586, 530
[koha.git] / serials / subscription-add.pl
blobdbda323602c6d7148e626e955e31be9ee730cd49
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
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use strict;
19 use warnings;
21 use CGI;
22 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
23 use C4::Koha;
24 use C4::Biblio;
25 use C4::Auth;
26 use C4::Dates qw/format_date format_date_in_iso/;
27 use C4::Acquisition;
28 use C4::Output;
29 use C4::Context;
30 use C4::Branch; # GetBranches
31 use C4::Serials;
32 use C4::Letters;
33 use Carp;
35 #use Smart::Comments;
37 my $query = CGI->new;
38 my $op = $query->param('op') || '';
39 my $dbh = C4::Context->dbh;
40 my $sub_length;
42 my @budgets;
44 # Permission needed if it is a modification : edit_subscription
45 # Permission needed otherwise (nothing or dup) : create_subscription
46 my $permission = ($op eq "mod") ? "edit_subscription" : "create_subscription";
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
50 query => $query,
51 type => "intranet",
52 authnotrequired => 0,
53 flagsrequired => {serials => $permission},
54 debug => 1,
55 });
59 my $sub_on;
60 my @subscription_types = (
61 'issues', 'weeks', 'months'
63 my @sub_type_data;
65 my $subs;
66 my $firstissuedate;
67 my $nextexpected;
69 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
71 my $subscriptionid = $query->param('subscriptionid');
72 $subs = GetSubscription($subscriptionid);
73 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
74 if ($subs->{'cannotedit'} && $op eq 'mod'){
75 carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
76 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
78 $firstissuedate = $subs->{firstacquidate}; # in iso format.
79 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
80 next unless defined $subs->{$_};
81 # TODO : Handle date formats properly.
82 if ($subs->{$_} eq '0000-00-00') {
83 $subs->{$_} = ''
84 } else {
85 $subs->{$_} = format_date($subs->{$_});
88 if (!defined $subs->{letter}) {
89 $subs->{letter}= q{};
91 letter_loop($subs->{'letter'}, $template);
92 $nextexpected = GetNextExpected($subscriptionid);
93 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
94 $subs->{nextacquidate} = $nextexpected->{planneddate}->output() if($op eq 'mod');
95 unless($op eq 'modsubscription') {
96 foreach my $length_unit qw(numberlength weeklength monthlength){
97 if ($subs->{$length_unit}){
98 $sub_length=$subs->{$length_unit};
99 $sub_on=$length_unit;
100 last;
104 $template->param($subs);
105 $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
106 $template->param(
107 $op => 1,
108 "subtype_$sub_on" => 1,
109 sublength =>$sub_length,
110 history => ($op eq 'mod'),
111 "periodicity".$subs->{'periodicity'} => 1,
112 "numberpattern".$subs->{'numberpattern'} => 1,
113 firstacquiyear => substr($firstissuedate,0,4),
118 my $onlymine=C4::Context->preference('IndependantBranches') &&
119 C4::Context->userenv &&
120 C4::Context->userenv->{flags} % 2 !=1 &&
121 C4::Context->userenv->{branch};
122 my $branches = GetBranches($onlymine);
123 my $branchloop;
124 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
125 my $selected = 0;
126 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
127 push @{$branchloop}, {
128 value => $thisbranch,
129 selected => $selected,
130 branchname => $branches->{$thisbranch}->{'branchname'},
134 my $locations_loop = GetAuthorisedValues("LOC",$subs->{'location'});
136 $template->param(branchloop => $branchloop,
137 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
138 locations_loop=>$locations_loop,
140 # prepare template variables common to all $op conditions:
141 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 );
142 if ($op!~/^mod/) {
143 letter_loop(q{}, $template);
146 if ($op eq 'addsubscription') {
147 redirect_add_subscription();
148 } elsif ($op eq 'modsubscription') {
149 redirect_mod_subscription();
150 } else {
151 while (@subscription_types) {
152 my $sub_type = shift @subscription_types;
153 my %row = ( 'name' => $sub_type );
154 if ( defined $sub_on and $sub_on eq $sub_type ) {
155 $row{'selected'} = ' selected';
156 } else {
157 $row{'selected'} = '';
159 push( @sub_type_data, \%row );
161 $template->param(subtype => \@sub_type_data);
163 letter_loop( '', $template ) if ($op ne 'modsubscription' && $op ne 'dup' && $op ne 'mod');
165 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
166 if (defined $new_biblionumber) {
167 my $bib = GetBiblioData($new_biblionumber);
168 if (defined $bib) {
169 $template->param(bibnum => $new_biblionumber);
170 $template->param(bibliotitle => $bib->{title});
173 output_html_with_http_headers $query, $cookie, $template->output;
176 sub letter_loop {
177 my ($selected_letter, $templte) = @_;
178 my $letters = GetLetters('serial');
179 my $letterloop;
180 foreach my $thisletter (keys %{$letters}) {
181 push @{$letterloop}, {
182 value => $thisletter,
183 selected => $thisletter eq $selected_letter,
184 lettername => $letters->{$thisletter},
187 $templte->param(letterloop => $letterloop);
188 return;
191 sub _get_sub_length {
192 my ($type, $length) = @_;
193 return
195 $type eq 'numberlength' ? $length : 0,
196 $type eq 'weeklength' ? $length : 0,
197 $type eq 'monthlength' ? $length : 0,
201 sub redirect_add_subscription {
202 my $auser = $query->param('user');
203 my $branchcode = $query->param('branchcode');
204 my $aqbooksellerid = $query->param('aqbooksellerid');
205 my $cost = $query->param('cost');
206 my $aqbudgetid = $query->param('aqbudgetid');
207 my $periodicity = $query->param('periodicity');
208 my $dow = $query->param('dow');
209 my @irregularity = $query->param('irregularity_select');
210 my $numberpattern = $query->param('numbering_pattern');
211 my $graceperiod = $query->param('graceperiod') || 0;
213 my ( $numberlength, $weeklength, $monthlength )
214 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
215 my $add1 = $query->param('add1');
216 my $every1 = $query->param('every1');
217 my $whenmorethan1 = $query->param('whenmorethan1');
218 my $setto1 = $query->param('setto1');
219 my $lastvalue1 = $query->param('lastvalue1');
220 my $innerloop1 = $query->param('innerloop1');
221 my $add2 = $query->param('add2');
222 my $every2 = $query->param('every2');
223 my $whenmorethan2 = $query->param('whenmorethan2');
224 my $setto2 = $query->param('setto2');
225 my $innerloop2 = $query->param('innerloop2');
226 my $lastvalue2 = $query->param('lastvalue2');
227 my $add3 = $query->param('add3');
228 my $every3 = $query->param('every3');
229 my $whenmorethan3 = $query->param('whenmorethan3');
230 my $setto3 = $query->param('setto3');
231 my $lastvalue3 = $query->param('lastvalue3');
232 my $innerloop3 = $query->param('innerloop3');
233 my $numberingmethod = $query->param('numberingmethod');
234 my $status = 1;
235 my $biblionumber = $query->param('biblionumber');
236 my $callnumber = $query->param('callnumber');
237 my $notes = $query->param('notes');
238 my $internalnotes = $query->param('internalnotes');
239 my $hemisphere = $query->param('hemisphere') || 1;
240 my $letter = $query->param('letter');
241 my $manualhistory = $query->param('manualhist');
242 my $serialsadditems = $query->param('serialsadditems');
243 my $staffdisplaycount = $query->param('staffdisplaycount');
244 my $opacdisplaycount = $query->param('opacdisplaycount');
245 my $location = $query->param('location');
246 my $startdate = format_date_in_iso( $query->param('startdate') );
247 my $enddate = format_date_in_iso( $query->param('enddate') );
248 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
249 my $histenddate = format_date_in_iso($query->param('histenddate'));
250 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
251 my $recievedlist = $query->param('recievedlist');
252 my $missinglist = $query->param('missinglist');
253 my $opacnote = $query->param('opacnote');
254 my $librariannote = $query->param('librariannote');
255 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
256 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
257 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
258 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
259 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
260 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
261 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
262 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
264 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
266 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
267 return;
270 sub redirect_mod_subscription {
271 my $subscriptionid = $query->param('subscriptionid');
272 my @irregularity = $query->param('irregularity_select');
273 my $auser = $query->param('user');
274 my $librarian => $query->param('librarian'),
275 my $branchcode = $query->param('branchcode');
276 my $cost = $query->param('cost');
277 my $aqbooksellerid = $query->param('aqbooksellerid');
278 my $biblionumber = $query->param('biblionumber');
279 my $aqbudgetid = $query->param('aqbudgetid');
280 my $startdate = format_date_in_iso($query->param('startdate'));
281 my $nextacquidate = $query->param('nextacquidate') ?
282 format_date_in_iso($query->param('nextacquidate')):
283 format_date_in_iso($query->param('startdate'));
284 my $enddate = format_date_in_iso($query->param('enddate'));
285 my $periodicity = $query->param('periodicity');
286 my $dow = $query->param('dow');
288 my ($numberlength, $weeklength, $monthlength)
289 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
290 my $numberpattern = $query->param('numbering_pattern');
291 my $add1 = $query->param('add1');
292 my $every1 = $query->param('every1');
293 my $whenmorethan1 = $query->param('whenmorethan1');
294 my $setto1 = $query->param('setto1');
295 my $lastvalue1 = $query->param('lastvalue1');
296 my $innerloop1 = $query->param('innerloop1');
297 my $add2 = $query->param('add2');
298 my $every2 = $query->param('every2');
299 my $whenmorethan2 = $query->param('whenmorethan2');
300 my $setto2 = $query->param('setto2');
301 my $lastvalue2 = $query->param('lastvalue2');
302 my $innerloop2 = $query->param('innerloop2');
303 my $add3 = $query->param('add3');
304 my $every3 = $query->param('every3');
305 my $whenmorethan3 = $query->param('whenmorethan3');
306 my $setto3 = $query->param('setto3');
307 my $lastvalue3 = $query->param('lastvalue3');
308 my $innerloop3 = $query->param('innerloop3');
309 my $numberingmethod = $query->param('numberingmethod');
310 my $status = 1;
311 my $callnumber = $query->param('callnumber');
312 my $notes = $query->param('notes');
313 my $internalnotes = $query->param('internalnotes');
314 my $hemisphere = $query->param('hemisphere');
315 my $letter = $query->param('letter');
316 my $manualhistory = $query->param('manualhist');
317 my $serialsadditems = $query->param('serialsadditems');
318 # subscription history
319 my $histenddate = format_date_in_iso($query->param('histenddate'));
320 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
321 my $recievedlist = $query->param('recievedlist');
322 my $missinglist = $query->param('missinglist');
323 my $opacnote = $query->param('opacnote');
324 my $librariannote = $query->param('librariannote');
325 my $staffdisplaycount = $query->param('staffdisplaycount');
326 my $opacdisplaycount = $query->param('opacdisplaycount');
327 my $graceperiod = $query->param('graceperiod') || 0;
328 my $location = $query->param('location');
329 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
330 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
331 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
332 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
333 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
336 ModSubscription(
337 $auser, $branchcode, $aqbooksellerid, $cost,
338 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
339 $dow, join(q{,},@irregularity), $numberpattern, $numberlength,
340 $weeklength, $monthlength, $add1, $every1,
341 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
342 $add2, $every2, $whenmorethan2, $setto2,
343 $lastvalue2, $innerloop2, $add3, $every3,
344 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
345 $numberingmethod, $status, $biblionumber, $callnumber,
346 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
347 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
349 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
350 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
351 return;