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