Clean up before final commits
[koha.git] / serials / subscription-add.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2003 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 # $Id$
21
22 =head1 NAME
23
24 subscription-add.pl
25
26 =head1 DESCRIPTION
27
28 this script add a subscription into the database.
29
30 =head1 PARAMETERS
31
32 =over 4
33
34 =item op
35 op use to know the operation to do on this template.
36  * mod : to modify an existing subscription
37  * addsubscription : to add a subscription
38
39 Note that if op = mod or addsubscription there are a lot of other params.
40
41
42 =back
43
44 =cut
45
46
47 use strict;
48 use CGI;
49 use C4::Koha;
50 use C4::Auth;
51 use C4::Date;
52 use C4::Serials;
53 use C4::Bookfund;
54 use C4::Interface::CGI::Output;
55 use C4::Context;
56 use C4::Letters;
57 use C4::Members;
58 use Date::Manip;
59
60 my $query = new CGI;
61 my $op = $query->param('op');
62 my $dbh = C4::Context->dbh;
63 my ($subscriptionid,$auser,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
64     $publisheddate,$dow,$irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
65     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
66     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
67     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
68     $numberingmethod, $status, $biblionumber,
69     $title, $notes, $letter,$callnumber,$hemisphere);
70
71     my @budgets;
72 my ($template, $loggedinuser, $cookie)
73 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
74                 query => $query,
75                 type => "intranet",
76                 authnotrequired => 0,
77                 flagsrequired => {catalogue => 1},
78                 debug => 1,
79                 });
80
81 my $weekarrayjs='';
82 my $count = 0;
83 my ($year, $month, $day) = UnixDate("today", "%Y", "%m", "%d");
84 my $firstday = Date_DayOfYear($month,$day,$year);
85 my $wkno = Date_WeekOfYear($month,$day,$year,1); # week starting monday
86 my $weekno = $wkno;
87 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
88         $count = $i;
89         if($wkno > 52){$year++; $wkno=1;}
90         if($count>365){$count=$i-365;}    
91         my ($y,$m,$d) = Date_NthDayOfYear($year,$count);
92         my $output = "$y-$m-$d";
93         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
94         $wkno++;    
95 }
96 chop($weekarrayjs);
97 # warn $weekarrayjs;
98
99 my $sub_on;
100 my @subscription_types = (
101             'issues', 'weeks', 'months'
102         ); 
103 my @sub_type_data;
104 if ($op eq 'mod') {
105     my $subscriptionid = $query->param('subscriptionid');
106     my $subs = &GetSubscription($subscriptionid);
107     $auser = $subs->{'user'};
108     $librarian = $subs->{'librarian'};
109     $cost = $subs->{'cost'};
110     $aqbooksellerid = $subs->{'aqbooksellerid'};
111     $aqbooksellername = $subs->{'aqbooksellername'};
112     $bookfundid = $subs->{'bookfundid'};
113     $aqbudgetid = $subs->{'aqbudgetid'};
114     defined $aqbudgetid or $aqbudgetid='';
115     $startdate = $subs->{'startdate'};
116    $publisheddate = $subs->{'publisheddate'};
117     $periodicity = $subs->{'periodicity'};
118     $dow = $subs->{'dow'};
119         $irregularity = $subs->{'irregularity'};
120         $numberpattern = $subs->{'numberpattern'};
121     $numberlength = $subs->{'numberlength'};
122     $weeklength = $subs->{'weeklength'};
123     $monthlength = $subs->{'monthlength'};
124        if($monthlength > 0){
125             $sublength = $monthlength;
126             $sub_on = $subscription_types[2];
127         } elsif ($weeklength>0){
128             $sublength = $weeklength;
129             $sub_on = $subscription_types[1];
130         } else {
131             $sublength = $numberlength;
132             $sub_on = $subscription_types[0];
133         }
134         
135
136         while (@subscription_types) {
137            my $sub_type = shift @subscription_types;
138            my %row = ( 'name' => $sub_type );
139            if ( $sub_on eq $sub_type ) {
140              $row{'selected'} = ' selected';
141            } else {
142              $row{'selected'} = '';
143            }
144            push( @sub_type_data, \%row );
145         }
146     $add1 = $subs->{'add1'};
147     $every1 = $subs->{'every1'};
148     $whenmorethan1 = $subs->{'whenmorethan1'};
149     $setto1 = $subs->{'setto1'};
150     $lastvalue1 = $subs->{'lastvalue1'};
151     $innerloop1 = $subs->{'innerloop1'};
152     $add2 = $subs->{'add2'};
153     $every2 = $subs->{'every2'};
154     $whenmorethan2 = $subs->{'whenmorethan2'};
155     $setto2 = $subs->{'setto2'};
156     $lastvalue2 = $subs->{'lastvalue2'};
157     $innerloop2 = $subs->{'innerloop2'};
158     $add3 = $subs->{'add3'};
159     $every3 = $subs->{'every3'};
160     $whenmorethan3 = $subs->{'whenmorethan3'};
161     $setto3 = $subs->{'setto3'};
162     $lastvalue3 = $subs->{'lastvalue3'};
163     $innerloop3 = $subs->{'innerloop3'};
164     $numberingmethod = $subs->{'numberingmethod'};
165     $status = $subs->{status};
166     $biblionumber = $subs->{'biblionumber'};
167     $title = $subs->{'title'},
168            $callnumber = $subs->{'callnumber'};
169            $hemisphere = $subs->{'hemisphere'};
170     $notes = $subs->{'notes'};
171     $letter = $subs->{'letter'};
172     defined $letter or $letter='';
173     $template->param(
174         $op => 1,
175         user => $auser,
176         librarian => $librarian,
177         aqbooksellerid => $aqbooksellerid,
178         aqbooksellername => $aqbooksellername,
179         cost => $cost,
180         aqbudgetid => $aqbudgetid,
181         bookfundid => $bookfundid,
182         startdate => format_date($startdate),    
183          publisheddate => format_date($publisheddate),    
184         periodicity => $periodicity,
185         numberpattern=>$numberpattern,
186         dow => $dow,
187         numberlength => $numberlength,
188         weeklength => $weeklength,
189         monthlength => $monthlength,
190         sublength=>$sublength,
191         add1 => $add1,
192         every1 => $every1,
193         whenmorethan1 => $whenmorethan1,
194         setto1 => $setto1,
195         lastvalue1 => $lastvalue1,
196         innerloop1 => $innerloop1,
197         add2 => $add2,
198         every2 => $every2,
199         whenmorethan2 => $whenmorethan2,
200         setto2 => $setto2,
201         lastvalue2 => $lastvalue2,
202         innerloop2 => $innerloop2,
203         add3 => $add3,
204         every3 => $every3,
205         whenmorethan3 => $whenmorethan3,
206         setto3 => $setto3,
207         lastvalue3 => $lastvalue3,
208         innerloop3 => $innerloop3,
209         numberingmethod => $numberingmethod,
210         status => $status,
211         biblionumber => $biblionumber,
212         title => $title,
213                callnumber => $callnumber,
214         notes => $notes,
215         letter => $letter,
216         subscriptionid => $subscriptionid,
217         weekarrayjs => $weekarrayjs,
218                 weekno => $weekno,
219                 hemisphere => $hemisphere,
220         "periodicity$periodicity" => 1,
221         "dow$dow" => 1,
222         "numberpattern$numberpattern" => 1,
223         );
224 }
225
226 my @letterlist = GetLetterList('serial');
227 for (my $i=0;$i<=$#letterlist;$i++) {
228     $letterlist[$i]->{'selected'} =1 if $letterlist[$i]->{'code'} eq $letter;
229 }
230 $template->param(letters => \@letterlist);
231
232 if ($op eq 'addsubscription') {
233   my @irregular = $query->param('irregular');
234         my $irregular_count = @irregular;
235         for(my $i =0;$i<$irregular_count;$i++){
236             $irregularity .=$irregular[$i]."|";
237         }
238         $irregularity =~ s/\|$//;
239         
240     my $auser = $query->param('user');
241     my $aqbooksellerid = $query->param('aqbooksellerid');
242     my $cost = $query->param('cost');
243     my $aqbudgetid = $query->param('aqbudgetid');
244     my $startdate = $query->param('startdate');
245      my $publisheddate = $query->param('publisheddate');
246     my $callnumber=$query->param('callnumber');   
247     my $periodicity = $query->param('periodicity');
248     my $dow = $query->param('dow');
249         my $numberlength = 0;
250         my $weeklength = 0;
251         my $monthlength = 0;
252         my $numberpattern = $query->param('numbering_pattern');
253         my $sublength = $query->param('sublength');
254         my $subtype = $query->param('subtype');
255         if ($subtype eq 'months'){
256             $monthlength = $sublength;
257         } elsif ($subtype eq 'weeks'){
258             $weeklength = $sublength;
259         } else {
260             $numberlength = $sublength;
261         }
262
263
264         my $add1 = $query->param('add1');
265         my $every1 = $query->param('every1');
266         my $whenmorethan1 = $query->param('whenmorethan1');
267         my $setto1 = $query->param('setto1');
268         my $lastvalue1 = $query->param('lastvalue1');
269         my $add2 = $query->param('add2');
270         my $every2 = $query->param('every2');
271         my $whenmorethan2 = $query->param('whenmorethan2');
272         my $setto2 = $query->param('setto2');
273         my $lastvalue2 = $query->param('lastvalue2');
274         my $add3 = $query->param('add3');
275         my $every3 = $query->param('every3');
276         my $whenmorethan3 = $query->param('whenmorethan3');
277         my $setto3 = $query->param('setto3');
278         my $lastvalue3 = $query->param('lastvalue3');
279         my $numberingmethod = $query->param('numberingmethod');
280         my $status = 1;
281     my $biblionumber = $query->param('biblionumber');
282     my $notes = $query->param('notes');
283     my $letter = $query->param('letter');
284             my $hemisphere = $query->param('hemisphere') || 1;
285
286     my $subscriptionid = NewSubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
287                     $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
288                     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
289                     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
290                     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
291                     $numberingmethod, $status, $notes, $letter,$irregularity,$hemisphere,$callnumber,$numberpattern,$publisheddate    );
292     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
293 } else {
294      while (@subscription_types) {
295            my $sub_type = shift @subscription_types;
296            my %row = ( 'name' => $sub_type );
297            if ( $sub_on eq $sub_type ) {
298              $row{'selected'} = ' selected';
299            } else {
300              $row{'selected'} = '';
301            }
302            push( @sub_type_data, \%row );
303         }    
304     $template->param(subtype => \@sub_type_data,
305                  weekarrayjs => $weekarrayjs,
306                  weekno => $weekno,
307         );
308     output_html_with_http_headers $query, $cookie, $template->output;
309 }