b2cf3a8f2a1dcf08a178b3ee17fdb4b4d9b0d037
[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 my $nextexpected;
96
97 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
98
99     $subscriptionid = $query->param('subscriptionid');
100     $subs = &GetSubscription($subscriptionid);
101 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
102     if ($subs->{'cannotedit'} && $op eq 'mod'){
103       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
104       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
105     } 
106     $firstissuedate = $subs->{firstacquidate};  # in iso format.
107     for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
108         # TODO : Handle date formats properly.
109          if ($subs->{$_} eq '0000-00-00') {
110             $subs->{$_} = ''
111         } else {
112             $subs->{$_} = format_date($subs->{$_});  
113         }
114         }
115     $subs->{'letter'}='' unless($subs->{'letter'});
116     $irregularity   = $subs->{'irregularity'};
117     $numberpattern  = $subs->{'numberpattern'};
118     $nextexpected = GetNextExpected($subscriptionid);
119     $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
120     $subs->{nextacquidate} = $nextexpected->{planneddate}->output()  if($op eq 'mod');
121   unless($op eq 'modsubscription') {
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                 "periodicity".$subs->{'periodicity'} => 1,
150                 "dow".$subs->{'dow'} => 1,
151                 "numberpattern".$subs->{'numberpattern'} => 1,
152                 firstacquiyear => substr($firstissuedate,0,4),
153                 );
154   }
155 }
156
157 my $count = 0;
158 # prepare template variables common to all $op conditions:
159 $template->param(  'dateformat_' . C4::Context->preference('dateformat') => 1 ,
160                 );
161
162 if ($op eq 'addsubscription') {
163     my $auser = $query->param('user');
164     my $branchcode = $query->param('branchcode');
165     my $aqbooksellerid = $query->param('aqbooksellerid');
166     my $cost = $query->param('cost');
167     my $aqbudgetid = $query->param('aqbudgetid'); 
168     my $startdate = $query->param('startdate');
169     my $firstacquidate = $query->param('firstacquidate');    
170     my $periodicity = $query->param('periodicity');
171     my $dow = $query->param('dow');
172     my @irregularity = $query->param('irregularity_select');
173     my $numberlength = 0;
174     my $weeklength = 0;
175     my $monthlength = 0;
176     my $numberpattern = $query->param('numbering_pattern');
177     my $sublength = $query->param('sublength');
178     my $subtype = $query->param('subtype');
179     if ($subtype eq 'months'){
180         $monthlength = $sublength;
181     } elsif ($subtype eq 'weeks'){
182         $weeklength = $sublength;
183     } else {
184         $numberlength = $sublength;
185     }
186     my $add1 = $query->param('add1');
187     my $every1 = $query->param('every1');
188     my $whenmorethan1 = $query->param('whenmorethan1');
189     my $setto1 = $query->param('setto1');
190     my $lastvalue1 = $query->param('lastvalue1');
191     my $innerloop1 =$query->param('innerloop1');
192     my $add2 = $query->param('add2');
193     my $every2 = $query->param('every2');
194     my $whenmorethan2 = $query->param('whenmorethan2');
195     my $setto2 = $query->param('setto2');
196     my $innerloop2 =$query->param('innerloop2');
197     my $lastvalue2 = $query->param('lastvalue2');
198     my $add3 = $query->param('add3');
199     my $every3 = $query->param('every3');
200     my $whenmorethan3 = $query->param('whenmorethan3');
201     my $setto3 = $query->param('setto3');
202     my $lastvalue3 = $query->param('lastvalue3');
203     my $innerloop3 =$query->param('innerloop3');
204     my $numberingmethod = $query->param('numberingmethod');
205     my $status = 1;
206     my $biblionumber = $query->param('biblionumber');
207     my $callnumber = $query->param('callnumber');
208     my $notes = $query->param('notes');
209     my $internalnotes = $query->param('internalnotes');
210     my $hemisphere = $query->param('hemisphere') || 1;
211         my $letter = $query->param('letter');
212     # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
213     my $manualhistory = $query->param('manualhist');
214     my $serialsadditems = $query->param('serialsadditems');
215         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
216                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
217                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
218                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
219                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
220                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
221                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
222                     $serialsadditems,
223                                 );
224
225     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
226 } elsif ($op eq 'modsubscription') {
227     my $subscriptionid = $query->param('subscriptionid');
228         my @irregularity = $query->param('irregularity_select');
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 $nextacquidate = format_date_in_iso($query->param('nextacquidate'));    
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 $serialsadditems = $query->param('serialsadditems');
279     # subscription history
280     my $histenddate = format_date_in_iso($query->param('histenddate'));
281     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
282     my $recievedlist = $query->param('recievedlist');
283     my $missinglist = $query->param('missinglist');
284     my $opacnote = $query->param('opacnote');
285     my $librariannote = $query->param('librariannote');
286     my $history_only = $query->param('history_only');
287         #  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
288     if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
289         ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
290         # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
291         $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
292     }
293     
294     if ($history_only) {
295         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
296     } else {
297         &ModSubscription(
298             $auser,           $branchcode,   $aqbooksellerid, $cost,
299             $aqbudgetid,      $startdate,    $periodicity,    $firstissuedate,
300             $dow,             join(",",@irregularity), $numberpattern,  $numberlength,
301             $weeklength,      $monthlength,  $add1,           $every1,
302             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
303             $add2,            $every2,       $whenmorethan2,  $setto2,
304             $lastvalue2,      $innerloop2,   $add3,           $every3,
305             $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
306             $numberingmethod, $status,       $biblionumber,   $callnumber,
307             $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
308             $serialsadditems, $subscriptionid,
309         );
310     }
311     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
312 } else {
313
314         while (@subscription_types) {
315            my $sub_type = shift @subscription_types;
316            my %row = ( 'name' => $sub_type );
317            if ( $sub_on eq $sub_type ) {
318              $row{'selected'} = ' selected';
319            } else {
320              $row{'selected'} = '';
321            }
322            push( @sub_type_data, \%row );
323         }    
324     $template->param(subtype => \@sub_type_data,
325         );
326         output_html_with_http_headers $query, $cookie, $template->output;
327 }