A new Date.pm to use for all date calculations. Mysql date calculations removed from...
[koha.git] / serials / alt_subscription-detail.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use CGI;
5 use C4::Auth;
6 use C4::Koha;
7 use C4::Date;
8 use C4::Serials;
9 use C4::Output;
10 use C4::Interface::CGI::Output;
11 use C4::Context;
12 use HTML::Template;
13 use Date::Manip;
14
15 my $query = new CGI;
16 my $op = $query->param('op');
17 my $dbh = C4::Context->dbh;
18 my $sth;
19 # my $id;
20 my ($template, $loggedinuser, $cookie, $subs);
21 my ($subscriptionid,$auser,$librarian,$cost,$aqbooksellerid, $aqbooksellername,$aqbudgetid, $bookfundid, $startdate, $periodicity,
22         $firstacquidate, $dow, $irregularity, $sublength, $subtype, $numberpattern, $numberlength, $weeklength, $monthlength,
23         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
24         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
25         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
26         $numberingmethod, $status, $biblionumber, $bibliotitle, $callnumber, $notes, $hemisphere);
27
28 $subscriptionid = $query->param('subscriptionid');
29
30 if ($op eq 'modsubscription') {
31     my @irregular = $query->param('irregular');
32     my $irregular_count = @irregular;
33     for(my $i =0;$i<$irregular_count;$i++){
34         $irregularity .=$irregular[$i]."|";
35     }
36     $irregularity =~ s/\|$//;
37
38     
39         $auser = $query->param('user');
40         $librarian => $query->param('librarian'),
41         $cost = $query->param('cost');
42         $aqbooksellerid = $query->param('aqbooksellerid');
43         $biblionumber = $query->param('biblionumber');
44         $aqbudgetid = $query->param('aqbudgetid');
45         $startdate = format_date_in_iso($query->param('startdate'));
46         $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
47         $periodicity = $query->param('periodicity');
48         $dow = $query->param('dow');
49         $sublength = $query->param('sublength');
50         $subtype = $query->param('subtype');
51
52         if($subtype eq 'months'){
53             $monthlength = $sublength;
54         } elsif ($subtype eq 'weeks'){
55             $weeklength = $sublength;
56         } else {
57             $numberlength = $sublength;
58         }
59         $numberpattern = $query->param('numbering_pattern');
60         $add1 = $query->param('add1');
61         $every1 = $query->param('every1');
62         $whenmorethan1 = $query->param('whenmorethan1');
63         $setto1 = $query->param('setto1');
64         $lastvalue1 = $query->param('lastvalue1');
65         $innerloop1 = $query->param('innerloop1');
66         $add2 = $query->param('add2');
67         $every2 = $query->param('every2');
68         $whenmorethan2 = $query->param('whenmorethan2');
69         $setto2 = $query->param('setto2');
70         $lastvalue2 = $query->param('lastvalue2');
71         $innerloop2 = $query->param('innerloop2');
72         $add3 = $query->param('add3');
73         $every3 = $query->param('every3');
74         $whenmorethan3 = $query->param('whenmorethan3');
75         $setto3 = $query->param('setto3');
76         $lastvalue3 = $query->param('lastvalue3');
77         $innerloop3 = $query->param('innerloop3');
78         $numberingmethod = $query->param('numberingmethod');
79         $status = 1;
80         $callnumber = $query->param('callnumber');
81         $notes = $query->param('notes');
82         $hemisphere = $query->param('hemisphere');
83
84         &ModSubscription($auser,$aqbooksellerid,$cost,$aqbudgetid,$startdate,
85                                         $periodicity,$firstacquidate,$dow,$irregularity,$numberpattern,$numberlength,$weeklength,$monthlength,
86                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
87                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
88                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
89                                         $numberingmethod, $status, $biblionumber, $callnumber, $notes, $hemisphere, $subscriptionid);
90 }
91
92 if ($op eq 'del') {
93         &DelSubscription($subscriptionid);
94         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=serials-home.pl\"></html>";
95         exit;
96
97 }
98 my $subs = &GetSubscription($subscriptionid);
99 my ($routing, @routinglist) = getroutinglist($subscriptionid);
100 my ($totalissues,@serialslist) = old_getserials($subscriptionid);
101 $totalissues-- if $totalissues; # the -1 is to have 0 if this is a new subscription (only 1 issue)
102 # the subscription must be deletable if there is NO issues for a reason or another (should not happend, but...)
103
104 ($template, $loggedinuser, $cookie)
105 = get_template_and_user({template_name => "serials/alt_subscription-detail.tmpl",
106                                 query => $query,
107                                 type => "intranet",
108                                 authnotrequired => 0,
109                                 flagsrequired => {catalogue => 1},
110                                 debug => 1,
111                                 });
112
113 my ($user, $cookie, $sessionID, $flags)
114         = checkauth($query, 0, {catalogue => 1}, "intranet");
115
116 my $weekarrayjs='';
117 my $count = 0;
118 my ($year, $month, $day) = UnixDate("today", "%Y", "%m", "%d");
119 my $firstday = Date_DayOfYear($month,$day,$year);
120 my $wkno = Date_WeekOfYear($month,$day,$year,1); # week starting monday
121 my $weekno = $wkno;
122 for(my $i=$firstday;$i<($firstday+365);$i=$i+7){
123             $count = $i;
124             if($wkno > 52){$year++; $wkno=1;}
125             if($count>365){$count=$i-365;}
126             my ($y,$m,$d) = Date_NthDayOfYear($year,$count);
127             my $output = "$y-$m-$d";
128             $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
129             $wkno++;
130     }
131 chop($weekarrayjs);
132
133 $template->param(
134         routing => $routing,
135         user => $subs->{auser},
136         librarian => $subs->{librarian},
137         aqbooksellerid => $subs->{aqbooksellerid},
138         aqbooksellername => $subs->{aqbooksellername},
139         cost => $subs->{cost},
140         aqbudgetid => $subs->{aqbudgetid},
141         bookfundid => $subs->{bookfundid},
142         startdate => format_date($subs->{startdate}),
143         firstacquidate => format_date($subs->{firstacquidate}),    
144         periodicity => $subs->{periodicity},
145         dow => $subs->{dow},
146         irregularity => $subs->{irregularity},
147         numberlength => $subs->{numberlength},
148         weeklength => $subs->{weeklength},
149         monthlength => $subs->{monthlength},
150         numberpattern => $subs->{numberpattern},
151         add1 => $subs->{add1},
152         every1 => $subs->{every1},
153         whenmorethan1 => $subs->{whenmorethan1},
154         innerloop1 => $subs->{innerloop1},
155         setto1 => $subs->{setto1},
156         lastvalue1 => $subs->{lastvalue1},
157         add2 => $subs->{add2},
158         every2 => $subs->{every2},
159         whenmorethan2 => $subs->{whenmorethan2},
160         setto2 => $subs->{setto2},
161         lastvalue2 => $subs->{lastvalue2},
162         innerloop2 => $subs->{innerloop2},
163         add3 => $subs->{add3},
164         every3 => $subs->{every3},
165         whenmorethan3 => $subs->{whenmorethan3},
166         setto3 => $subs->{setto3},
167         lastvalue3 => $subs->{lastvalue3},
168         innerloop3 => $subs->{innerloop3},
169         weekarrayjs => $weekarrayjs,
170         numberingmethod => $subs->{numberingmethod},
171         status => $subs->{status},
172         biblionumber => $subs->{biblionumber},
173         bibliotitle => $subs->{bibliotitle},
174         callnumber => $subs->{callnumber},
175         notes => $subs->{notes},
176         subscriptionid => $subs->{subscriptionid},
177         serialslist => \@serialslist,
178         totalissues => $totalissues,
179         hemisphere => $hemisphere,
180         );
181 $template->param(
182                         "periodicity$subs->{periodicity}" => 1,
183                         "arrival$subs->{dow}" => 1,
184                         "numberpattern$subs->{numberpattern}" => 1,
185                         intranetstylesheet => C4::Context->preference("intranetstylesheet"),
186                         intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"), 
187                         );
188
189 output_html_with_http_headers $query, $cookie, $template->output;