Bug 3857: Add tools menu to left of CSV Profiles page
[koha.git] / serials / subscription-add.pl
blob6ee32f5d23cacba6396890ab14e26a72fd9426fc
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 warnings;
21 use CGI;
22 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
23 use C4::Koha;
24 use C4::Biblio;
25 use C4::Auth;
26 use C4::Dates qw/format_date format_date_in_iso/;
27 use C4::Acquisition;
28 use C4::Output;
29 use C4::Context;
30 use C4::Branch; # GetBranches
31 use C4::Serials;
32 use C4::Letters;
34 #use Smart::Comments;
36 my $query = new CGI;
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);
47 my @budgets;
48 my ($template, $loggedinuser, $cookie)
49 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
50 query => $query,
51 type => "intranet",
52 authnotrequired => 0,
53 flagsrequired => {serials => 1},
54 debug => 1,
55 });
59 my $sub_on;
60 my @subscription_types = (
61 'issues', 'weeks', 'months'
63 my @sub_type_data;
65 my $subs;
66 my $firstissuedate;
67 my $nextexpected;
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') {
83 $subs->{$_} = ''
84 } else {
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};
99 $sub_on=$length_unit;
100 last;
104 $template->param($subs);
105 $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
106 $template->param(
107 $op => 1,
108 "subtype_$sub_on" => 1,
109 sublength =>$sublength,
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);
123 my @branchloop;
124 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
125 my $selected = 0;
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(),
137 my $count = 0;
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;
154 my $weeklength = 0;
155 my $monthlength = 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;
165 } else {
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');
187 my $status = 1;
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 $histenddate = format_date_in_iso($query->param('histenddate'));
203 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
204 my $recievedlist = $query->param('recievedlist');
205 my $missinglist = $query->param('missinglist');
206 my $opacnote = $query->param('opacnote');
207 my $librariannote = $query->param('librariannote');
208 my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
209 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
210 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
211 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
212 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
213 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
214 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
215 $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
217 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
219 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
220 } elsif ($op eq 'modsubscription') {
221 my $subscriptionid = $query->param('subscriptionid');
222 my @irregularity = $query->param('irregularity_select');
223 my $auser = $query->param('user');
224 my $librarian => $query->param('librarian'),
225 my $branchcode = $query->param('branchcode');
226 my $cost = $query->param('cost');
227 my $aqbooksellerid = $query->param('aqbooksellerid');
228 my $biblionumber = $query->param('biblionumber');
229 my $aqbudgetid = $query->param('aqbudgetid');
230 my $startdate = format_date_in_iso($query->param('startdate'));
231 my $enddate = format_date_in_iso($query->param('enddate'));
232 my $nextacquidate = $query->param('nextacquidate') ?
233 format_date_in_iso($query->param('nextacquidate')):
234 format_date_in_iso($query->param('startdate'));
235 my $enddate = format_date_in_iso($query->param('enddate'));
236 my $periodicity = $query->param('periodicity');
237 my $dow = $query->param('dow');
238 my $sublength = $query->param('sublength');
239 my $subtype = $query->param('subtype');
241 if($subtype eq 'months'){
242 $monthlength = $sublength;
243 } elsif ($subtype eq 'weeks'){
244 $weeklength = $sublength;
245 } else {
246 $numberlength = $sublength;
248 my $numberpattern = $query->param('numbering_pattern');
249 my $add1 = $query->param('add1');
250 my $every1 = $query->param('every1');
251 my $whenmorethan1 = $query->param('whenmorethan1');
252 my $setto1 = $query->param('setto1');
253 my $lastvalue1 = $query->param('lastvalue1');
254 my $innerloop1 = $query->param('innerloop1');
255 my $add2 = $query->param('add2');
256 my $every2 = $query->param('every2');
257 my $whenmorethan2 = $query->param('whenmorethan2');
258 my $setto2 = $query->param('setto2');
259 my $lastvalue2 = $query->param('lastvalue2');
260 my $innerloop2 = $query->param('innerloop2');
261 my $add3 = $query->param('add3');
262 my $every3 = $query->param('every3');
263 my $whenmorethan3 = $query->param('whenmorethan3');
264 my $setto3 = $query->param('setto3');
265 my $lastvalue3 = $query->param('lastvalue3');
266 my $innerloop3 = $query->param('innerloop3');
267 my $numberingmethod = $query->param('numberingmethod');
268 my $status = 1;
269 my $callnumber = $query->param('callnumber');
270 my $notes = $query->param('notes');
271 my $internalnotes = $query->param('internalnotes');
272 my $hemisphere = $query->param('hemisphere');
273 my $letter = $query->param('letter');
274 my $manualhistory = $query->param('manualhist');
275 my $serialsadditems = $query->param('serialsadditems');
276 # subscription history
277 my $histenddate = format_date_in_iso($query->param('histenddate'));
278 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
279 my $recievedlist = $query->param('recievedlist');
280 my $missinglist = $query->param('missinglist');
281 my $opacnote = $query->param('opacnote');
282 my $librariannote = $query->param('librariannote');
283 my $staffdisplaycount = $query->param('staffdisplaycount');
284 my $opacdisplaycount = $query->param('opacdisplaycount');
285 my $graceperiod = $query->param('graceperiod') || 0;
286 my $location = $query->param('location');
287 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
288 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
289 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
290 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
291 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
294 &ModSubscription(
295 $auser, $branchcode, $aqbooksellerid, $cost,
296 $aqbudgetid, $startdate, $periodicity, $firstissuedate,
297 $dow, join(",",@irregularity), $numberpattern, $numberlength,
298 $weeklength, $monthlength, $add1, $every1,
299 $whenmorethan1, $setto1, $lastvalue1, $innerloop1,
300 $add2, $every2, $whenmorethan2, $setto2,
301 $lastvalue2, $innerloop2, $add3, $every3,
302 $whenmorethan3, $setto3, $lastvalue3, $innerloop3,
303 $numberingmethod, $status, $biblionumber, $callnumber,
304 $notes, $letter, $hemisphere, $manualhistory,$internalnotes,
305 $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
307 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
308 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
309 } else {
310 while (@subscription_types) {
311 my $sub_type = shift @subscription_types;
312 my %row = ( 'name' => $sub_type );
313 if ( defined $sub_on and $sub_on eq $sub_type ) {
314 $row{'selected'} = ' selected';
315 } else {
316 $row{'selected'} = '';
318 push( @sub_type_data, \%row );
320 $template->param(subtype => \@sub_type_data,
323 letter_loop('', $template);
325 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
326 if (defined $new_biblionumber) {
327 my $bib = GetBiblioData($new_biblionumber);
328 if (defined $bib) {
329 $template->param(bibnum => $new_biblionumber);
330 $template->param(bibliotitle => $bib->{title});
333 output_html_with_http_headers $query, $cookie, $template->output;
336 sub letter_loop {
337 my ($selected_letter, $template) = @_;
338 my $letters = GetLetters('serial');
339 my @letterloop;
340 foreach my $thisletter (keys %$letters) {
341 my $selected = $thisletter eq $selected_letter ? 1 : 0;
342 push @letterloop, {
343 value => $thisletter,
344 selected => $selected,
345 lettername => $letters->{$thisletter},
348 $template->param(letterloop => \@letterloop) if @letterloop;
349 return;