Add a UNIMARC to Dublin Core XSL
[koha.git] / serials / subscription-add.pl
blob39bd8ff8d5e8dfa49731443f2a0d5ab9f35cad52
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 $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
217 $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
218 $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
219 $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
220 $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
221 $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
222 $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
223 $serialsadditems,
226 print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
227 } elsif ($op eq 'modsubscription') {
228 my $subscriptionid = $query->param('subscriptionid');
229 my @irregularity = $query->param('irregularity_select');
230 my $auser = $query->param('user');
231 my $librarian => $query->param('librarian'),
232 my $branchcode = $query->param('branchcode');
233 my $cost = $query->param('cost');
234 my $aqbooksellerid = $query->param('aqbooksellerid');
235 my $biblionumber = $query->param('biblionumber');
236 my $aqbudgetid = $query->param('aqbudgetid');
237 my $startdate = format_date_in_iso($query->param('startdate'));
238 my $nextacquidate = $query->param('nextacquidate') ?
239 format_date_in_iso($query->param('nextacquidate')):
240 format_date_in_iso($query->param('startdate'));
241 my $periodicity = $query->param('periodicity');
242 my $dow = $query->param('dow');
243 my $sublength = $query->param('sublength');
244 my $subtype = $query->param('subtype');
246 if($subtype eq 'months'){
247 $monthlength = $sublength;
248 } elsif ($subtype eq 'weeks'){
249 $weeklength = $sublength;
250 } else {
251 $numberlength = $sublength;
253 my $numberpattern = $query->param('numbering_pattern');
254 my $add1 = $query->param('add1');
255 my $every1 = $query->param('every1');
256 my $whenmorethan1 = $query->param('whenmorethan1');
257 my $setto1 = $query->param('setto1');
258 my $lastvalue1 = $query->param('lastvalue1');
259 my $innerloop1 = $query->param('innerloop1');
260 my $add2 = $query->param('add2');
261 my $every2 = $query->param('every2');
262 my $whenmorethan2 = $query->param('whenmorethan2');
263 my $setto2 = $query->param('setto2');
264 my $lastvalue2 = $query->param('lastvalue2');
265 my $innerloop2 = $query->param('innerloop2');
266 my $add3 = $query->param('add3');
267 my $every3 = $query->param('every3');
268 my $whenmorethan3 = $query->param('whenmorethan3');
269 my $setto3 = $query->param('setto3');
270 my $lastvalue3 = $query->param('lastvalue3');
271 my $innerloop3 = $query->param('innerloop3');
272 my $numberingmethod = $query->param('numberingmethod');
273 my $status = 1;
274 my $callnumber = $query->param('callnumber');
275 my $notes = $query->param('notes');
276 my $internalnotes = $query->param('internalnotes');
277 my $hemisphere = $query->param('hemisphere');
278 my $letter = $query->param('letter');
279 my $manualhistory = $query->param('manualhist');
280 my $enddate = $query->param('enddate');
281 my $serialsadditems = $query->param('serialsadditems');
282 # subscription history
283 my $histenddate = format_date_in_iso($query->param('histenddate'));
284 my $histstartdate = format_date_in_iso($query->param('histstartdate'));
285 my $recievedlist = $query->param('recievedlist');
286 my $missinglist = $query->param('missinglist');
287 my $opacnote = $query->param('opacnote');
288 my $librariannote = $query->param('librariannote');
289 my $history_only = $query->param('history_only');
290 # If it's a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
291 if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
292 ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
293 # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
294 $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
297 if ($history_only) {
298 ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
299 } else {
300 &ModSubscription(
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");
315 } else {
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';
322 } else {
323 $row{'selected'} = '';
325 push( @sub_type_data, \%row );
327 $template->param(subtype => \@sub_type_data,
330 my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
331 if (defined $new_biblionumber) {
332 my $bib = GetBiblioData($new_biblionumber);
333 if (defined $bib) {
334 $template->param(bibnum => $new_biblionumber);
335 $template->param(bibliotitle => $bib->{title});
338 output_html_with_http_headers $query, $cookie, $template->output;