Bug 22960: Typo found in circulation.pref in UpdateItemLocationOnCheckin preference
[koha.git] / serials / subscription-add.pl
blobebc19a05e9f12fbb23a315152baf5c1a05ad639f
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 Modern::Perl;
20 use CGI qw ( -utf8 );
21 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM);
22 use C4::Koha;
23 use C4::Biblio;
24 use C4::Auth;
25 use C4::Acquisition;
26 use C4::Output;
27 use C4::Context;
28 use C4::Serials;
29 use C4::Serials::Frequency;
30 use C4::Serials::Numberpattern;
31 use C4::Letters;
32 use Koha::AdditionalFields;
33 use Koha::Biblios;
34 use Koha::DateUtils;
35 use Koha::ItemTypes;
36 use Carp;
38 use Koha::Subscription::Numberpattern;
39 use Koha::Subscription::Frequency;
40 use Koha::SharedContent;
42 #use Smart::Comments;
44 our $query = CGI->new;
45 my $op = $query->param('op') || '';
46 my $dbh = C4::Context->dbh;
47 my $sub_length;
50 # Permission needed if it is a modification : edit_subscription
51 # Permission needed otherwise (nothing or dup) : create_subscription
52 my $permission =
53 ( $op eq 'modify' || $op eq 'modsubscription' ) ? "edit_subscription" : "create_subscription";
55 our ($template, $loggedinuser, $cookie)
56 = get_template_and_user({template_name => "serials/subscription-add.tt",
57 query => $query,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {serials => $permission},
61 debug => 1,
62 });
66 my $sub_on;
68 my $subs;
69 our $firstissuedate;
71 my $mana_url = C4::Context->config('mana_config');
72 $template->param( 'mana_url' => $mana_url );
73 my $subscriptionid = $query->param('subscriptionid');
75 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
77 $subs = GetSubscription($subscriptionid);
79 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
80 unless $subs;
82 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
83 if ($subs->{'cannotedit'} && $op eq 'modify'){
84 carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
85 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
87 $firstissuedate = $subs->{firstacquidate} || ''; # in iso format.
88 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
89 next unless defined $subs->{$_};
90 # TODO : Handle date formats properly.
91 if ($subs->{$_} eq '0000-00-00') {
92 $subs->{$_} = ''
93 } else {
94 $subs->{$_} = $subs->{$_};
97 if (!defined $subs->{letter}) {
98 $subs->{letter}= q{};
100 my $nextexpected = GetNextExpected($subscriptionid);
101 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate} eq $firstissuedate ;
102 $subs->{nextacquidate} = $nextexpected->{planneddate} if($op eq 'modify');
103 unless($op eq 'modsubscription') {
104 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
105 if ($subs->{$length_unit}) {
106 $sub_length=$subs->{$length_unit};
107 $sub_on=$length_unit;
108 last;
112 $template->param( %{$subs} );
113 $template->param(
114 $op => 1,
115 "subtype_$sub_on" => 1,
116 sublength =>$sub_length,
117 history => ($op eq 'modify'),
118 firstacquiyear => substr($firstissuedate,0,4),
121 if($op eq 'modify') {
122 my ($serials_number) = GetSerials($subscriptionid);
123 if($serials_number > 1) {
124 $template->param(more_than_one_serial => 1);
129 if ( $op eq 'dup' ) {
130 my $dont_copy_fields = C4::Context->preference('SubscriptionDuplicateDroppedInput');
131 my @fields_id = map { fieldid => $_ }, split '\|', $dont_copy_fields;
132 $template->param( dont_export_field_loop => \@fields_id );
135 my $letters = get_letter_loop( $subs->{letter} );
136 $template->param( letterloop => $letters );
140 my $locations_loop = GetAuthorisedValues("LOC");
142 $template->param(
143 branchcode => $subs->{branchcode},
144 locations_loop=>$locations_loop,
147 my @additional_fields = Koha::AdditionalFields->search({ tablename => 'subscription' });
148 my %additional_field_values;
149 if ($subscriptionid) {
150 my $subscription = Koha::Subscriptions->find($subscriptionid);
151 foreach my $value ($subscription->additional_field_values->as_list) {
152 $additional_field_values{$value->field_id} = $value->value;
156 $template->param(
157 additional_fields => \@additional_fields,
158 additional_field_values => \%additional_field_values,
161 my $typeloop = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
163 # FIXME We should use the translated_description for item types
164 my @typearg =
165 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{itemtype} and $_ eq $subs->{itemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
166 my @previoustypearg =
167 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{previousitemtype} and $_ eq $subs->{previousitemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
169 $template->param(
170 typeloop => \@typearg,
171 previoustypeloop => \@previoustypearg,
172 locations_loop=>$locations_loop,
175 # prepare template variables common to all $op conditions:
176 $template->param('makePreviousSerialAvailable' => 1) if (C4::Context->preference('makePreviousSerialAvailable'));
178 if ($op!~/^mod/) {
179 my $letters = get_letter_loop();
180 $template->param( letterloop => $letters );
183 if ($op eq 'addsubscription') {
184 redirect_add_subscription();
185 } elsif ($op eq 'modsubscription') {
186 redirect_mod_subscription();
187 } else {
189 $template->param(
190 subtypes => [ qw( numberlength weeklength monthlength ) ],
191 subtype => $sub_on,
194 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
195 my $letters = get_letter_loop();
196 $template->param( letterloop => $letters );
199 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
200 if (defined $new_biblionumber) {
201 my $biblio = Koha::Biblios->find( $new_biblionumber );
202 if (defined $biblio) {
203 $template->param(bibnum => $new_biblionumber);
204 $template->param(bibliotitle => $biblio->title);
208 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
210 my @frequencies = GetSubscriptionFrequencies;
211 my @frqloop;
212 foreach my $freq (@frequencies) {
213 my $selected = 0;
214 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
215 my $row = {
216 id => $freq->{'id'},
217 selected => $selected,
218 label => $freq->{'description'},
220 push @frqloop, $row;
222 $template->param(frequencies => \@frqloop);
224 my @numpatterns = GetSubscriptionNumberpatterns;
225 my @numberpatternloop;
226 foreach my $numpattern (@numpatterns) {
227 my $selected = 0;
228 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
229 my $row = {
230 id => $numpattern->{'id'},
231 selected => $selected,
232 label => $numpattern->{'label'},
234 push @numberpatternloop, $row;
236 $template->param(numberpatterns => \@numberpatternloop);
238 my $languages = [ map {
240 language => $_->{iso639_2_code},
241 description => $_->{language_description} || $_->{language}
243 } @{ C4::Languages::getAllLanguages() } ];
245 $template->param( locales => $languages );
247 output_html_with_http_headers $query, $cookie, $template->output;
250 sub get_letter_loop {
251 my ($selected_lettercode) = @_;
252 $selected_lettercode //= '';
253 my $letters = GetLetters({ module => 'serial' });
254 return [
255 map {
257 value => $_->{code},
258 lettername => $_->{name},
259 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
261 } @$letters
265 sub _get_sub_length {
266 my ($type, $length) = @_;
267 return
269 $type eq 'issues' ? $length : 0,
270 $type eq 'weeks' ? $length : 0,
271 $type eq 'months' ? $length : 0,
275 sub _guess_enddate {
276 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
277 my ($year, $month, $day);
278 my $enddate;
279 if($numberlength != 0) {
280 my $frequency = GetSubscriptionFrequency($frequencyid);
281 if($frequency->{'unit'} eq 'day') {
282 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
283 } elsif($frequency->{'unit'} eq 'week') {
284 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
285 } elsif($frequency->{'unit'} eq 'month') {
286 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
287 } elsif($frequency->{'unit'} eq 'year') {
288 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
290 } elsif($weeklength != 0) {
291 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
292 } elsif($monthlength != 0) {
293 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
295 if(defined $year) {
296 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
297 } else {
298 undef $enddate;
300 return $enddate;
303 sub redirect_add_subscription {
304 my $periodicity = $query->param('frequency');
305 if ($periodicity eq 'mana') {
306 my $subscription_freq = Koha::Subscription::Frequency->new()->set(
308 description => $query->param('sfdescription'),
309 unit => $query->param('unit'),
310 unitsperissue => $query->param('unitsperissue'),
311 issuesperunit => $query->param('issuesperunit'),
313 )->store();
314 $periodicity = $subscription_freq->id;
316 my $numberpattern = Koha::Subscription::Numberpatterns->new_or_existing({ $query->Vars });
318 my $auser = $query->param('user');
319 my $branchcode = $query->param('branchcode');
320 my $aqbooksellerid = $query->param('aqbooksellerid');
321 my $cost = $query->param('cost');
322 my $aqbudgetid = $query->param('aqbudgetid');
323 my @irregularity = $query->multi_param('irregularity');
324 my $locale = $query->param('locale');
325 my $graceperiod = $query->param('graceperiod') || 0;
327 my $subtype = $query->param('subtype');
328 my $sublength = $query->param('sublength');
329 my ( $numberlength, $weeklength, $monthlength )
330 = _get_sub_length( $subtype, $sublength );
331 my $add1 = $query->param('add1');
332 my $lastvalue1 = $query->param('lastvalue1');
333 my $innerloop1 = $query->param('innerloop1');
334 my $innerloop2 = $query->param('innerloop2');
335 my $lastvalue2 = $query->param('lastvalue2');
336 my $lastvalue3 = $query->param('lastvalue3');
337 my $innerloop3 = $query->param('innerloop3');
338 my $status = 1;
339 my $biblionumber = $query->param('biblionumber');
340 my $callnumber = $query->param('callnumber');
341 my $notes = $query->param('notes');
342 my $internalnotes = $query->param('internalnotes');
343 my $letter = $query->param('letter');
344 my $manualhistory = $query->param('manualhist') ? 1 : 0;
345 my $serialsadditems = $query->param('serialsadditems');
346 my $staffdisplaycount = $query->param('staffdisplaycount');
347 my $opacdisplaycount = $query->param('opacdisplaycount');
348 my $location = $query->param('location');
349 my $itemtype = $query->param('itemtype');
350 my $previousitemtype = $query->param('previousitemtype');
351 my $skip_serialseq = $query->param('skip_serialseq');
353 my $mana_id;
354 if ( $query->param('mana_id') ne "" ) {
355 $mana_id = $query->param('mana_id');
356 Koha::SharedContent::increment_entity_value("subscription",$mana_id, "nbofusers");
359 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
360 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
361 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
363 if(!defined $enddate || $enddate eq '') {
364 if($subtype eq "issues") {
365 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
366 } else {
367 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
370 my $subscriptionid = NewSubscription(
371 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
372 $startdate, $periodicity, $numberlength, $weeklength,
373 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
374 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
375 join(";",@irregularity), $numberpattern, $locale, $callnumber,
376 $manualhistory, $internalnotes, $serialsadditems,
377 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
378 $skip_serialseq, $itemtype, $previousitemtype, $mana_id
380 if ( (C4::Context->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4::Context->preference('AutoShareWithMana'))) ){
381 my $result = Koha::SharedContent::send_entity( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription');
382 $template->param( mana_msg => $result->{msg} );
385 my @additional_fields;
386 my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 });
387 my $subscription_fields = Koha::AdditionalFields->search({ tablename => 'subscription' });
388 while ( my $field = $subscription_fields->next ) {
389 my $value = $query->param('additional_field_' . $field->id);
390 if ($field->marcfield) {
391 my ($field, $subfield) = split /\$/, $field->marcfield;
392 if ( $record and $field and $subfield ) {
393 $value = $record->subfield( $field, $subfield );
396 push @additional_fields, {
397 id => $field->id,
398 value => $value,
401 Koha::Subscriptions->find($subscriptionid)->set_additional_fields(\@additional_fields);
403 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
404 return;
407 sub redirect_mod_subscription {
408 my $subscriptionid = $query->param('subscriptionid');
409 my @irregularity = $query->multi_param('irregularity');
410 my $auser = $query->param('user');
411 my $librarian => scalar $query->param('librarian'),
412 my $branchcode = $query->param('branchcode');
413 my $cost = $query->param('cost');
414 my $aqbooksellerid = $query->param('aqbooksellerid');
415 my $biblionumber = $query->param('biblionumber');
416 my $aqbudgetid = $query->param('aqbudgetid');
418 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
419 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
420 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
422 my $nextacquidate = $query->param('nextacquidate');
423 $nextacquidate = $nextacquidate
424 ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
425 : $firstacquidate;
427 my $periodicity = $query->param('frequency');
428 if ($periodicity eq 'mana') {
429 my $subscription_freq = Koha::Subscription::Frequency->new()->set(
431 description => $query->param('sfdescription'),
432 unit => $query->param('unit'),
433 unitsperissue => $query->param('unitsperissue'),
434 issuesperunit => $query->param('issuesperunit'),
436 )->store();
437 $periodicity = $subscription_freq->id;
439 my $numberpattern = Koha::Subscription::Numberpatterns->new_or_existing({ $query->Vars });
441 my $subtype = $query->param('subtype');
442 my $sublength = $query->param('sublength');
443 my ($numberlength, $weeklength, $monthlength)
444 = _get_sub_length( $subtype, $sublength );
445 my $locale = $query->param('locale');
446 my $lastvalue1 = $query->param('lastvalue1');
447 my $innerloop1 = $query->param('innerloop1');
448 my $lastvalue2 = $query->param('lastvalue2');
449 my $innerloop2 = $query->param('innerloop2');
450 my $lastvalue3 = $query->param('lastvalue3');
451 my $innerloop3 = $query->param('innerloop3');
452 my $status = 1;
453 my $callnumber = $query->param('callnumber');
454 my $notes = $query->param('notes');
455 my $internalnotes = $query->param('internalnotes');
456 my $letter = $query->param('letter');
457 my $manualhistory = $query->param('manualhist') ? 1 : 0;
458 my $serialsadditems = $query->param('serialsadditems');
459 my $staffdisplaycount = $query->param('staffdisplaycount');
460 my $opacdisplaycount = $query->param('opacdisplaycount');
461 my $graceperiod = $query->param('graceperiod') || 0;
462 my $location = $query->param('location');
463 my $itemtype = $query->param('itemtype');
464 my $previousitemtype = $query->param('previousitemtype');
465 my $skip_serialseq = $query->param('skip_serialseq');
467 my $mana_id;
468 if ( $query->param('mana_id') ne "" ) {
469 $mana_id = $query->param('mana_id');
470 Koha::SharedContent::increment_entity_value("subscription",$mana_id, "nbofusers");
472 else {
473 $mana_id = undef;
476 # Guess end date
477 if(!defined $enddate || $enddate eq '') {
478 if($subtype eq "issues") {
479 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
480 } else {
481 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
485 my $nextexpected = GetNextExpected($subscriptionid);
486 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
487 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
488 ModNextExpected($subscriptionid, $nextacquidate);
489 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
490 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
493 ModSubscription(
494 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
495 $periodicity, $firstacquidate, join(";",@irregularity),
496 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
497 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
498 $status, $biblionumber, $callnumber, $notes, $letter,
499 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
500 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
501 $skip_serialseq, $itemtype, $previousitemtype, $mana_id
504 my @additional_fields;
505 my $record = GetMarcBiblio({ biblionumber => $biblionumber, embed_items => 1 });
506 my $subscription_fields = Koha::AdditionalFields->search({ tablename => 'subscription' });
507 while ( my $field = $subscription_fields->next ) {
508 my $value = $query->param('additional_field_' . $field->id);
509 if ($field->marcfield) {
510 my ($field, $subfield) = split /\$/, $field->marcfield;
511 if ( $record and $field and $subfield ) {
512 $value = $record->subfield( $field, $subfield );
515 push @additional_fields, {
516 id => $field->id,
517 value => $value,
520 Koha::Subscriptions->find($subscriptionid)->set_additional_fields(\@additional_fields);
522 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
523 return;