bug 4816: require authentication for placerequest.pl
[koha.git] / serials / subscription-add.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use strict;
19 use warnings;
20
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;
33 use Carp;
34
35 #use Smart::Comments;
36
37 my $query = CGI->new;
38 my $op = $query->param('op') || '';
39 my $dbh = C4::Context->dbh;
40 my $sub_length;
41
42 my ($template, $loggedinuser, $cookie)
43 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
44                                 query => $query,
45                                 type => "intranet",
46                                 authnotrequired => 0,
47                                 flagsrequired => {serials => 1},
48                                 debug => 1,
49                                 });
50
51
52
53 my $sub_on;
54 my @subscription_types = (
55             'issues', 'weeks', 'months'
56         );
57 my @sub_type_data;
58
59 my $subs;
60 my $firstissuedate;
61 my $nextexpected;
62
63 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
64
65     my $subscriptionid = $query->param('subscriptionid');
66     $subs = GetSubscription($subscriptionid);
67 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
68     if ($subs->{'cannotedit'} && $op eq 'mod'){
69       carp "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
70       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
71     }
72     $firstissuedate = $subs->{firstacquidate};  # in iso format.
73     for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
74         next unless defined $subs->{$_};
75         # TODO : Handle date formats properly.
76          if ($subs->{$_} eq '0000-00-00') {
77             $subs->{$_} = ''
78         } else {
79             $subs->{$_} = format_date($subs->{$_});
80         }
81           }
82     $subs->{'letter'}='' unless($subs->{'letter'});
83     letter_loop($subs->{'letter'}, $template);
84     $nextexpected = GetNextExpected($subscriptionid);
85     $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
86     $subs->{nextacquidate} = $nextexpected->{planneddate}->output()  if($op eq 'mod');
87     unless($op eq 'modsubscription') {
88                 foreach my $length_unit qw(numberlength weeklength monthlength){
89                         if ($subs->{$length_unit}){
90                                 $sub_length=$subs->{$length_unit};
91                                 $sub_on=$length_unit;
92                                 last;
93                         }
94                 }
95
96         $template->param($subs);
97         $template->param("dow".$subs->{'dow'} => 1) if defined $subs->{'dow'};
98         $template->param(
99                     $op => 1,
100                     "subtype_$sub_on" => 1,
101                     sublength =>$sub_length,
102                     history => ($op eq 'mod'),
103                     "periodicity".$subs->{'periodicity'} => 1,
104                     "numberpattern".$subs->{'numberpattern'} => 1,
105                     firstacquiyear => substr($firstissuedate,0,4),
106                     );
107     }
108 }
109
110 my $onlymine=C4::Context->preference('IndependantBranches') &&
111              C4::Context->userenv &&
112              C4::Context->userenv->{flags} % 2 !=1 &&
113              C4::Context->userenv->{branch};
114 my $branches = GetBranches($onlymine);
115 my $branchloop;
116 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %{$branches}) {
117     my $selected = 0;
118     $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
119     push @{$branchloop}, {
120         value => $thisbranch,
121         selected => $selected,
122         branchname => $branches->{$thisbranch}->{'branchname'},
123     };
124 }
125 $template->param(branchloop => $branchloop,
126     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
127 );
128 my $count = 0;
129 # prepare template variables common to all $op conditions:
130 $template->param(  'dateformat_' . C4::Context->preference('dateformat') => 1 ,
131                 );
132
133 if ($op eq 'addsubscription') {
134     redirect_add_subscription();
135 } elsif ($op eq 'modsubscription') {
136     redirect_mod_subscription();
137 } else {
138         while (@subscription_types) {
139            my $sub_type = shift @subscription_types;
140            my %row = ( 'name' => $sub_type );
141            if ( defined $sub_on and $sub_on eq $sub_type ) {
142              $row{'selected'} = ' selected';
143            } else {
144              $row{'selected'} = '';
145            }
146            push( @sub_type_data, \%row );
147         }
148     $template->param(subtype => \@sub_type_data,
149         );
150
151     letter_loop('', $template);
152
153     my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
154     if (defined $new_biblionumber) {
155         my $bib = GetBiblioData($new_biblionumber);
156         if (defined $bib) {
157             $template->param(bibnum      => $new_biblionumber);
158             $template->param(bibliotitle => $bib->{title});
159         }
160     }
161         output_html_with_http_headers $query, $cookie, $template->output;
162 }
163
164 sub letter_loop {
165     my ($selected_letter, $templte) = @_;
166     my $letters = GetLetters('serial');
167     my @letterloop;
168     foreach my $thisletter (keys %$letters) {
169         my $selected = $thisletter eq $selected_letter ? 1 : 0;
170         push @letterloop, {
171             value => $thisletter,
172             selected => $selected,
173             lettername => $letters->{$thisletter},
174         };
175     }
176     $templte->param(letterloop => \@letterloop) if @letterloop;
177     return;
178 }
179
180 sub _get_sub_length {
181     my ($type, $length) = @_;
182     return
183         (
184             $type eq 'numberlength' ? $length : 0,
185             $type eq 'weeklength'   ? $length : 0,
186             $type eq 'monthlength'  ? $length : 0,
187         );
188 }
189
190 sub redirect_add_subscription {
191     my $auser           = $query->param('user');
192     my $branchcode      = $query->param('branchcode');
193     my $aqbooksellerid  = $query->param('aqbooksellerid');
194     my $cost            = $query->param('cost');
195     my $aqbudgetid      = $query->param('aqbudgetid');
196     my $periodicity     = $query->param('periodicity');
197     my $dow             = $query->param('dow');
198     my @irregularity    = $query->param('irregularity_select');
199     my $numberpattern   = $query->param('numbering_pattern');
200     my $graceperiod     = $query->param('graceperiod') || 0;
201
202     my ($numberlength, $weeklength, $monthlength)
203         = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
204     my $add1 = $query->param('add1');
205     my $every1 = $query->param('every1');
206     my $whenmorethan1 = $query->param('whenmorethan1');
207     my $setto1 = $query->param('setto1');
208     my $lastvalue1 = $query->param('lastvalue1');
209     my $innerloop1 =$query->param('innerloop1');
210     my $add2 = $query->param('add2');
211     my $every2 = $query->param('every2');
212     my $whenmorethan2 = $query->param('whenmorethan2');
213     my $setto2 = $query->param('setto2');
214     my $innerloop2 =$query->param('innerloop2');
215     my $lastvalue2 = $query->param('lastvalue2');
216     my $add3 = $query->param('add3');
217     my $every3 = $query->param('every3');
218     my $whenmorethan3 = $query->param('whenmorethan3');
219     my $setto3 = $query->param('setto3');
220     my $lastvalue3 = $query->param('lastvalue3');
221     my $innerloop3 =$query->param('innerloop3');
222     my $numberingmethod = $query->param('numberingmethod');
223     my $status = 1;
224     my $biblionumber = $query->param('biblionumber');
225     my $callnumber = $query->param('callnumber');
226     my $notes = $query->param('notes');
227     my $internalnotes = $query->param('internalnotes');
228     my $hemisphere = $query->param('hemisphere') || 1;
229         my $letter = $query->param('letter');
230     my $manualhistory = $query->param('manualhist');
231     my $serialsadditems = $query->param('serialsadditems');
232         my $staffdisplaycount = $query->param('staffdisplaycount');
233         my $opacdisplaycount = $query->param('opacdisplaycount');
234     my $location = $query->param('location');
235     my $startdate       = format_date_in_iso($query->param('startdate'));
236     my $enddate       = format_date_in_iso($query->param('enddate'));
237     my $firstacquidate  = format_date_in_iso($query->param('firstacquidate'));
238     my $histenddate = format_date_in_iso($query->param('histenddate'));
239     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
240     my $recievedlist = $query->param('recievedlist');
241     my $missinglist = $query->param('missinglist');
242     my $opacnote = $query->param('opacnote');
243     my $librariannote = $query->param('librariannote');
244         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
245                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
246                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
247                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
248                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
249                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
250                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
251                     $serialsadditems,$staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate
252                                 );
253     ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
254
255     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
256     return;
257 }
258
259 sub redirect_mod_subscription {
260     my $subscriptionid = $query->param('subscriptionid');
261           my @irregularity = $query->param('irregularity_select');
262     my $auser = $query->param('user');
263     my $librarian => $query->param('librarian'),
264     my $branchcode = $query->param('branchcode');
265     my $cost = $query->param('cost');
266     my $aqbooksellerid = $query->param('aqbooksellerid');
267     my $biblionumber = $query->param('biblionumber');
268     my $aqbudgetid = $query->param('aqbudgetid');
269     my $startdate = format_date_in_iso($query->param('startdate'));
270     my $nextacquidate = $query->param('nextacquidate') ?
271                             format_date_in_iso($query->param('nextacquidate')):
272                             format_date_in_iso($query->param('startdate'));
273     my $enddate = format_date_in_iso($query->param('enddate'));
274     my $periodicity = $query->param('periodicity');
275     my $dow = $query->param('dow');
276
277     my ($numberlength, $weeklength, $monthlength)
278         = _get_sub_length( $query->param('subtype'), $query->param('sublength') );
279     my $numberpattern = $query->param('numbering_pattern');
280     my $add1 = $query->param('add1');
281     my $every1 = $query->param('every1');
282     my $whenmorethan1 = $query->param('whenmorethan1');
283     my $setto1 = $query->param('setto1');
284     my $lastvalue1 = $query->param('lastvalue1');
285     my $innerloop1 = $query->param('innerloop1');
286     my $add2 = $query->param('add2');
287     my $every2 = $query->param('every2');
288     my $whenmorethan2 = $query->param('whenmorethan2');
289     my $setto2 = $query->param('setto2');
290     my $lastvalue2 = $query->param('lastvalue2');
291     my $innerloop2 = $query->param('innerloop2');
292     my $add3 = $query->param('add3');
293     my $every3 = $query->param('every3');
294     my $whenmorethan3 = $query->param('whenmorethan3');
295     my $setto3 = $query->param('setto3');
296     my $lastvalue3 = $query->param('lastvalue3');
297     my $innerloop3 = $query->param('innerloop3');
298     my $numberingmethod = $query->param('numberingmethod');
299     my $status = 1;
300     my $callnumber = $query->param('callnumber');
301     my $notes = $query->param('notes');
302     my $internalnotes = $query->param('internalnotes');
303     my $hemisphere = $query->param('hemisphere');
304     my $letter = $query->param('letter');
305     my $manualhistory = $query->param('manualhist');
306     my $serialsadditems = $query->param('serialsadditems');
307     # subscription history
308     my $histenddate = format_date_in_iso($query->param('histenddate'));
309     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
310     my $recievedlist = $query->param('recievedlist');
311     my $missinglist = $query->param('missinglist');
312     my $opacnote = $query->param('opacnote');
313     my $librariannote = $query->param('librariannote');
314         my $staffdisplaycount = $query->param('staffdisplaycount');
315         my $opacdisplaycount = $query->param('opacdisplaycount');
316     my $graceperiod     = $query->param('graceperiod') || 0;
317     my $location = $query->param('location');
318         #  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
319     if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
320         ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
321         # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
322         $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
323     }
324
325         ModSubscription(
326             $auser,           $branchcode,   $aqbooksellerid, $cost,
327             $aqbudgetid,      $startdate,    $periodicity,    $firstissuedate,
328             $dow,             join(q{,},@irregularity), $numberpattern,  $numberlength,
329             $weeklength,      $monthlength,  $add1,           $every1,
330             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
331             $add2,            $every2,       $whenmorethan2,  $setto2,
332             $lastvalue2,      $innerloop2,   $add3,           $every3,
333             $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
334             $numberingmethod, $status,       $biblionumber,   $callnumber,
335             $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
336             $serialsadditems, $staffdisplaycount,$opacdisplaycount,$graceperiod,$location,$enddate,$subscriptionid
337         );
338         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
339     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
340     return;
341 }