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
20 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
24 use C4::Dates qw/format_date format_date_in_iso/;
28 use C4::Branch; # GetBranches
35 my $op = $query->param('op');
36 my $dbh = C4::Context->dbh;
37 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
38 $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
39 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
40 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
41 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
42 $numberingmethod, $status, $biblionumber,
43 $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory,$serialsadditems);
46 my ($template, $loggedinuser, $cookie)
47 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
51 flagsrequired => {serials => 1},
58 my @subscription_types = (
59 'issues', 'weeks', 'months'
63 my $letters = GetLetters('serial');
65 foreach my $thisletter (keys %$letters) {
66 my $selected = 1 if $thisletter eq $letter;
67 my %row =(value => $thisletter,
68 selected => $selected,
69 lettername => $letters->{$thisletter},
71 push @letterloop, \%row;
73 $template->param(letterloop => \@letterloop);
80 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
82 $subscriptionid = $query->param('subscriptionid');
83 $subs = &GetSubscription($subscriptionid);
84 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
85 if ($subs->{'cannotedit'} && $op eq 'mod'){
86 warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
87 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
89 $firstissuedate = $subs->{firstacquidate}; # in iso format.
90 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
91 # TODO : Handle date formats properly.
92 if ($subs->{$_} eq '0000-00-00') {
95 $subs->{$_} = format_date($subs->{$_});
98 $subs->{'letter'}='' unless($subs->{'letter'});
99 $irregularity = $subs->{'irregularity'};
100 $numberpattern = $subs->{'numberpattern'};
101 $nextexpected = GetNextExpected($subscriptionid);
102 $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
103 $subs->{nextacquidate} = $nextexpected->{planneddate}->output() if($op eq 'mod');
104 unless($op eq 'modsubscription') {
105 if($subs->{numberlength} > 0){
106 $sublength = $subs->{numberlength};
107 $sub_on = $subscription_types[0];
108 } elsif ($subs->{weeklength}>0){
109 $sublength = $subs->{weeklength};
110 $sub_on = $subscription_types[1];
112 $sublength = $subs->{monthlength};
113 $sub_on = $subscription_types[2];
115 while (@subscription_types) {
116 my $sub_type = shift @subscription_types;
117 my %row = ( 'name' => $sub_type );
118 if ( $sub_on eq $sub_type ) {
119 $row{'selected'} = ' selected';
121 $row{'selected'} = '';
123 push( @sub_type_data, \%row );
126 $template->param($subs);
129 subtype => \@sub_type_data,
130 sublength =>$sublength,
131 history => ($op eq 'mod' && $subs->{manualhistory} == 1 ),
132 "periodicity".$subs->{'periodicity'} => 1,
133 "dow".$subs->{'dow'} => 1,
134 "numberpattern".$subs->{'numberpattern'} => 1,
135 firstacquiyear => substr($firstissuedate,0,4),
140 my $onlymine=C4::Context->preference('IndependantBranches') &&
141 C4::Context->userenv &&
142 C4::Context->userenv->{flags}!=1 &&
143 C4::Context->userenv->{branch};
144 my $branches = GetBranches($onlymine);
146 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
147 my $selected = 1 if ($thisbranch eq C4::Context->userenv->{'branch'});
148 my $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
149 my %row =(value => $thisbranch,
150 selected => $selected,
151 branchname => $branches->{$thisbranch}->{'branchname'},
153 push @branchloop, \%row;
155 $template->param(branchloop => \@branchloop,
156 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
159 # prepare template variables common to all $op conditions:
160 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 ,
163 if ($op eq 'addsubscription') {
164 my $auser = $query->param('user');
165 my $branchcode = $query->param('branchcode');
166 my $aqbooksellerid = $query->param('aqbooksellerid');
167 my $cost = $query->param('cost');
168 my $aqbudgetid = $query->param('aqbudgetid');
169 my $startdate = $query->param('startdate');
170 my $firstacquidate = $query->param('firstacquidate');
171 my $periodicity = $query->param('periodicity');
172 my $dow = $query->param('dow');
173 my @irregularity = $query->param('irregularity_select');
174 my $numberlength = 0;
177 my $numberpattern = $query->param('numbering_pattern');
178 my $sublength = $query->param('sublength');
179 my $subtype = $query->param('subtype');
180 my $graceperiod = $query->param('graceperiod') || 0;
182 if ($subtype eq 'months'){
183 $monthlength = $sublength;
184 } elsif ($subtype eq 'weeks'){
185 $weeklength = $sublength;
187 $numberlength = $sublength;
189 my $add1 = $query->param('add1');
190 my $every1 = $query->param('every1');
191 my $whenmorethan1 = $query->param('whenmorethan1');
192 my $setto1 = $query->param('setto1');
193 my $lastvalue1 = $query->param('lastvalue1');
194 my $innerloop1 =$query->param('innerloop1');
195 my $add2 = $query->param('add2');
196 my $every2 = $query->param('every2');
197 my $whenmorethan2 = $query->param('whenmorethan2');
198 my $setto2 = $query->param('setto2');
199 my $innerloop2 =$query->param('innerloop2');
200 my $lastvalue2 = $query->param('lastvalue2');
201 my $add3 = $query->param('add3');
202 my $every3 = $query->param('every3');
203 my $whenmorethan3 = $query->param('whenmorethan3');
204 my $setto3 = $query->param('setto3');
205 my $lastvalue3 = $query->param('lastvalue3');
206 my $innerloop3 =$query->param('innerloop3');
207 my $numberingmethod = $query->param('numberingmethod');
209 my $biblionumber = $query->param('biblionumber');
210 my $callnumber = $query->param('callnumber');
211 my $notes = $query->param('notes');
212 my $internalnotes = $query->param('internalnotes');
213 my $hemisphere = $query->param('hemisphere') || 1;
214 my $letter = $query->param('letter');
215 # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
216 my $manualhistory = $query->param('manualhist');
217 my $serialsadditems = $query->param('serialsadditems');
218 my $staffdisplaycount = $query->param('staffdisplaycount');
219 my $opacdisplaycount = $query->param('opacdisplaycount');
220 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
221 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
222 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
223 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
224 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
225 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
226 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
227 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod
230 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
231 } elsif ($op eq 'modsubscription') {
232 my $subscriptionid = $query->param('subscriptionid');
233 my @irregularity = $query->param('irregularity_select');
234 my $auser = $query->param('user');
235 my $librarian => $query->param('librarian'),
236 my $branchcode = $query->param('branchcode');
237 my $cost = $query->param('cost');
238 my $aqbooksellerid = $query->param('aqbooksellerid');
239 my $biblionumber = $query->param('biblionumber');
240 my $aqbudgetid = $query->param('aqbudgetid');
241 my $startdate = format_date_in_iso($query->param('startdate'));
242 my $nextacquidate = $query->param('nextacquidate') ?
243 format_date_in_iso($query->param('nextacquidate')):
244 format_date_in_iso($query->param('startdate'));
245 my $periodicity = $query->param('periodicity');
246 my $dow = $query->param('dow');
247 my $sublength = $query->param('sublength');
248 my $subtype = $query->param('subtype');
250 if($subtype eq 'months'){
251 $monthlength = $sublength;
252 } elsif ($subtype eq 'weeks'){
253 $weeklength = $sublength;
255 $numberlength = $sublength;
257 my $numberpattern = $query->param('numbering_pattern');
258 my $add1 = $query->param('add1');
259 my $every1 = $query->param('every1');
260 my $whenmorethan1 = $query->param('whenmorethan1');
261 my $setto1 = $query->param('setto1');
262 my $lastvalue1 = $query->param('lastvalue1');
263 my $innerloop1 = $query->param('innerloop1');
264 my $add2 = $query->param('add2');
265 my $every2 = $query->param('every2');
266 my $whenmorethan2 = $query->param('whenmorethan2');
267 my $setto2 = $query->param('setto2');
268 my $lastvalue2 = $query->param('lastvalue2');
269 my $innerloop2 = $query->param('innerloop2');
270 my $add3 = $query->param('add3');
271 my $every3 = $query->param('every3');
272 my $whenmorethan3 = $query->param('whenmorethan3');
273 my $setto3 = $query->param('setto3');
274 my $lastvalue3 = $query->param('lastvalue3');
275 my $innerloop3 = $query->param('innerloop3');
276 my $numberingmethod = $query->param('numberingmethod');
278 my $callnumber = $query->param('callnumber');
279 my $notes = $query->param('notes');
280 my $internalnotes = $query->param('internalnotes');
281 my $hemisphere = $query->param('hemisphere');
282 my $letter = $query->param('letter');
283 my $manualhistory = $query->param('manualhist');
284 my $enddate = $query->param('enddate');
285 my $serialsadditems = $query->param('serialsadditems');
286 # subscription history
287 my $histenddate = format_date_in_iso($query->param('histenddate'));
288 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
289 my $recievedlist = $query->param('recievedlist');
290 my $missinglist = $query->param('missinglist');
291 my $opacnote = $query->param('opacnote');
292 my $librariannote = $query->param('librariannote');
293 my $history_only = $query->param('history_only');
294 my $staffdisplaycount = $query->param('staffdisplaycount');
295 my $opacdisplaycount = $query->param('opacdisplaycount');
296 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
297 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
298 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
299 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
300 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
304 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
307 $auser, $branchcode, $aqbooksellerid, $cost,
308 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
309 $dow, join(",",@irregularity), $numberpattern, $numberlength,
310 $weeklength, $monthlength, $add1, $every1,
311 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
312 $add2, $every2, $whenmorethan2, $setto2,
313 $lastvalue2, $innerloop2, $add3, $every3,
314 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
315 $numberingmethod, $status, $biblionumber, $callnumber,
316 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
317 $serialsadditems, $subscriptionid,$staffdisplaycount,$opacdisplaycount
320 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
323 while (@subscription_types) {
324 my $sub_type = shift @subscription_types;
325 my %row = ( 'name' => $sub_type );
326 if ( $sub_on eq $sub_type ) {
327 $row{'selected'} = ' selected';
329 $row{'selected'} = '';
331 push( @sub_type_data, \%row );
333 $template->param(subtype => \@sub_type_data,
336 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
337 if (defined $new_biblionumber) {
338 my $bib = GetBiblioData($new_biblionumber);
340 $template->param(bibnum => $new_biblionumber);
341 $template->param(bibliotitle => $bib->{title});
344 output_html_with_http_headers $query, $cookie, $template->output;