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 with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 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
37 my $op = $query->param('op') || '';
38 my $dbh = C4
::Context
->dbh;
39 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
40 $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
41 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
42 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
43 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
44 $numberingmethod, $status, $biblionumber,
45 $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory,$serialsadditems, $location);
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user
({template_name
=> "serials/subscription-add.tmpl",
53 flagsrequired
=> {serials
=> 1},
60 my @subscription_types = (
61 'issues', 'weeks', 'months'
69 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
71 $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 warn "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 $subs->{'letter'}='' unless($subs->{'letter'});
89 letter_loop
($subs->{'letter'}, $template);
90 $irregularity = $subs->{'irregularity'};
91 $numberpattern = $subs->{'numberpattern'};
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 $sublength=$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
=>$sublength,
110 history
=> ($op eq 'mod' && $subs->{manualhistory
} == 1 ),
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 ($thisbranch eq C4
::Context
->userenv->{'branch'});
127 $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
128 my %row =(value
=> $thisbranch,
129 selected
=> $selected,
130 branchname
=> $branches->{$thisbranch}->{'branchname'},
132 push @branchloop, \
%row;
134 $template->param(branchloop
=> \
@branchloop,
135 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
138 # prepare template variables common to all $op conditions:
139 $template->param( 'dateformat_' . C4
::Context
->preference('dateformat') => 1 ,
142 if ($op eq 'addsubscription') {
143 my $auser = $query->param('user');
144 my $branchcode = $query->param('branchcode');
145 my $aqbooksellerid = $query->param('aqbooksellerid');
146 my $cost = $query->param('cost');
147 my $aqbudgetid = $query->param('aqbudgetid');
148 my $startdate = $query->param('startdate');
149 my $firstacquidate = $query->param('firstacquidate');
150 my $periodicity = $query->param('periodicity');
151 my $dow = $query->param('dow');
152 my @irregularity = $query->param('irregularity_select');
153 my $numberlength = 0;
156 my $numberpattern = $query->param('numbering_pattern');
157 my $sublength = $query->param('sublength');
158 my $subtype = $query->param('subtype');
159 my $graceperiod = $query->param('graceperiod') || 0;
161 if ($subtype eq 'months'){
162 $monthlength = $sublength;
163 } elsif ($subtype eq 'weeks'){
164 $weeklength = $sublength;
166 $numberlength = $sublength;
168 my $add1 = $query->param('add1');
169 my $every1 = $query->param('every1');
170 my $whenmorethan1 = $query->param('whenmorethan1');
171 my $setto1 = $query->param('setto1');
172 my $lastvalue1 = $query->param('lastvalue1');
173 my $innerloop1 =$query->param('innerloop1');
174 my $add2 = $query->param('add2');
175 my $every2 = $query->param('every2');
176 my $whenmorethan2 = $query->param('whenmorethan2');
177 my $setto2 = $query->param('setto2');
178 my $innerloop2 =$query->param('innerloop2');
179 my $lastvalue2 = $query->param('lastvalue2');
180 my $add3 = $query->param('add3');
181 my $every3 = $query->param('every3');
182 my $whenmorethan3 = $query->param('whenmorethan3');
183 my $setto3 = $query->param('setto3');
184 my $lastvalue3 = $query->param('lastvalue3');
185 my $innerloop3 =$query->param('innerloop3');
186 my $numberingmethod = $query->param('numberingmethod');
188 my $biblionumber = $query->param('biblionumber');
189 my $callnumber = $query->param('callnumber');
190 my $notes = $query->param('notes');
191 my $internalnotes = $query->param('internalnotes');
192 my $hemisphere = $query->param('hemisphere') || 1;
193 my $letter = $query->param('letter');
194 my $manualhistory = $query->param('manualhist');
195 my $serialsadditems = $query->param('serialsadditems');
196 my $staffdisplaycount = $query->param('staffdisplaycount');
197 my $opacdisplaycount = $query->param('opacdisplaycount');
198 my $location = $query->param('location');
199 my $startdate = format_date_in_iso
($query->param('startdate'));
200 my $enddate = format_date_in_iso
($query->param('enddate'));
201 my $firstacquidate = format_date_in_iso
($query->param('firstacquidate'));
202 my $subscriptionid = NewSubscription
($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
203 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
204 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
205 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
206 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
207 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
208 $numberpattern, $callnumber, $hemisphere,($manualhistory?
$manualhistory:0),$internalnotes,
209 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
212 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
213 } elsif ($op eq 'modsubscription') {
214 my $subscriptionid = $query->param('subscriptionid');
215 my @irregularity = $query->param('irregularity_select');
216 my $auser = $query->param('user');
217 my $librarian => $query->param('librarian'),
218 my $branchcode = $query->param('branchcode');
219 my $cost = $query->param('cost');
220 my $aqbooksellerid = $query->param('aqbooksellerid');
221 my $biblionumber = $query->param('biblionumber');
222 my $aqbudgetid = $query->param('aqbudgetid');
223 my $startdate = format_date_in_iso
($query->param('startdate'));
224 my $enddate = format_date_in_iso
($query->param('enddate'));
225 my $nextacquidate = $query->param('nextacquidate') ?
226 format_date_in_iso
($query->param('nextacquidate')):
227 format_date_in_iso
($query->param('startdate'));
228 my $enddate = format_date_in_iso
($query->param('enddate'));
229 my $periodicity = $query->param('periodicity');
230 my $dow = $query->param('dow');
231 my $sublength = $query->param('sublength');
232 my $subtype = $query->param('subtype');
234 if($subtype eq 'months'){
235 $monthlength = $sublength;
236 } elsif ($subtype eq 'weeks'){
237 $weeklength = $sublength;
239 $numberlength = $sublength;
241 my $numberpattern = $query->param('numbering_pattern');
242 my $add1 = $query->param('add1');
243 my $every1 = $query->param('every1');
244 my $whenmorethan1 = $query->param('whenmorethan1');
245 my $setto1 = $query->param('setto1');
246 my $lastvalue1 = $query->param('lastvalue1');
247 my $innerloop1 = $query->param('innerloop1');
248 my $add2 = $query->param('add2');
249 my $every2 = $query->param('every2');
250 my $whenmorethan2 = $query->param('whenmorethan2');
251 my $setto2 = $query->param('setto2');
252 my $lastvalue2 = $query->param('lastvalue2');
253 my $innerloop2 = $query->param('innerloop2');
254 my $add3 = $query->param('add3');
255 my $every3 = $query->param('every3');
256 my $whenmorethan3 = $query->param('whenmorethan3');
257 my $setto3 = $query->param('setto3');
258 my $lastvalue3 = $query->param('lastvalue3');
259 my $innerloop3 = $query->param('innerloop3');
260 my $numberingmethod = $query->param('numberingmethod');
262 my $callnumber = $query->param('callnumber');
263 my $notes = $query->param('notes');
264 my $internalnotes = $query->param('internalnotes');
265 my $hemisphere = $query->param('hemisphere');
266 my $letter = $query->param('letter');
267 my $manualhistory = $query->param('manualhist');
268 my $serialsadditems = $query->param('serialsadditems');
269 # subscription history
270 my $histenddate = format_date_in_iso
($query->param('histenddate'));
271 my $histstartdate = format_date_in_iso
($query->param('histstartdate'));
272 my $recievedlist = $query->param('recievedlist');
273 my $missinglist = $query->param('missinglist');
274 my $opacnote = $query->param('opacnote');
275 my $librariannote = $query->param('librariannote');
276 my $history_only = $query->param('history_only');
277 my $staffdisplaycount = $query->param('staffdisplaycount');
278 my $opacdisplaycount = $query->param('opacdisplaycount');
279 my $graceperiod = $query->param('graceperiod') || 0;
280 my $location = $query->param('location');
281 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
282 if ( $nextacquidate ne $nextexpected->{planneddate
}->output('iso') ) {
283 ModNextExpected
($subscriptionid,C4
::Dates
->new($nextacquidate,'iso'));
284 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
285 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue
});
289 ModSubscriptionHistory
($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
292 $auser, $branchcode, $aqbooksellerid, $cost,
293 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
294 $dow, join(",",@irregularity), $numberpattern, $numberlength,
295 $weeklength, $monthlength, $add1, $every1,
296 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
297 $add2, $every2, $whenmorethan2, $setto2,
298 $lastvalue2, $innerloop2, $add3, $every3,
299 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
300 $numberingmethod, $status, $biblionumber, $callnumber,
301 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
302 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
305 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
308 while (@subscription_types) {
309 my $sub_type = shift @subscription_types;
310 my %row = ( 'name' => $sub_type );
311 if ( defined $sub_on and $sub_on eq $sub_type ) {
312 $row{'selected'} = ' selected';
314 $row{'selected'} = '';
316 push( @sub_type_data, \
%row );
318 $template->param(subtype
=> \
@sub_type_data,
321 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
322 if (defined $new_biblionumber) {
323 my $bib = GetBiblioData
($new_biblionumber);
325 $template->param(bibnum
=> $new_biblionumber);
326 $template->param(bibliotitle
=> $bib->{title
});
329 output_html_with_http_headers
$query, $cookie, $template->output;
333 my ($selected_letter, $template) = @_;
334 my $letters = GetLetters
('serial');
336 foreach my $thisletter (keys %$letters) {
337 my $selected = $thisletter eq $selected_letter ?
1 : 0;
339 value
=> $thisletter,
340 selected
=> $selected,
341 lettername
=> $letters->{$thisletter},
344 $template->param(letterloop
=> \
@letterloop) if @letterloop;