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);
30 use C4
::Serials
::Frequency
;
31 use C4
::Serials
::Numberpattern
;
33 use Koha
::AdditionalField
;
39 our $query = CGI
->new;
40 my $op = $query->param('op') || '';
41 my $dbh = C4
::Context
->dbh;
45 # Permission needed if it is a modification : edit_subscription
46 # Permission needed otherwise (nothing or dup) : create_subscription
47 my $permission = ($op eq "modify") ?
"edit_subscription" : "create_subscription";
49 my ($template, $loggedinuser, $cookie)
50 = get_template_and_user
({template_name
=> "serials/subscription-add.tt",
54 flagsrequired
=> {serials
=> $permission},
65 if ($op eq 'modify' || $op eq 'dup' || $op eq 'modsubscription') {
67 my $subscriptionid = $query->param('subscriptionid');
68 $subs = GetSubscription
($subscriptionid);
70 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
71 if ($subs->{'cannotedit'} && $op eq 'modify'){
72 carp
"Attempt to modify subscription $subscriptionid by ".C4
::Context
->userenv->{'id'}." not allowed";
73 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
75 $firstissuedate = $subs->{firstacquidate
} || ''; # in iso format.
76 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
77 next unless defined $subs->{$_};
78 # TODO : Handle date formats properly.
79 if ($subs->{$_} eq '0000-00-00') {
82 $subs->{$_} = $subs->{$_};
85 if (!defined $subs->{letter
}) {
88 my $nextexpected = GetNextExpected
($subscriptionid);
89 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate
} eq $firstissuedate ;
90 $subs->{nextacquidate
} = $nextexpected->{planneddate
} if($op eq 'modify');
91 unless($op eq 'modsubscription') {
92 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
93 if ($subs->{$length_unit}) {
94 $sub_length=$subs->{$length_unit};
100 $template->param( %{$subs} );
103 "subtype_$sub_on" => 1,
104 sublength
=>$sub_length,
105 history
=> ($op eq 'modify'),
106 firstacquiyear
=> substr($firstissuedate,0,4),
109 if($op eq 'modify') {
110 my ($serials_number) = GetSerials
($subscriptionid);
111 if($serials_number > 1) {
112 $template->param(more_than_one_serial
=> 1);
117 if ( $op eq 'dup' ) {
118 my $dont_copy_fields = C4
::Context
->preference('SubscriptionDuplicateDroppedInput');
119 my @fields_id = map { fieldid
=> $_ }, split '\|', $dont_copy_fields;
120 $template->param( dont_export_field_loop
=> \
@fields_id );
123 my $letters = get_letter_loop
( $subs->{letter
} );
124 $template->param( letterloop
=> $letters );
128 my $locations_loop = GetAuthorisedValues
("LOC");
131 branchcode
=> $subs->{branchcode
},
132 locations_loop
=>$locations_loop,
136 my $additional_fields = Koha
::AdditionalField
->all( { tablename
=> 'subscription' } );
137 for my $field ( @
$additional_fields ) {
138 if ( $field->{authorised_value_category
} ) {
139 $field->{authorised_value_choices
} = GetAuthorisedValues
( $field->{authorised_value_category
} );
142 $template->param( additional_fields_for_subscription
=> $additional_fields );
144 # prepare template variables common to all $op conditions:
146 my $letters = get_letter_loop
();
147 $template->param( letterloop
=> $letters );
150 if ($op eq 'addsubscription') {
151 redirect_add_subscription
();
152 } elsif ($op eq 'modsubscription') {
153 redirect_mod_subscription
();
157 subtypes
=> [ qw( numberlength weeklength monthlength ) ],
161 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
162 my $letters = get_letter_loop
();
163 $template->param( letterloop
=> $letters );
166 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
167 if (defined $new_biblionumber) {
168 my $bib = GetBiblioData
($new_biblionumber);
170 $template->param(bibnum
=> $new_biblionumber);
171 $template->param(bibliotitle
=> $bib->{title
});
175 $template->param((uc(C4
::Context
->preference("marcflavour"))) => 1);
177 my @frequencies = GetSubscriptionFrequencies
;
179 foreach my $freq (@frequencies) {
181 $selected = 1 if ($subs->{periodicity
} and $freq->{id
} eq $subs->{periodicity
});
184 selected
=> $selected,
185 label
=> $freq->{'description'},
189 $template->param(frequencies
=> \
@frqloop);
191 my @numpatterns = GetSubscriptionNumberpatterns
;
192 my @numberpatternloop;
193 foreach my $numpattern (@numpatterns) {
195 $selected = 1 if($subs->{numberpattern
} and $numpattern->{id
} eq $subs->{numberpattern
});
197 id
=> $numpattern->{'id'},
198 selected
=> $selected,
199 label
=> $numpattern->{'label'},
201 push @numberpatternloop, $row;
203 $template->param(numberpatterns
=> \
@numberpatternloop);
205 my $languages = [ map {
207 language
=> $_->{iso639_2_code
},
208 description
=> $_->{language_description
} || $_->{language
}
210 } @
{ C4
::Languages
::getAllLanguages
() } ];
212 $template->param( locales
=> $languages );
214 output_html_with_http_headers
$query, $cookie, $template->output;
217 sub get_letter_loop
{
218 my ($selected_lettercode) = @_;
219 $selected_lettercode //= '';
220 my $letters = GetLetters
({ module
=> 'serial' });
225 lettername
=> $_->{name
},
226 ( $_->{code
} eq $selected_lettercode ?
( selected
=> 1 ) : () ),
232 sub _get_sub_length
{
233 my ($type, $length) = @_;
236 $type eq 'issues' ?
$length : 0,
237 $type eq 'weeks' ?
$length : 0,
238 $type eq 'months' ?
$length : 0,
243 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
244 my ($year, $month, $day);
246 if($numberlength != 0) {
247 my $frequency = GetSubscriptionFrequency
($frequencyid);
248 if($frequency->{'unit'} eq 'day') {
249 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
250 } elsif($frequency->{'unit'} eq 'week') {
251 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
252 } elsif($frequency->{'unit'} eq 'month') {
253 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
254 } elsif($frequency->{'unit'} eq 'year') {
255 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
257 } elsif($weeklength != 0) {
258 ($year, $month, $day) = Add_Delta_Days
(split(/-/, $startdate_iso), $weeklength * 7);
259 } elsif($monthlength != 0) {
260 ($year, $month, $day) = Add_Delta_YM
(split(/-/, $startdate_iso), 0, $monthlength);
263 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
270 sub redirect_add_subscription
{
271 my $auser = $query->param('user');
272 my $branchcode = $query->param('branchcode');
273 my $aqbooksellerid = $query->param('aqbooksellerid');
274 my $cost = $query->param('cost');
275 my $aqbudgetid = $query->param('aqbudgetid');
276 my $periodicity = $query->param('frequency');
277 my @irregularity = $query->multi_param('irregularity');
278 my $numberpattern = $query->param('numbering_pattern');
279 my $locale = $query->param('locale');
280 my $graceperiod = $query->param('graceperiod') || 0;
282 my $subtype = $query->param('subtype');
283 my $sublength = $query->param('sublength');
284 my ( $numberlength, $weeklength, $monthlength )
285 = _get_sub_length
( $subtype, $sublength );
286 my $add1 = $query->param('add1');
287 my $lastvalue1 = $query->param('lastvalue1');
288 my $innerloop1 = $query->param('innerloop1');
289 my $innerloop2 = $query->param('innerloop2');
290 my $lastvalue2 = $query->param('lastvalue2');
291 my $lastvalue3 = $query->param('lastvalue3');
292 my $innerloop3 = $query->param('innerloop3');
294 my $biblionumber = $query->param('biblionumber');
295 my $callnumber = $query->param('callnumber');
296 my $notes = $query->param('notes');
297 my $internalnotes = $query->param('internalnotes');
298 my $letter = $query->param('letter');
299 my $manualhistory = $query->param('manualhist') ?
1 : 0;
300 my $serialsadditems = $query->param('serialsadditems');
301 my $staffdisplaycount = $query->param('staffdisplaycount');
302 my $opacdisplaycount = $query->param('opacdisplaycount');
303 my $location = $query->param('location');
304 my $skip_serialseq = $query->param('skip_serialseq');
306 my $startdate = output_pref
( { str
=> scalar $query->param('startdate'), dateonly
=> 1, dateformat
=> 'iso' } );
307 my $enddate = output_pref
( { str
=> scalar $query->param('enddate'), dateonly
=> 1, dateformat
=> 'iso' } );
308 my $firstacquidate = output_pref
( { str
=> scalar $query->param('firstacquidate'), dateonly
=> 1, dateformat
=> 'iso' } );
310 if(!defined $enddate || $enddate eq '') {
311 if($subtype eq "issues") {
312 $enddate = _guess_enddate
($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength)
314 $enddate = _guess_enddate
($startdate, $periodicity, $numberlength, $weeklength, $monthlength)
318 my $subscriptionid = NewSubscription
(
319 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
320 $startdate, $periodicity, $numberlength, $weeklength,
321 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
322 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
323 join(";",@irregularity), $numberpattern, $locale, $callnumber,
324 $manualhistory, $internalnotes, $serialsadditems,
325 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
329 my $additional_fields = Koha
::AdditionalField
->all( { tablename
=> 'subscription' } );
330 insert_additional_fields
( $additional_fields, $biblionumber, $subscriptionid );
332 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
336 sub redirect_mod_subscription
{
337 my $subscriptionid = $query->param('subscriptionid');
338 my @irregularity = $query->multi_param('irregularity');
339 my $auser = $query->param('user');
340 my $librarian => scalar $query->param('librarian'),
341 my $branchcode = $query->param('branchcode');
342 my $cost = $query->param('cost');
343 my $aqbooksellerid = $query->param('aqbooksellerid');
344 my $biblionumber = $query->param('biblionumber');
345 my $aqbudgetid = $query->param('aqbudgetid');
347 my $startdate = output_pref
( { str
=> scalar $query->param('startdate'), dateonly
=> 1, dateformat
=> 'iso' } );
348 my $enddate = output_pref
( { str
=> scalar $query->param('enddate'), dateonly
=> 1, dateformat
=> 'iso' } );
349 my $firstacquidate = output_pref
( { str
=> scalar $query->param('firstacquidate'), dateonly
=> 1, dateformat
=> 'iso' } );
351 my $nextacquidate = $query->param('nextacquidate');
352 $nextacquidate = $nextacquidate
353 ? output_pref
( { str
=> $nextacquidate, dateonly
=> 1, dateformat
=> 'iso' } )
356 my $periodicity = $query->param('frequency');
358 my $subtype = $query->param('subtype');
359 my $sublength = $query->param('sublength');
360 my ($numberlength, $weeklength, $monthlength)
361 = _get_sub_length
( $subtype, $sublength );
362 my $numberpattern = $query->param('numbering_pattern');
363 my $locale = $query->param('locale');
364 my $lastvalue1 = $query->param('lastvalue1');
365 my $innerloop1 = $query->param('innerloop1');
366 my $lastvalue2 = $query->param('lastvalue2');
367 my $innerloop2 = $query->param('innerloop2');
368 my $lastvalue3 = $query->param('lastvalue3');
369 my $innerloop3 = $query->param('innerloop3');
371 my $callnumber = $query->param('callnumber');
372 my $notes = $query->param('notes');
373 my $internalnotes = $query->param('internalnotes');
374 my $letter = $query->param('letter');
375 my $manualhistory = $query->param('manualhist') ?
1 : 0;
376 my $serialsadditems = $query->param('serialsadditems');
377 my $staffdisplaycount = $query->param('staffdisplaycount');
378 my $opacdisplaycount = $query->param('opacdisplaycount');
379 my $graceperiod = $query->param('graceperiod') || 0;
380 my $location = $query->param('location');
381 my $skip_serialseq = $query->param('skip_serialseq');
384 if(!defined $enddate || $enddate eq '') {
385 if($subtype eq "issues") {
386 $enddate = _guess_enddate
($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
388 $enddate = _guess_enddate
($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
392 my $nextexpected = GetNextExpected
($subscriptionid);
393 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
394 if ( $nextexpected->{planneddate
} && $nextacquidate ne $nextexpected->{planneddate
} ) {
395 ModNextExpected
($subscriptionid, $nextacquidate);
396 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
397 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue
});
401 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
402 $periodicity, $firstacquidate, join(";",@irregularity),
403 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
404 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
405 $status, $biblionumber, $callnumber, $notes, $letter,
406 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
407 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
411 my $additional_fields = Koha
::AdditionalField
->all( { tablename
=> 'subscription' } );
412 insert_additional_fields
( $additional_fields, $biblionumber, $subscriptionid );
414 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
418 sub insert_additional_fields
{
419 my ( $additional_fields, $biblionumber, $subscriptionid ) = @_;
420 my $record = GetMarcBiblio
( $biblionumber, 1 );
421 for my $field ( @
$additional_fields ) {
422 my $af = Koha
::AdditionalField
->new({ id
=> $field->{id
} })->fetch;
423 if ( $af->{marcfield
} ) {
424 my ( $field, $subfield ) = split /\$/, $af->{marcfield
};
425 $af->{values} = undef;
426 if ( $field and $subfield ) {
427 my $value = $record->subfield( $field, $subfield );
429 $subscriptionid => $value
434 $subscriptionid => scalar $query->param('additional_field_' . $field->{id
})
435 } if defined $query->param('additional_field_' . $field->{id
});