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