rel_3_0 moved to HEAD
[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 C4::Koha;
21 use C4::Auth;
22 use C4::Date;
23 use C4::Output;
24 use C4::Acquisition;
25 use C4::Interface::CGI::Output;
26 use C4::Context;
27 use C4::Branch; # GetBranches
28 use C4::Serials;
29 use Date::Manip;
30 use C4::Letters;
31
32 my $query = new CGI;
33 my $op = $query->param('op');
34 my $dbh = C4::Context->dbh;
35 my ($subscriptionid,$auser,$branchcode,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
36         $firstacquidate, $dow, $irregularity, $numberpattern, $numberlength, $weeklength, $monthlength, $sublength,
37         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
38         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
39         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
40         $numberingmethod, $status, $biblionumber, 
41         $bibliotitle, $callnumber, $notes, $hemisphere, $letter, $manualhistory);
42
43         my @budgets;
44 my ($template, $loggedinuser, $cookie)
45 = get_template_and_user({template_name => "serials/subscription-add.tmpl",
46                                 query => $query,
47                                 type => "intranet",
48                                 authnotrequired => 0,
49                                 flagsrequired => {serials => 1},
50                                 debug => 1,
51                                 });
52
53
54 my $weekarrayjs='';
55 my $count = 0;
56 my ($year, $month, $day) = UnixDate("today", "%Y", "%m", "%d");
57 my $firstday = Date_DayOfYear($month,$day,$year);
58 my $wkno = Date_WeekOfYear($month,$day,$year,1); # week starting monday
59 my $weekno = $wkno;
60 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
61         $count = $i;
62         if($wkno > 52){$year++; $wkno=1;}
63         if($count>365){$count=$i-365;}    
64         my ($y,$m,$d) = Date_NthDayOfYear($year,$count);
65         my $output = "$y-$m-$d";
66         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
67         $wkno++;    
68 }
69 chop($weekarrayjs);
70 # warn $weekarrayjs;
71
72 my $sub_on;
73 my @subscription_types = (
74             'issues', 'weeks', 'months'
75         ); 
76 my @sub_type_data;
77
78 my $letters = GetLetters('serial');
79 my @letterloop;
80 foreach my $thisletter (keys %$letters) {
81     my $selected = 1 if $thisletter eq $letter;
82     my %row =(value => $thisletter,
83                 selected => $selected,
84                 lettername => $letters->{$thisletter},
85             );
86     push @letterloop, \%row;
87 }
88 $template->param(letterloop => \@letterloop);
89
90 my $onlymine=C4::Context->preference('IndependantBranches') && 
91              C4::Context->userenv && 
92              C4::Context->userenv->{flags}!=1 && 
93              C4::Context->userenv->{branch};
94 my $branches = GetBranches($onlymine);
95 my @branchloop;
96 foreach my $thisbranch (keys %$branches) {
97     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
98     my %row =(value => $thisbranch,
99                 selected => $selected,
100                 branchname => $branches->{$thisbranch}->{'branchname'},
101             );
102     push @branchloop, \%row;
103 }
104 $template->param(branchloop => \@branchloop);
105
106 if ($op eq 'mod'||$op eq 'dup') {
107     my $subscriptionid = $query->param('subscriptionid');
108 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
109     my $subs = &GetSubscription($subscriptionid);
110     $subs->{'startdate'}=format_date($subs->{'startdate'});
111     $subs->{'firstacquidate'}=format_date($subs->{'firstacquidate'});
112     $subs->{'histstartdate'}=format_date($subs->{'histstartdate'});
113     $subs->{'enddate'}=format_date($subs->{enddate});
114     $subs->{'letter'}='' unless($subs->{'letter'});
115
116     if($subs->{numberlength} > 0){
117         $sublength = $subs->{numberlength};
118         $sub_on = $subscription_types[0];
119     } elsif ($subs->{weeklength}>0){
120         $sublength = $weeklength;
121         $sub_on = $subscription_types[1];
122     } else {
123         $sublength = $subs->{monthlength};
124         $sub_on = $subscription_types[2];
125     }
126     while (@subscription_types) {
127         my $sub_type = shift @subscription_types;
128         my %row = ( 'name' => $sub_type );
129         if ( $sub_on eq $sub_type ) {
130             $row{'selected'} = ' selected';
131         } else {
132             $row{'selected'} = '';
133         }
134         push( @sub_type_data, \%row );
135     }
136
137     $template->param($subs);
138     $template->param(
139             $op => 1,
140             subtype => \@sub_type_data,
141             sublength =>$sublength,
142             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
143             );
144     $template->param(
145                 "periodicity".$subs->{'periodicity'} => 1,
146                 "dow".$subs->{'periodicity'} => 1,
147                 "numberpattern".$subs->{'periodicity'} => 1,
148                 );
149 }
150
151 if ($op eq 'addsubscription') {
152     my @irregular = $query->param('irregular');
153     my $irregular_count = scalar(@irregular);
154     for(my $i =0;$i<$irregular_count;$i++){
155         $irregularity .=$irregular[$i].",";
156     }
157     $irregularity =~ s/\,$//;
158
159     my $auser = $query->param('user');
160     my $branchcode = $query->param('branchcode');
161     my $aqbooksellerid = $query->param('aqbooksellerid');
162     my $cost = $query->param('cost');
163     my $aqbudgetid = $query->param('aqbudgetid'); 
164     my $startdate = $query->param('startdate');
165     my $firstacquidate = $query->param('firstacquidate');    
166     my $periodicity = $query->param('periodicity');
167     my $dow = $query->param('dow');
168     my $irregularity = $query->param('irregularity');
169     my $numberlength = 0;
170     my $weeklength = 0;
171     my $monthlength = 0;
172     my $numberpattern = $query->param('numbering_pattern');
173     my $sublength = $query->param('sublength');
174     my $subtype = $query->param('subtype');
175     if ($subtype eq 'months'){
176         $monthlength = $sublength;
177     } elsif ($subtype eq 'weeks'){
178         $weeklength = $sublength;
179     } else {
180         $numberlength = $sublength;
181     }
182
183     my $add1 = $query->param('add1');
184     my $every1 = $query->param('every1');
185     my $whenmorethan1 = $query->param('whenmorethan1');
186     my $setto1 = $query->param('setto1');
187     my $lastvalue1 = $query->param('lastvalue1');
188     my $add2 = $query->param('add2');
189     my $every2 = $query->param('every2');
190     my $whenmorethan2 = $query->param('whenmorethan2');
191     my $setto2 = $query->param('setto2');
192     my $lastvalue2 = $query->param('lastvalue2');
193     my $add3 = $query->param('add3');
194     my $every3 = $query->param('every3');
195     my $whenmorethan3 = $query->param('whenmorethan3');
196     my $setto3 = $query->param('setto3');
197     my $lastvalue3 = $query->param('lastvalue3');
198     my $numberingmethod = $query->param('numberingmethod');
199     my $status = 1;
200     my $biblionumber = $query->param('biblionumber');
201     my $callnumber = $query->param('callnumber');
202     my $notes = $query->param('notes');
203     my $internalnotes = $query->param('internalnotes');
204     my $hemisphere = $query->param('hemisphere') || 1;
205         my $letter = $query->param('letter');
206     ### BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
207     my $manualhistory = $query->param('manualhist');
208     my ($innerloop1,$innerloop2,$innerloop3);
209         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
210                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
211                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
212                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
213                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
214                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,$irregularity,
215                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
216                                 );
217
218     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
219 } elsif ($op eq 'modsubscription') {
220     my $subscriptionid = $query->param('subscriptionid');
221     my @irregular = $query->param('irregular');
222     my $irregular_count = @irregular;
223     for(my $i =0;$i<$irregular_count;$i++){
224       $irregularity .=$irregular[$i].",";
225       warn "irregular : $irregular[$i] string :$irregularity";
226     }
227     $irregularity =~ s/\,$//;
228
229     my $auser = $query->param('user');
230     my $librarian => $query->param('librarian'),
231     my $branchcode = $query->param('branchcode');
232     my $cost = $query->param('cost');
233     my $aqbooksellerid = $query->param('aqbooksellerid');
234     my $biblionumber = $query->param('biblionumber');
235     my $aqbudgetid = $query->param('aqbudgetid');
236     my $startdate = format_date_in_iso($query->param('startdate'));
237     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
238     my $periodicity = $query->param('periodicity');
239     my $dow = $query->param('dow');
240     my $sublength = $query->param('sublength');
241     my $subtype = $query->param('subtype');
242
243     if($subtype eq 'months'){
244         $monthlength = $sublength;
245     } elsif ($subtype eq 'weeks'){
246         $weeklength = $sublength;
247     } else {
248         $numberlength = $sublength;
249     }
250     my $numberpattern = $query->param('numbering_pattern');
251     my $add1 = $query->param('add1');
252     my $every1 = $query->param('every1');
253     my $whenmorethan1 = $query->param('whenmorethan1');
254     my $setto1 = $query->param('setto1');
255     my $lastvalue1 = $query->param('lastvalue1');
256     my $innerloop1 = $query->param('innerloop1');
257     my $add2 = $query->param('add2');
258     my $every2 = $query->param('every2');
259     my $whenmorethan2 = $query->param('whenmorethan2');
260     my $setto2 = $query->param('setto2');
261     my $lastvalue2 = $query->param('lastvalue2');
262     my $innerloop2 = $query->param('innerloop2');
263     my $add3 = $query->param('add3');
264     my $every3 = $query->param('every3');
265     my $whenmorethan3 = $query->param('whenmorethan3');
266     my $setto3 = $query->param('setto3');
267     my $lastvalue3 = $query->param('lastvalue3');
268     my $innerloop3 = $query->param('innerloop3');
269     my $numberingmethod = $query->param('numberingmethod');
270     my $status = 1;
271     my $callnumber = $query->param('callnumber');
272     my $notes = $query->param('notes');
273     my $internalnotes = $query->param('internalnotes');
274     my $hemisphere = $query->param('hemisphere');
275     my $letter = $query->param('letter');
276     my $manualhistory = $query->param('manualhist');
277     my $enddate = $query->param('enddate');
278     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
279     my $recievedlist = $query->param('recievedlist');
280     my $missinglist = $query->param('missinglist');
281     my $opacnote = $query->param('opacnote');
282     my $librariannote = $query->param('librariannote');
283     &ModSubscription(
284         $auser,           $branchcode,   $aqbooksellerid, $cost,
285         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
286         $dow,             $irregularity, $numberpattern,  $numberlength,
287         $weeklength,      $monthlength,  $add1,           $every1,
288         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
289         $add2,            $every2,       $whenmorethan2,  $setto2,
290         $lastvalue2,      $innerloop2,   $add3,           $every3,
291         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
292         $numberingmethod, $status,       $biblionumber,   $callnumber,
293         $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
294         $subscriptionid);
295
296     ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
297     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
298 } else {
299
300         while (@subscription_types) {
301            my $sub_type = shift @subscription_types;
302            my %row = ( 'name' => $sub_type );
303            if ( $sub_on eq $sub_type ) {
304              $row{'selected'} = ' selected';
305            } else {
306              $row{'selected'} = '';
307            }
308            push( @sub_type_data, \%row );
309         }    
310     $template->param(subtype => \@sub_type_data,
311                  weekarrayjs => $weekarrayjs,
312                  weekno => $weekno,
313         );
314         output_html_with_http_headers $query, $cookie, $template->output;
315 }