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;
44 # Permission needed if it is a modification : edit_subscription
45 # Permission needed otherwise (nothing or dup) : create_subscription
46 my $permission = ($op eq "mod") ? "edit_subscription" : "create_subscription";
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
53 flagsrequired => {serials => $permission},
60 my @subscription_types = (
61 'issues', 'weeks', 'months'
69 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
71 my $subscriptionid = $query->param('subscriptionid');
72 $subs = GetSubscription($subscriptionid);
73 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
74 if ($subs->{'cannotedit'} && $op eq 'mod'){
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') {
85 $subs->{$_} = format_date($subs->{$_});
88 if (!defined $subs->{letter}) {
91 letter_loop($subs->{'letter'}, $template);
92 $nextexpected = GetNextExpected($subscriptionid);
93 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
94 $subs->{nextacquidate} = $nextexpected->{planneddate}->output() if($op eq 'mod');
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};
104 $template->param($subs);
105 $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
108 "subtype_$sub_on" => 1,
109 sublength =>$sub_length,
110 history => ($op eq 'mod'),
111 "periodicity".$subs->{'periodicity'} => 1,
112 "numberpattern".$subs->{'numberpattern'} => 1,
113 firstacquiyear => substr($firstissuedate,0,4),
118 my $onlymine=C4::Context->preference('IndependantBranches') &&
119 C4::Context->userenv &&
120 C4::Context->userenv->{flags} % 2 !=1 &&
121 C4::Context->userenv->{branch};
122 my $branches = GetBranches($onlymine);
124 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
126 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
127 push @{$branchloop}, {
128 value => $thisbranch,
129 selected => $selected,
130 branchname => $branches->{$thisbranch}->{'branchname'},
133 $template->param(branchloop => $branchloop,
134 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
136 # prepare template variables common to all $op conditions:
137 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 );
139 letter_loop(q{}, $template);
142 if ($op eq 'addsubscription') {
143 redirect_add_subscription();
144 } elsif ($op eq 'modsubscription') {
145 redirect_mod_subscription();
147 while (@subscription_types) {
148 my $sub_type = shift @subscription_types;
149 my %row = ( 'name' => $sub_type );
150 if ( defined $sub_on and $sub_on eq $sub_type ) {
151 $row{'selected'} = ' selected';
153 $row{'selected'} = '';
155 push( @sub_type_data, \%row );
157 $template->param(subtype => \@sub_type_data);
160 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
161 if (defined $new_biblionumber) {
162 my $bib = GetBiblioData($new_biblionumber);
164 $template->param(bibnum => $new_biblionumber);
165 $template->param(bibliotitle => $bib->{title});
168 output_html_with_http_headers $query, $cookie, $template->output;
172 my ($selected_letter, $templte) = @_;
173 my $letters = GetLetters('serial');
175 foreach my $thisletter (keys %{$letters}) {
176 push @{$letterloop}, {
177 value => $thisletter,
178 selected => $thisletter eq $selected_letter,
179 lettername => $letters->{$thisletter},
182 $templte->param(letterloop => $letterloop);
186 sub _get_sub_length {
187 my ($type, $length) = @_;
190 $type eq 'numberlength' ? $length : 0,
191 $type eq 'weeklength' ? $length : 0,
192 $type eq 'monthlength' ? $length : 0,
196 sub redirect_add_subscription {
197 my $auser = $query->param('user');
198 my $branchcode = $query->param('branchcode');
199 my $aqbooksellerid = $query->param('aqbooksellerid');
200 my $cost = $query->param('cost');
201 my $aqbudgetid = $query->param('aqbudgetid');
202 my $periodicity = $query->param('periodicity');
203 my $dow = $query->param('dow');
204 my @irregularity = $query->param('irregularity_select');
205 my $numberpattern = $query->param('numbering_pattern');
206 my $graceperiod = $query->param('graceperiod') || 0;
208 my ($numberlength, $weeklength, $monthlength)
209 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
210 my $add1 = $query->param('add1');
211 my $every1 = $query->param('every1');
212 my $whenmorethan1 = $query->param('whenmorethan1');
213 my $setto1 = $query->param('setto1');
214 my $lastvalue1 = $query->param('lastvalue1');
215 my $innerloop1 =$query->param('innerloop1');
216 my $add2 = $query->param('add2');
217 my $every2 = $query->param('every2');
218 my $whenmorethan2 = $query->param('whenmorethan2');
219 my $setto2 = $query->param('setto2');
220 my $innerloop2 =$query->param('innerloop2');
221 my $lastvalue2 = $query->param('lastvalue2');
222 my $add3 = $query->param('add3');
223 my $every3 = $query->param('every3');
224 my $whenmorethan3 = $query->param('whenmorethan3');
225 my $setto3 = $query->param('setto3');
226 my $lastvalue3 = $query->param('lastvalue3');
227 my $innerloop3 =$query->param('innerloop3');
228 my $numberingmethod = $query->param('numberingmethod');
230 my $biblionumber = $query->param('biblionumber');
231 my $callnumber = $query->param('callnumber');
232 my $notes = $query->param('notes');
233 my $internalnotes = $query->param('internalnotes');
234 my $hemisphere = $query->param('hemisphere') || 1;
235 my $letter = $query->param('letter');
236 my $manualhistory = $query->param('manualhist');
237 my $serialsadditems = $query->param('serialsadditems');
238 my $staffdisplaycount = $query->param('staffdisplaycount');
239 my $opacdisplaycount = $query->param('opacdisplaycount');
240 my $location = $query->param('location');
241 my $startdate = format_date_in_iso($query->param('startdate'));
242 my $enddate = format_date_in_iso($query->param('enddate'));
243 my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));
244 my $histenddate = format_date_in_iso($query->param('histenddate'));
245 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
246 my $recievedlist = $query->param('recievedlist');
247 my $missinglist = $query->param('missinglist');
248 my $opacnote = $query->param('opacnote');
249 my $librariannote = $query->param('librariannote');
250 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
251 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
252 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
253 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
254 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
255 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
256 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
257 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
259 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
261 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
265 sub redirect_mod_subscription {
266 my $subscriptionid = $query->param('subscriptionid');
267 my @irregularity = $query->param('irregularity_select');
268 my $auser = $query->param('user');
269 my $librarian => $query->param('librarian'),
270 my $branchcode = $query->param('branchcode');
271 my $cost = $query->param('cost');
272 my $aqbooksellerid = $query->param('aqbooksellerid');
273 my $biblionumber = $query->param('biblionumber');
274 my $aqbudgetid = $query->param('aqbudgetid');
275 my $startdate = format_date_in_iso($query->param('startdate'));
276 my $nextacquidate = $query->param('nextacquidate') ?
277 format_date_in_iso($query->param('nextacquidate')):
278 format_date_in_iso($query->param('startdate'));
279 my $enddate = format_date_in_iso($query->param('enddate'));
280 my $periodicity = $query->param('periodicity');
281 my $dow = $query->param('dow');
283 my ($numberlength, $weeklength, $monthlength)
284 = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
285 my $numberpattern = $query->param('numbering_pattern');
286 my $add1 = $query->param('add1');
287 my $every1 = $query->param('every1');
288 my $whenmorethan1 = $query->param('whenmorethan1');
289 my $setto1 = $query->param('setto1');
290 my $lastvalue1 = $query->param('lastvalue1');
291 my $innerloop1 = $query->param('innerloop1');
292 my $add2 = $query->param('add2');
293 my $every2 = $query->param('every2');
294 my $whenmorethan2 = $query->param('whenmorethan2');
295 my $setto2 = $query->param('setto2');
296 my $lastvalue2 = $query->param('lastvalue2');
297 my $innerloop2 = $query->param('innerloop2');
298 my $add3 = $query->param('add3');
299 my $every3 = $query->param('every3');
300 my $whenmorethan3 = $query->param('whenmorethan3');
301 my $setto3 = $query->param('setto3');
302 my $lastvalue3 = $query->param('lastvalue3');
303 my $innerloop3 = $query->param('innerloop3');
304 my $numberingmethod = $query->param('numberingmethod');
306 my $callnumber = $query->param('callnumber');
307 my $notes = $query->param('notes');
308 my $internalnotes = $query->param('internalnotes');
309 my $hemisphere = $query->param('hemisphere');
310 my $letter = $query->param('letter');
311 my $manualhistory = $query->param('manualhist');
312 my $serialsadditems = $query->param('serialsadditems');
313 # subscription history
314 my $histenddate = format_date_in_iso($query->param('histenddate'));
315 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
316 my $recievedlist = $query->param('recievedlist');
317 my $missinglist = $query->param('missinglist');
318 my $opacnote = $query->param('opacnote');
319 my $librariannote = $query->param('librariannote');
320 my $staffdisplaycount = $query->param('staffdisplaycount');
321 my $opacdisplaycount = $query->param('opacdisplaycount');
322 my $graceperiod = $query->param('graceperiod') || 0;
323 my $location = $query->param('location');
324 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
325 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
326 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
327 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
328 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
332 $auser, $branchcode, $aqbooksellerid, $cost,
333 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
334 $dow, join(q{,},@irregularity), $numberpattern, $numberlength,
335 $weeklength, $monthlength, $add1, $every1,
336 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
337 $add2, $every2, $whenmorethan2, $setto2,
338 $lastvalue2, $innerloop2, $add3, $every3,
339 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
340 $numberingmethod, $status, $biblionumber, $callnumber,
341 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
342 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
344 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
345 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");