Bug 10951 - Tweaked opac-auth.tt sentence.
[koha.git] / serials / subscription-add.pl
blobf60910281772575df36a3582a783d3873a708bb4
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 use strict;
19 use warnings;
21 use CGI;
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::Dates qw/format_date format_date_in_iso/;
27 use C4::Acquisition;
28 use C4::Output;
29 use C4::Context;
30 use C4::Branch; # GetBranches
31 use C4::Serials;
32 use C4::Serials::Frequency;
33 use C4::Serials::Numberpattern;
34 use C4::Letters;
35 use Carp;
37 #use Smart::Comments;
39 our $query = CGI->new;
40 my $op = $query->param('op') || '';
41 my $dbh = C4::Context->dbh;
42 my $sub_length;
44 my @budgets;
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",
52 query => $query,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => {serials => $permission},
56 debug => 1,
57 });
61 my $sub_on;
62 my @subscription_types = (qw(issues weeks months));
63 my @sub_type_data;
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 letter_loop($subs->{'letter'}, $template);
92 my $nextexpected = GetNextExpected($subscriptionid);
93 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate} eq $firstissuedate ;
94 $subs->{nextacquidate} = $nextexpected->{planneddate} if($op eq 'modify');
95 unless($op eq 'modsubscription') {
96 foreach my $length_unit (qw(numberlength weeklength monthlength)) {
97 if ($subs->{$length_unit}) {
98 $sub_length=$subs->{$length_unit};
99 $sub_on=$length_unit;
100 last;
104 $template->param( %{$subs} );
105 $template->param(
106 $op => 1,
107 "subtype_$sub_on" => 1,
108 sublength =>$sub_length,
109 history => ($op eq 'modify'),
110 firstacquiyear => substr($firstissuedate,0,4),
113 if($op eq 'modify') {
114 my ($serials_number) = GetSerials($subscriptionid);
115 if($serials_number > 1) {
116 $template->param(more_than_one_serial => 1);
121 if ( $op eq 'dup' ) {
122 my $dont_copy_fields = C4::Context->preference('SubscriptionDuplicateDroppedInput');
123 my @fields_id = map { fieldid => $_ }, split '\|', $dont_copy_fields;
124 $template->param( dont_export_field_loop => \@fields_id );
128 my $onlymine =
129 C4::Context->preference('IndependentBranches')
130 && C4::Context->userenv
131 && !C4::Context->IsSuperLibrarian
132 && (
133 not C4::Auth::haspermission( C4::Context->userenv->{id}, { serials => 'superserials' } )
135 && C4::Context->userenv->{branch};
136 my $branches = GetBranches($onlymine);
137 my $branchloop;
138 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
139 my $selected = 0;
140 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
141 push @{$branchloop}, {
142 value => $thisbranch,
143 selected => $selected,
144 branchname => $branches->{$thisbranch}->{'branchname'},
148 my $locations_loop = GetAuthorisedValues("LOC",$subs->{'location'});
150 $template->param(branchloop => $branchloop,
151 locations_loop=>$locations_loop,
153 # prepare template variables common to all $op conditions:
154 if ($op!~/^mod/) {
155 letter_loop(q{}, $template);
158 if ($op eq 'addsubscription') {
159 redirect_add_subscription();
160 } elsif ($op eq 'modsubscription') {
161 redirect_mod_subscription();
162 } else {
163 while (@subscription_types) {
164 my $sub_type = shift @subscription_types;
165 my %row = ( 'name' => $sub_type );
166 if ( defined $sub_on and $sub_on eq $sub_type ) {
167 $row{'selected'} = ' selected';
168 } else {
169 $row{'selected'} = '';
171 push( @sub_type_data, \%row );
173 $template->param(subtype => \@sub_type_data);
175 letter_loop( '', $template ) if ($op ne 'modsubscription' && $op ne 'dup' && $op ne 'modify');
177 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
178 if (defined $new_biblionumber) {
179 my $bib = GetBiblioData($new_biblionumber);
180 if (defined $bib) {
181 $template->param(bibnum => $new_biblionumber);
182 $template->param(bibliotitle => $bib->{title});
186 $template->param((uc(C4::Context->preference("marcflavour"))) => 1);
188 my @frequencies = GetSubscriptionFrequencies;
189 my @frqloop;
190 foreach my $freq (@frequencies) {
191 my $selected = 0;
192 $selected = 1 if ($subs->{periodicity} and $freq->{id} eq $subs->{periodicity});
193 my $row = {
194 id => $freq->{'id'},
195 selected => $selected,
196 label => $freq->{'description'},
198 push @frqloop, $row;
200 $template->param(frequencies => \@frqloop);
202 my @numpatterns = GetSubscriptionNumberpatterns;
203 my @numberpatternloop;
204 foreach my $numpattern (@numpatterns) {
205 my $selected = 0;
206 $selected = 1 if($subs->{numberpattern} and $numpattern->{id} eq $subs->{numberpattern});
207 my $row = {
208 id => $numpattern->{'id'},
209 selected => $selected,
210 label => $numpattern->{'label'},
212 push @numberpatternloop, $row;
214 $template->param(numberpatterns => \@numberpatternloop);
216 # Get installed locales
217 # FIXME this will not work with all environments.
218 # If call to locale fails, @locales will be an empty array, which is fine.
219 my @locales = map {
220 chomp;
221 # we don't want POSIX and C locales
222 /^C|^POSIX$/ ? () : $_
223 } `locale -a`;
224 $template->param(locales => \@locales);
226 output_html_with_http_headers $query, $cookie, $template->output;
229 sub letter_loop {
230 my ($selected_letter, $templte) = @_;
231 my $letters = GetLetters('serial');
232 my $letterloop;
233 foreach my $thisletter (keys %{$letters}) {
234 push @{$letterloop}, {
235 value => $thisletter,
236 selected => $thisletter eq $selected_letter,
237 lettername => $letters->{$thisletter},
240 $templte->param(letterloop => $letterloop);
241 return;
244 sub _get_sub_length {
245 my ($type, $length) = @_;
246 return
248 $type eq 'issues' ? $length : 0,
249 $type eq 'weeks' ? $length : 0,
250 $type eq 'months' ? $length : 0,
254 sub _guess_enddate {
255 my ($startdate_iso, $frequencyid, $numberlength, $weeklength, $monthlength) = @_;
256 my ($year, $month, $day);
257 my $enddate;
258 if($numberlength != 0) {
259 my $frequency = GetSubscriptionFrequency($frequencyid);
260 if($frequency->{'unit'} eq 'day') {
261 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
262 } elsif($frequency->{'unit'} eq 'week') {
263 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $numberlength * 7 * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
264 } elsif($frequency->{'unit'} eq 'month') {
265 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'});
266 } elsif($frequency->{'unit'} eq 'year') {
267 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), $numberlength * $frequency->{'unitsperissue'} / $frequency->{'issuesperunit'}, 0);
269 } elsif($weeklength != 0) {
270 ($year, $month, $day) = Add_Delta_Days(split(/-/, $startdate_iso), $weeklength * 7);
271 } elsif($monthlength != 0) {
272 ($year, $month, $day) = Add_Delta_YM(split(/-/, $startdate_iso), 0, $monthlength);
274 if(defined $year) {
275 $enddate = sprintf("%04d-%02d-%02d", $year, $month, $day);
276 } else {
277 undef $enddate;
279 return $enddate;
282 sub redirect_add_subscription {
283 my $auser = $query->param('user');
284 my $branchcode = $query->param('branchcode');
285 my $aqbooksellerid = $query->param('aqbooksellerid');
286 my $cost = $query->param('cost');
287 my $aqbudgetid = $query->param('aqbudgetid');
288 my $periodicity = $query->param('frequency');
289 my @irregularity = $query->param('irregularity');
290 my $numberpattern = $query->param('numbering_pattern');
291 my $locale = $query->param('locale');
292 my $graceperiod = $query->param('graceperiod') || 0;
294 my $subtype = $query->param('subtype');
295 my $sublength = $query->param('sublength');
296 my ( $numberlength, $weeklength, $monthlength )
297 = _get_sub_length( $subtype, $sublength );
298 my $add1 = $query->param('add1');
299 my $lastvalue1 = $query->param('lastvalue1');
300 my $innerloop1 = $query->param('innerloop1');
301 my $innerloop2 = $query->param('innerloop2');
302 my $lastvalue2 = $query->param('lastvalue2');
303 my $lastvalue3 = $query->param('lastvalue3');
304 my $innerloop3 = $query->param('innerloop3');
305 my $status = 1;
306 my $biblionumber = $query->param('biblionumber');
307 my $callnumber = $query->param('callnumber');
308 my $notes = $query->param('notes');
309 my $internalnotes = $query->param('internalnotes');
310 my $letter = $query->param('letter');
311 my $manualhistory = $query->param('manualhist') ? 1 : 0;
312 my $serialsadditems = $query->param('serialsadditems');
313 my $staffdisplaycount = $query->param('staffdisplaycount');
314 my $opacdisplaycount = $query->param('opacdisplaycount');
315 my $location = $query->param('location');
316 my $skip_serialseq = $query->param('skip_serialseq');
317 my $startdate = format_date_in_iso( $query->param('startdate') );
318 my $enddate = format_date_in_iso( $query->param('enddate') );
319 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
320 if(!defined $enddate || $enddate eq '') {
321 if($subtype eq "issues") {
322 $enddate = _guess_enddate($firstacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
323 } else {
324 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
328 my $subscriptionid = NewSubscription(
329 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $biblionumber,
330 $startdate, $periodicity, $numberlength, $weeklength,
331 $monthlength, $lastvalue1, $innerloop1, $lastvalue2, $innerloop2,
332 $lastvalue3, $innerloop3, $status, $notes, $letter, $firstacquidate,
333 join(";",@irregularity), $numberpattern, $locale, $callnumber,
334 $manualhistory, $internalnotes, $serialsadditems,
335 $staffdisplaycount, $opacdisplaycount, $graceperiod, $location, $enddate,
336 $skip_serialseq
339 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
340 return;
343 sub redirect_mod_subscription {
344 my $subscriptionid = $query->param('subscriptionid');
345 my @irregularity = $query->param('irregularity');
346 my $auser = $query->param('user');
347 my $librarian => $query->param('librarian'),
348 my $branchcode = $query->param('branchcode');
349 my $cost = $query->param('cost');
350 my $aqbooksellerid = $query->param('aqbooksellerid');
351 my $biblionumber = $query->param('biblionumber');
352 my $aqbudgetid = $query->param('aqbudgetid');
353 my $startdate = format_date_in_iso($query->param('startdate'));
354 my $firstacquidate = format_date_in_iso( $query->param('firstacquidate') );
355 my $nextacquidate = $query->param('nextacquidate') ?
356 format_date_in_iso($query->param('nextacquidate')):
357 $firstacquidate;
358 my $enddate = format_date_in_iso($query->param('enddate'));
359 my $periodicity = $query->param('frequency');
361 my $subtype = $query->param('subtype');
362 my $sublength = $query->param('sublength');
363 my ($numberlength, $weeklength, $monthlength)
364 = _get_sub_length( $subtype, $sublength );
365 my $numberpattern = $query->param('numbering_pattern');
366 my $locale = $query->param('locale');
367 my $lastvalue1 = $query->param('lastvalue1');
368 my $innerloop1 = $query->param('innerloop1');
369 my $lastvalue2 = $query->param('lastvalue2');
370 my $innerloop2 = $query->param('innerloop2');
371 my $lastvalue3 = $query->param('lastvalue3');
372 my $innerloop3 = $query->param('innerloop3');
373 my $status = 1;
374 my $callnumber = $query->param('callnumber');
375 my $notes = $query->param('notes');
376 my $internalnotes = $query->param('internalnotes');
377 my $letter = $query->param('letter');
378 my $manualhistory = $query->param('manualhist') ? 1 : 0;
379 my $serialsadditems = $query->param('serialsadditems');
380 my $staffdisplaycount = $query->param('staffdisplaycount');
381 my $opacdisplaycount = $query->param('opacdisplaycount');
382 my $graceperiod = $query->param('graceperiod') || 0;
383 my $location = $query->param('location');
384 my $skip_serialseq = $query->param('skip_serialseq');
386 # Guess end date
387 if(!defined $enddate || $enddate eq '') {
388 if($subtype eq "issues") {
389 $enddate = _guess_enddate($nextacquidate, $periodicity, $numberlength, $weeklength, $monthlength);
390 } else {
391 $enddate = _guess_enddate($startdate, $periodicity, $numberlength, $weeklength, $monthlength);
395 my $nextexpected = GetNextExpected($subscriptionid);
396 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
397 if ( $nextexpected->{planneddate} && $nextacquidate ne $nextexpected->{planneddate} ) {
398 ModNextExpected($subscriptionid, $nextacquidate);
399 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
400 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
403 ModSubscription(
404 $auser, $branchcode, $aqbooksellerid, $cost, $aqbudgetid, $startdate,
405 $periodicity, $firstacquidate, join(";",@irregularity),
406 $numberpattern, $locale, $numberlength, $weeklength, $monthlength, $lastvalue1,
407 $innerloop1, $lastvalue2, $innerloop2, $lastvalue3, $innerloop3,
408 $status, $biblionumber, $callnumber, $notes, $letter,
409 $manualhistory, $internalnotes, $serialsadditems, $staffdisplaycount,
410 $opacdisplaycount, $graceperiod, $location, $enddate, $subscriptionid,
411 $skip_serialseq
414 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
415 return;