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
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.
22 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
26 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Branch; # GetBranches
38 my $op = $query->param('op') || '';
39 my $dbh = C4::Context->dbh;
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
47 flagsrequired => {serials => 1},
54 my @subscription_types = (
55 'issues', 'weeks', 'months'
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') {
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};
96 $template->param($subs);
97 $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
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);
116 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
118 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
119 push @{$branchloop}, {
120 value => $thisbranch,
121 selected => $selected,
122 branchname => $branches->{$thisbranch}->{'branchname'},
125 $template->param(branchloop => $branchloop,
126 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
129 # prepare template variables common to all $op conditions:
130 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 ,
133 if ($op eq 'addsubscription') {
134 redirect_add_subscription();
135 } elsif ($op eq 'modsubscription') {
136 redirect_mod_subscription();
138 while (@subscription_types) {
139 my $sub_type = shift @subscription_types;
140 my %row = ( 'name' => $sub_type );
141 if ( defined $sub_on and $sub_on eq $sub_type ) {
142 $row{'selected'} = ' selected';
144 $row{'selected'} = '';
146 push( @sub_type_data, \%row );
148 $template->param(subtype => \@sub_type_data,
151 letter_loop('', $template);
153 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
154 if (defined $new_biblionumber) {
155 my $bib = GetBiblioData($new_biblionumber);
157 $template->param(bibnum => $new_biblionumber);
158 $template->param(bibliotitle => $bib->{title});
161 output_html_with_http_headers $query, $cookie, $template->output;
165 my ($selected_letter, $templte) = @_;
166 my $letters = GetLetters('serial');
168 foreach my $thisletter (keys %$letters) {
169 my $selected = $thisletter eq $selected_letter ? 1 : 0;
171 value => $thisletter,
172 selected => $selected,
173 lettername => $letters->{$thisletter},
176 $templte->param(letterloop => \@letterloop) if @letterloop;
180 sub _get_sub_length {
181 my ($type, $length) = @_;
184 $type eq 'numberlength' ? $length : 0,
185 $type eq 'weeklength' ? $length : 0,
186 $type eq 'monthlength' ? $length : 0,
190 sub redirect_add_subscription {
191 my $auser = $query->param('user');
192 my $branchcode = $query->param('branchcode');
193 my $aqbooksellerid = $query->param('aqbooksellerid');
194 my $cost = $query->param('cost');
195 my $aqbudgetid = $query->param('aqbudgetid');
196 my $periodicity = $query->param('periodicity');
197 my $dow = $query->param('dow');
198 my @irregularity = $query->param('irregularity_select');
199 my $numberpattern = $query->param('numbering_pattern');
200 my $graceperiod = $query->param('graceperiod') || 0;
202 my ($numberlength, $weeklength, $monthlength)
203 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
204 my $add1 = $query->param('add1');
205 my $every1 = $query->param('every1');
206 my $whenmorethan1 = $query->param('whenmorethan1');
207 my $setto1 = $query->param('setto1');
208 my $lastvalue1 = $query->param('lastvalue1');
209 my $innerloop1 =$query->param('innerloop1');
210 my $add2 = $query->param('add2');
211 my $every2 = $query->param('every2');
212 my $whenmorethan2 = $query->param('whenmorethan2');
213 my $setto2 = $query->param('setto2');
214 my $innerloop2 =$query->param('innerloop2');
215 my $lastvalue2 = $query->param('lastvalue2');
216 my $add3 = $query->param('add3');
217 my $every3 = $query->param('every3');
218 my $whenmorethan3 = $query->param('whenmorethan3');
219 my $setto3 = $query->param('setto3');
220 my $lastvalue3 = $query->param('lastvalue3');
221 my $innerloop3 =$query->param('innerloop3');
222 my $numberingmethod = $query->param('numberingmethod');
224 my $biblionumber = $query->param('biblionumber');
225 my $callnumber = $query->param('callnumber');
226 my $notes = $query->param('notes');
227 my $internalnotes = $query->param('internalnotes');
228 my $hemisphere = $query->param('hemisphere') || 1;
229 my $letter = $query->param('letter');
230 my $manualhistory = $query->param('manualhist');
231 my $serialsadditems = $query->param('serialsadditems');
232 my $staffdisplaycount = $query->param('staffdisplaycount');
233 my $opacdisplaycount = $query->param('opacdisplaycount');
234 my $location = $query->param('location');
235 my $startdate = format_date_in_iso($query->param('startdate'));
236 my $enddate = format_date_in_iso($query->param('enddate'));
237 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
238 my $histenddate = format_date_in_iso($query->param('histenddate'));
239 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
240 my $recievedlist = $query->param('recievedlist');
241 my $missinglist = $query->param('missinglist');
242 my $opacnote = $query->param('opacnote');
243 my $librariannote = $query->param('librariannote');
244 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
245 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
246 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
247 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
248 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
249 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
250 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
251 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
253 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
255 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
259 sub redirect_mod_subscription {
260 my $subscriptionid = $query->param('subscriptionid');
261 my @irregularity = $query->param('irregularity_select');
262 my $auser = $query->param('user');
263 my $librarian => $query->param('librarian'),
264 my $branchcode = $query->param('branchcode');
265 my $cost = $query->param('cost');
266 my $aqbooksellerid = $query->param('aqbooksellerid');
267 my $biblionumber = $query->param('biblionumber');
268 my $aqbudgetid = $query->param('aqbudgetid');
269 my $startdate = format_date_in_iso($query->param('startdate'));
270 my $nextacquidate = $query->param('nextacquidate') ?
271 format_date_in_iso($query->param('nextacquidate')):
272 format_date_in_iso($query->param('startdate'));
273 my $enddate = format_date_in_iso($query->param('enddate'));
274 my $periodicity = $query->param('periodicity');
275 my $dow = $query->param('dow');
277 my ($numberlength, $weeklength, $monthlength)
278 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
279 my $numberpattern = $query->param('numbering_pattern');
280 my $add1 = $query->param('add1');
281 my $every1 = $query->param('every1');
282 my $whenmorethan1 = $query->param('whenmorethan1');
283 my $setto1 = $query->param('setto1');
284 my $lastvalue1 = $query->param('lastvalue1');
285 my $innerloop1 = $query->param('innerloop1');
286 my $add2 = $query->param('add2');
287 my $every2 = $query->param('every2');
288 my $whenmorethan2 = $query->param('whenmorethan2');
289 my $setto2 = $query->param('setto2');
290 my $lastvalue2 = $query->param('lastvalue2');
291 my $innerloop2 = $query->param('innerloop2');
292 my $add3 = $query->param('add3');
293 my $every3 = $query->param('every3');
294 my $whenmorethan3 = $query->param('whenmorethan3');
295 my $setto3 = $query->param('setto3');
296 my $lastvalue3 = $query->param('lastvalue3');
297 my $innerloop3 = $query->param('innerloop3');
298 my $numberingmethod = $query->param('numberingmethod');
300 my $callnumber = $query->param('callnumber');
301 my $notes = $query->param('notes');
302 my $internalnotes = $query->param('internalnotes');
303 my $hemisphere = $query->param('hemisphere');
304 my $letter = $query->param('letter');
305 my $manualhistory = $query->param('manualhist');
306 my $serialsadditems = $query->param('serialsadditems');
307 # subscription history
308 my $histenddate = format_date_in_iso($query->param('histenddate'));
309 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
310 my $recievedlist = $query->param('recievedlist');
311 my $missinglist = $query->param('missinglist');
312 my $opacnote = $query->param('opacnote');
313 my $librariannote = $query->param('librariannote');
314 my $staffdisplaycount = $query->param('staffdisplaycount');
315 my $opacdisplaycount = $query->param('opacdisplaycount');
316 my $graceperiod = $query->param('graceperiod') || 0;
317 my $location = $query->param('location');
318 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
319 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
320 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
321 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
322 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
326 $auser, $branchcode, $aqbooksellerid, $cost,
327 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
328 $dow, join(q{,},@irregularity), $numberpattern, $numberlength,
329 $weeklength, $monthlength, $add1, $every1,
330 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
331 $add2, $every2, $whenmorethan2, $setto2,
332 $lastvalue2, $innerloop2, $add3, $every3,
333 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
334 $numberingmethod, $status, $biblionumber, $callnumber,
335 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
336 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
338 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
339 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");