reintroducing history manual modification feature
[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);
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 my $weekarrayjs='';
57 my $count = 0;
58 # FIXME - This assumes first pub date of today().
59 # You can't enter past-date irregularities.
60 my ($year, $month, $day) = Today;
61 my $firstday   =  Day_of_Year($year,$month,$day);
62 my ($wkno,$yr) = Week_of_Year($year,$month,$day); # week starting monday
63 my $weekno = $wkno;
64 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
65         #$count = $i;
66         #if($wkno > 52){$year++; $wkno=1;}
67         #if($count>365){$count=$i-365;}    
68         my ($y,$m,$d) = Add_Delta_Days($year,1,1,$i - 1);
69 #warn "$y-$m-$d";
70         #BUGFIX padding add_delta_days() date
71         my $output  =  sprintf("%04d-%02d-%02d",$y , $m, $d );
72         $weekarrayjs .= "'Wk $wkno: ". format_date($output) ."',";
73         $wkno++;    
74 }
75 chop($weekarrayjs);
76 # warn $weekarrayjs;
77
78 my $sub_on;
79 my @subscription_types = (
80             'issues', 'weeks', 'months'
81         ); 
82 my @sub_type_data;
83
84 my $letters = GetLetters('serial');
85 my @letterloop;
86 foreach my $thisletter (keys %$letters) {
87     my $selected = 1 if $thisletter eq $letter;
88     my %row =(value => $thisletter,
89                 selected => $selected,
90                 lettername => $letters->{$thisletter},
91             );
92     push @letterloop, \%row;
93 }
94 $template->param(letterloop => \@letterloop);
95
96 my $onlymine=C4::Context->preference('IndependantBranches') && 
97              C4::Context->userenv && 
98              C4::Context->userenv->{flags}!=1 && 
99              C4::Context->userenv->{branch};
100 my $branches = GetBranches($onlymine);
101 my @branchloop;
102 foreach my $thisbranch (keys %$branches) {
103     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
104     my %row =(value => $thisbranch,
105                 selected => $selected,
106                 branchname => $branches->{$thisbranch}->{'branchname'},
107             );
108     push @branchloop, \%row;
109 }
110 $template->param(branchloop => \@branchloop,
111     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
112 );
113
114 if ($op eq 'mod'||$op eq 'dup') {
115
116     my $subscriptionid = $query->param('subscriptionid');
117 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
118     my $subs = &GetSubscription($subscriptionid);
119 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
120     if ($subs->{'cannotedit'} && $op eq 'mod'){
121       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
122       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
123     }  
124         for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
125          if ($subs->{$_} eq '0000-00-00') {
126             $subs->{$_} = ''
127         } else {
128             $subs->{$_} = format_date($subs->{$_});
129         }
130         }
131     $subs->{'letter'}='' unless($subs->{'letter'});
132     $irregularity   = $subs->{'irregularity'};
133     $numberpattern  = $subs->{'numberpattern'};
134
135
136     if($subs->{numberlength} > 0){
137         $sublength = $subs->{numberlength};
138         $sub_on = $subscription_types[0];
139     } elsif ($subs->{weeklength}>0){
140         $sublength = $subs->{weeklength};
141         $sub_on = $subscription_types[1];
142     } else {
143         $sublength = $subs->{monthlength};
144         $sub_on = $subscription_types[2];
145     }
146     while (@subscription_types) {
147         my $sub_type = shift @subscription_types;
148         my %row = ( 'name' => $sub_type );
149         if ( $sub_on eq $sub_type ) {
150             $row{'selected'} = ' selected';
151         } else {
152             $row{'selected'} = '';
153         }
154         push( @sub_type_data, \%row );
155     }
156
157     $template->param($subs);
158     $template->param(
159             $op => 1,
160             subtype => \@sub_type_data,
161             sublength =>$sublength,
162             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
163             );
164     $template->param(
165                 "periodicity".$subs->{'periodicity'} => 1,
166                 "dow".$subs->{'dow'} => 1,
167                 "numberpattern".$subs->{'numberpattern'} => 1,
168                 );
169 }
170
171 if ($op eq 'addsubscription') {
172     my $auser = $query->param('user');
173     my $branchcode = $query->param('branchcode');
174     my $aqbooksellerid = $query->param('aqbooksellerid');
175     my $cost = $query->param('cost');
176     my $aqbudgetid = $query->param('aqbudgetid'); 
177     my $startdate = $query->param('startdate');
178     my $firstacquidate = $query->param('firstacquidate');    
179     my $periodicity = $query->param('periodicity');
180     my $dow = $query->param('dow');
181     my @irregularity = $query->param('irregular');
182     my $numberlength = 0;
183     my $weeklength = 0;
184     my $monthlength = 0;
185     my $numberpattern = $query->param('numbering_pattern');
186     my $sublength = $query->param('sublength');
187     my $subtype = $query->param('subtype');
188     if ($subtype eq 'months'){
189         $monthlength = $sublength;
190     } elsif ($subtype eq 'weeks'){
191         $weeklength = $sublength;
192     } else {
193         $numberlength = $sublength;
194     }
195     my $add1 = $query->param('add1');
196     my $every1 = $query->param('every1');
197     my $whenmorethan1 = $query->param('whenmorethan1');
198     my $setto1 = $query->param('setto1');
199     my $lastvalue1 = $query->param('lastvalue1');
200     my $innerloop1 =$query->param('innerloop1');
201     my $add2 = $query->param('add2');
202     my $every2 = $query->param('every2');
203     my $whenmorethan2 = $query->param('whenmorethan2');
204     my $setto2 = $query->param('setto2');
205     my $innerloop2 =$query->param('innerloop2');
206     my $lastvalue2 = $query->param('lastvalue2');
207     my $add3 = $query->param('add3');
208     my $every3 = $query->param('every3');
209     my $whenmorethan3 = $query->param('whenmorethan3');
210     my $setto3 = $query->param('setto3');
211     my $lastvalue3 = $query->param('lastvalue3');
212     my $innerloop3 =$query->param('innerloop3');
213     my $numberingmethod = $query->param('numberingmethod');
214     my $status = 1;
215     my $biblionumber = $query->param('biblionumber');
216     my $callnumber = $query->param('callnumber');
217     my $notes = $query->param('notes');
218     my $internalnotes = $query->param('internalnotes');
219     my $hemisphere = $query->param('hemisphere') || 1;
220         my $letter = $query->param('letter');
221     # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
222     my $manualhistory = $query->param('manualhist');
223         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
224                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
225                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
226                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
227                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
228                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
229                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
230                                 );
231
232     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
233 } elsif ($op eq 'modsubscription') {
234     my $subscriptionid = $query->param('subscriptionid');
235     my @irregular = $query->param('irregular');
236     my $irregular_count = @irregular;
237     for(my $i =0;$i<$irregular_count;$i++){
238       $irregularity .=$irregular[$i].",";
239       warn "irregular : $irregular[$i] string :$irregularity";
240     }
241     $irregularity =~ s/\,$//;
242
243     my $auser = $query->param('user');
244     my $librarian => $query->param('librarian'),
245     my $branchcode = $query->param('branchcode');
246     my $cost = $query->param('cost');
247     my $aqbooksellerid = $query->param('aqbooksellerid');
248     my $biblionumber = $query->param('biblionumber');
249     my $aqbudgetid = $query->param('aqbudgetid');
250     my $startdate = format_date_in_iso($query->param('startdate'));
251     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
252     my $periodicity = $query->param('periodicity');
253     my $dow = $query->param('dow');
254     my $sublength = $query->param('sublength');
255     my $subtype = $query->param('subtype');
256
257     if($subtype eq 'months'){
258         $monthlength = $sublength;
259     } elsif ($subtype eq 'weeks'){
260         $weeklength = $sublength;
261     } else {
262         $numberlength = $sublength;
263     }
264     my $numberpattern = $query->param('numbering_pattern');
265     my $add1 = $query->param('add1');
266     my $every1 = $query->param('every1');
267     my $whenmorethan1 = $query->param('whenmorethan1');
268     my $setto1 = $query->param('setto1');
269     my $lastvalue1 = $query->param('lastvalue1');
270     my $innerloop1 = $query->param('innerloop1');
271     my $add2 = $query->param('add2');
272     my $every2 = $query->param('every2');
273     my $whenmorethan2 = $query->param('whenmorethan2');
274     my $setto2 = $query->param('setto2');
275     my $lastvalue2 = $query->param('lastvalue2');
276     my $innerloop2 = $query->param('innerloop2');
277     my $add3 = $query->param('add3');
278     my $every3 = $query->param('every3');
279     my $whenmorethan3 = $query->param('whenmorethan3');
280     my $setto3 = $query->param('setto3');
281     my $lastvalue3 = $query->param('lastvalue3');
282     my $innerloop3 = $query->param('innerloop3');
283     my $numberingmethod = $query->param('numberingmethod');
284     my $status = 1;
285     my $callnumber = $query->param('callnumber');
286     my $notes = $query->param('notes');
287     my $internalnotes = $query->param('internalnotes');
288     my $hemisphere = $query->param('hemisphere');
289     my $letter = $query->param('letter');
290     my $manualhistory = $query->param('manualhist');
291     my $enddate = $query->param('enddate');
292     # subscription history
293     my $histenddate = format_date_in_iso($query->param('histenddate'));
294     warn "HIST END : $histenddate";
295     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
296     my $recievedlist = $query->param('recievedlist');
297     my $missinglist = $query->param('missinglist');
298     my $opacnote = $query->param('opacnote');
299     my $librariannote = $query->param('librariannote');
300     my $history_only = $query->param('history_only');
301     if ($history_only) {
302         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
303     } else {
304         &ModSubscription(
305             $auser,           $branchcode,   $aqbooksellerid, $cost,
306             $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
307             $dow,             $irregularity, $numberpattern,  $numberlength,
308             $weeklength,      $monthlength,  $add1,           $every1,
309             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
310             $add2,            $every2,       $whenmorethan2,  $setto2,
311             $lastvalue2,      $innerloop2,   $add3,           $every3,
312             $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
313             $numberingmethod, $status,       $biblionumber,   $callnumber,
314             $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
315             $subscriptionid);
316     }
317     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
318 } else {
319
320         while (@subscription_types) {
321            my $sub_type = shift @subscription_types;
322            my %row = ( 'name' => $sub_type );
323            if ( $sub_on eq $sub_type ) {
324              $row{'selected'} = ' selected';
325            } else {
326              $row{'selected'} = '';
327            }
328            push( @sub_type_data, \%row );
329         }    
330     $template->param(subtype => \@sub_type_data,
331                  weekarrayjs => $weekarrayjs,
332                  weekno => $weekno,
333         );
334         output_html_with_http_headers $query, $cookie, $template->output;
335 }