Followup C4::Circulation.pm previous commit
[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 $subscriptionid;
76 my $subs;
77 my $firstissuedate;
78 my $nextexpected;
79
80 if ($op eq 'mod' || $op eq 'dup' || $op eq 'modsubscription') {
81
82     $subscriptionid = $query->param('subscriptionid');
83     $subs = &GetSubscription($subscriptionid);
84 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
85     if ($subs->{'cannotedit'} && $op eq 'mod'){
86       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
87       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
88     }
89     $firstissuedate = $subs->{firstacquidate};  # in iso format.
90     for (qw(startdate firstacquidate histstartdate enddate histenddate)) {
91         # TODO : Handle date formats properly.
92          if ($subs->{$_} eq '0000-00-00') {
93             $subs->{$_} = ''
94         } else {
95             $subs->{$_} = format_date($subs->{$_});
96         }
97           }
98     $subs->{'letter'}='' unless($subs->{'letter'});
99     $irregularity   = $subs->{'irregularity'};
100     $numberpattern  = $subs->{'numberpattern'};
101     $nextexpected = GetNextExpected($subscriptionid);
102     $nextexpected->{'isfirstissue'} = $nextexpected->{planneddate}->output('iso') eq $firstissuedate ;
103     $subs->{nextacquidate} = $nextexpected->{planneddate}->output()  if($op eq 'mod');
104     unless($op eq 'modsubscription') {
105                 foreach my $length_unit qw(numberlength weeklength monthlength){
106                         if ($subs->{$length_unit}){
107                                 $sublength=$subs->{$length_unit};
108                                 $sub_on=$length_unit;
109                                 last;
110                         }
111                 }
112
113         $template->param($subs);
114         $template->param(
115                     $op => 1,
116                     "subtype_$sub_on" => 1,
117                     sublength =>$sublength,
118                     history => ($op eq 'mod' && $subs->{manualhistory} == 1 ),
119                     "periodicity".$subs->{'periodicity'} => 1,
120                     "dow".$subs->{'dow'} => 1,
121                     "numberpattern".$subs->{'numberpattern'} => 1,
122                     firstacquiyear => substr($firstissuedate,0,4),
123                     );
124     }
125 }
126
127 my $onlymine=C4::Context->preference('IndependantBranches') &&
128              C4::Context->userenv &&
129              C4::Context->userenv->{flags} % 2 !=1 &&
130              C4::Context->userenv->{branch};
131 my $branches = GetBranches($onlymine);
132 my @branchloop;
133 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
134     my $selected = 1 if ($thisbranch eq C4::Context->userenv->{'branch'});
135     my $selected = 1 if (defined($subs) && $thisbranch eq $subs->{'branchcode'});
136     my %row =(value => $thisbranch,
137                 selected => $selected,
138                 branchname => $branches->{$thisbranch}->{'branchname'},
139             );
140     push @branchloop, \%row;
141 }
142 $template->param(branchloop => \@branchloop,
143     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
144 );
145 my $count = 0;
146 # prepare template variables common to all $op conditions:
147 $template->param(  'dateformat_' . C4::Context->preference('dateformat') => 1 ,
148                 );
149
150 if ($op eq 'addsubscription') {
151     my $auser = $query->param('user');
152     my $branchcode = $query->param('branchcode');
153     my $aqbooksellerid = $query->param('aqbooksellerid');
154     my $cost = $query->param('cost');
155     my $aqbudgetid = $query->param('aqbudgetid'); 
156     my $startdate = $query->param('startdate');
157     my $firstacquidate = $query->param('firstacquidate');    
158     my $periodicity = $query->param('periodicity');
159     my $dow = $query->param('dow');
160     my @irregularity = $query->param('irregularity_select');
161     my $numberlength = 0;
162     my $weeklength = 0;
163     my $monthlength = 0;
164     my $numberpattern = $query->param('numbering_pattern');
165     my $sublength = $query->param('sublength');
166     my $subtype = $query->param('subtype');
167     if ($subtype eq 'months'){
168         $monthlength = $sublength;
169     } elsif ($subtype eq 'weeks'){
170         $weeklength = $sublength;
171     } else {
172         $numberlength = $sublength;
173     }
174     my $add1 = $query->param('add1');
175     my $every1 = $query->param('every1');
176     my $whenmorethan1 = $query->param('whenmorethan1');
177     my $setto1 = $query->param('setto1');
178     my $lastvalue1 = $query->param('lastvalue1');
179     my $innerloop1 =$query->param('innerloop1');
180     my $add2 = $query->param('add2');
181     my $every2 = $query->param('every2');
182     my $whenmorethan2 = $query->param('whenmorethan2');
183     my $setto2 = $query->param('setto2');
184     my $innerloop2 =$query->param('innerloop2');
185     my $lastvalue2 = $query->param('lastvalue2');
186     my $add3 = $query->param('add3');
187     my $every3 = $query->param('every3');
188     my $whenmorethan3 = $query->param('whenmorethan3');
189     my $setto3 = $query->param('setto3');
190     my $lastvalue3 = $query->param('lastvalue3');
191     my $innerloop3 =$query->param('innerloop3');
192     my $numberingmethod = $query->param('numberingmethod');
193     my $status = 1;
194     my $biblionumber = $query->param('biblionumber');
195     my $callnumber = $query->param('callnumber');
196     my $notes = $query->param('notes');
197     my $internalnotes = $query->param('internalnotes');
198     my $hemisphere = $query->param('hemisphere') || 1;
199         my $letter = $query->param('letter');
200     # ## BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
201     my $manualhistory = $query->param('manualhist');
202     my $serialsadditems = $query->param('serialsadditems');
203         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
204                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
205                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
206                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
207                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
208                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
209                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes,
210                     $serialsadditems,
211                                 );
212
213     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
214 } elsif ($op eq 'modsubscription') {
215     my $subscriptionid = $query->param('subscriptionid');
216           my @irregularity = $query->param('irregularity_select');
217     my $auser = $query->param('user');
218     my $librarian => $query->param('librarian'),
219     my $branchcode = $query->param('branchcode');
220     my $cost = $query->param('cost');
221     my $aqbooksellerid = $query->param('aqbooksellerid');
222     my $biblionumber = $query->param('biblionumber');
223     my $aqbudgetid = $query->param('aqbudgetid');
224     my $startdate = format_date_in_iso($query->param('startdate'));
225     my $nextacquidate = $query->param('nextacquidate') ?
226                             format_date_in_iso($query->param('nextacquidate')):
227                             format_date_in_iso($query->param('startdate'));
228     my $periodicity = $query->param('periodicity');
229     my $dow = $query->param('dow');
230     my $sublength = $query->param('sublength');
231     my $subtype = $query->param('subtype');
232
233     if($subtype eq 'months'){
234         $monthlength = $sublength;
235     } elsif ($subtype eq 'weeks'){
236         $weeklength = $sublength;
237     } else {
238         $numberlength = $sublength;
239     }
240     my $numberpattern = $query->param('numbering_pattern');
241     my $add1 = $query->param('add1');
242     my $every1 = $query->param('every1');
243     my $whenmorethan1 = $query->param('whenmorethan1');
244     my $setto1 = $query->param('setto1');
245     my $lastvalue1 = $query->param('lastvalue1');
246     my $innerloop1 = $query->param('innerloop1');
247     my $add2 = $query->param('add2');
248     my $every2 = $query->param('every2');
249     my $whenmorethan2 = $query->param('whenmorethan2');
250     my $setto2 = $query->param('setto2');
251     my $lastvalue2 = $query->param('lastvalue2');
252     my $innerloop2 = $query->param('innerloop2');
253     my $add3 = $query->param('add3');
254     my $every3 = $query->param('every3');
255     my $whenmorethan3 = $query->param('whenmorethan3');
256     my $setto3 = $query->param('setto3');
257     my $lastvalue3 = $query->param('lastvalue3');
258     my $innerloop3 = $query->param('innerloop3');
259     my $numberingmethod = $query->param('numberingmethod');
260     my $status = 1;
261     my $callnumber = $query->param('callnumber');
262     my $notes = $query->param('notes');
263     my $internalnotes = $query->param('internalnotes');
264     my $hemisphere = $query->param('hemisphere');
265     my $letter = $query->param('letter');
266     my $manualhistory = $query->param('manualhist');
267     my $enddate = $query->param('enddate');
268     my $serialsadditems = $query->param('serialsadditems');
269     # subscription history
270     my $histenddate = format_date_in_iso($query->param('histenddate'));
271     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
272     my $recievedlist = $query->param('recievedlist');
273     my $missinglist = $query->param('missinglist');
274     my $opacnote = $query->param('opacnote');
275     my $librariannote = $query->param('librariannote');
276     my $history_only = $query->param('history_only');
277         #  If it's  a mod, we need to check the current 'expected' issue, and mod it in the serials table if necessary.
278     if ( $nextacquidate ne $nextexpected->{planneddate}->output('iso') ) {
279         ModNextExpected($subscriptionid,C4::Dates->new($nextacquidate,'iso'));
280         # if we have not received any issues yet, then we also must change the firstacquidate for the subs.
281         $firstissuedate = $nextacquidate if($nextexpected->{isfirstissue});
282     }
283
284     if ($history_only) {
285         ModSubscriptionHistory ($subscriptionid,$histstartdate,$histenddate,$recievedlist,$missinglist,$opacnote,$librariannote);
286     } else {
287         &ModSubscription(
288             $auser,           $branchcode,   $aqbooksellerid, $cost,
289             $aqbudgetid,      $startdate,    $periodicity,    $firstissuedate,
290             $dow,             join(",",@irregularity), $numberpattern,  $numberlength,
291             $weeklength,      $monthlength,  $add1,           $every1,
292             $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
293             $add2,            $every2,       $whenmorethan2,  $setto2,
294             $lastvalue2,      $innerloop2,   $add3,           $every3,
295             $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
296             $numberingmethod, $status,       $biblionumber,   $callnumber,
297             $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
298             $serialsadditems, $subscriptionid,
299         );
300     }
301     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
302 } else {
303
304         while (@subscription_types) {
305            my $sub_type = shift @subscription_types;
306            my %row = ( 'name' => $sub_type );
307            if ( $sub_on eq $sub_type ) {
308              $row{'selected'} = ' selected';
309            } else {
310              $row{'selected'} = '';
311            }
312            push( @sub_type_data, \%row );
313         }
314     $template->param(subtype => \@sub_type_data,
315         );
316
317     my $new_biblionumber = $query->param('biblionumber_for_new_subscription');
318     if (defined $new_biblionumber) {
319         my $bib = GetBiblioData($new_biblionumber);
320         if (defined $bib) {
321             $template->param(bibnum      => $new_biblionumber);
322             $template->param(bibliotitle => $bib->{title});
323         }
324     }
325         output_html_with_http_headers $query, $cookie, $template->output;
326 }
327
328 sub letter_loop {
329     my ($selected_letter, $template) = @_;
330     my $letters = GetLetters('serial');
331     my @letterloop;
332     foreach my $thisletter (keys %$letters) {
333         my $selected = $thisletter eq $selected_letter ? 1 : 0;
334         push @letterloop, {
335             value => $thisletter,
336             selected => $selected,
337             lettername => $letters->{$thisletter},
338         };
339     }
340     $template->param(letterloop => \@letterloop) if @letterloop;
341     return;
342 }