Bug 4085 - Subscription type length wasn't taken into account
[koha.git] / serials / subscription-add.pl
blobf7f23e76f4dd464462748f2445ab55d43a3faf22
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 with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 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);
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::Letters;
33 use Carp;
35 #use Smart::Comments;
37 my $query = CGI->new;
38 my $op = $query->param('op') || '';
39 my $dbh = C4::Context->dbh;
40 my $sub_length;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
44 query => $query,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => {serials => 1},
48 debug => 1,
49 });
53 my $sub_on;
54 my @subscription_types = (
55 'issues', 'weeks', 'months'
57 my @sub_type_data;
59 my $subs;
60 my $firstissuedate;
61 my $nextexpected;
63 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
65 my $subscriptionid = $query->param('subscriptionid');
66 $subs = GetSubscription($subscriptionid);
67 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
68 if ($subs->{'cannotedit'} && $op eq 'mod'){
69 carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
70 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
72 $firstissuedate = $subs->{firstacquidate}; # in iso format.
73 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
74 next unless defined $subs->{$_};
75 # TODO : Handle date formats properly.
76 if ($subs->{$_} eq '0000-00-00') {
77 $subs->{$_} = ''
78 } else {
79 $subs->{$_} = format_date($subs->{$_});
82 $subs->{'letter'}='' unless($subs->{'letter'});
83 letter_loop($subs->{'letter'}, $template);
84 $nextexpected = GetNextExpected($subscriptionid);
85 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
86 $subs->{nextacquidate} = $nextexpected->{planneddate}->output() if($op eq 'mod');
87 unless($op eq 'modsubscription') {
88 foreach my $length_unit qw(numberlength weeklength monthlength){
89 if ($subs->{$length_unit}){
90 $sub_length=$subs->{$length_unit};
91 $sub_on=$length_unit;
92 last;
96 $template->param($subs);
97 $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
98 $template->param(
99 $op => 1,
100 "subtype_$sub_on" => 1,
101 sublength =>$sub_length,
102 history => ($op eq 'mod'),
103 "periodicity".$subs->{'periodicity'} => 1,
104 "numberpattern".$subs->{'numberpattern'} => 1,
105 firstacquiyear => substr($firstissuedate,0,4),
110 my $onlymine=C4::Context->preference('IndependantBranches') &&
111 C4::Context->userenv &&
112 C4::Context->userenv->{flags} % 2 !=1 &&
113 C4::Context->userenv->{branch};
114 my $branches = GetBranches($onlymine);
115 my $branchloop;
116 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
117 my $selected = 0;
118 $selected = 1 if ($thisbranch eq C4::Context->userenv->{'branch'});
119 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
120 push @{$branchloop}, {
121 value => $thisbranch,
122 selected => $selected,
123 branchname => $branches->{$thisbranch}->{'branchname'},
126 $template->param(branchloop => $branchloop,
127 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
129 my $count = 0;
130 # prepare template variables common to all $op conditions:
131 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 ,
134 if ($op eq 'addsubscription') {
135 redirect_add_subscription();
136 } elsif ($op eq 'modsubscription') {
137 redirect_mod_subscription();
138 } else {
139 while (@subscription_types) {
140 my $sub_type = shift @subscription_types;
141 my %row = ( 'name' => $sub_type );
142 if ( defined $sub_on and $sub_on eq $sub_type ) {
143 $row{'selected'} = ' selected';
144 } else {
145 $row{'selected'} = '';
147 push( @sub_type_data, \%row );
149 $template->param(subtype => \@sub_type_data,
152 letter_loop('', $template);
154 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
155 if (defined $new_biblionumber) {
156 my $bib = GetBiblioData($new_biblionumber);
157 if (defined $bib) {
158 $template->param(bibnum => $new_biblionumber);
159 $template->param(bibliotitle => $bib->{title});
162 output_html_with_http_headers $query, $cookie, $template->output;
165 sub letter_loop {
166 my ($selected_letter, $templte) = @_;
167 my $letters = GetLetters('serial');
168 my @letterloop;
169 foreach my $thisletter (keys %$letters) {
170 my $selected = $thisletter eq $selected_letter ? 1 : 0;
171 push @letterloop, {
172 value => $thisletter,
173 selected => $selected,
174 lettername => $letters->{$thisletter},
177 $templte->param(letterloop => \@letterloop) if @letterloop;
178 return;
181 sub _get_sub_length {
182 my ($type, $length) = @_;
183 return
185 $type eq 'numberlength' ? $length : 0,
186 $type eq 'weeklength' ? $length : 0,
187 $type eq 'monthlength' ? $length : 0,
191 sub redirect_add_subscription {
192 my $auser = $query->param('user');
193 my $branchcode = $query->param('branchcode');
194 my $aqbooksellerid = $query->param('aqbooksellerid');
195 my $cost = $query->param('cost');
196 my $aqbudgetid = $query->param('aqbudgetid');
197 my $periodicity = $query->param('periodicity');
198 my $dow = $query->param('dow');
199 my @irregularity = $query->param('irregularity_select');
200 my $numberpattern = $query->param('numbering_pattern');
201 my $graceperiod = $query->param('graceperiod') || 0;
203 my ($numberlength, $weeklength, $monthlength)
204 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
205 my $add1 = $query->param('add1');
206 my $every1 = $query->param('every1');
207 my $whenmorethan1 = $query->param('whenmorethan1');
208 my $setto1 = $query->param('setto1');
209 my $lastvalue1 = $query->param('lastvalue1');
210 my $innerloop1 =$query->param('innerloop1');
211 my $add2 = $query->param('add2');
212 my $every2 = $query->param('every2');
213 my $whenmorethan2 = $query->param('whenmorethan2');
214 my $setto2 = $query->param('setto2');
215 my $innerloop2 =$query->param('innerloop2');
216 my $lastvalue2 = $query->param('lastvalue2');
217 my $add3 = $query->param('add3');
218 my $every3 = $query->param('every3');
219 my $whenmorethan3 = $query->param('whenmorethan3');
220 my $setto3 = $query->param('setto3');
221 my $lastvalue3 = $query->param('lastvalue3');
222 my $innerloop3 =$query->param('innerloop3');
223 my $numberingmethod = $query->param('numberingmethod');
224 my $status = 1;
225 my $biblionumber = $query->param('biblionumber');
226 my $callnumber = $query->param('callnumber');
227 my $notes = $query->param('notes');
228 my $internalnotes = $query->param('internalnotes');
229 my $hemisphere = $query->param('hemisphere') || 1;
230 my $letter = $query->param('letter');
231 my $manualhistory = $query->param('manualhist');
232 my $serialsadditems = $query->param('serialsadditems');
233 my $staffdisplaycount = $query->param('staffdisplaycount');
234 my $opacdisplaycount = $query->param('opacdisplaycount');
235 my $location = $query->param('location');
236 my $startdate = format_date_in_iso($query->param('startdate'));
237 my $enddate = format_date_in_iso($query->param('enddate'));
238 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
239 my $histenddate = format_date_in_iso($query->param('histenddate'));
240 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
241 my $recievedlist = $query->param('recievedlist');
242 my $missinglist = $query->param('missinglist');
243 my $opacnote = $query->param('opacnote');
244 my $librariannote = $query->param('librariannote');
245 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
246 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
247 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
248 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
249 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
250 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
251 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
252 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
254 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
256 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
257 return;
260 sub redirect_mod_subscription {
261 my $subscriptionid = $query->param('subscriptionid');
262 my @irregularity = $query->param('irregularity_select');
263 my $auser = $query->param('user');
264 my $librarian => $query->param('librarian'),
265 my $branchcode = $query->param('branchcode');
266 my $cost = $query->param('cost');
267 my $aqbooksellerid = $query->param('aqbooksellerid');
268 my $biblionumber = $query->param('biblionumber');
269 my $aqbudgetid = $query->param('aqbudgetid');
270 my $startdate = format_date_in_iso($query->param('startdate'));
271 my $nextacquidate = $query->param('nextacquidate') ?
272 format_date_in_iso($query->param('nextacquidate')):
273 format_date_in_iso($query->param('startdate'));
274 my $enddate = format_date_in_iso($query->param('enddate'));
275 my $periodicity = $query->param('periodicity');
276 my $dow = $query->param('dow');
278 my ($numberlength, $weeklength, $monthlength)
279 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
280 my $numberpattern = $query->param('numbering_pattern');
281 my $add1 = $query->param('add1');
282 my $every1 = $query->param('every1');
283 my $whenmorethan1 = $query->param('whenmorethan1');
284 my $setto1 = $query->param('setto1');
285 my $lastvalue1 = $query->param('lastvalue1');
286 my $innerloop1 = $query->param('innerloop1');
287 my $add2 = $query->param('add2');
288 my $every2 = $query->param('every2');
289 my $whenmorethan2 = $query->param('whenmorethan2');
290 my $setto2 = $query->param('setto2');
291 my $lastvalue2 = $query->param('lastvalue2');
292 my $innerloop2 = $query->param('innerloop2');
293 my $add3 = $query->param('add3');
294 my $every3 = $query->param('every3');
295 my $whenmorethan3 = $query->param('whenmorethan3');
296 my $setto3 = $query->param('setto3');
297 my $lastvalue3 = $query->param('lastvalue3');
298 my $innerloop3 = $query->param('innerloop3');
299 my $numberingmethod = $query->param('numberingmethod');
300 my $status = 1;
301 my $callnumber = $query->param('callnumber');
302 my $notes = $query->param('notes');
303 my $internalnotes = $query->param('internalnotes');
304 my $hemisphere = $query->param('hemisphere');
305 my $letter = $query->param('letter');
306 my $manualhistory = $query->param('manualhist');
307 my $serialsadditems = $query->param('serialsadditems');
308 # subscription history
309 my $histenddate = format_date_in_iso($query->param('histenddate'));
310 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
311 my $recievedlist = $query->param('recievedlist');
312 my $missinglist = $query->param('missinglist');
313 my $opacnote = $query->param('opacnote');
314 my $librariannote = $query->param('librariannote');
315 my $staffdisplaycount = $query->param('staffdisplaycount');
316 my $opacdisplaycount = $query->param('opacdisplaycount');
317 my $graceperiod = $query->param('graceperiod') || 0;
318 my $location = $query->param('location');
319 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
320 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
321 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
322 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
323 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
326 ModSubscription(
327 $auser, $branchcode, $aqbooksellerid, $cost,
328 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
329 $dow, join(q{,},@irregularity), $numberpattern, $numberlength,
330 $weeklength, $monthlength, $add1, $every1,
331 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
332 $add2, $every2, $whenmorethan2, $setto2,
333 $lastvalue2, $innerloop2, $add3, $every3,
334 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
335 $numberingmethod, $status, $biblionumber, $callnumber,
336 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
337 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
339 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
340 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
341 return;