Bug 2944 fix courtesy of Amr Denni
[koha.git] / serials / subscription-add.pl
blobaeac6840d7766a54b10d5585582931ceada5ab45
1 #!/usr/bin/perl
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
8 # version.
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
18 use strict;
19 use CGI;
20 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
21 use C4::Koha;
22 use C4::Biblio;
23 use C4::Auth;
24 use C4::Dates qw/format_date format_date_in_iso/;
25 use C4::Acquisition;
26 use C4::Output;
27 use C4::Context;
28 use C4::Branch; # GetBranches
29 use C4::Serials;
30 use C4::Letters;
32 #use Smart::Comments;
34 my $query = new CGI;
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);
45 my @budgets;
46 my ($template, $loggedinuser, $cookie)
47 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
48 query => $query,
49 type => "intranet",
50 authnotrequired => 0,
51 flagsrequired => {serials => 1},
52 debug => 1,
53 });
57 my $sub_on;
58 my @subscription_types = (
59 'issues', 'weeks', 'months'
60 );
61 my @sub_type_data;
63 my $letters = GetLetters('serial');
64 my @letterloop;
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 $subscriptionid;
76 my $subs;
77 my $firstissuedate;
78 my $nextexpected;
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') {
93 $subs->{$_} = ''
94 } else {
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];
111 } else {
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';
120 } else {
121 $row{'selected'} = '';
123 push( @sub_type_data, \%row );
126 $template->param($subs);
127 $template->param(
128 $op => 1,
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);
145 my @branchloop;
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(),
158 my $count = 0;
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;
175 my $weeklength = 0;
176 my $monthlength = 0;
177 my $numberpattern = $query->param('numbering_pattern');
178 my $sublength = $query->param('sublength');
179 my $subtype = $query->param('subtype');
180 if ($subtype eq 'months'){
181 $monthlength = $sublength;
182 } elsif ($subtype eq 'weeks'){
183 $weeklength = $sublength;
184 } else {
185 $numberlength = $sublength;
187 my $add1 = $query->param('add1');
188 my $every1 = $query->param('every1');
189 my $whenmorethan1 = $query->param('whenmorethan1');
190 my $setto1 = $query->param('setto1');
191 my $lastvalue1 = $query->param('lastvalue1');
192 my $innerloop1 =$query->param('innerloop1');
193 my $add2 = $query->param('add2');
194 my $every2 = $query->param('every2');
195 my $whenmorethan2 = $query->param('whenmorethan2');
196 my $setto2 = $query->param('setto2');
197 my $innerloop2 =$query->param('innerloop2');
198 my $lastvalue2 = $query->param('lastvalue2');
199 my $add3 = $query->param('add3');
200 my $every3 = $query->param('every3');
201 my $whenmorethan3 = $query->param('whenmorethan3');
202 my $setto3 = $query->param('setto3');
203 my $lastvalue3 = $query->param('lastvalue3');
204 my $innerloop3 =$query->param('innerloop3');
205 my $numberingmethod = $query->param('numberingmethod');
206 my $status = 1;
207 my $biblionumber = $query->param('biblionumber');
208 my $callnumber = $query->param('callnumber');
209 my $notes = $query->param('notes');
210 my $internalnotes = $query->param('internalnotes');
211 my $hemisphere = $query->param('hemisphere') || 1;
212 my $letter = $query->param('letter');
213 # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
214 my $manualhistory = $query->param('manualhist');
215 my $serialsadditems = $query->param('serialsadditems');
216 my $staffdisplaycount = $query->param('staffdisplaycount');
217 my $opacdisplaycount = $query->param('opacdisplaycount');
218 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
219 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
220 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
221 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
222 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
223 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
224 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
225 $serialsadditems,$staffdisplaycount,$opacdisplaycount
228 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
229 } elsif ($op eq 'modsubscription') {
230 my $subscriptionid = $query->param('subscriptionid');
231 my @irregularity = $query->param('irregularity_select');
232 my $auser = $query->param('user');
233 my $librarian => $query->param('librarian'),
234 my $branchcode = $query->param('branchcode');
235 my $cost = $query->param('cost');
236 my $aqbooksellerid = $query->param('aqbooksellerid');
237 my $biblionumber = $query->param('biblionumber');
238 my $aqbudgetid = $query->param('aqbudgetid');
239 my $startdate = format_date_in_iso($query->param('startdate'));
240 my $nextacquidate = $query->param('nextacquidate') ?
241 format_date_in_iso($query->param('nextacquidate')):
242 format_date_in_iso($query->param('startdate'));
243 my $periodicity = $query->param('periodicity');
244 my $dow = $query->param('dow');
245 my $sublength = $query->param('sublength');
246 my $subtype = $query->param('subtype');
248 if($subtype eq 'months'){
249 $monthlength = $sublength;
250 } elsif ($subtype eq 'weeks'){
251 $weeklength = $sublength;
252 } else {
253 $numberlength = $sublength;
255 my $numberpattern = $query->param('numbering_pattern');
256 my $add1 = $query->param('add1');
257 my $every1 = $query->param('every1');
258 my $whenmorethan1 = $query->param('whenmorethan1');
259 my $setto1 = $query->param('setto1');
260 my $lastvalue1 = $query->param('lastvalue1');
261 my $innerloop1 = $query->param('innerloop1');
262 my $add2 = $query->param('add2');
263 my $every2 = $query->param('every2');
264 my $whenmorethan2 = $query->param('whenmorethan2');
265 my $setto2 = $query->param('setto2');
266 my $lastvalue2 = $query->param('lastvalue2');
267 my $innerloop2 = $query->param('innerloop2');
268 my $add3 = $query->param('add3');
269 my $every3 = $query->param('every3');
270 my $whenmorethan3 = $query->param('whenmorethan3');
271 my $setto3 = $query->param('setto3');
272 my $lastvalue3 = $query->param('lastvalue3');
273 my $innerloop3 = $query->param('innerloop3');
274 my $numberingmethod = $query->param('numberingmethod');
275 my $status = 1;
276 my $callnumber = $query->param('callnumber');
277 my $notes = $query->param('notes');
278 my $internalnotes = $query->param('internalnotes');
279 my $hemisphere = $query->param('hemisphere');
280 my $letter = $query->param('letter');
281 my $manualhistory = $query->param('manualhist');
282 my $enddate = $query->param('enddate');
283 my $serialsadditems = $query->param('serialsadditems');
284 # subscription history
285 my $histenddate = format_date_in_iso($query->param('histenddate'));
286 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
287 my $recievedlist = $query->param('recievedlist');
288 my $missinglist = $query->param('missinglist');
289 my $opacnote = $query->param('opacnote');
290 my $librariannote = $query->param('librariannote');
291 my $history_only = $query->param('history_only');
292 my $staffdisplaycount = $query->param('staffdisplaycount');
293 my $opacdisplaycount = $query->param('opacdisplaycount');
294 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
295 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
296 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
297 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
298 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
301 if ($history_only) {
302 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
303 } else {
304 &ModSubscription(
305 $auser, $branchcode, $aqbooksellerid, $cost,
306 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
307 $dow, join(",",@irregularity), $numberpattern, $numberlength,
308 $weeklength, $monthlength, $add1, $every1,
309 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
310 $add2, $every2, $whenmorethan2, $setto2,
311 $lastvalue2, $innerloop2, $add3, $every3,
312 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
313 $numberingmethod, $status, $biblionumber, $callnumber,
314 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
315 $serialsadditems, $subscriptionid,$staffdisplaycount,$opacdisplaycount
318 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
319 } else {
321 while (@subscription_types) {
322 my $sub_type = shift @subscription_types;
323 my %row = ( 'name' => $sub_type );
324 if ( $sub_on eq $sub_type ) {
325 $row{'selected'} = ' selected';
326 } else {
327 $row{'selected'} = '';
329 push( @sub_type_data, \%row );
331 $template->param(subtype => \@sub_type_data,
334 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
335 if (defined $new_biblionumber) {
336 my $bib = GetBiblioData($new_biblionumber);
337 if (defined $bib) {
338 $template->param(bibnum => $new_biblionumber);
339 $template->param(bibliotitle => $bib->{title});
342 output_html_with_http_headers $query, $cookie, $template->output;