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