set of serials fixes to subscriptions
[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::Auth;
23 use C4::Dates qw/format_date format_date_in_iso/;
24 use C4::Acquisition;
25 use C4::Output;
26 use C4::Context;
27 use C4::Branch; # GetBranches
28 use C4::Serials;
29 use C4::Letters;
30
31 #use Smart::Comments;
32
33 my $query = new CGI;
34 my $op = $query->param('op');
35 my $dbh = C4::Context->dbh;
36 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
37         $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
38         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
39         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
40         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
41         $numberingmethod, $status, $biblionumber, 
42         $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory);
43
44         my @budgets;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
47                                 query => $query,
48                                 type => "intranet",
49                                 authnotrequired => 0,
50                                 flagsrequired => {serials => 1},
51                                 debug => 1,
52                                 });
53
54
55 my $weekarrayjs='';
56 my $count = 0;
57 my ($year, $month, $day) = Today;
58 my $firstday   =  Day_of_Year($year,$month,$day);
59 my ($wkno,$yr) = Week_of_Year($year,$month,$day); # week starting monday
60 my $weekno = $wkno;
61 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
62         $count = $i;
63         if($wkno > 52){$year++; $wkno=1;}
64         if($count>365){$count=$i-365;}    
65         my ($y,$m,$d) = Add_Delta_Days(1,1,1,$i - 1);
66
67         #BUGFIX padding add_delta_days() date
68         my $output  = sprintf("%04d-%02d-%02d",$y , $m, $d );
69
70         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
71         $wkno++;    
72 }
73 chop($weekarrayjs);
74 # warn $weekarrayjs;
75
76 my $sub_on;
77 my @subscription_types = (
78             'issues', 'weeks', 'months'
79         ); 
80 my @sub_type_data;
81
82 my $letters = GetLetters('serial');
83 my @letterloop;
84 foreach my $thisletter (keys %$letters) {
85     my $selected = 1 if $thisletter eq $letter;
86     my %row =(value => $thisletter,
87                 selected => $selected,
88                 lettername => $letters->{$thisletter},
89             );
90     push @letterloop, \%row;
91 }
92 $template->param(letterloop => \@letterloop);
93
94 my $onlymine=C4::Context->preference('IndependantBranches') && 
95              C4::Context->userenv && 
96              C4::Context->userenv->{flags}!=1 && 
97              C4::Context->userenv->{branch};
98 my $branches = GetBranches($onlymine);
99 my @branchloop;
100 foreach my $thisbranch (keys %$branches) {
101     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
102     my %row =(value => $thisbranch,
103                 selected => $selected,
104                 branchname => $branches->{$thisbranch}->{'branchname'},
105             );
106     push @branchloop, \%row;
107 }
108 $template->param(branchloop => \@branchloop,
109     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
110 );
111
112 if ($op eq 'mod'||$op eq 'dup') {
113
114     my $subscriptionid = $query->param('subscriptionid');
115 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
116     my $subs = &GetSubscription($subscriptionid);
117 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
118     if ($subs->{'cannotedit'} && $op eq 'mod'){
119       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
120       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
121     }  
122         for (qw(startdate firstacquidate histstartdate enddate)) {
123         $subs->{$_} = format_date($subs->{$_}) if $subs->{$_};
124         }
125     $subs->{'letter'}='' unless($subs->{'letter'});
126     $irregularity   = $subs->{'irregularity'};
127     $numberpattern  = $subs->{'numberpattern'};
128
129
130     if($subs->{numberlength} > 0){
131         $sublength = $subs->{numberlength};
132         $sub_on = $subscription_types[0];
133     } elsif ($subs->{weeklength}>0){
134         $sublength = $subs->{weeklength};
135         $sub_on = $subscription_types[1];
136     } else {
137         $sublength = $subs->{monthlength};
138         $sub_on = $subscription_types[2];
139     }
140     while (@subscription_types) {
141         my $sub_type = shift @subscription_types;
142         my %row = ( 'name' => $sub_type );
143         if ( $sub_on eq $sub_type ) {
144             $row{'selected'} = ' selected';
145         } else {
146             $row{'selected'} = '';
147         }
148         push( @sub_type_data, \%row );
149     }
150
151     $template->param($subs);
152     $template->param(
153             $op => 1,
154             subtype => \@sub_type_data,
155             sublength =>$sublength,
156             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
157             );
158     $template->param(
159                 "periodicity".$subs->{'periodicity'} => 1,
160                 "dow".$subs->{'dow'} => 1,
161                 "numberpattern".$subs->{'numberpattern'} => 1,
162                 );
163 }
164
165 if ($op eq 'addsubscription') {
166
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 }