Bug 19274: Translatability: Fix new splitting problems related to database warnings
[koha.git] / serials / subscription-add.pl
blob16d397bae8acf550fcbcb0e2481dd88294d15083
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::Serials;
30 use C4::Serials::Frequency;
31 use C4::Serials::Numberpattern;
32 use C4::Letters;
33 use Koha::AdditionalField;
34 use Koha::Biblios;
35 use Koha::DateUtils;
36 use Koha::ItemTypes;
37 use Carp;
39 #use Smart::Comments;
41 our $query = CGI->new;
42 my $op = $query->param('op') || '';
43 my $dbh = C4::Context->dbh;
44 my $sub_length;
47 # Permission needed if it is a modification : edit_subscription
48 # Permission needed otherwise (nothing or dup) : create_subscription
49 my $permission =
50 ( $op eq 'modify' || $op eq 'modsubscription' ) ? "edit_subscription" : "create_subscription";
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "serials/subscription-add.tt",
54 query => $query,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => {serials => $permission},
58 debug => 1,
59 });
63 my $sub_on;
65 my $subs;
66 our $firstissuedate;
68 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
70 my $subscriptionid = $query->param('subscriptionid');
71 $subs = GetSubscription($subscriptionid);
73 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
74 if ($subs->{'cannotedit'} && $op eq 'modify'){
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->{$_} = $subs->{$_};
88 if (!defined $subs->{letter}) {
89 $subs->{letter}= q{};
91 my $nextexpected = GetNextExpected($subscriptionid);
92 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate} eq $firstissuedate ;
93 $subs->{nextacquidate} = $nextexpected->{planneddate} if($op eq 'modify');
94 unless($op eq 'modsubscription') {
95 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
96 if ($subs->{$length_unit}) {
97 $sub_length=$subs->{$length_unit};
98 $sub_on=$length_unit;
99 last;
103 $template->param( %{$subs} );
104 $template->param(
105 $op => 1,
106 "subtype_$sub_on" => 1,
107 sublength =>$sub_length,
108 history => ($op eq 'modify'),
109 firstacquiyear => substr($firstissuedate,0,4),
112 if($op eq 'modify') {
113 my ($serials_number) = GetSerials($subscriptionid);
114 if($serials_number > 1) {
115 $template->param(more_than_one_serial => 1);
120 if ( $op eq 'dup' ) {
121 my $dont_copy_fields = C4::Context->preference('SubscriptionDuplicateDroppedInput');
122 my @fields_id = map { fieldid => $_ }, split '\|', $dont_copy_fields;
123 $template->param( dont_export_field_loop => \@fields_id );
126 my $letters = get_letter_loop( $subs->{letter} );
127 $template->param( letterloop => $letters );
131 my $locations_loop = GetAuthorisedValues("LOC");
133 $template->param(
134 branchcode => $subs->{branchcode},
135 locations_loop=>$locations_loop,
139 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
140 for my $field ( @$additional_fields ) {
141 if ( $field->{authorised_value_category} ) {
142 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
145 $template->param( additional_fields_for_subscription => $additional_fields );
147 my $typeloop = { map { $_->{itemtype} => $_ } @{ Koha::ItemTypes->search_with_localization->unblessed } };
149 # FIXME We should use the translated_description for item types
150 my @typearg =
151 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{itemtype} and $_ eq $subs->{itemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
152 my @previoustypearg =
153 map { { code => $_, value => $typeloop->{$_}{'description'}, selected => ( ( $subs->{previousitemtype} and $_ eq $subs->{previousitemtype} ) ? "selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
155 $template->param(
156 typeloop => \@typearg,
157 previoustypeloop => \@previoustypearg,
158 locations_loop=>$locations_loop,
161 # prepare template variables common to all $op conditions:
162 $template->param('makePreviousSerialAvailable' => 1) if (C4::Context->preference('makePreviousSerialAvailable'));
164 if ($op!~/^mod/) {
165 my $letters = get_letter_loop();
166 $template->param( letterloop => $letters );
169 if ($op eq 'addsubscription') {
170 redirect_add_subscription();
171 } elsif ($op eq 'modsubscription') {
172 redirect_mod_subscription();
173 } else {
175 $template->param(
176 subtypes => [ qw( numberlength weeklength monthlength ) ],
177 subtype => $sub_on,
180 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
181 my $letters = get_letter_loop();
182 $template->param( letterloop => $letters );
185 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
186 if (defined $new_biblionumber) {
187 my $biblio = Koha::Biblios->find( $new_biblionumber );
188 if (defined $biblio) {
189 $template->param(bibnum => $new_biblionumber);
190 $template->param(bibliotitle => $biblio->title);
194 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
196 my @frequencies = GetSubscriptionFrequencies;
197 my @frqloop;
198 foreach my $freq (@frequencies) {
199 my $selected = 0;
200 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
201 my $row = {
202 id => $freq->{'id'},
203 selected => $selected,
204 label => $freq->{'description'},
206 push @frqloop, $row;
208 $template->param(frequencies => \@frqloop);
210 my @numpatterns = GetSubscriptionNumberpatterns;
211 my @numberpatternloop;
212 foreach my $numpattern (@numpatterns) {
213 my $selected = 0;
214 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
215 my $row = {
216 id => $numpattern->{'id'},
217 selected => $selected,
218 label => $numpattern->{'label'},
220 push @numberpatternloop, $row;
222 $template->param(numberpatterns => \@numberpatternloop);
224 my $languages = [ map {
226 language => $_->{iso639_2_code},
227 description => $_->{language_description} || $_->{language}
229 } @{ C4::Languages::getAllLanguages() } ];
231 $template->param( locales => $languages );
233 output_html_with_http_headers $query, $cookie, $template->output;
236 sub get_letter_loop {
237 my ($selected_lettercode) = @_;
238 $selected_lettercode //= '';
239 my $letters = GetLetters({ module => 'serial' });
240 return [
241 map {
243 value => $_->{code},
244 lettername => $_->{name},
245 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
247 } @$letters
251 sub _get_sub_length {
252 my ($type, $length) = @_;
253 return
255 $type eq 'issues' ? $length : 0,
256 $type eq 'weeks' ? $length : 0,
257 $type eq 'months' ? $length : 0,
261 sub _guess_enddate {
262 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
263 my ($year, $month, $day);
264 my $enddate;
265 if($numberlength != 0) {
266 my $frequency = GetSubscriptionFrequency($frequencyid);
267 if($frequency->{'unit'} eq 'day') {
268 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
269 } elsif($frequency->{'unit'} eq 'week') {
270 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
271 } elsif($frequency->{'unit'} eq 'month') {
272 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
273 } elsif($frequency->{'unit'} eq 'year') {
274 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
276 } elsif($weeklength != 0) {
277 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
278 } elsif($monthlength != 0) {
279 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
281 if(defined $year) {
282 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
283 } else {
284 undef $enddate;
286 return $enddate;
289 sub redirect_add_subscription {
290 my $auser = $query->param('user');
291 my $branchcode = $query->param('branchcode');
292 my $aqbooksellerid = $query->param('aqbooksellerid');
293 my $cost = $query->param('cost');
294 my $aqbudgetid = $query->param('aqbudgetid');
295 my $periodicity = $query->param('frequency');
296 my @irregularity = $query->multi_param('irregularity');
297 my $numberpattern = $query->param('numbering_pattern');
298 my $locale = $query->param('locale');
299 my $graceperiod = $query->param('graceperiod') || 0;
301 my $subtype = $query->param('subtype');
302 my $sublength = $query->param('sublength');
303 my ( $numberlength, $weeklength, $monthlength )
304 = _get_sub_length( $subtype, $sublength );
305 my $add1 = $query->param('add1');
306 my $lastvalue1 = $query->param('lastvalue1');
307 my $innerloop1 = $query->param('innerloop1');
308 my $innerloop2 = $query->param('innerloop2');
309 my $lastvalue2 = $query->param('lastvalue2');
310 my $lastvalue3 = $query->param('lastvalue3');
311 my $innerloop3 = $query->param('innerloop3');
312 my $status = 1;
313 my $biblionumber = $query->param('biblionumber');
314 my $callnumber = $query->param('callnumber');
315 my $notes = $query->param('notes');
316 my $internalnotes = $query->param('internalnotes');
317 my $letter = $query->param('letter');
318 my $manualhistory = $query->param('manualhist') ? 1 : 0;
319 my $serialsadditems = $query->param('serialsadditems');
320 my $staffdisplaycount = $query->param('staffdisplaycount');
321 my $opacdisplaycount = $query->param('opacdisplaycount');
322 my $location = $query->param('location');
323 my $itemtype = $query->param('itemtype');
324 my $previousitemtype = $query->param('previousitemtype');
325 my $skip_serialseq = $query->param('skip_serialseq');
327 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
328 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
329 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
331 if(!defined $enddate || $enddate eq '') {
332 if($subtype eq "issues") {
333 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
334 } else {
335 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
339 my $subscriptionid = NewSubscription(
340 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
341 $startdate, $periodicity, $numberlength, $weeklength,
342 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
343 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
344 join(";",@irregularity), $numberpattern, $locale, $callnumber,
345 $manualhistory, $internalnotes, $serialsadditems,
346 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
347 $skip_serialseq
350 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
351 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
353 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
354 return;
357 sub redirect_mod_subscription {
358 my $subscriptionid = $query->param('subscriptionid');
359 my @irregularity = $query->multi_param('irregularity');
360 my $auser = $query->param('user');
361 my $librarian => scalar $query->param('librarian'),
362 my $branchcode = $query->param('branchcode');
363 my $cost = $query->param('cost');
364 my $aqbooksellerid = $query->param('aqbooksellerid');
365 my $biblionumber = $query->param('biblionumber');
366 my $aqbudgetid = $query->param('aqbudgetid');
368 my $startdate = output_pref( { str => scalar $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
369 my $enddate = output_pref( { str => scalar $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
370 my $firstacquidate = output_pref( { str => scalar $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
372 my $nextacquidate = $query->param('nextacquidate');
373 $nextacquidate = $nextacquidate
374 ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
375 : $firstacquidate;
377 my $periodicity = $query->param('frequency');
379 my $subtype = $query->param('subtype');
380 my $sublength = $query->param('sublength');
381 my ($numberlength, $weeklength, $monthlength)
382 = _get_sub_length( $subtype, $sublength );
383 my $numberpattern = $query->param('numbering_pattern');
384 my $locale = $query->param('locale');
385 my $lastvalue1 = $query->param('lastvalue1');
386 my $innerloop1 = $query->param('innerloop1');
387 my $lastvalue2 = $query->param('lastvalue2');
388 my $innerloop2 = $query->param('innerloop2');
389 my $lastvalue3 = $query->param('lastvalue3');
390 my $innerloop3 = $query->param('innerloop3');
391 my $status = 1;
392 my $callnumber = $query->param('callnumber');
393 my $notes = $query->param('notes');
394 my $internalnotes = $query->param('internalnotes');
395 my $letter = $query->param('letter');
396 my $manualhistory = $query->param('manualhist') ? 1 : 0;
397 my $serialsadditems = $query->param('serialsadditems');
398 my $staffdisplaycount = $query->param('staffdisplaycount');
399 my $opacdisplaycount = $query->param('opacdisplaycount');
400 my $graceperiod = $query->param('graceperiod') || 0;
401 my $location = $query->param('location');
402 my $itemtype = $query->param('itemtype');
403 my $previousitemtype = $query->param('previousitemtype');
404 my $skip_serialseq = $query->param('skip_serialseq');
406 # Guess end date
407 if(!defined $enddate || $enddate eq '') {
408 if($subtype eq "issues") {
409 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
410 } else {
411 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
415 my $nextexpected = GetNextExpected($subscriptionid);
416 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
417 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
418 ModNextExpected($subscriptionid, $nextacquidate);
419 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
420 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
423 ModSubscription(
424 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
425 $periodicity, $firstacquidate, join(";",@irregularity),
426 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
427 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
428 $status, $biblionumber, $callnumber, $notes, $letter,
429 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
430 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
431 $skip_serialseq, $itemtype, $previousitemtype
434 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
435 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
437 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
438 return;
441 sub insert_additional_fields {
442 my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
443 my $record = GetMarcBiblio({
444 biblionumber => $biblionumber,
445 embed_items => 1 });
446 for my $field ( @$additional_fields ) {
447 my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
448 if ( $af->{marcfield} ) {
449 my ( $field, $subfield ) = split /\$/, $af->{marcfield};
450 $af->{values} = undef;
451 if ( $field and $subfield ) {
452 my $value = $record->subfield( $field, $subfield );
453 $af->{values} = {
454 $subscriptionid => $value
457 } else {
458 $af->{values} = {
459 $subscriptionid => scalar $query->param('additional_field_' . $field->{id})
460 } if defined $query->param('additional_field_' . $field->{id});
462 $af->insert_values;