Bug 17573: Remove itemtype-related warnings from DecreaseLoanHighHolds.t
[koha.git] / serials / subscription-add.pl
blob31a6b6b4eaf3e568599b7c02e97a680ccce226df
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use strict;
19 use warnings;
21 use CGI qw ( -utf8 );
22 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM);
23 use C4::Koha;
24 use C4::Biblio;
25 use C4::Auth;
26 use C4::Acquisition;
27 use C4::Output;
28 use C4::Context;
29 use C4::Branch; # GetBranches
30 use C4::Serials;
31 use C4::Serials::Frequency;
32 use C4::Serials::Numberpattern;
33 use C4::Letters;
34 use Koha::AdditionalField;
35 use Koha::DateUtils;
36 use Carp;
38 #use Smart::Comments;
40 our $query = CGI->new;
41 my $op = $query->param('op') || '';
42 my $dbh = C4::Context->dbh;
43 my $sub_length;
46 # Permission needed if it is a modification : edit_subscription
47 # Permission needed otherwise (nothing or dup) : create_subscription
48 my $permission = ($op eq "modify") ? "edit_subscription" : "create_subscription";
50 my ($template, $loggedinuser, $cookie)
51 = get_template_and_user({template_name => "serials/subscription-add.tt",
52 query => $query,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => {serials => $permission},
56 debug => 1,
57 });
61 my $sub_on;
63 my $subs;
64 our $firstissuedate;
66 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
68 my $subscriptionid = $query->param('subscriptionid');
69 $subs = GetSubscription($subscriptionid);
71 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
72 if ($subs->{'cannotedit'} && $op eq 'modify'){
73 carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
74 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
76 $firstissuedate = $subs->{firstacquidate} || ''; # in iso format.
77 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
78 next unless defined $subs->{$_};
79 # TODO : Handle date formats properly.
80 if ($subs->{$_} eq '0000-00-00') {
81 $subs->{$_} = ''
82 } else {
83 $subs->{$_} = $subs->{$_};
86 if (!defined $subs->{letter}) {
87 $subs->{letter}= q{};
89 my $nextexpected = GetNextExpected($subscriptionid);
90 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate} eq $firstissuedate ;
91 $subs->{nextacquidate} = $nextexpected->{planneddate} if($op eq 'modify');
92 unless($op eq 'modsubscription') {
93 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
94 if ($subs->{$length_unit}) {
95 $sub_length=$subs->{$length_unit};
96 $sub_on=$length_unit;
97 last;
101 $template->param( %{$subs} );
102 $template->param(
103 $op => 1,
104 "subtype_$sub_on" => 1,
105 sublength =>$sub_length,
106 history => ($op eq 'modify'),
107 firstacquiyear => substr($firstissuedate,0,4),
110 if($op eq 'modify') {
111 my ($serials_number) = GetSerials($subscriptionid);
112 if($serials_number > 1) {
113 $template->param(more_than_one_serial => 1);
118 if ( $op eq 'dup' ) {
119 my $dont_copy_fields = C4::Context->preference('SubscriptionDuplicateDroppedInput');
120 my @fields_id = map { fieldid => $_ }, split '\|', $dont_copy_fields;
121 $template->param( dont_export_field_loop => \@fields_id );
124 my $letters = get_letter_loop( $subs->{letter} );
125 $template->param( letterloop => $letters );
129 my $onlymine =
130 C4::Context->preference('IndependentBranches')
131 && C4::Context->userenv
132 && !C4::Context->IsSuperLibrarian
133 && (
134 not C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'superserials' } )
136 && C4::Context->userenv->{branch};
137 my $branches = GetBranches($onlymine);
138 my $branchloop;
139 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
140 my $selected = 0;
141 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
142 push @{$branchloop}, {
143 value => $thisbranch,
144 selected => $selected,
145 branchname => $branches->{$thisbranch}->{'branchname'},
149 my $locations_loop = GetAuthorisedValues("LOC");
151 $template->param(branchloop => $branchloop,
152 locations_loop=>$locations_loop,
156 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
157 for my $field ( @$additional_fields ) {
158 if ( $field->{authorised_value_category} ) {
159 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
162 $template->param( additional_fields_for_subscription => $additional_fields );
164 # prepare template variables common to all $op conditions:
165 if ($op!~/^mod/) {
166 my $letters = get_letter_loop();
167 $template->param( letterloop => $letters );
170 if ($op eq 'addsubscription') {
171 redirect_add_subscription();
172 } elsif ($op eq 'modsubscription') {
173 redirect_mod_subscription();
174 } else {
176 $template->param(
177 subtypes => [ qw( numberlength weeklength monthlength ) ],
178 subtype => $sub_on,
181 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
182 my $letters = get_letter_loop();
183 $template->param( letterloop => $letters );
186 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
187 if (defined $new_biblionumber) {
188 my $bib = GetBiblioData($new_biblionumber);
189 if (defined $bib) {
190 $template->param(bibnum => $new_biblionumber);
191 $template->param(bibliotitle => $bib->{title});
195 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
197 my @frequencies = GetSubscriptionFrequencies;
198 my @frqloop;
199 foreach my $freq (@frequencies) {
200 my $selected = 0;
201 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
202 my $row = {
203 id => $freq->{'id'},
204 selected => $selected,
205 label => $freq->{'description'},
207 push @frqloop, $row;
209 $template->param(frequencies => \@frqloop);
211 my @numpatterns = GetSubscriptionNumberpatterns;
212 my @numberpatternloop;
213 foreach my $numpattern (@numpatterns) {
214 my $selected = 0;
215 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
216 my $row = {
217 id => $numpattern->{'id'},
218 selected => $selected,
219 label => $numpattern->{'label'},
221 push @numberpatternloop, $row;
223 $template->param(numberpatterns => \@numberpatternloop);
225 my $languages = [ map {
227 language => $_->{iso639_2_code},
228 description => $_->{language_description} || $_->{language}
230 } @{ C4::Languages::getAllLanguages() } ];
232 $template->param( locales => $languages );
234 output_html_with_http_headers $query, $cookie, $template->output;
237 sub get_letter_loop {
238 my ($selected_lettercode) = @_;
239 $selected_lettercode //= '';
240 my $letters = GetLetters({ module => 'serial' });
241 return [
242 map {
244 value => $_->{code},
245 lettername => $_->{name},
246 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
248 } @$letters
252 sub _get_sub_length {
253 my ($type, $length) = @_;
254 return
256 $type eq 'issues' ? $length : 0,
257 $type eq 'weeks' ? $length : 0,
258 $type eq 'months' ? $length : 0,
262 sub _guess_enddate {
263 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
264 my ($year, $month, $day);
265 my $enddate;
266 if($numberlength != 0) {
267 my $frequency = GetSubscriptionFrequency($frequencyid);
268 if($frequency->{'unit'} eq 'day') {
269 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
270 } elsif($frequency->{'unit'} eq 'week') {
271 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
272 } elsif($frequency->{'unit'} eq 'month') {
273 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
274 } elsif($frequency->{'unit'} eq 'year') {
275 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
277 } elsif($weeklength != 0) {
278 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
279 } elsif($monthlength != 0) {
280 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
282 if(defined $year) {
283 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
284 } else {
285 undef $enddate;
287 return $enddate;
290 sub redirect_add_subscription {
291 my $auser = $query->param('user');
292 my $branchcode = $query->param('branchcode');
293 my $aqbooksellerid = $query->param('aqbooksellerid');
294 my $cost = $query->param('cost');
295 my $aqbudgetid = $query->param('aqbudgetid');
296 my $periodicity = $query->param('frequency');
297 my @irregularity = $query->multi_param('irregularity');
298 my $numberpattern = $query->param('numbering_pattern');
299 my $locale = $query->param('locale');
300 my $graceperiod = $query->param('graceperiod') || 0;
302 my $subtype = $query->param('subtype');
303 my $sublength = $query->param('sublength');
304 my ( $numberlength, $weeklength, $monthlength )
305 = _get_sub_length( $subtype, $sublength );
306 my $add1 = $query->param('add1');
307 my $lastvalue1 = $query->param('lastvalue1');
308 my $innerloop1 = $query->param('innerloop1');
309 my $innerloop2 = $query->param('innerloop2');
310 my $lastvalue2 = $query->param('lastvalue2');
311 my $lastvalue3 = $query->param('lastvalue3');
312 my $innerloop3 = $query->param('innerloop3');
313 my $status = 1;
314 my $biblionumber = $query->param('biblionumber');
315 my $callnumber = $query->param('callnumber');
316 my $notes = $query->param('notes');
317 my $internalnotes = $query->param('internalnotes');
318 my $letter = $query->param('letter');
319 my $manualhistory = $query->param('manualhist') ? 1 : 0;
320 my $serialsadditems = $query->param('serialsadditems');
321 my $staffdisplaycount = $query->param('staffdisplaycount');
322 my $opacdisplaycount = $query->param('opacdisplaycount');
323 my $location = $query->param('location');
324 my $skip_serialseq = $query->param('skip_serialseq');
326 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
327 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
328 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
330 if(!defined $enddate || $enddate eq '') {
331 if($subtype eq "issues") {
332 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
333 } else {
334 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
338 my $subscriptionid = NewSubscription(
339 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
340 $startdate, $periodicity, $numberlength, $weeklength,
341 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
342 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
343 join(";",@irregularity), $numberpattern, $locale, $callnumber,
344 $manualhistory, $internalnotes, $serialsadditems,
345 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
346 $skip_serialseq
349 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
350 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
352 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
353 return;
356 sub redirect_mod_subscription {
357 my $subscriptionid = $query->param('subscriptionid');
358 my @irregularity = $query->multi_param('irregularity');
359 my $auser = $query->param('user');
360 my $librarian => scalar $query->param('librarian'),
361 my $branchcode = $query->param('branchcode');
362 my $cost = $query->param('cost');
363 my $aqbooksellerid = $query->param('aqbooksellerid');
364 my $biblionumber = $query->param('biblionumber');
365 my $aqbudgetid = $query->param('aqbudgetid');
367 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
368 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
369 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
371 my $nextacquidate = $query->param('nextacquidate');
372 $nextacquidate = $nextacquidate
373 ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
374 : $firstacquidate;
376 my $periodicity = $query->param('frequency');
378 my $subtype = $query->param('subtype');
379 my $sublength = $query->param('sublength');
380 my ($numberlength, $weeklength, $monthlength)
381 = _get_sub_length( $subtype, $sublength );
382 my $numberpattern = $query->param('numbering_pattern');
383 my $locale = $query->param('locale');
384 my $lastvalue1 = $query->param('lastvalue1');
385 my $innerloop1 = $query->param('innerloop1');
386 my $lastvalue2 = $query->param('lastvalue2');
387 my $innerloop2 = $query->param('innerloop2');
388 my $lastvalue3 = $query->param('lastvalue3');
389 my $innerloop3 = $query->param('innerloop3');
390 my $status = 1;
391 my $callnumber = $query->param('callnumber');
392 my $notes = $query->param('notes');
393 my $internalnotes = $query->param('internalnotes');
394 my $letter = $query->param('letter');
395 my $manualhistory = $query->param('manualhist') ? 1 : 0;
396 my $serialsadditems = $query->param('serialsadditems');
397 my $staffdisplaycount = $query->param('staffdisplaycount');
398 my $opacdisplaycount = $query->param('opacdisplaycount');
399 my $graceperiod = $query->param('graceperiod') || 0;
400 my $location = $query->param('location');
401 my $skip_serialseq = $query->param('skip_serialseq');
403 # Guess end date
404 if(!defined $enddate || $enddate eq '') {
405 if($subtype eq "issues") {
406 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
407 } else {
408 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
412 my $nextexpected = GetNextExpected($subscriptionid);
413 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
414 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
415 ModNextExpected($subscriptionid, $nextacquidate);
416 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
417 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
420 ModSubscription(
421 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
422 $periodicity, $firstacquidate, join(";",@irregularity),
423 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
424 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
425 $status, $biblionumber, $callnumber, $notes, $letter,
426 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
427 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
428 $skip_serialseq
431 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
432 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
434 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
435 return;
438 sub insert_additional_fields {
439 my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
440 my $record = GetMarcBiblio( $biblionumber, 1 );
441 for my $field ( @$additional_fields ) {
442 my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
443 if ( $af->{marcfield} ) {
444 my ( $field, $subfield ) = split /\$/, $af->{marcfield};
445 $af->{values} = undef;
446 if ( $field and $subfield ) {
447 my $value = $record->subfield( $field, $subfield );
448 $af->{values} = {
449 $subscriptionid => $value
452 } else {
453 $af->{values} = {
454 $subscriptionid => scalar $query->param('additional_field_' . $field->{id})
455 } if defined $query->param('additional_field_' . $field->{id});
457 $af->insert_values;