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