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