Updating claims - commenting out the bits of code that were not implemented in the end
[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 DateTime;
59 my $query = new CGI;
60 my $op = $query->param('op');
61 my $dbh = C4::Context->dbh;
62 my ($subscriptionid,$auser,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
63     $publisheddate,$dow,$irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
64     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
65     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
66     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
67     $numberingmethod, $status, $biblionumber,
68     $title, $notes, $letter,$callnumber,$hemisphere);
69
70     my @budgets;
71 my ($template, $loggedinuser, $cookie)
72 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
73                 query => $query,
74                 type => "intranet",
75                 authnotrequired => 0,
76                 flagsrequired => {catalogue => 1},
77                 debug => 1,
78                 });
79
80 my $weekarrayjs='';
81 my $count = 0;
82 my $today=get_today();
83  my $dateobj=DATE_obj($today);
84   my $year=$dateobj->year;
85   my $month=$dateobj->month;
86   my $day=$dateobj->day_of_month;
87 my $firstday = $dateobj->day_of_year;
88 my $wkno = $dateobj->week_number;
89 my $weekno = $wkno;
90 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
91         $count = $i;
92         if($wkno > 52){$year++; $wkno=1;}
93         if($count>365){$count=$i-365;}    
94      my $newdate=DateTime->from_day_of_year(year=>$year,day_of_year=>$count);
95         $weekarrayjs .= "'Wk $wkno: ".format_date($newdate->ymd)."',";
96         $wkno++;    
97 }
98 chop($weekarrayjs);
99 # warn $weekarrayjs;
100
101 my $sub_on;
102 my @subscription_types = (
103             'issues', 'weeks', 'months'
104         ); 
105 my @sub_type_data;
106 if ($op eq 'mod') {
107     my $subscriptionid = $query->param('subscriptionid');
108     my $subs = &GetSubscription($subscriptionid);
109     $auser = $subs->{'user'};
110     $librarian = $subs->{'librarian'};
111     $cost = $subs->{'cost'};
112     $aqbooksellerid = $subs->{'aqbooksellerid'};
113     $aqbooksellername = $subs->{'aqbooksellername'};
114     $bookfundid = $subs->{'bookfundid'};
115     $aqbudgetid = $subs->{'aqbudgetid'};
116     defined $aqbudgetid or $aqbudgetid='';
117     $startdate = $subs->{'startdate'};
118    $publisheddate = $subs->{'publisheddate'};
119     $periodicity = $subs->{'periodicity'};
120     $dow = $subs->{'dow'};
121         $irregularity = $subs->{'irregularity'};
122         $numberpattern = $subs->{'numberpattern'};
123     $numberlength = $subs->{'numberlength'};
124     $weeklength = $subs->{'weeklength'};
125     $monthlength = $subs->{'monthlength'};
126        if($monthlength > 0){
127             $sublength = $monthlength;
128             $sub_on = $subscription_types[2];
129         } elsif ($weeklength>0){
130             $sublength = $weeklength;
131             $sub_on = $subscription_types[1];
132         } else {
133             $sublength = $numberlength;
134             $sub_on = $subscription_types[0];
135         }
136         
137
138         while (@subscription_types) {
139            my $sub_type = shift @subscription_types;
140            my %row = ( 'name' => $sub_type );
141            if ( $sub_on eq $sub_type ) {
142              $row{'selected'} = ' selected';
143            } else {
144              $row{'selected'} = '';
145            }
146            push( @sub_type_data, \%row );
147         }
148     $add1 = $subs->{'add1'};
149     $every1 = $subs->{'every1'};
150     $whenmorethan1 = $subs->{'whenmorethan1'};
151     $setto1 = $subs->{'setto1'};
152     $lastvalue1 = $subs->{'lastvalue1'};
153     $innerloop1 = $subs->{'innerloop1'};
154     $add2 = $subs->{'add2'};
155     $every2 = $subs->{'every2'};
156     $whenmorethan2 = $subs->{'whenmorethan2'};
157     $setto2 = $subs->{'setto2'};
158     $lastvalue2 = $subs->{'lastvalue2'};
159     $innerloop2 = $subs->{'innerloop2'};
160     $add3 = $subs->{'add3'};
161     $every3 = $subs->{'every3'};
162     $whenmorethan3 = $subs->{'whenmorethan3'};
163     $setto3 = $subs->{'setto3'};
164     $lastvalue3 = $subs->{'lastvalue3'};
165     $innerloop3 = $subs->{'innerloop3'};
166     $numberingmethod = $subs->{'numberingmethod'};
167     $status = $subs->{status};
168     $biblionumber = $subs->{'biblionumber'};
169     $title = $subs->{'title'},
170            $callnumber = $subs->{'callnumber'};
171            $hemisphere = $subs->{'hemisphere'};
172     $notes = $subs->{'notes'};
173     $letter = $subs->{'letter'};
174     defined $letter or $letter='';
175     $template->param(
176         $op => 1,
177         user => $auser,
178         librarian => $librarian,
179         aqbooksellerid => $aqbooksellerid,
180         aqbooksellername => $aqbooksellername,
181         cost => $cost,
182         aqbudgetid => $aqbudgetid,
183         bookfundid => $bookfundid,
184         startdate => format_date($startdate),    
185          publisheddate => format_date($publisheddate),    
186         periodicity => $periodicity,
187         numberpattern=>$numberpattern,
188         dow => $dow,
189         numberlength => $numberlength,
190         weeklength => $weeklength,
191         monthlength => $monthlength,
192         sublength=>$sublength,
193         add1 => $add1,
194         every1 => $every1,
195         whenmorethan1 => $whenmorethan1,
196         setto1 => $setto1,
197         lastvalue1 => $lastvalue1,
198         innerloop1 => $innerloop1,
199         add2 => $add2,
200         every2 => $every2,
201         whenmorethan2 => $whenmorethan2,
202         setto2 => $setto2,
203         lastvalue2 => $lastvalue2,
204         innerloop2 => $innerloop2,
205         add3 => $add3,
206         every3 => $every3,
207         whenmorethan3 => $whenmorethan3,
208         setto3 => $setto3,
209         lastvalue3 => $lastvalue3,
210         innerloop3 => $innerloop3,
211         numberingmethod => $numberingmethod,
212         status => $status,
213         biblionumber => $biblionumber,
214         title => $title,
215                callnumber => $callnumber,
216         notes => $notes,
217         letter => $letter,
218         subscriptionid => $subscriptionid,
219         weekarrayjs => $weekarrayjs,
220                 weekno => $weekno,
221                 hemisphere => $hemisphere,
222         "periodicity$periodicity" => 1,
223         "dow$dow" => 1,
224         "numberpattern$numberpattern" => 1,
225         );
226 }
227
228 my @letterlist = GetLetterList('serial');
229 for (my $i=0;$i<=$#letterlist;$i++) {
230     $letterlist[$i]->{'selected'} =1 if $letterlist[$i]->{'code'} eq $letter;
231 }
232 $template->param(letters => \@letterlist);
233
234 if ($op eq 'addsubscription') {
235   my @irregular = $query->param('irregular');
236         my $irregular_count = @irregular;
237         for(my $i =0;$i<$irregular_count;$i++){
238             $irregularity .=$irregular[$i]."|";
239         }
240         $irregularity =~ s/\|$//;
241         
242     my $auser = $query->param('user');
243     my $aqbooksellerid = $query->param('aqbooksellerid');
244     my $cost = $query->param('cost');
245     my $aqbudgetid = $query->param('aqbudgetid');
246     my $startdate = $query->param('startdate');
247      my $publisheddate = $query->param('publisheddate');
248     my $callnumber=$query->param('callnumber');   
249     my $periodicity = $query->param('periodicity');
250     my $dow = $query->param('dow');
251         my $numberlength = 0;
252         my $weeklength = 0;
253         my $monthlength = 0;
254         my $numberpattern = $query->param('numbering_pattern');
255         my $sublength = $query->param('sublength');
256         my $subtype = $query->param('subtype');
257         if ($subtype eq 'months'){
258             $monthlength = $sublength;
259         } elsif ($subtype eq 'weeks'){
260             $weeklength = $sublength;
261         } else {
262             $numberlength = $sublength;
263         }
264
265
266         my $add1 = $query->param('add1');
267         my $every1 = $query->param('every1');
268         my $whenmorethan1 = $query->param('whenmorethan1');
269         my $setto1 = $query->param('setto1');
270         my $lastvalue1 = $query->param('lastvalue1');
271         my $add2 = $query->param('add2');
272         my $every2 = $query->param('every2');
273         my $whenmorethan2 = $query->param('whenmorethan2');
274         my $setto2 = $query->param('setto2');
275         my $lastvalue2 = $query->param('lastvalue2');
276         my $add3 = $query->param('add3');
277         my $every3 = $query->param('every3');
278         my $whenmorethan3 = $query->param('whenmorethan3');
279         my $setto3 = $query->param('setto3');
280         my $lastvalue3 = $query->param('lastvalue3');
281         my $numberingmethod = $query->param('numberingmethod');
282         my $status = 1;
283     my $biblionumber = $query->param('biblionumber');
284     my $notes = $query->param('notes');
285     my $letter = $query->param('letter');
286             my $hemisphere = $query->param('hemisphere') || 1;
287
288     my $subscriptionid = NewSubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
289                     $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
290                     $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
291                     $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
292                     $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
293                     $numberingmethod, $status, $notes, $letter,$irregularity,$hemisphere,$callnumber,$numberpattern,$publisheddate    );
294     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
295 } else {
296      while (@subscription_types) {
297            my $sub_type = shift @subscription_types;
298            my %row = ( 'name' => $sub_type );
299            if ( $sub_on eq $sub_type ) {
300              $row{'selected'} = ' selected';
301            } else {
302              $row{'selected'} = '';
303            }
304            push( @sub_type_data, \%row );
305         }    
306     $template->param(subtype => \@sub_type_data,
307                  weekarrayjs => $weekarrayjs,
308                  weekno => $weekno,
309         );
310     output_html_with_http_headers $query, $cookie, $template->output;
311 }