More test files
[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
105 if ($op eq 'mod'||$op eq 'dup') {
106     my $subscriptionid = $query->param('subscriptionid');
107 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
108     my $subs = &GetSubscription($subscriptionid);
109     $subs->{'startdate'}=format_date($subs->{'startdate'});
110     $subs->{'firstacquidate'}=format_date($subs->{'firstacquidate'});
111     $subs->{'histstartdate'}=format_date($subs->{'histstartdate'});
112     $subs->{'enddate'}=format_date($subs->{enddate});
113     $subs->{'letter'}='' unless($subs->{'letter'});
114
115     if($subs->{numberlength} > 0){
116         $sublength = $subs->{numberlength};
117         $sub_on = $subscription_types[0];
118     } elsif ($subs->{weeklength}>0){
119         $sublength = $weeklength;
120         $sub_on = $subscription_types[1];
121     } else {
122         $sublength = $subs->{monthlength};
123         $sub_on = $subscription_types[2];
124     }
125     while (@subscription_types) {
126         my $sub_type = shift @subscription_types;
127         my %row = ( 'name' => $sub_type );
128         if ( $sub_on eq $sub_type ) {
129             $row{'selected'} = ' selected';
130         } else {
131             $row{'selected'} = '';
132         }
133         push( @sub_type_data, \%row );
134     }
135
136     $template->param($subs);
137     $template->param(
138             $op => 1,
139             subtype => \@sub_type_data,
140             sublength =>$sublength,
141             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
142             );
143     $template->param(
144                 "periodicity".$subs->{'periodicity'} => 1,
145                 "dow".$subs->{'periodicity'} => 1,
146                 "numberpattern".$subs->{'periodicity'} => 1,
147                 );
148 }
149
150 if ($op eq 'addsubscription') {
151     my @irregular = $query->param('irregular');
152     my $irregular_count = scalar(@irregular);
153     for(my $i =0;$i<$irregular_count;$i++){
154         $irregularity .=$irregular[$i].",";
155     }
156     $irregularity =~ s/\,$//;
157
158     my $auser = $query->param('user');
159     my $branchcode = $query->param('branchcode');
160     my $aqbooksellerid = $query->param('aqbooksellerid');
161     my $cost = $query->param('cost');
162     my $aqbudgetid = $query->param('aqbudgetid'); 
163     my $startdate = $query->param('startdate');
164     my $firstacquidate = $query->param('firstacquidate');    
165     my $periodicity = $query->param('periodicity');
166     my $dow = $query->param('dow');
167     my $irregularity = $query->param('irregularity');
168     my $numberlength = 0;
169     my $weeklength = 0;
170     my $monthlength = 0;
171     my $numberpattern = $query->param('numbering_pattern');
172     my $sublength = $query->param('sublength');
173     my $subtype = $query->param('subtype');
174     if ($subtype eq 'months'){
175         $monthlength = $sublength;
176     } elsif ($subtype eq 'weeks'){
177         $weeklength = $sublength;
178     } else {
179         $numberlength = $sublength;
180     }
181
182     my $add1 = $query->param('add1');
183     my $every1 = $query->param('every1');
184     my $whenmorethan1 = $query->param('whenmorethan1');
185     my $setto1 = $query->param('setto1');
186     my $lastvalue1 = $query->param('lastvalue1');
187     my $add2 = $query->param('add2');
188     my $every2 = $query->param('every2');
189     my $whenmorethan2 = $query->param('whenmorethan2');
190     my $setto2 = $query->param('setto2');
191     my $lastvalue2 = $query->param('lastvalue2');
192     my $add3 = $query->param('add3');
193     my $every3 = $query->param('every3');
194     my $whenmorethan3 = $query->param('whenmorethan3');
195     my $setto3 = $query->param('setto3');
196     my $lastvalue3 = $query->param('lastvalue3');
197     my $numberingmethod = $query->param('numberingmethod');
198     my $status = 1;
199     my $biblionumber = $query->param('biblionumber');
200     my $callnumber = $query->param('callnumber');
201     my $notes = $query->param('notes');
202     my $internalnotes = $query->param('internalnotes');
203     my $hemisphere = $query->param('hemisphere') || 1;
204         my $letter = $query->param('letter');
205     ### BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
206     my $manualhistory = $query->param('manualhist');
207     my ($innerloop1,$innerloop2,$innerloop3);
208         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
209                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
210                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
211                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
212                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
213                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,$irregularity,
214                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
215                                 );
216
217     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
218 } elsif ($op eq 'modsubscription') {
219     my $subscriptionid = $query->param('subscriptionid');
220     my @irregular = $query->param('irregular');
221     my $irregular_count = @irregular;
222     for(my $i =0;$i<$irregular_count;$i++){
223       $irregularity .=$irregular[$i].",";
224       warn "irregular : $irregular[$i] string :$irregularity";
225     }
226     $irregularity =~ s/\,$//;
227
228     my $auser = $query->param('user');
229     my $librarian => $query->param('librarian'),
230     my $branchcode = $query->param('branchcode');
231     my $cost = $query->param('cost');
232     my $aqbooksellerid = $query->param('aqbooksellerid');
233     my $biblionumber = $query->param('biblionumber');
234     my $aqbudgetid = $query->param('aqbudgetid');
235     my $startdate = format_date_in_iso($query->param('startdate'));
236     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
237     my $periodicity = $query->param('periodicity');
238     my $dow = $query->param('dow');
239     my $sublength = $query->param('sublength');
240     my $subtype = $query->param('subtype');
241
242     if($subtype eq 'months'){
243         $monthlength = $sublength;
244     } elsif ($subtype eq 'weeks'){
245         $weeklength = $sublength;
246     } else {
247         $numberlength = $sublength;
248     }
249     my $numberpattern = $query->param('numbering_pattern');
250     my $add1 = $query->param('add1');
251     my $every1 = $query->param('every1');
252     my $whenmorethan1 = $query->param('whenmorethan1');
253     my $setto1 = $query->param('setto1');
254     my $lastvalue1 = $query->param('lastvalue1');
255     my $innerloop1 = $query->param('innerloop1');
256     my $add2 = $query->param('add2');
257     my $every2 = $query->param('every2');
258     my $whenmorethan2 = $query->param('whenmorethan2');
259     my $setto2 = $query->param('setto2');
260     my $lastvalue2 = $query->param('lastvalue2');
261     my $innerloop2 = $query->param('innerloop2');
262     my $add3 = $query->param('add3');
263     my $every3 = $query->param('every3');
264     my $whenmorethan3 = $query->param('whenmorethan3');
265     my $setto3 = $query->param('setto3');
266     my $lastvalue3 = $query->param('lastvalue3');
267     my $innerloop3 = $query->param('innerloop3');
268     my $numberingmethod = $query->param('numberingmethod');
269     my $status = 1;
270     my $callnumber = $query->param('callnumber');
271     my $notes = $query->param('notes');
272     my $internalnotes = $query->param('internalnotes');
273     my $hemisphere = $query->param('hemisphere');
274     my $letter = $query->param('letter');
275     my $manualhistory = $query->param('manualhist');
276     my $enddate = $query->param('enddate');
277     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
278     my $recievedlist = $query->param('recievedlist');
279     my $missinglist = $query->param('missinglist');
280     my $opacnote = $query->param('opacnote');
281     my $librariannote = $query->param('librariannote');
282     &ModSubscription(
283         $auser,           $branchcode,   $aqbooksellerid, $cost,
284         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
285         $dow,             $irregularity, $numberpattern,  $numberlength,
286         $weeklength,      $monthlength,  $add1,           $every1,
287         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
288         $add2,            $every2,       $whenmorethan2,  $setto2,
289         $lastvalue2,      $innerloop2,   $add3,           $every3,
290         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
291         $numberingmethod, $status,       $biblionumber,   $callnumber,
292         $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
293         $subscriptionid);
294
295     ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
296     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
297 } else {
298
299         while (@subscription_types) {
300            my $sub_type = shift @subscription_types;
301            my %row = ( 'name' => $sub_type );
302            if ( $sub_on eq $sub_type ) {
303              $row{'selected'} = ' selected';
304            } else {
305              $row{'selected'} = '';
306            }
307            push( @sub_type_data, \%row );
308         }    
309     $template->param(subtype => \@sub_type_data,
310                  weekarrayjs => $weekarrayjs,
311                  weekno => $weekno,
312         );
313         output_html_with_http_headers $query, $cookie, $template->output;
314 }