Bug 9302: Use patron-title.inc
[koha.git] / serials / subscription-add.pl
blobbb9e8cfc9b07630ee5c83d640d53e9d852f4d71f
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::AdditionalField;
33 use Koha::Biblios;
34 use Koha::DateUtils;
35 use Koha::ItemTypes;
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 =
49 ( $op eq 'modify' || $op eq 'modsubscription' ) ? "edit_subscription" : "create_subscription";
51 my ($template, $loggedinuser, $cookie)
52 = get_template_and_user({template_name => "serials/subscription-add.tt",
53 query => $query,
54 type => "intranet",
55 authnotrequired => 0,
56 flagsrequired => {serials => $permission},
57 debug => 1,
58 });
62 my $sub_on;
64 my $subs;
65 our $firstissuedate;
67 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
69 my $subscriptionid = $query->param('subscriptionid');
70 $subs = GetSubscription($subscriptionid);
72 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
73 if ($subs->{'cannotedit'} && $op eq 'modify'){
74 carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
75 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
77 $firstissuedate = $subs->{firstacquidate} || ''; # in iso format.
78 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
79 next unless defined $subs->{$_};
80 # TODO : Handle date formats properly.
81 if ($subs->{$_} eq '0000-00-00') {
82 $subs->{$_} = ''
83 } else {
84 $subs->{$_} = $subs->{$_};
87 if (!defined $subs->{letter}) {
88 $subs->{letter}= q{};
90 my $nextexpected = GetNextExpected($subscriptionid);
91 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate} eq $firstissuedate ;
92 $subs->{nextacquidate} = $nextexpected->{planneddate} if($op eq 'modify');
93 unless($op eq 'modsubscription') {
94 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
95 if ($subs->{$length_unit}) {
96 $sub_length=$subs->{$length_unit};
97 $sub_on=$length_unit;
98 last;
102 $template->param( %{$subs} );
103 $template->param(
104 $op => 1,
105 "subtype_$sub_on" => 1,
106 sublength =>$sub_length,
107 history => ($op eq 'modify'),
108 firstacquiyear => substr($firstissuedate,0,4),
111 if($op eq 'modify') {
112 my ($serials_number) = GetSerials($subscriptionid);
113 if($serials_number > 1) {
114 $template->param(more_than_one_serial => 1);
119 if ( $op eq 'dup' ) {
120 my $dont_copy_fields = C4::Context->preference('SubscriptionDuplicateDroppedInput');
121 my @fields_id = map { fieldid => $_ }, split '\|', $dont_copy_fields;
122 $template->param( dont_export_field_loop => \@fields_id );
125 my $letters = get_letter_loop( $subs->{letter} );
126 $template->param( letterloop => $letters );
130 my $locations_loop = GetAuthorisedValues("LOC");
132 $template->param(
133 branchcode => $subs->{branchcode},
134 locations_loop=>$locations_loop,
138 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
139 for my $field ( @$additional_fields ) {
140 if ( $field->{authorised_value_category} ) {
141 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
144 $template->param( additional_fields_for_subscription => $additional_fields );
146 my $typeloop = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
148 # FIXME We should use the translated_description for item types
149 my @typearg =
150 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{itemtype} and $_ eq $subs->{itemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
151 my @previoustypearg =
152 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{previousitemtype} and $_ eq $subs->{previousitemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
154 $template->param(
155 typeloop => \@typearg,
156 previoustypeloop => \@previoustypearg,
157 locations_loop=>$locations_loop,
160 # prepare template variables common to all $op conditions:
161 $template->param('makePreviousSerialAvailable' => 1) if (C4::Context->preference('makePreviousSerialAvailable'));
163 if ($op!~/^mod/) {
164 my $letters = get_letter_loop();
165 $template->param( letterloop => $letters );
168 if ($op eq 'addsubscription') {
169 redirect_add_subscription();
170 } elsif ($op eq 'modsubscription') {
171 redirect_mod_subscription();
172 } else {
174 $template->param(
175 subtypes => [ qw( numberlength weeklength monthlength ) ],
176 subtype => $sub_on,
179 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
180 my $letters = get_letter_loop();
181 $template->param( letterloop => $letters );
184 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
185 if (defined $new_biblionumber) {
186 my $biblio = Koha::Biblios->find( $new_biblionumber );
187 if (defined $biblio) {
188 $template->param(bibnum => $new_biblionumber);
189 $template->param(bibliotitle => $biblio->title);
193 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
195 my @frequencies = GetSubscriptionFrequencies;
196 my @frqloop;
197 foreach my $freq (@frequencies) {
198 my $selected = 0;
199 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
200 my $row = {
201 id => $freq->{'id'},
202 selected => $selected,
203 label => $freq->{'description'},
205 push @frqloop, $row;
207 $template->param(frequencies => \@frqloop);
209 my @numpatterns = GetSubscriptionNumberpatterns;
210 my @numberpatternloop;
211 foreach my $numpattern (@numpatterns) {
212 my $selected = 0;
213 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
214 my $row = {
215 id => $numpattern->{'id'},
216 selected => $selected,
217 label => $numpattern->{'label'},
219 push @numberpatternloop, $row;
221 $template->param(numberpatterns => \@numberpatternloop);
223 my $languages = [ map {
225 language => $_->{iso639_2_code},
226 description => $_->{language_description} || $_->{language}
228 } @{ C4::Languages::getAllLanguages() } ];
230 $template->param( locales => $languages );
232 output_html_with_http_headers $query, $cookie, $template->output;
235 sub get_letter_loop {
236 my ($selected_lettercode) = @_;
237 $selected_lettercode //= '';
238 my $letters = GetLetters({ module => 'serial' });
239 return [
240 map {
242 value => $_->{code},
243 lettername => $_->{name},
244 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
246 } @$letters
250 sub _get_sub_length {
251 my ($type, $length) = @_;
252 return
254 $type eq 'issues' ? $length : 0,
255 $type eq 'weeks' ? $length : 0,
256 $type eq 'months' ? $length : 0,
260 sub _guess_enddate {
261 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
262 my ($year, $month, $day);
263 my $enddate;
264 if($numberlength != 0) {
265 my $frequency = GetSubscriptionFrequency($frequencyid);
266 if($frequency->{'unit'} eq 'day') {
267 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
268 } elsif($frequency->{'unit'} eq 'week') {
269 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
270 } elsif($frequency->{'unit'} eq 'month') {
271 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
272 } elsif($frequency->{'unit'} eq 'year') {
273 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
275 } elsif($weeklength != 0) {
276 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
277 } elsif($monthlength != 0) {
278 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
280 if(defined $year) {
281 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
282 } else {
283 undef $enddate;
285 return $enddate;
288 sub redirect_add_subscription {
289 my $auser = $query->param('user');
290 my $branchcode = $query->param('branchcode');
291 my $aqbooksellerid = $query->param('aqbooksellerid');
292 my $cost = $query->param('cost');
293 my $aqbudgetid = $query->param('aqbudgetid');
294 my $periodicity = $query->param('frequency');
295 my @irregularity = $query->multi_param('irregularity');
296 my $numberpattern = $query->param('numbering_pattern');
297 my $locale = $query->param('locale');
298 my $graceperiod = $query->param('graceperiod') || 0;
300 my $subtype = $query->param('subtype');
301 my $sublength = $query->param('sublength');
302 my ( $numberlength, $weeklength, $monthlength )
303 = _get_sub_length( $subtype, $sublength );
304 my $add1 = $query->param('add1');
305 my $lastvalue1 = $query->param('lastvalue1');
306 my $innerloop1 = $query->param('innerloop1');
307 my $innerloop2 = $query->param('innerloop2');
308 my $lastvalue2 = $query->param('lastvalue2');
309 my $lastvalue3 = $query->param('lastvalue3');
310 my $innerloop3 = $query->param('innerloop3');
311 my $status = 1;
312 my $biblionumber = $query->param('biblionumber');
313 my $callnumber = $query->param('callnumber');
314 my $notes = $query->param('notes');
315 my $internalnotes = $query->param('internalnotes');
316 my $letter = $query->param('letter');
317 my $manualhistory = $query->param('manualhist') ? 1 : 0;
318 my $serialsadditems = $query->param('serialsadditems');
319 my $staffdisplaycount = $query->param('staffdisplaycount');
320 my $opacdisplaycount = $query->param('opacdisplaycount');
321 my $location = $query->param('location');
322 my $itemtype = $query->param('itemtype');
323 my $previousitemtype = $query->param('previousitemtype');
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, $itemtype, $previousitemtype
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 $itemtype = $query->param('itemtype');
402 my $previousitemtype = $query->param('previousitemtype');
403 my $skip_serialseq = $query->param('skip_serialseq');
405 # Guess end date
406 if(!defined $enddate || $enddate eq '') {
407 if($subtype eq "issues") {
408 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
409 } else {
410 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
414 my $nextexpected = GetNextExpected($subscriptionid);
415 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
416 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
417 ModNextExpected($subscriptionid, $nextacquidate);
418 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
419 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
422 ModSubscription(
423 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
424 $periodicity, $firstacquidate, join(";",@irregularity),
425 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
426 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
427 $status, $biblionumber, $callnumber, $notes, $letter,
428 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
429 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
430 $skip_serialseq, $itemtype, $previousitemtype
433 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
434 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
436 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
437 return;
440 sub insert_additional_fields {
441 my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
442 my $record = GetMarcBiblio({
443 biblionumber => $biblionumber,
444 embed_items => 1 });
445 for my $field ( @$additional_fields ) {
446 my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
447 if ( $af->{marcfield} ) {
448 my ( $field, $subfield ) = split /\$/, $af->{marcfield};
449 $af->{values} = undef;
450 if ( $field and $subfield ) {
451 my $value = $record->subfield( $field, $subfield );
452 $af->{values} = {
453 $subscriptionid => $value
456 } else {
457 $af->{values} = {
458 $subscriptionid => scalar $query->param('additional_field_' . $field->{id})
459 } if defined $query->param('additional_field_' . $field->{id});
461 $af->insert_values;