Serials planning: Update subscription edit to properly deal with date changes. (...
[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 CGI;
20 use Date::Calc qw(Today Day_of_Year Week_of_Year Add_Delta_Days);
21 use C4::Koha;
22 use C4::Biblio;
23 use C4::Auth;
24 use C4::Dates qw/format_date format_date_in_iso/;
25 use C4::Acquisition;
26 use C4::Output;
27 use C4::Context;
28 use C4::Branch; # GetBranches
29 use C4::Serials;
30 use C4::Letters;
31
32 #use Smart::Comments;
33
34 my $query = new CGI;
35 my $op = $query->param('op');
36 my $dbh = C4::Context->dbh;
37 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
38         $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
39         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
40         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
41         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
42         $numberingmethod, $status, $biblionumber, 
43         $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory,$serialsadditems);
44
45         my @budgets;
46 my ($template, $loggedinuser, $cookie)
47 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
48                                 query => $query,
49                                 type => "intranet",
50                                 authnotrequired => 0,
51                                 flagsrequired => {serials => 1},
52                                 debug => 1,
53                                 });
54
55
56
57 my $sub_on;
58 my @subscription_types = (
59             'issues', 'weeks', 'months'
60         ); 
61 my @sub_type_data;
62
63 my $letters = GetLetters('serial');
64 my @letterloop;
65 foreach my $thisletter (keys %$letters) {
66     my $selected = 1 if $thisletter eq $letter;
67     my %row =(value => $thisletter,
68                 selected => $selected,
69                 lettername => $letters->{$thisletter},
70             );
71     push @letterloop, \%row;
72 }
73 $template->param(letterloop => \@letterloop);
74
75 my $onlymine=C4::Context->preference('IndependantBranches') && 
76              C4::Context->userenv && 
77              C4::Context->userenv->{flags}!=1 && 
78              C4::Context->userenv->{branch};
79 my $branches = GetBranches($onlymine);
80 my @branchloop;
81 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
82     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
83     my %row =(value => $thisbranch,
84                 selected => $selected,
85                 branchname => $branches->{$thisbranch}->{'branchname'},
86             );
87     push @branchloop, \%row;
88 }
89 $template->param(branchloop => \@branchloop,
90     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
91 );
92 my $subscriptionid;
93 my $subs;
94 my $firstissuedate;
95 my $nextexpected;
96
97 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
98
99     $subscriptionid = $query->param('subscriptionid');
100     $subs = &GetSubscription($subscriptionid);
101 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
102     if ($subs->{'cannotedit'} && $op eq 'mod'){
103       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
104       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
105     } 
106     $firstissuedate = $subs->{firstacquidate};  # in iso format.
107     for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
108         # TODO : Handle date formats properly.
109          if ($subs->{$_} eq '0000-00-00') {
110             $subs->{$_} = ''
111         } else {
112             $subs->{$_} = format_date($subs->{$_});  
113         }
114         }
115     $subs->{'letter'}='' unless($subs->{'letter'});
116     $irregularity   = $subs->{'irregularity'};
117     $numberpattern  = $subs->{'numberpattern'};
118     $nextexpected = GetNextExpected($subscriptionid);
119     $nextexpected->{ isfirstissue => ($nextexpected->{planneddate}->output('iso') eq $firstissuedate)};
120     # firstacquidate is taken to be the upcoming issue's planned date if we're modifying the sub
121     $subs->{nextacquidate} = $nextexpected->{planneddate}->output()  if($op eq 'mod');
122
123   unless($op eq 'modsubscription') {
124     if($subs->{numberlength} > 0){
125         $sublength = $subs->{numberlength};
126         $sub_on = $subscription_types[0];
127     } elsif ($subs->{weeklength}>0){
128         $sublength = $subs->{weeklength};
129         $sub_on = $subscription_types[1];
130     } else {
131         $sublength = $subs->{monthlength};
132         $sub_on = $subscription_types[2];
133     }
134     while (@subscription_types) {
135         my $sub_type = shift @subscription_types;
136         my %row = ( 'name' => $sub_type );
137         if ( $sub_on eq $sub_type ) {
138             $row{'selected'} = ' selected';
139         } else {
140             $row{'selected'} = '';
141         }
142         push( @sub_type_data, \%row );
143     }
144
145     $template->param($subs);
146     $template->param(
147                 $op => 1,
148                 subtype => \@sub_type_data,
149                 sublength =>$sublength,
150                 history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote})),
151                 "periodicity".$subs->{'periodicity'} => 1,
152                 "dow".$subs->{'dow'} => 1,
153                 "numberpattern".$subs->{'numberpattern'} => 1,
154                 firstacquiyear => substr($firstissuedate,0,4),
155                 );
156   }
157 }
158
159 my $count = 0;
160 # prepare template variables common to all $op conditions:
161 $template->param(  'dateformat_' . C4::Context->preference('dateformat') => 1 ,
162                 );
163
164 if ($op eq 'addsubscription') {
165     my $auser = $query->param('user');
166     my $branchcode = $query->param('branchcode');
167     my $aqbooksellerid = $query->param('aqbooksellerid');
168     my $cost = $query->param('cost');
169     my $aqbudgetid = $query->param('aqbudgetid'); 
170     my $startdate = $query->param('startdate');
171     my $firstacquidate = $query->param('firstacquidate');    
172     my $periodicity = $query->param('periodicity');
173     my $dow = $query->param('dow');
174     my @irregularity = $query->param('irregularity_select');
175     my $numberlength = 0;
176     my $weeklength = 0;
177     my $monthlength = 0;
178     my $numberpattern = $query->param('numbering_pattern');
179     my $sublength = $query->param('sublength');
180     my $subtype = $query->param('subtype');
181     if ($subtype eq 'months'){
182         $monthlength = $sublength;
183     } elsif ($subtype eq 'weeks'){
184         $weeklength = $sublength;
185     } else {
186         $numberlength = $sublength;
187     }
188     my $add1 = $query->param('add1');
189     my $every1 = $query->param('every1');
190     my $whenmorethan1 = $query->param('whenmorethan1');
191     my $setto1 = $query->param('setto1');
192     my $lastvalue1 = $query->param('lastvalue1');
193     my $innerloop1 =$query->param('innerloop1');
194     my $add2 = $query->param('add2');
195     my $every2 = $query->param('every2');
196     my $whenmorethan2 = $query->param('whenmorethan2');
197     my $setto2 = $query->param('setto2');
198     my $innerloop2 =$query->param('innerloop2');
199     my $lastvalue2 = $query->param('lastvalue2');
200     my $add3 = $query->param('add3');
201     my $every3 = $query->param('every3');
202     my $whenmorethan3 = $query->param('whenmorethan3');
203     my $setto3 = $query->param('setto3');
204     my $lastvalue3 = $query->param('lastvalue3');
205     my $innerloop3 =$query->param('innerloop3');
206     my $numberingmethod = $query->param('numberingmethod');
207     my $status = 1;
208     my $biblionumber = $query->param('biblionumber');
209     my $callnumber = $query->param('callnumber');
210     my $notes = $query->param('notes');
211     my $internalnotes = $query->param('internalnotes');
212     my $hemisphere = $query->param('hemisphere') || 1;
213         my $letter = $query->param('letter');
214     # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
215     my $manualhistory = $query->param('manualhist');
216     my $serialsadditems = $query->param('serialsadditems');
217         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
218                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
219                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
220                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
221                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
222                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
223                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
224                     $serialsadditems,
225                                 );
226
227     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
228 } elsif ($op eq 'modsubscription') {
229     my $subscriptionid = $query->param('subscriptionid');
230         my @irregularity = $query->param('irregularity_select');
231     my $auser = $query->param('user');
232     my $librarian => $query->param('librarian'),
233     my $branchcode = $query->param('branchcode');
234     my $cost = $query->param('cost');
235     my $aqbooksellerid = $query->param('aqbooksellerid');
236     my $biblionumber = $query->param('biblionumber');
237     my $aqbudgetid = $query->param('aqbudgetid');
238     my $startdate = format_date_in_iso($query->param('startdate'));
239     my $nextacquidate = format_date_in_iso($query->param('nextacquidate'));    
240     my $periodicity = $query->param('periodicity');
241     my $dow = $query->param('dow');
242     my $sublength = $query->param('sublength');
243     my $subtype = $query->param('subtype');
244
245     if($subtype eq 'months'){
246         $monthlength = $sublength;
247     } elsif ($subtype eq 'weeks'){
248         $weeklength = $sublength;
249     } else {
250         $numberlength = $sublength;
251     }
252     my $numberpattern = $query->param('numbering_pattern');
253     my $add1 = $query->param('add1');
254     my $every1 = $query->param('every1');
255     my $whenmorethan1 = $query->param('whenmorethan1');
256     my $setto1 = $query->param('setto1');
257     my $lastvalue1 = $query->param('lastvalue1');
258     my $innerloop1 = $query->param('innerloop1');
259     my $add2 = $query->param('add2');
260     my $every2 = $query->param('every2');
261     my $whenmorethan2 = $query->param('whenmorethan2');
262     my $setto2 = $query->param('setto2');
263     my $lastvalue2 = $query->param('lastvalue2');
264     my $innerloop2 = $query->param('innerloop2');
265     my $add3 = $query->param('add3');
266     my $every3 = $query->param('every3');
267     my $whenmorethan3 = $query->param('whenmorethan3');
268     my $setto3 = $query->param('setto3');
269     my $lastvalue3 = $query->param('lastvalue3');
270     my $innerloop3 = $query->param('innerloop3');
271     my $numberingmethod = $query->param('numberingmethod');
272     my $status = 1;
273     my $callnumber = $query->param('callnumber');
274     my $notes = $query->param('notes');
275     my $internalnotes = $query->param('internalnotes');
276     my $hemisphere = $query->param('hemisphere');
277     my $letter = $query->param('letter');
278     my $manualhistory = $query->param('manualhist');
279     my $enddate = $query->param('enddate');
280     my $serialsadditems = $query->param('serialsadditems');
281     # subscription history
282     my $histenddate = format_date_in_iso($query->param('histenddate'));
283     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
284     my $recievedlist = $query->param('recievedlist');
285     my $missinglist = $query->param('missinglist');
286     my $opacnote = $query->param('opacnote');
287     my $librariannote = $query->param('librariannote');
288     my $history_only = $query->param('history_only');
289         #  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
290     #  Here firstacquidate is interpreted as nextacquidate.     
291
292     if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
293         ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
294         $firstissuedate = $nextexpected->{planneddate}->output('iso') if($nextexpected->{isfirstissue});
295     }
296     
297     if ($history_only) {
298         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
299     } else {
300         &ModSubscription(
301             $auser,           $branchcode,   $aqbooksellerid, $cost,
302             $aqbudgetid,      $startdate,    $periodicity,    $firstissuedate,
303             $dow,             join(",",@irregularity), $numberpattern,  $numberlength,
304             $weeklength,      $monthlength,  $add1,           $every1,
305             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
306             $add2,            $every2,       $whenmorethan2,  $setto2,
307             $lastvalue2,      $innerloop2,   $add3,           $every3,
308             $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
309             $numberingmethod, $status,       $biblionumber,   $callnumber,
310             $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
311             $serialsadditems, $subscriptionid,
312         );
313     }
314     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
315 } else {
316
317         while (@subscription_types) {
318            my $sub_type = shift @subscription_types;
319            my %row = ( 'name' => $sub_type );
320            if ( $sub_on eq $sub_type ) {
321              $row{'selected'} = ' selected';
322            } else {
323              $row{'selected'} = '';
324            }
325            push( @sub_type_data, \%row );
326         }    
327     $template->param(subtype => \@sub_type_data,
328         );
329         output_html_with_http_headers $query, $cookie, $template->output;
330 }