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>.
21 use Date
::Calc
qw(Today Day_of_Year Week_of_Year Add_Delta_Days Add_Delta_YM);
29 use C4
::Serials
::Frequency
;
30 use C4
::Serials
::Numberpattern
;
32 use Koha
::AdditionalFields
;
38 use Koha
::Subscription
::Numberpattern
;
39 use Koha
::Subscription
::Frequency
;
40 use Koha
::SharedContent
;
44 our $query = CGI
->new;
45 my $op = $query->param('op') || '';
46 my $dbh = C4
::Context
->dbh;
50 # Permission needed if it is a modification : edit_subscription
51 # Permission needed otherwise (nothing or dup) : create_subscription
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",
59 flagsrequired
=> {serials
=> $permission},
70 my $mana_url = C4
::Context
->config('mana_config');
71 $template->param( 'mana_url' => $mana_url );
72 my $subscriptionid = $query->param('subscriptionid');
74 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
76 $subs = GetSubscription
($subscriptionid);
78 output_and_exit
( $query, $cookie, $template, 'unknown_subscription')
81 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
82 if ($subs->{'cannotedit'} && $op eq 'modify'){
83 carp
"Attempt to modify subscription $subscriptionid by ".C4
::Context
->userenv->{'id'}." not allowed";
84 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
86 $firstissuedate = $subs->{firstacquidate
} || ''; # in iso format.
87 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
88 next unless defined $subs->{$_};
89 # TODO : Handle date formats properly.
90 if ($subs->{$_} eq '0000-00-00') {
93 $subs->{$_} = $subs->{$_};
96 if (!defined $subs->{letter
}) {
99 my $nextexpected = GetNextExpected
($subscriptionid);
100 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate
} eq $firstissuedate ;
101 $subs->{nextacquidate
} = $nextexpected->{planneddate
} if($op eq 'modify');
102 unless($op eq 'modsubscription') {
103 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
104 if ($subs->{$length_unit}) {
105 $sub_length=$subs->{$length_unit};
106 $sub_on=$length_unit;
111 $template->param( %{$subs} );
114 "subtype_$sub_on" => 1,
115 sublength
=>$sub_length,
116 history
=> ($op eq 'modify'),
117 firstacquiyear
=> substr($firstissuedate,0,4),
120 if($op eq 'modify') {
121 my ($serials_number) = GetSerials
($subscriptionid);
122 if($serials_number > 1) {
123 $template->param(more_than_one_serial
=> 1);
128 if ( $op eq 'dup' ) {
129 my $dont_copy_fields = C4
::Context
->preference('SubscriptionDuplicateDroppedInput');
130 my @fields_id = map { fieldid
=> $_ }, split '\|', $dont_copy_fields;
131 $template->param( dont_export_field_loop
=> \
@fields_id );
134 my $letters = get_letter_loop
( $subs->{letter
} );
135 $template->param( letterloop
=> $letters );
139 my $locations_loop = GetAuthorisedValues
("LOC");
142 branchcode
=> $subs->{branchcode
},
143 locations_loop
=>$locations_loop,
146 my @additional_fields = Koha
::AdditionalFields
->search({ tablename
=> 'subscription' });
147 my %additional_field_values;
148 if ($subscriptionid) {
149 my $subscription = Koha
::Subscriptions
->find($subscriptionid);
150 foreach my $value ($subscription->additional_field_values->as_list) {
151 $additional_field_values{$value->field_id} = $value->value;
156 additional_fields
=> \
@additional_fields,
157 additional_field_values
=> \
%additional_field_values,
160 my $typeloop = { map { $_->{itemtype
} => $_ } @
{ Koha
::ItemTypes
->search_with_localization->unblessed } };
162 # FIXME We should use the translated_description for item types
164 map { { code
=> $_, value
=> $typeloop->{$_}{'description'}, selected
=> ( ( $subs->{itemtype
} and $_ eq $subs->{itemtype
} ) ?
"selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
165 my @previoustypearg =
166 map { { code
=> $_, value
=> $typeloop->{$_}{'description'}, selected
=> ( ( $subs->{previousitemtype
} and $_ eq $subs->{previousitemtype
} ) ?
"selected=\"selected\"" : "" ), } } sort keys %{$typeloop};
169 typeloop
=> \
@typearg,
170 previoustypeloop
=> \
@previoustypearg,
171 locations_loop
=>$locations_loop,
174 # prepare template variables common to all $op conditions:
175 $template->param('makePreviousSerialAvailable' => 1) if (C4
::Context
->preference('makePreviousSerialAvailable'));
178 my $letters = get_letter_loop
();
179 $template->param( letterloop
=> $letters );
182 if ($op eq 'addsubscription') {
183 redirect_add_subscription
();
184 } elsif ($op eq 'modsubscription') {
185 redirect_mod_subscription
();
189 subtypes
=> [ qw( numberlength weeklength monthlength ) ],
193 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
194 my $letters = get_letter_loop
();
195 $template->param( letterloop
=> $letters );
198 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
199 if (defined $new_biblionumber) {
200 my $biblio = Koha
::Biblios
->find( $new_biblionumber );
201 if (defined $biblio) {
202 $template->param(bibnum
=> $new_biblionumber);
203 $template->param(bibliotitle
=> $biblio->title);
207 $template->param((uc(C4
::Context
->preference("marcflavour"))) => 1);
209 my @frequencies = GetSubscriptionFrequencies
;
211 foreach my $freq (@frequencies) {
213 $selected = 1 if ($subs->{periodicity
} and $freq->{id
} eq $subs->{periodicity
});
216 selected
=> $selected,
217 label
=> $freq->{'description'},
221 $template->param(frequencies
=> \
@frqloop);
223 my @numpatterns = GetSubscriptionNumberpatterns
;
224 my @numberpatternloop;
225 foreach my $numpattern (@numpatterns) {
227 $selected = 1 if($subs->{numberpattern
} and $numpattern->{id
} eq $subs->{numberpattern
});
229 id
=> $numpattern->{'id'},
230 selected
=> $selected,
231 label
=> $numpattern->{'label'},
233 push @numberpatternloop, $row;
235 $template->param(numberpatterns
=> \
@numberpatternloop);
237 my $languages = [ map {
239 language
=> $_->{iso639_2_code
},
240 description
=> $_->{language_description
} || $_->{language
}
242 } @
{ C4
::Languages
::getAllLanguages
() } ];
244 $template->param( locales
=> $languages );
246 my @bookseller_ids = Koha
::Acquisition
::Booksellers
->search->get_column('id');
247 $template->param( bookseller_ids
=> \
@bookseller_ids );
249 output_html_with_http_headers
$query, $cookie, $template->output;
252 sub get_letter_loop
{
253 my ($selected_lettercode) = @_;
254 $selected_lettercode //= '';
255 my $letters = GetLetters
({ module
=> 'serial' });
260 lettername
=> $_->{name
},
261 ( $_->{code
} eq $selected_lettercode ?
( selected
=> 1 ) : () ),
268 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
269 my ($year, $month, $day);
271 if($numberlength != 0) {
272 my $frequency = GetSubscriptionFrequency
($frequencyid);
273 if($frequency->{'unit'} eq 'day') {
274 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
275 } elsif($frequency->{'unit'} eq 'week') {
276 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
277 } elsif($frequency->{'unit'} eq 'month') {
278 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
279 } elsif($frequency->{'unit'} eq 'year') {
280 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
282 } elsif($weeklength != 0) {
283 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $weeklength * 7);
284 } elsif($monthlength != 0) {
285 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), 0, $monthlength);
288 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
295 sub redirect_add_subscription
{
296 my $periodicity = $query->param('frequency');
297 if ($periodicity eq 'mana') {
298 my $subscription_freq = Koha
::Subscription
::Frequency
->new()->set(
300 description
=> $query->param('sfdescription'),
301 unit
=> $query->param('unit'),
302 unitsperissue
=> $query->param('unitsperissue'),
303 issuesperunit
=> $query->param('issuesperunit'),
306 $periodicity = $subscription_freq->id;
308 my $numberpattern = Koha
::Subscription
::Numberpatterns
->new_or_existing({ $query->Vars });
310 my $auser = $query->param('user');
311 my $branchcode = $query->param('branchcode');
312 my $aqbooksellerid = $query->param('aqbooksellerid');
313 my $cost = $query->param('cost');
314 my $aqbudgetid = $query->param('aqbudgetid');
315 my @irregularity = $query->multi_param('irregularity');
316 my $locale = $query->param('locale');
317 my $graceperiod = $query->param('graceperiod') || 0;
319 my $subtype = $query->param('subtype');
320 my $sublength = $query->param('sublength');
321 my ( $numberlength, $weeklength, $monthlength )
322 = GetSubscriptionLength
( $subtype, $sublength );
323 my $add1 = $query->param('add1');
324 my $lastvalue1 = $query->param('lastvalue1');
325 my $innerloop1 = $query->param('innerloop1');
326 my $innerloop2 = $query->param('innerloop2');
327 my $lastvalue2 = $query->param('lastvalue2');
328 my $lastvalue3 = $query->param('lastvalue3');
329 my $innerloop3 = $query->param('innerloop3');
331 my $biblionumber = $query->param('biblionumber');
332 my $callnumber = $query->param('callnumber');
333 my $notes = $query->param('notes');
334 my $internalnotes = $query->param('internalnotes');
335 my $letter = $query->param('letter');
336 my $manualhistory = $query->param('manualhist') ?
1 : 0;
337 my $serialsadditems = $query->param('serialsadditems');
338 my $staffdisplaycount = $query->param('staffdisplaycount');
339 my $opacdisplaycount = $query->param('opacdisplaycount');
340 my $location = $query->param('location');
341 my $itemtype = $query->param('itemtype');
342 my $previousitemtype = $query->param('previousitemtype');
343 my $skip_serialseq = $query->param('skip_serialseq');
346 if ( $query->param('mana_id') ne "" ) {
347 $mana_id = $query->param('mana_id');
348 Koha
::SharedContent
::increment_entity_value
("subscription",$mana_id, "nbofusers");
351 my $startdate = output_pref
( { str
=> scalar $query->param('startdate'), dateonly
=> 1, dateformat
=> 'iso' } );
352 my $enddate = output_pref
( { str
=> scalar $query->param('enddate'), dateonly
=> 1, dateformat
=> 'iso' } );
353 my $firstacquidate = output_pref
( { str
=> scalar $query->param('firstacquidate'), dateonly
=> 1, dateformat
=> 'iso' } );
355 if(!defined $enddate || $enddate eq '') {
356 if($subtype eq "issues") {
357 $enddate = _guess_enddate
($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
359 $enddate = _guess_enddate
($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
362 my $subscriptionid = NewSubscription
(
363 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
364 $startdate, $periodicity, $numberlength, $weeklength,
365 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
366 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
367 join(";",@irregularity), $numberpattern, $locale, $callnumber,
368 $manualhistory, $internalnotes, $serialsadditems,
369 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
370 $skip_serialseq, $itemtype, $previousitemtype, $mana_id
372 if ( (C4
::Context
->preference('Mana') == 1) and ( grep { $_ eq "subscription" } split(/,/, C4
::Context
->preference('AutoShareWithMana'))) ){
373 my $result = Koha
::SharedContent
::send_entity
( $query->param('mana_language') || '', $loggedinuser, $subscriptionid, 'subscription');
374 $template->param( mana_msg
=> $result->{msg
} );
377 my @additional_fields;
378 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber, embed_items
=> 1 });
379 my $subscription_fields = Koha
::AdditionalFields
->search({ tablename
=> 'subscription' });
380 while ( my $field = $subscription_fields->next ) {
381 my $value = $query->param('additional_field_' . $field->id);
382 if ($field->marcfield) {
383 my ($field, $subfield) = split /\$/, $field->marcfield;
384 if ( $record and $field and $subfield ) {
385 $value = $record->subfield( $field, $subfield );
388 push @additional_fields, {
393 Koha
::Subscriptions
->find($subscriptionid)->set_additional_fields(\
@additional_fields);
395 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
399 sub redirect_mod_subscription
{
400 my $subscriptionid = $query->param('subscriptionid');
401 my @irregularity = $query->multi_param('irregularity');
402 my $auser = $query->param('user');
403 my $librarian => scalar $query->param('librarian'),
404 my $branchcode = $query->param('branchcode');
405 my $cost = $query->param('cost');
406 my $aqbooksellerid = $query->param('aqbooksellerid');
407 my $biblionumber = $query->param('biblionumber');
408 my $aqbudgetid = $query->param('aqbudgetid');
410 my $startdate = output_pref
( { str
=> scalar $query->param('startdate'), dateonly
=> 1, dateformat
=> 'iso' } );
411 my $enddate = output_pref
( { str
=> scalar $query->param('enddate'), dateonly
=> 1, dateformat
=> 'iso' } );
412 my $firstacquidate = output_pref
( { str
=> scalar $query->param('firstacquidate'), dateonly
=> 1, dateformat
=> 'iso' } );
414 my $nextacquidate = $query->param('nextacquidate');
415 $nextacquidate = $nextacquidate
416 ? output_pref
( { str
=> $nextacquidate, dateonly
=> 1, dateformat
=> 'iso' } )
419 my $periodicity = $query->param('frequency');
420 if ($periodicity eq 'mana') {
421 my $subscription_freq = Koha
::Subscription
::Frequency
->new()->set(
423 description
=> $query->param('sfdescription'),
424 unit
=> $query->param('unit'),
425 unitsperissue
=> $query->param('unitsperissue'),
426 issuesperunit
=> $query->param('issuesperunit'),
429 $periodicity = $subscription_freq->id;
431 my $numberpattern = Koha
::Subscription
::Numberpatterns
->new_or_existing({ $query->Vars });
433 my $subtype = $query->param('subtype');
434 my $sublength = $query->param('sublength');
435 my ($numberlength, $weeklength, $monthlength) = GetSubscriptionLength
( $subtype, $sublength );
436 my $locale = $query->param('locale');
437 my $lastvalue1 = $query->param('lastvalue1');
438 my $innerloop1 = $query->param('innerloop1');
439 my $lastvalue2 = $query->param('lastvalue2');
440 my $innerloop2 = $query->param('innerloop2');
441 my $lastvalue3 = $query->param('lastvalue3');
442 my $innerloop3 = $query->param('innerloop3');
444 my $callnumber = $query->param('callnumber');
445 my $notes = $query->param('notes');
446 my $internalnotes = $query->param('internalnotes');
447 my $letter = $query->param('letter');
448 my $manualhistory = $query->param('manualhist') ?
1 : 0;
449 my $serialsadditems = $query->param('serialsadditems');
450 my $staffdisplaycount = $query->param('staffdisplaycount');
451 my $opacdisplaycount = $query->param('opacdisplaycount');
452 my $graceperiod = $query->param('graceperiod') || 0;
453 my $location = $query->param('location');
454 my $itemtype = $query->param('itemtype');
455 my $previousitemtype = $query->param('previousitemtype');
456 my $skip_serialseq = $query->param('skip_serialseq');
459 if ( $query->param('mana_id') ne "" ) {
460 $mana_id = $query->param('mana_id');
461 Koha
::SharedContent
::increment_entity_value
("subscription",$mana_id, "nbofusers");
468 if(!defined $enddate || $enddate eq '') {
469 if($subtype eq "issues") {
470 $enddate = _guess_enddate
($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
472 $enddate = _guess_enddate
($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
476 my $nextexpected = GetNextExpected
($subscriptionid);
477 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
478 if ( $nextexpected->{planneddate
} && $nextacquidate ne $nextexpected->{planneddate
} ) {
479 ModNextExpected
($subscriptionid, $nextacquidate);
480 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
481 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue
});
485 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
486 $periodicity, $firstacquidate, join(";",@irregularity),
487 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
488 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
489 $status, $biblionumber, $callnumber, $notes, $letter,
490 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
491 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
492 $skip_serialseq, $itemtype, $previousitemtype, $mana_id
495 my @additional_fields;
496 my $record = GetMarcBiblio
({ biblionumber
=> $biblionumber, embed_items
=> 1 });
497 my $subscription_fields = Koha
::AdditionalFields
->search({ tablename
=> 'subscription' });
498 while ( my $field = $subscription_fields->next ) {
499 my $value = $query->param('additional_field_' . $field->id);
500 if ($field->marcfield) {
501 my ($field, $subfield) = split /\$/, $field->marcfield;
502 if ( $record and $field and $subfield ) {
503 $value = $record->subfield( $field, $subfield );
506 push @additional_fields, {
511 Koha
::Subscriptions
->find($subscriptionid)->set_additional_fields(\
@additional_fields);
513 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");