Adding ability to return biblio results from acquisition search that contain no items
[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         $startdate = $subs->{'startdate'};
71         $firstacquidate = $subs->{'firstacquidate'};    
72         $periodicity = $subs->{'periodicity'};
73         $dow = $subs->{'dow'};
74         $irregularity = $subs->{'irregularity'};
75         $numberpattern = $subs->{'numberpattern'};
76         $numberlength = $subs->{'numberlength'};
77         $weeklength = $subs->{'weeklength'};
78         $monthlength = $subs->{'monthlength'};
79
80         if($monthlength > 0){
81             $sublength = $monthlength;
82             $sub_on = $subscription_types[2];
83         } elsif ($weeklength>0){
84             $sublength = $weeklength;
85             $sub_on = $subscription_types[1];
86         } else {
87             $sublength = $numberlength;
88             $sub_on = $subscription_types[0];
89         }
90         
91
92         while (@subscription_types) {
93            my $sub_type = shift @subscription_types;
94            my %row = ( 'name' => $sub_type );
95            if ( $sub_on eq $sub_type ) {
96              $row{'selected'} = ' selected';
97            } else {
98              $row{'selected'} = '';
99            }
100            push( @sub_type_data, \%row );
101         }
102     
103         $add1 = $subs->{'add1'};
104         $every1 = $subs->{'every1'};
105         $whenmorethan1 = $subs->{'whenmorethan1'};
106         $setto1 = $subs->{'setto1'};
107         $lastvalue1 = $subs->{'lastvalue1'};
108         $innerloop1 = $subs->{'innerloop1'};
109         $add2 = $subs->{'add2'};
110         $every2 = $subs->{'every2'};
111         $whenmorethan2 = $subs->{'whenmorethan2'};
112         $setto2 = $subs->{'setto2'};
113         $lastvalue2 = $subs->{'lastvalue2'};
114         $innerloop2 = $subs->{'innerloop2'};
115         $add3 = $subs->{'add3'};
116         $every3 = $subs->{'every3'};
117         $whenmorethan3 = $subs->{'whenmorethan3'};
118         $setto3 = $subs->{'setto3'};
119         $lastvalue3 = $subs->{'lastvalue3'};
120         $innerloop3 = $subs->{'innerloop3'};
121         $numberingmethod = $subs->{'numberingmethod'};
122         $status = $subs->{status};
123         $biblionumber = $subs->{'biblionumber'};
124         $bibliotitle = $subs->{'bibliotitle'};
125         $callnumber = $subs->{'callnumber'};
126         $notes = $subs->{'notes'};
127         $hemisphere = $subs->{'hemisphere'};
128         $template->param(
129                 $op => 1,
130                 user => $auser,
131                 librarian => $librarian,
132                 aqbooksellerid => $aqbooksellerid,
133                 aqbooksellername => $aqbooksellername,
134                 cost => $cost,
135                 aqbudgetid => $aqbudgetid,
136                 bookfundid => $bookfundid,
137                 startdate => format_date($startdate),
138                 firstacquidate => format_date($firstacquidate),     
139                 periodicity => $periodicity,
140                 dow => $dow,
141                 irregularity => $irregularity,
142                 numberpattern => $numberpattern,
143                 sublength => $sublength,
144                 subtype => \@sub_type_data,
145                 add1 => $add1,
146                 every1 => $every1,
147                 whenmorethan1 => $whenmorethan1,
148                 setto1 => $setto1,
149                 lastvalue1 => $lastvalue1,
150                 innerloop1 => $innerloop1,
151                 add2 => $add2,
152                 every2 => $every2,
153                 whenmorethan2 => $whenmorethan2,
154                 setto2 => $setto2,
155                 lastvalue2 => $lastvalue2,
156                 innerloop2 => $innerloop2,
157                 add3 => $add3,
158                 every3 => $every3,
159                 whenmorethan3 => $whenmorethan3,
160                 setto3 => $setto3,
161                 lastvalue3 => $lastvalue3,
162                 innerloop3 => $innerloop3,
163                 numberingmethod => $numberingmethod,
164                 status => $status,
165                 biblionumber => $biblionumber,
166                 bibliotitle => $bibliotitle,
167                 callnumber => $callnumber,
168                 notes => $notes,
169                 subscriptionid => $subscriptionid,
170                 weekarrayjs => $weekarrayjs,
171                 weekno => $weekno,
172                 hemisphere => $hemisphere,
173                 );
174
175         $template->param(
176                                 "periodicity$periodicity" => 1,
177                                 "dow$dow" => 1,
178                                 "numberpattern$numberpattern" => 1,
179                                 );
180 }
181
182 if ($op eq 'addsubscription') {
183         my @irregular = $query->param('irregular');
184         my $irregular_count = @irregular;
185         for(my $i =0;$i<$irregular_count;$i++){
186             $irregularity .=$irregular[$i]."|";
187         }
188         $irregularity =~ s/\|$//;
189
190         my $auser = $query->param('user');
191         my $aqbooksellerid = $query->param('aqbooksellerid');
192         my $cost = $query->param('cost');
193         my $aqbudgetid = $query->param('aqbudgetid'); 
194         my $startdate = $query->param('startdate');
195         my $firstacquidate = $query->param('firstacquidate');    
196         my $periodicity = $query->param('periodicity');
197         my $dow = $query->param('dow');
198         # my $irregularity = $query->param('irregularity');
199         my $numberlength = 0;
200         my $weeklength = 0;
201         my $monthlength = 0;
202         my $numberpattern = $query->param('numbering_pattern');
203         my $sublength = $query->param('sublength');
204         my $subtype = $query->param('subtype');
205         if ($subtype eq 'months'){
206             $monthlength = $sublength;
207         } elsif ($subtype eq 'weeks'){
208             $weeklength = $sublength;
209         } else {
210             $numberlength = $sublength;
211         }
212
213         my $add1 = $query->param('add1');
214         my $every1 = $query->param('every1');
215         my $whenmorethan1 = $query->param('whenmorethan1');
216         my $setto1 = $query->param('setto1');
217         my $lastvalue1 = $query->param('lastvalue1');
218         my $add2 = $query->param('add2');
219         my $every2 = $query->param('every2');
220         my $whenmorethan2 = $query->param('whenmorethan2');
221         my $setto2 = $query->param('setto2');
222         my $lastvalue2 = $query->param('lastvalue2');
223         my $add3 = $query->param('add3');
224         my $every3 = $query->param('every3');
225         my $whenmorethan3 = $query->param('whenmorethan3');
226         my $setto3 = $query->param('setto3');
227         my $lastvalue3 = $query->param('lastvalue3');
228         my $numberingmethod = $query->param('numberingmethod');
229         my $status = 1;
230         my $biblionumber = $query->param('biblionumber');
231         my $callnumber = $query->param('callnumber');
232         my $notes = $query->param('notes');
233         my $hemisphere = $query->param('hemisphere') || 1;
234
235         my $subscriptionid = old_newsubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
236                                         $startdate,$periodicity,$firstacquidate,$dow,$irregularity,$numberpattern,$numberlength,$weeklength,$monthlength,
237                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,
238                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,
239                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,
240                                         $numberingmethod, $status, $callnumber, $notes, $hemisphere
241                                 );
242         print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
243 } else {
244
245         while (@subscription_types) {
246            my $sub_type = shift @subscription_types;
247            my %row = ( 'name' => $sub_type );
248            if ( $sub_on eq $sub_type ) {
249              $row{'selected'} = ' selected';
250            } else {
251              $row{'selected'} = '';
252            }
253            push( @sub_type_data, \%row );
254         }    
255     $template->param(subtype => \@sub_type_data,
256                  weekarrayjs => $weekarrayjs,
257                  weekno => $weekno,
258         );
259         output_html_with_http_headers $query, $cookie, $template->output;
260 }