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