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