fix for #1430 (subscription add fails)
[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::Manip;
21 use C4::Koha;
22 use C4::Auth;
23 use C4::Date;
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 my $query = new CGI;
32 my $op = $query->param('op');
33 my $dbh = C4::Context->dbh;
34 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
35         $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
36         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
37         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
38         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
39         $numberingmethod, $status, $biblionumber, 
40         $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory);
41
42         my @budgets;
43 my ($template, $loggedinuser, $cookie)
44 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
45                                 query => $query,
46                                 type => "intranet",
47                                 authnotrequired => 0,
48                                 flagsrequired => {serials => 1},
49                                 debug => 1,
50                                 });
51
52
53 my $weekarrayjs='';
54 my $count = 0;
55 my ($year, $month, $day) = UnixDate("today", "%Y", "%m", "%d");
56 my $firstday = Date_DayOfYear($month,$day,$year);
57 my $wkno = Date_WeekOfYear($month,$day,$year,1); # week starting monday
58 my $weekno = $wkno;
59 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
60         $count = $i;
61         if($wkno > 52){$year++; $wkno=1;}
62         if($count>365){$count=$i-365;}    
63         my ($y,$m,$d) = Date_NthDayOfYear($year,$count);
64         my $output = "$y-$m-$d";
65         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
66         $wkno++;    
67 }
68 chop($weekarrayjs);
69 # warn $weekarrayjs;
70
71 my $sub_on;
72 my @subscription_types = (
73             'issues', 'weeks', 'months'
74         ); 
75 my @sub_type_data;
76
77 my $letters = GetLetters('serial');
78 my @letterloop;
79 foreach my $thisletter (keys %$letters) {
80     my $selected = 1 if $thisletter eq $letter;
81     my %row =(value => $thisletter,
82                 selected => $selected,
83                 lettername => $letters->{$thisletter},
84             );
85     push @letterloop, \%row;
86 }
87 $template->param(letterloop => \@letterloop);
88
89 my $onlymine=C4::Context->preference('IndependantBranches') && 
90              C4::Context->userenv && 
91              C4::Context->userenv->{flags}!=1 && 
92              C4::Context->userenv->{branch};
93 my $branches = GetBranches($onlymine);
94 my @branchloop;
95 foreach my $thisbranch (keys %$branches) {
96     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
97     my %row =(value => $thisbranch,
98                 selected => $selected,
99                 branchname => $branches->{$thisbranch}->{'branchname'},
100             );
101     push @branchloop, \%row;
102 }
103 $template->param(branchloop => \@branchloop,
104     DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
105 );
106
107 if ($op eq 'mod'||$op eq 'dup') {
108     my $subscriptionid = $query->param('subscriptionid');
109 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
110     my $subs = &GetSubscription($subscriptionid);
111     $subs->{'startdate'}=format_date($subs->{'startdate'});
112     $subs->{'firstacquidate'}=format_date($subs->{'firstacquidate'});
113     $subs->{'histstartdate'}=format_date($subs->{'histstartdate'});
114     $subs->{'enddate'}=format_date($subs->{enddate});
115     $subs->{'letter'}='' unless($subs->{'letter'});
116
117     if($subs->{numberlength} > 0){
118         $sublength = $subs->{numberlength};
119         $sub_on = $subscription_types[0];
120     } elsif ($subs->{weeklength}>0){
121         $sublength = $weeklength;
122         $sub_on = $subscription_types[1];
123     } else {
124         $sublength = $subs->{monthlength};
125         $sub_on = $subscription_types[2];
126     }
127     while (@subscription_types) {
128         my $sub_type = shift @subscription_types;
129         my %row = ( 'name' => $sub_type );
130         if ( $sub_on eq $sub_type ) {
131             $row{'selected'} = ' selected';
132         } else {
133             $row{'selected'} = '';
134         }
135         push( @sub_type_data, \%row );
136     }
137
138     $template->param($subs);
139     $template->param(
140             $op => 1,
141             subtype => \@sub_type_data,
142             sublength =>$sublength,
143             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
144             );
145     $template->param(
146                 "periodicity".$subs->{'periodicity'} => 1,
147                 "dow".$subs->{'periodicity'} => 1,
148                 "numberpattern".$subs->{'periodicity'} => 1,
149                 );
150 }
151
152 if ($op eq 'addsubscription') {
153     my @irregular = $query->param('irregular');
154     my $irregular_count = scalar(@irregular);
155     for(my $i =0;$i<$irregular_count;$i++){
156         $irregularity .=$irregular[$i].",";
157     }
158     $irregularity =~ s/\,$//;
159
160     my $auser = $query->param('user');
161     my $branchcode = $query->param('branchcode');
162     my $aqbooksellerid = $query->param('aqbooksellerid');
163     my $cost = $query->param('cost');
164     my $aqbudgetid = $query->param('aqbudgetid'); 
165     my $startdate = $query->param('startdate');
166     my $firstacquidate = $query->param('firstacquidate');    
167     my $periodicity = $query->param('periodicity');
168     my $dow = $query->param('dow');
169     my $irregularity = $query->param('irregularity');
170     my $numberlength = 0;
171     my $weeklength = 0;
172     my $monthlength = 0;
173     my $numberpattern = $query->param('numbering_pattern');
174     my $sublength = $query->param('sublength');
175     my $subtype = $query->param('subtype');
176     if ($subtype eq 'months'){
177         $monthlength = $sublength;
178     } elsif ($subtype eq 'weeks'){
179         $weeklength = $sublength;
180     } else {
181         $numberlength = $sublength;
182     }
183
184     my $add1 = $query->param('add1');
185     my $every1 = $query->param('every1');
186     my $whenmorethan1 = $query->param('whenmorethan1');
187     my $setto1 = $query->param('setto1');
188     my $lastvalue1 = $query->param('lastvalue1');
189     my $add2 = $query->param('add2');
190     my $every2 = $query->param('every2');
191     my $whenmorethan2 = $query->param('whenmorethan2');
192     my $setto2 = $query->param('setto2');
193     my $lastvalue2 = $query->param('lastvalue2');
194     my $add3 = $query->param('add3');
195     my $every3 = $query->param('every3');
196     my $whenmorethan3 = $query->param('whenmorethan3');
197     my $setto3 = $query->param('setto3');
198     my $lastvalue3 = $query->param('lastvalue3');
199     my $numberingmethod = $query->param('numberingmethod');
200     my $status = 1;
201     my $biblionumber = $query->param('biblionumber');
202     my $callnumber = $query->param('callnumber');
203     my $notes = $query->param('notes');
204     my $internalnotes = $query->param('internalnotes');
205     my $hemisphere = $query->param('hemisphere') || 1;
206         my $letter = $query->param('letter');
207     ### BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
208     my $manualhistory = $query->param('manualhist');
209     my ($innerloop1,$innerloop2,$innerloop3);
210         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
211                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
212                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
213                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
214                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
215                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,$irregularity,
216                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
217                                 );
218
219     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
220 } elsif ($op eq 'modsubscription') {
221     my $subscriptionid = $query->param('subscriptionid');
222     my @irregular = $query->param('irregular');
223     my $irregular_count = @irregular;
224     for(my $i =0;$i<$irregular_count;$i++){
225       $irregularity .=$irregular[$i].",";
226       warn "irregular : $irregular[$i] string :$irregularity";
227     }
228     $irregularity =~ s/\,$//;
229
230     my $auser = $query->param('user');
231     my $librarian => $query->param('librarian'),
232     my $branchcode = $query->param('branchcode');
233     my $cost = $query->param('cost');
234     my $aqbooksellerid = $query->param('aqbooksellerid');
235     my $biblionumber = $query->param('biblionumber');
236     my $aqbudgetid = $query->param('aqbudgetid');
237     my $startdate = format_date_in_iso($query->param('startdate'));
238     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
239     my $periodicity = $query->param('periodicity');
240     my $dow = $query->param('dow');
241     my $sublength = $query->param('sublength');
242     my $subtype = $query->param('subtype');
243
244     if($subtype eq 'months'){
245         $monthlength = $sublength;
246     } elsif ($subtype eq 'weeks'){
247         $weeklength = $sublength;
248     } else {
249         $numberlength = $sublength;
250     }
251     my $numberpattern = $query->param('numbering_pattern');
252     my $add1 = $query->param('add1');
253     my $every1 = $query->param('every1');
254     my $whenmorethan1 = $query->param('whenmorethan1');
255     my $setto1 = $query->param('setto1');
256     my $lastvalue1 = $query->param('lastvalue1');
257     my $innerloop1 = $query->param('innerloop1');
258     my $add2 = $query->param('add2');
259     my $every2 = $query->param('every2');
260     my $whenmorethan2 = $query->param('whenmorethan2');
261     my $setto2 = $query->param('setto2');
262     my $lastvalue2 = $query->param('lastvalue2');
263     my $innerloop2 = $query->param('innerloop2');
264     my $add3 = $query->param('add3');
265     my $every3 = $query->param('every3');
266     my $whenmorethan3 = $query->param('whenmorethan3');
267     my $setto3 = $query->param('setto3');
268     my $lastvalue3 = $query->param('lastvalue3');
269     my $innerloop3 = $query->param('innerloop3');
270     my $numberingmethod = $query->param('numberingmethod');
271     my $status = 1;
272     my $callnumber = $query->param('callnumber');
273     my $notes = $query->param('notes');
274     my $internalnotes = $query->param('internalnotes');
275     my $hemisphere = $query->param('hemisphere');
276     my $letter = $query->param('letter');
277     my $manualhistory = $query->param('manualhist');
278     my $enddate = $query->param('enddate');
279     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
280     my $recievedlist = $query->param('recievedlist');
281     my $missinglist = $query->param('missinglist');
282     my $opacnote = $query->param('opacnote');
283     my $librariannote = $query->param('librariannote');
284     &ModSubscription(
285         $auser,           $branchcode,   $aqbooksellerid, $cost,
286         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
287         $dow,             $irregularity, $numberpattern,  $numberlength,
288         $weeklength,      $monthlength,  $add1,           $every1,
289         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
290         $add2,            $every2,       $whenmorethan2,  $setto2,
291         $lastvalue2,      $innerloop2,   $add3,           $every3,
292         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
293         $numberingmethod, $status,       $biblionumber,   $callnumber,
294         $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
295         $subscriptionid);
296
297     ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
298     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
299 } else {
300
301         while (@subscription_types) {
302            my $sub_type = shift @subscription_types;
303            my %row = ( 'name' => $sub_type );
304            if ( $sub_on eq $sub_type ) {
305              $row{'selected'} = ' selected';
306            } else {
307              $row{'selected'} = '';
308            }
309            push( @sub_type_data, \%row );
310         }    
311     $template->param(subtype => \@sub_type_data,
312                  weekarrayjs => $weekarrayjs,
313                  weekno => $weekno,
314         );
315         output_html_with_http_headers $query, $cookie, $template->output;
316 }