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);
26 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Branch; # GetBranches
32 use C4::Serials::Frequency;
33 use C4::Serials::Numberpattern;
35 use Koha::AdditionalField;
40 our $query = CGI->new;
41 my $op = $query->param('op') || '';
42 my $dbh = C4::Context->dbh;
47 # Permission needed if it is a modification : edit_subscription
48 # Permission needed otherwise (nothing or dup) : create_subscription
49 my $permission = ($op eq "modify") ? "edit_subscription" : "create_subscription";
51 my ($template, $loggedinuser, $cookie)
52 = get_template_and_user({template_name => "serials/subscription-add.tt",
56 flagsrequired => {serials => $permission},
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') {
84 $subs->{$_} = $subs->{$_};
87 if (!defined $subs->{letter}) {
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};
102 $template->param( %{$subs} );
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 );
131 C4::Context->preference('IndependentBranches')
132 && C4::Context->userenv
133 && !C4::Context->IsSuperLibrarian
135 not C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'superserials' } )
137 && C4::Context->userenv->{branch};
138 my $branches = GetBranches($onlymine);
140 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
142 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
143 push @{$branchloop}, {
144 value => $thisbranch,
145 selected => $selected,
146 branchname => $branches->{$thisbranch}->{'branchname'},
150 my $locations_loop = GetAuthorisedValues("LOC",$subs->{'location'});
152 $template->param(branchloop => $branchloop,
153 locations_loop=>$locations_loop,
157 my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
158 for my $field ( @$additional_fields ) {
159 if ( $field->{authorised_value_category} ) {
160 $field->{authorised_value_choices} = GetAuthorisedValues( $field->{authorised_value_category} );
163 $template->param( additional_fields_for_subscription => $additional_fields );
165 # prepare template variables common to all $op conditions:
167 my $letters = get_letter_loop();
168 $template->param( letterloop => $letters );
171 if ($op eq 'addsubscription') {
172 redirect_add_subscription();
173 } elsif ($op eq 'modsubscription') {
174 redirect_mod_subscription();
178 subtypes => [ qw( numberlength weeklength monthlength ) ],
182 if ( $op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify' ) {
183 my $letters = get_letter_loop();
184 $template->param( letterloop => $letters );
187 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
188 if (defined $new_biblionumber) {
189 my $bib = GetBiblioData($new_biblionumber);
191 $template->param(bibnum => $new_biblionumber);
192 $template->param(bibliotitle => $bib->{title});
196 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
198 my @frequencies = GetSubscriptionFrequencies;
200 foreach my $freq (@frequencies) {
202 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
205 selected => $selected,
206 label => $freq->{'description'},
210 $template->param(frequencies => \@frqloop);
212 my @numpatterns = GetSubscriptionNumberpatterns;
213 my @numberpatternloop;
214 foreach my $numpattern (@numpatterns) {
216 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
218 id => $numpattern->{'id'},
219 selected => $selected,
220 label => $numpattern->{'label'},
222 push @numberpatternloop, $row;
224 $template->param(numberpatterns => \@numberpatternloop);
226 my $languages = [ map {
228 language => $_->{iso639_2_code},
229 description => $_->{language_description} || $_->{language}
231 } @{ C4::Languages::getAllLanguages() } ];
233 $template->param( locales => $languages );
235 output_html_with_http_headers $query, $cookie, $template->output;
238 sub get_letter_loop {
239 my ($selected_lettercode) = @_;
240 my $letters = GetLetters({ module => 'serial' });
245 lettername => $_->{name},
246 ( $_->{code} eq $selected_lettercode ? ( selected => 1 ) : () ),
252 sub _get_sub_length {
253 my ($type, $length) = @_;
256 $type eq 'issues' ? $length : 0,
257 $type eq 'weeks' ? $length : 0,
258 $type eq 'months' ? $length : 0,
263 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
264 my ($year, $month, $day);
266 if($numberlength != 0) {
267 my $frequency = GetSubscriptionFrequency($frequencyid);
268 if($frequency->{'unit'} eq 'day') {
269 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
270 } elsif($frequency->{'unit'} eq 'week') {
271 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
272 } elsif($frequency->{'unit'} eq 'month') {
273 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
274 } elsif($frequency->{'unit'} eq 'year') {
275 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
277 } elsif($weeklength != 0) {
278 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
279 } elsif($monthlength != 0) {
280 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
283 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
290 sub redirect_add_subscription {
291 my $auser = $query->param('user');
292 my $branchcode = $query->param('branchcode');
293 my $aqbooksellerid = $query->param('aqbooksellerid');
294 my $cost = $query->param('cost');
295 my $aqbudgetid = $query->param('aqbudgetid');
296 my $periodicity = $query->param('frequency');
297 my @irregularity = $query->param('irregularity');
298 my $numberpattern = $query->param('numbering_pattern');
299 my $locale = $query->param('locale');
300 my $graceperiod = $query->param('graceperiod') || 0;
302 my $subtype = $query->param('subtype');
303 my $sublength = $query->param('sublength');
304 my ( $numberlength, $weeklength, $monthlength )
305 = _get_sub_length( $subtype, $sublength );
306 my $add1 = $query->param('add1');
307 my $lastvalue1 = $query->param('lastvalue1');
308 my $innerloop1 = $query->param('innerloop1');
309 my $innerloop2 = $query->param('innerloop2');
310 my $lastvalue2 = $query->param('lastvalue2');
311 my $lastvalue3 = $query->param('lastvalue3');
312 my $innerloop3 = $query->param('innerloop3');
314 my $biblionumber = $query->param('biblionumber');
315 my $callnumber = $query->param('callnumber');
316 my $notes = $query->param('notes');
317 my $internalnotes = $query->param('internalnotes');
318 my $letter = $query->param('letter');
319 my $manualhistory = $query->param('manualhist') ? 1 : 0;
320 my $serialsadditems = $query->param('serialsadditems');
321 my $staffdisplaycount = $query->param('staffdisplaycount');
322 my $opacdisplaycount = $query->param('opacdisplaycount');
323 my $location = $query->param('location');
324 my $skip_serialseq = $query->param('skip_serialseq');
325 my $startdate = format_date_in_iso( $query->param('startdate') );
326 my $enddate = format_date_in_iso( $query->param('enddate') );
327 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
328 if(!defined $enddate || $enddate eq '') {
329 if($subtype eq "issues") {
330 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
332 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
336 my $subscriptionid = NewSubscription(
337 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
338 $startdate, $periodicity, $numberlength, $weeklength,
339 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
340 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
341 join(";",@irregularity), $numberpattern, $locale, $callnumber,
342 $manualhistory, $internalnotes, $serialsadditems,
343 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
347 my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
348 my @additional_field_values;
349 for my $field ( @$additional_fields ) {
350 my $af = Koha::AdditionalField->new({ id => $field->{id} });
352 $subscriptionid => $query->param('additional_fields_' . $field->{name})
357 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
361 sub redirect_mod_subscription {
362 my $subscriptionid = $query->param('subscriptionid');
363 my @irregularity = $query->param('irregularity');
364 my $auser = $query->param('user');
365 my $librarian => $query->param('librarian'),
366 my $branchcode = $query->param('branchcode');
367 my $cost = $query->param('cost');
368 my $aqbooksellerid = $query->param('aqbooksellerid');
369 my $biblionumber = $query->param('biblionumber');
370 my $aqbudgetid = $query->param('aqbudgetid');
371 my $startdate = format_date_in_iso($query->param('startdate'));
372 my $firstacquidate = format_date_in_iso( $query->param('firstacquidate') );
373 my $nextacquidate = $query->param('nextacquidate') ?
374 format_date_in_iso($query->param('nextacquidate')):
376 my $enddate = format_date_in_iso($query->param('enddate'));
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');
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 $skip_serialseq = $query->param('skip_serialseq');
405 if(!defined $enddate || $enddate eq '') {
406 if($subtype eq "issues") {
407 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
409 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
413 my $nextexpected = GetNextExpected($subscriptionid);
414 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
415 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
416 ModNextExpected($subscriptionid, $nextacquidate);
417 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
418 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
422 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
423 $periodicity, $firstacquidate, join(";",@irregularity),
424 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
425 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
426 $status, $biblionumber, $callnumber, $notes, $letter,
427 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
428 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
432 my $additional_fields = Koha::AdditionalField->all( { table => 'subscription' } );
433 my @additional_field_values;
434 for my $field ( @$additional_fields ) {
435 my $af = Koha::AdditionalField->new({ id => $field->{id} })->fetch;
436 if ( $af->{marcfield} ) {
437 my $record = GetMarcBiblio( $biblionumber, 1 );
438 my ( $field, $subfield ) = split /\$/, $af->{marcfield};
439 next unless $field and $subfield;
440 my $value = $record->subfield( $field, $subfield );
442 $subscriptionid => $value
446 $subscriptionid => $query->param('additional_fields_' . $field->{name})
452 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");