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>.
22 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM);
29 use C4::Branch; # GetBranches
31 use C4::Serials::Frequency;
32 use C4::Serials::Numberpattern;
34 use Koha::AdditionalField;
40 our $query = CGI->new;
41 my $op = $query->param('op') || '';
42 my $dbh = C4::Context->dbh;
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",
55 flagsrequired => {serials => $permission},
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') {
83 $subs->{$_} = $subs->{$_};
86 if (!defined $subs->{letter}) {
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};
101 $template->param( %{$subs} );
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 );
130 C4::Context->preference('IndependentBranches')
131 && C4::Context->userenv
132 && !C4::Context->IsSuperLibrarian
134 not C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'superserials' } )
136 && C4::Context->userenv->{branch};
137 my $branches = GetBranches($onlymine);
139 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
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",$subs->{'location'});
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:
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();
177 subtypes => [ qw( numberlength weeklength monthlength ) ],
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);
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;
199 foreach my $freq (@frequencies) {
201 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
204 selected => $selected,
205 label => $freq->{'description'},
209 $template->param(frequencies => \@frqloop);
211 my @numpatterns = GetSubscriptionNumberpatterns;
212 my @numberpatternloop;
213 foreach my $numpattern (@numpatterns) {
215 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
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 my $letters = GetLetters({ module => 'serial' });
244 lettername => $_->{name},
245 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
251 sub _get_sub_length {
252 my ($type, $length) = @_;
255 $type eq 'issues' ? $length : 0,
256 $type eq 'weeks' ? $length : 0,
257 $type eq 'months' ? $length : 0,
262 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
263 my ($year, $month, $day);
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);
282 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
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->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');
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 $skip_serialseq = $query->param('skip_serialseq');
325 my $startdate = output_pref( { str => $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
326 my $enddate = output_pref( { str => $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
327 my $firstacquidate = output_pref( { str => $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
329 if(!defined $enddate || $enddate eq '') {
330 if($subtype eq "issues") {
331 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
333 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
337 my $subscriptionid = NewSubscription(
338 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
339 $startdate, $periodicity, $numberlength, $weeklength,
340 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
341 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
342 join(";",@irregularity), $numberpattern, $locale, $callnumber,
343 $manualhistory, $internalnotes, $serialsadditems,
344 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
348 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
349 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
351 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
355 sub redirect_mod_subscription {
356 my $subscriptionid = $query->param('subscriptionid');
357 my @irregularity = $query->param('irregularity');
358 my $auser = $query->param('user');
359 my $librarian => $query->param('librarian'),
360 my $branchcode = $query->param('branchcode');
361 my $cost = $query->param('cost');
362 my $aqbooksellerid = $query->param('aqbooksellerid');
363 my $biblionumber = $query->param('biblionumber');
364 my $aqbudgetid = $query->param('aqbudgetid');
366 my $startdate = output_pref( { str => $query->param('startdate'), dateonly => 1, dateformat => 'iso' } );
367 my $enddate = output_pref( { str => $query->param('enddate'), dateonly => 1, dateformat => 'iso' } );
368 my $firstacquidate = output_pref( { str => $query->param('firstacquidate'), dateonly => 1, dateformat => 'iso' } );
370 my $nextacquidate = $query->param('nextacquidate');
371 $nextacquidate = $nextacquidate
372 ? output_pref( { str => $nextacquidate, dateonly => 1, dateformat => 'iso' } )
375 my $periodicity = $query->param('frequency');
377 my $subtype = $query->param('subtype');
378 my $sublength = $query->param('sublength');
379 my ($numberlength, $weeklength, $monthlength)
380 = _get_sub_length( $subtype, $sublength );
381 my $numberpattern = $query->param('numbering_pattern');
382 my $locale = $query->param('locale');
383 my $lastvalue1 = $query->param('lastvalue1');
384 my $innerloop1 = $query->param('innerloop1');
385 my $lastvalue2 = $query->param('lastvalue2');
386 my $innerloop2 = $query->param('innerloop2');
387 my $lastvalue3 = $query->param('lastvalue3');
388 my $innerloop3 = $query->param('innerloop3');
390 my $callnumber = $query->param('callnumber');
391 my $notes = $query->param('notes');
392 my $internalnotes = $query->param('internalnotes');
393 my $letter = $query->param('letter');
394 my $manualhistory = $query->param('manualhist') ? 1 : 0;
395 my $serialsadditems = $query->param('serialsadditems');
396 my $staffdisplaycount = $query->param('staffdisplaycount');
397 my $opacdisplaycount = $query->param('opacdisplaycount');
398 my $graceperiod = $query->param('graceperiod') || 0;
399 my $location = $query->param('location');
400 my $skip_serialseq = $query->param('skip_serialseq');
403 if(!defined $enddate || $enddate eq '') {
404 if($subtype eq "issues") {
405 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
407 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
411 my $nextexpected = GetNextExpected($subscriptionid);
412 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
413 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
414 ModNextExpected($subscriptionid, $nextacquidate);
415 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
416 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
420 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
421 $periodicity, $firstacquidate, join(";",@irregularity),
422 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
423 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
424 $status, $biblionumber, $callnumber, $notes, $letter,
425 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
426 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
430 my $additional_fields = Koha::AdditionalField->all( { tablename => 'subscription' } );
431 insert_additional_fields( $additional_fields, $biblionumber, $subscriptionid );
433 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
437 sub insert_additional_fields {
438 my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
439 my $record = GetMarcBiblio( $biblionumber, 1 );
440 for my $field ( @$additional_fields ) {
441 my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
442 if ( $af->{marcfield} ) {
443 my ( $field, $subfield ) = split /\$/, $af->{marcfield};
444 $af->{values} = undef;
445 if ( $field and $subfield ) {
446 my $value = $record->subfield( $field, $subfield );
448 $subscriptionid => $value
453 $subscriptionid => $query->param('additional_field_' . $field->{id})
454 } if defined $query->param('additional_field_' . $field->{id});