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);
75 my $onlymine=C4::Context->preference('IndependantBranches') &&
76 C4::Context->userenv &&
77 C4::Context->userenv->{flags}!=1 &&
78 C4::Context->userenv->{branch};
79 my $branches = GetBranches($onlymine);
81 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
82 my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
83 my %row =(value => $thisbranch,
84 selected => $selected,
85 branchname => $branches->{$thisbranch}->{'branchname'},
87 push @branchloop, \%row;
89 $template->param(branchloop => \@branchloop,
90 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
97 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
99 $subscriptionid = $query->param('subscriptionid');
100 $subs = &GetSubscription($subscriptionid);
101 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
102 if ($subs->{'cannotedit'} && $op eq 'mod'){
103 warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
104 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
106 $firstissuedate = $subs->{firstacquidate}; # in iso format.
107 for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
108 # TODO : Handle date formats properly.
109 if ($subs->{$_} eq '0000-00-00') {
112 $subs->{$_} = format_date($subs->{$_});
115 $subs->{'letter'}='' unless($subs->{'letter'});
116 $irregularity = $subs->{'irregularity'};
117 $numberpattern = $subs->{'numberpattern'};
118 $nextexpected = GetNextExpected($subscriptionid);
119 $nextexpected->{ isfirstissue => ($nextexpected->{planneddate}->output('iso') eq $firstissuedate)};
120 # firstacquidate is taken to be the upcoming issue's planned date if we're modifying the sub
121 $subs->{nextacquidate} = $nextexpected->{planneddate}->output() if($op eq 'mod');
123 unless($op eq 'modsubscription') {
124 if($subs->{numberlength} > 0){
125 $sublength = $subs->{numberlength};
126 $sub_on = $subscription_types[0];
127 } elsif ($subs->{weeklength}>0){
128 $sublength = $subs->{weeklength};
129 $sub_on = $subscription_types[1];
131 $sublength = $subs->{monthlength};
132 $sub_on = $subscription_types[2];
134 while (@subscription_types) {
135 my $sub_type = shift @subscription_types;
136 my %row = ( 'name' => $sub_type );
137 if ( $sub_on eq $sub_type ) {
138 $row{'selected'} = ' selected';
140 $row{'selected'} = '';
142 push( @sub_type_data, \%row );
145 $template->param($subs);
148 subtype => \@sub_type_data,
149 sublength =>$sublength,
150 history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote})),
151 "periodicity".$subs->{'periodicity'} => 1,
152 "dow".$subs->{'dow'} => 1,
153 "numberpattern".$subs->{'numberpattern'} => 1,
154 firstacquiyear => substr($firstissuedate,0,4),
160 # prepare template variables common to all $op conditions:
161 $template->param( 'dateformat_' . C4::Context->preference('dateformat') => 1 ,
164 if ($op eq 'addsubscription') {
165 my $auser = $query->param('user');
166 my $branchcode = $query->param('branchcode');
167 my $aqbooksellerid = $query->param('aqbooksellerid');
168 my $cost = $query->param('cost');
169 my $aqbudgetid = $query->param('aqbudgetid');
170 my $startdate = $query->param('startdate');
171 my $firstacquidate = $query->param('firstacquidate');
172 my $periodicity = $query->param('periodicity');
173 my $dow = $query->param('dow');
174 my @irregularity = $query->param('irregularity_select');
175 my $numberlength = 0;
178 my $numberpattern = $query->param('numbering_pattern');
179 my $sublength = $query->param('sublength');
180 my $subtype = $query->param('subtype');
181 if ($subtype eq 'months'){
182 $monthlength = $sublength;
183 } elsif ($subtype eq 'weeks'){
184 $weeklength = $sublength;
186 $numberlength = $sublength;
188 my $add1 = $query->param('add1');
189 my $every1 = $query->param('every1');
190 my $whenmorethan1 = $query->param('whenmorethan1');
191 my $setto1 = $query->param('setto1');
192 my $lastvalue1 = $query->param('lastvalue1');
193 my $innerloop1 =$query->param('innerloop1');
194 my $add2 = $query->param('add2');
195 my $every2 = $query->param('every2');
196 my $whenmorethan2 = $query->param('whenmorethan2');
197 my $setto2 = $query->param('setto2');
198 my $innerloop2 =$query->param('innerloop2');
199 my $lastvalue2 = $query->param('lastvalue2');
200 my $add3 = $query->param('add3');
201 my $every3 = $query->param('every3');
202 my $whenmorethan3 = $query->param('whenmorethan3');
203 my $setto3 = $query->param('setto3');
204 my $lastvalue3 = $query->param('lastvalue3');
205 my $innerloop3 =$query->param('innerloop3');
206 my $numberingmethod = $query->param('numberingmethod');
208 my $biblionumber = $query->param('biblionumber');
209 my $callnumber = $query->param('callnumber');
210 my $notes = $query->param('notes');
211 my $internalnotes = $query->param('internalnotes');
212 my $hemisphere = $query->param('hemisphere') || 1;
213 my $letter = $query->param('letter');
214 # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
215 my $manualhistory = $query->param('manualhist');
216 my $serialsadditems = $query->param('serialsadditems');
217 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
218 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
219 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
220 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
221 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
222 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
223 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
227 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
228 } elsif ($op eq 'modsubscription') {
229 my $subscriptionid = $query->param('subscriptionid');
230 my @irregularity = $query->param('irregularity_select');
231 my $auser = $query->param('user');
232 my $librarian => $query->param('librarian'),
233 my $branchcode = $query->param('branchcode');
234 my $cost = $query->param('cost');
235 my $aqbooksellerid = $query->param('aqbooksellerid');
236 my $biblionumber = $query->param('biblionumber');
237 my $aqbudgetid = $query->param('aqbudgetid');
238 my $startdate = format_date_in_iso($query->param('startdate'));
239 my $nextacquidate = format_date_in_iso($query->param('nextacquidate'));
240 my $periodicity = $query->param('periodicity');
241 my $dow = $query->param('dow');
242 my $sublength = $query->param('sublength');
243 my $subtype = $query->param('subtype');
245 if($subtype eq 'months'){
246 $monthlength = $sublength;
247 } elsif ($subtype eq 'weeks'){
248 $weeklength = $sublength;
250 $numberlength = $sublength;
252 my $numberpattern = $query->param('numbering_pattern');
253 my $add1 = $query->param('add1');
254 my $every1 = $query->param('every1');
255 my $whenmorethan1 = $query->param('whenmorethan1');
256 my $setto1 = $query->param('setto1');
257 my $lastvalue1 = $query->param('lastvalue1');
258 my $innerloop1 = $query->param('innerloop1');
259 my $add2 = $query->param('add2');
260 my $every2 = $query->param('every2');
261 my $whenmorethan2 = $query->param('whenmorethan2');
262 my $setto2 = $query->param('setto2');
263 my $lastvalue2 = $query->param('lastvalue2');
264 my $innerloop2 = $query->param('innerloop2');
265 my $add3 = $query->param('add3');
266 my $every3 = $query->param('every3');
267 my $whenmorethan3 = $query->param('whenmorethan3');
268 my $setto3 = $query->param('setto3');
269 my $lastvalue3 = $query->param('lastvalue3');
270 my $innerloop3 = $query->param('innerloop3');
271 my $numberingmethod = $query->param('numberingmethod');
273 my $callnumber = $query->param('callnumber');
274 my $notes = $query->param('notes');
275 my $internalnotes = $query->param('internalnotes');
276 my $hemisphere = $query->param('hemisphere');
277 my $letter = $query->param('letter');
278 my $manualhistory = $query->param('manualhist');
279 my $enddate = $query->param('enddate');
280 my $serialsadditems = $query->param('serialsadditems');
281 # subscription history
282 my $histenddate = format_date_in_iso($query->param('histenddate'));
283 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
284 my $recievedlist = $query->param('recievedlist');
285 my $missinglist = $query->param('missinglist');
286 my $opacnote = $query->param('opacnote');
287 my $librariannote = $query->param('librariannote');
288 my $history_only = $query->param('history_only');
289 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
290 # Here firstacquidate is interpreted as nextacquidate.
292 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
293 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
294 $firstissuedate = $nextexpected->{planneddate}->output('iso') if($nextexpected->{isfirstissue});
298 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
301 $auser, $branchcode, $aqbooksellerid, $cost,
302 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
303 $dow, join(",",@irregularity), $numberpattern, $numberlength,
304 $weeklength, $monthlength, $add1, $every1,
305 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
306 $add2, $every2, $whenmorethan2, $setto2,
307 $lastvalue2, $innerloop2, $add3, $every3,
308 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
309 $numberingmethod, $status, $biblionumber, $callnumber,
310 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
311 $serialsadditems, $subscriptionid,
314 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
317 while (@subscription_types) {
318 my $sub_type = shift @subscription_types;
319 my %row = ( 'name' => $sub_type );
320 if ( $sub_on eq $sub_type ) {
321 $row{'selected'} = ' selected';
323 $row{'selected'} = '';
325 push( @sub_type_data, \%row );
327 $template->param(subtype => \@sub_type_data,
329 output_html_with_http_headers $query, $cookie, $template->output;