Merging in katipo changes for serials
[koha.git] / serials / alt_subscription-add.pl
1 #!/usr/bin/perl
2
3
4 use strict;
5 use CGI;
6 use C4::Koha;
7 use C4::Auth;
8 use C4::Date;
9 use C4::Output;
10 use C4::Acquisition;
11 use C4::Interface::CGI::Output;
12 use C4::Context;
13 use HTML::Template;
14 use C4::Serials;
15 use Date::Manip;
16
17 my $query = new CGI;
18 my $op = $query->param('op');
19 my $dbh = C4::Context->dbh;
20 my ($subscriptionid,$auser,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
21         $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
22         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
23         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
24         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
25         $numberingmethod, $status, $biblionumber, 
26         $bibliotitle, $callnumber, $notes, $hemisphere);
27
28         my @budgets;
29 my ($template, $loggedinuser, $cookie)
30 = get_template_and_user({template_name => "serials/alt_subscription-add.tmpl",
31                                 query => $query,
32                                 type => "intranet",
33                                 authnotrequired => 0,
34                                 flagsrequired => {catalogue => 1},
35                                 debug => 1,
36                                 });
37
38
39 my $weekarrayjs='';
40 my $count = 0;
41 my ($year, $month, $day) = UnixDate("today", "%Y", "%m", "%d");
42 my $firstday = Date_DayOfYear($month,$day,$year);
43 my $wkno = Date_WeekOfYear($month,$day,$year,1); # week starting monday
44 my $weekno = $wkno;
45 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
46         $count = $i;
47         if($wkno > 52){$year++; $wkno=1;}
48         if($count>365){$count=$i-365;}    
49         my ($y,$m,$d) = Date_NthDayOfYear($year,$count);
50         my $output = "$y-$m-$d";
51         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
52         $wkno++;    
53 }
54 chop($weekarrayjs);
55 # warn $weekarrayjs;
56
57 my $sub_on;
58 my @subscription_types = (
59             'issues', 'weeks', 'months'
60         ); 
61 my @sub_type_data;
62 if ($op eq 'mod') {
63         my $subscriptionid = $query->param('subscriptionid');
64         my $subs = &GetSubscription($subscriptionid);
65         $auser = $subs->{'user'};
66         $librarian => $subs->{'librarian'},
67         $cost = $subs->{'cost'};
68         $aqbooksellerid = $subs->{'aqbooksellerid'};
69         $aqbooksellername = $subs->{'aqbooksellername'};
70         $bookfundid = $subs->{'bookfundid'};
71         $aqbudgetid = $subs->{'aqbudgetid'};
72         $startdate = $subs->{'startdate'};
73         $firstacquidate = $subs->{'firstacquidate'};    
74         $periodicity = $subs->{'periodicity'};
75         $dow = $subs->{'dow'};
76         $irregularity = $subs->{'irregularity'};
77         $numberpattern = $subs->{'numberpattern'};
78         $numberlength = $subs->{'numberlength'};
79         $weeklength = $subs->{'weeklength'};
80         $monthlength = $subs->{'monthlength'};
81
82         if($monthlength > 0){
83             $sublength = $monthlength;
84             $sub_on = $subscription_types[2];
85         } elsif ($weeklength>0){
86             $sublength = $weeklength;
87             $sub_on = $subscription_types[1];
88         } else {
89             $sublength = $numberlength;
90             $sub_on = $subscription_types[0];
91         }
92         
93
94         while (@subscription_types) {
95            my $sub_type = shift @subscription_types;
96            my %row = ( 'name' => $sub_type );
97            if ( $sub_on eq $sub_type ) {
98              $row{'selected'} = ' selected';
99            } else {
100              $row{'selected'} = '';
101            }
102            push( @sub_type_data, \%row );
103         }
104     
105         $add1 = $subs->{'add1'};
106         $every1 = $subs->{'every1'};
107         $whenmorethan1 = $subs->{'whenmorethan1'};
108         $setto1 = $subs->{'setto1'};
109         $lastvalue1 = $subs->{'lastvalue1'};
110         $innerloop1 = $subs->{'innerloop1'};
111         $add2 = $subs->{'add2'};
112         $every2 = $subs->{'every2'};
113         $whenmorethan2 = $subs->{'whenmorethan2'};
114         $setto2 = $subs->{'setto2'};
115         $lastvalue2 = $subs->{'lastvalue2'};
116         $innerloop2 = $subs->{'innerloop2'};
117         $add3 = $subs->{'add3'};
118         $every3 = $subs->{'every3'};
119         $whenmorethan3 = $subs->{'whenmorethan3'};
120         $setto3 = $subs->{'setto3'};
121         $lastvalue3 = $subs->{'lastvalue3'};
122         $innerloop3 = $subs->{'innerloop3'};
123         $numberingmethod = $subs->{'numberingmethod'};
124         $status = $subs->{status};
125         $biblionumber = $subs->{'biblionumber'};
126         $bibliotitle = $subs->{'bibliotitle'};
127         $callnumber = $subs->{'callnumber'};
128         $notes = $subs->{'notes'};
129         $hemisphere = $subs->{'hemisphere'};
130         $template->param(
131                 $op => 1,
132                 user => $auser,
133                 librarian => $librarian,
134                 aqbooksellerid => $aqbooksellerid,
135                 aqbooksellername => $aqbooksellername,
136                 cost => $cost,
137                 aqbudgetid => $aqbudgetid,
138                 bookfundid => $bookfundid,
139                 startdate => format_date($startdate),
140                 firstacquidate => format_date($firstacquidate),     
141                 periodicity => $periodicity,
142                 dow => $dow,
143                 irregularity => $irregularity,
144                 numberpattern => $numberpattern,
145                 sublength => $sublength,
146                 subtype => \@sub_type_data,
147                 add1 => $add1,
148                 every1 => $every1,
149                 whenmorethan1 => $whenmorethan1,
150                 setto1 => $setto1,
151                 lastvalue1 => $lastvalue1,
152                 innerloop1 => $innerloop1,
153                 add2 => $add2,
154                 every2 => $every2,
155                 whenmorethan2 => $whenmorethan2,
156                 setto2 => $setto2,
157                 lastvalue2 => $lastvalue2,
158                 innerloop2 => $innerloop2,
159                 add3 => $add3,
160                 every3 => $every3,
161                 whenmorethan3 => $whenmorethan3,
162                 setto3 => $setto3,
163                 lastvalue3 => $lastvalue3,
164                 innerloop3 => $innerloop3,
165                 numberingmethod => $numberingmethod,
166                 status => $status,
167                 biblionumber => $biblionumber,
168                 bibliotitle => $bibliotitle,
169                 callnumber => $callnumber,
170                 notes => $notes,
171                 subscriptionid => $subscriptionid,
172                 weekarrayjs => $weekarrayjs,
173                 weekno => $weekno,
174                 hemisphere => $hemisphere,
175                 );
176
177         $template->param(
178                                 "periodicity$periodicity" => 1,
179                                 "dow$dow" => 1,
180                                 "numberpattern$numberpattern" => 1,
181                                 );
182 }
183 (my $temp,@budgets) = bookfunds();
184 # find default value & set it for the template
185 for (my $i=0;$i<=$#budgets;$i++) {
186         if ($budgets[$i]->{'aqbudgetid'} eq $aqbudgetid) {
187                 $budgets[$i]->{'selected'}=1;
188         }
189 }
190 $template->param(budgets => \@budgets,
191                 intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
192                 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
193                 IntranetNav => C4::Context->preference("IntranetNav"),
194                 );
195
196 if ($op eq 'addsubscription') {
197         my @irregular = $query->param('irregular');
198         my $irregular_count = @irregular;
199         for(my $i =0;$i<$irregular_count;$i++){
200             $irregularity .=$irregular[$i]."|";
201         }
202         $irregularity =~ s/\|$//;
203
204         my $auser = $query->param('user');
205         my $aqbooksellerid = $query->param('aqbooksellerid');
206         my $cost = $query->param('cost');
207         my $aqbudgetid = $query->param('aqbudgetid'); 
208         my $startdate = $query->param('startdate');
209         my $firstacquidate = $query->param('firstacquidate');    
210         my $periodicity = $query->param('periodicity');
211         my $dow = $query->param('dow');
212         # my $irregularity = $query->param('irregularity');
213         my $numberlength = 0;
214         my $weeklength = 0;
215         my $monthlength = 0;
216         my $numberpattern = $query->param('numbering_pattern');
217         my $sublength = $query->param('sublength');
218         my $subtype = $query->param('subtype');
219         if ($subtype eq 'months'){
220             $monthlength = $sublength;
221         } elsif ($subtype eq 'weeks'){
222             $weeklength = $sublength;
223         } else {
224             $numberlength = $sublength;
225         }
226
227         my $add1 = $query->param('add1');
228         my $every1 = $query->param('every1');
229         my $whenmorethan1 = $query->param('whenmorethan1');
230         my $setto1 = $query->param('setto1');
231         my $lastvalue1 = $query->param('lastvalue1');
232         my $add2 = $query->param('add2');
233         my $every2 = $query->param('every2');
234         my $whenmorethan2 = $query->param('whenmorethan2');
235         my $setto2 = $query->param('setto2');
236         my $lastvalue2 = $query->param('lastvalue2');
237         my $add3 = $query->param('add3');
238         my $every3 = $query->param('every3');
239         my $whenmorethan3 = $query->param('whenmorethan3');
240         my $setto3 = $query->param('setto3');
241         my $lastvalue3 = $query->param('lastvalue3');
242         my $numberingmethod = $query->param('numberingmethod');
243         my $status = 1;
244         my $biblionumber = $query->param('biblionumber');
245         my $callnumber = $query->param('callnumber');
246         my $notes = $query->param('notes');
247         my $hemisphere = $query->param('hemisphere') || 1;
248
249         my $subscriptionid = old_newsubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
250                                         $startdate,$periodicity,$firstacquidate,$dow,$irregularity,$numberpattern,$numberlength,$weeklength,$monthlength,
251                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
252                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
253                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
254                                         $numberingmethod, $status, $callnumber, $notes, $hemisphere
255                                 );
256         print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
257 } else {
258
259         while (@subscription_types) {
260            my $sub_type = shift @subscription_types;
261            my %row = ( 'name' => $sub_type );
262            if ( $sub_on eq $sub_type ) {
263              $row{'selected'} = ' selected';
264            } else {
265              $row{'selected'} = '';
266            }
267            push( @sub_type_data, \%row );
268         }    
269     $template->param(subtype => \@sub_type_data,
270                  weekarrayjs => $weekarrayjs,
271                  weekno => $weekno,
272         );
273         output_html_with_http_headers $query, $cookie, $template->output;
274 }