start of BIB change -- introduce C4::Items
[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
65         #BUGFIX padding add_delta_days() date
66         my $output  = sprintf("%04d-%02d-%02d",$y , $m, $d );
67
68         $weekarrayjs .= "'Wk $wkno: ".format_date($output)."',";
69         $wkno++;    
70 }
71 chop($weekarrayjs);
72 # warn $weekarrayjs;
73
74 my $sub_on;
75 my @subscription_types = (
76             'issues', 'weeks', 'months'
77         ); 
78 my @sub_type_data;
79
80 my $letters = GetLetters('serial');
81 my @letterloop;
82 foreach my $thisletter (keys %$letters) {
83     my $selected = 1 if $thisletter eq $letter;
84     my %row =(value => $thisletter,
85                 selected => $selected,
86                 lettername => $letters->{$thisletter},
87             );
88     push @letterloop, \%row;
89 }
90 $template->param(letterloop => \@letterloop);
91
92 my $onlymine=C4::Context->preference('IndependantBranches') && 
93              C4::Context->userenv && 
94              C4::Context->userenv->{flags}!=1 && 
95              C4::Context->userenv->{branch};
96 my $branches = GetBranches($onlymine);
97 my @branchloop;
98 foreach my $thisbranch (keys %$branches) {
99     my $selected = 1 if $thisbranch eq C4::Context->userenv->{'branch'};
100     my %row =(value => $thisbranch,
101                 selected => $selected,
102                 branchname => $branches->{$thisbranch}->{'branchname'},
103             );
104     push @branchloop, \%row;
105 }
106 $template->param(branchloop => \@branchloop,
107     DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
108 );
109
110 if ($op eq 'mod'||$op eq 'dup') {
111
112     my $subscriptionid = $query->param('subscriptionid');
113 #     warn "irregularity :$irregularity numberpattern : $numberpattern, callnumber :$callnumber, firstacquidate :$firstacquidate";
114     my $subs = &GetSubscription($subscriptionid);
115 ## FIXME : Check rights to edit if mod. Could/Should display an error message.
116     if ($subs->{'cannotedit'} && $op eq 'mod'){
117       warn "Attempt to modify subscription $subscriptionid by ".C4::Context->userenv->{'id'}." not allowed";
118       print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
119     }  
120         for (qw(startdate firstacquidate histstartdate enddate)) {
121         $subs->{$_} = format_date($subs->{$_});
122         }
123     $subs->{'letter'}='' unless($subs->{'letter'});
124
125     if($subs->{numberlength} > 0){
126         $sublength = $subs->{numberlength};
127         $sub_on = $subscription_types[0];
128     } elsif ($subs->{weeklength}>0){
129         $sublength = $subs->{weeklength};
130         $sub_on = $subscription_types[1];
131     } else {
132         $sublength = $subs->{monthlength};
133         $sub_on = $subscription_types[2];
134     }
135     while (@subscription_types) {
136         my $sub_type = shift @subscription_types;
137         my %row = ( 'name' => $sub_type );
138         if ( $sub_on eq $sub_type ) {
139             $row{'selected'} = ' selected';
140         } else {
141             $row{'selected'} = '';
142         }
143         push( @sub_type_data, \%row );
144     }
145
146     $template->param($subs);
147     $template->param(
148             $op => 1,
149             subtype => \@sub_type_data,
150             sublength =>$sublength,
151             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
152             );
153     $template->param(
154                 "periodicity".$subs->{'periodicity'} => 1,
155                 "dow".$subs->{'dow'} => 1,
156                 "numberpattern".$subs->{'numberpattern'} => 1,
157                 );
158 }
159
160 if ($op eq 'addsubscription') {
161
162     my $auser = $query->param('user');
163     my $branchcode = $query->param('branchcode');
164     my $aqbooksellerid = $query->param('aqbooksellerid');
165     my $cost = $query->param('cost');
166     my $aqbudgetid = $query->param('aqbudgetid'); 
167     my $startdate = $query->param('startdate');
168     my $firstacquidate = $query->param('firstacquidate');    
169     my $periodicity = $query->param('periodicity');
170     my $dow = $query->param('dow');
171     my @irregularity = $query->param('irregular');
172     my $numberlength = 0;
173     my $weeklength = 0;
174     my $monthlength = 0;
175     my $numberpattern = $query->param('numbering_pattern');
176     my $sublength = $query->param('sublength');
177     my $subtype = $query->param('subtype');
178     if ($subtype eq 'months'){
179         $monthlength = $sublength;
180     } elsif ($subtype eq 'weeks'){
181         $weeklength = $sublength;
182     } else {
183         $numberlength = $sublength;
184     }
185     my $add1 = $query->param('add1');
186     my $every1 = $query->param('every1');
187     my $whenmorethan1 = $query->param('whenmorethan1');
188     my $setto1 = $query->param('setto1');
189     my $lastvalue1 = $query->param('lastvalue1');
190     my $innerloop1 =$query->param('innerloop1');
191     my $add2 = $query->param('add2');
192     my $every2 = $query->param('every2');
193     my $whenmorethan2 = $query->param('whenmorethan2');
194     my $setto2 = $query->param('setto2');
195     my $innerloop2 =$query->param('innerloop2');
196     my $lastvalue2 = $query->param('lastvalue2');
197     my $add3 = $query->param('add3');
198     my $every3 = $query->param('every3');
199     my $whenmorethan3 = $query->param('whenmorethan3');
200     my $setto3 = $query->param('setto3');
201     my $lastvalue3 = $query->param('lastvalue3');
202     my $innerloop3 =$query->param('innerloop3');
203     my $numberingmethod = $query->param('numberingmethod');
204     my $status = 1;
205     my $biblionumber = $query->param('biblionumber');
206     my $callnumber = $query->param('callnumber');
207     my $notes = $query->param('notes');
208     my $internalnotes = $query->param('internalnotes');
209     my $hemisphere = $query->param('hemisphere') || 1;
210         my $letter = $query->param('letter');
211     ### BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
212     my $manualhistory = $query->param('manualhist');
213         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
214                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
215                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
216                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
217                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
218                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
219                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
220                                 );
221
222     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
223 } elsif ($op eq 'modsubscription') {
224     my $subscriptionid = $query->param('subscriptionid');
225     my @irregular = $query->param('irregular');
226     my $irregular_count = @irregular;
227     for(my $i =0;$i<$irregular_count;$i++){
228       $irregularity .=$irregular[$i].",";
229       warn "irregular : $irregular[$i] string :$irregularity";
230     }
231     $irregularity =~ s/\,$//;
232
233     my $auser = $query->param('user');
234     my $librarian => $query->param('librarian'),
235     my $branchcode = $query->param('branchcode');
236     my $cost = $query->param('cost');
237     my $aqbooksellerid = $query->param('aqbooksellerid');
238     my $biblionumber = $query->param('biblionumber');
239     my $aqbudgetid = $query->param('aqbudgetid');
240     my $startdate = format_date_in_iso($query->param('startdate'));
241     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
242     my $periodicity = $query->param('periodicity');
243     my $dow = $query->param('dow');
244     my $sublength = $query->param('sublength');
245     my $subtype = $query->param('subtype');
246
247     if($subtype eq 'months'){
248         $monthlength = $sublength;
249     } elsif ($subtype eq 'weeks'){
250         $weeklength = $sublength;
251     } else {
252         $numberlength = $sublength;
253     }
254     my $numberpattern = $query->param('numbering_pattern');
255     my $add1 = $query->param('add1');
256     my $every1 = $query->param('every1');
257     my $whenmorethan1 = $query->param('whenmorethan1');
258     my $setto1 = $query->param('setto1');
259     my $lastvalue1 = $query->param('lastvalue1');
260     my $innerloop1 = $query->param('innerloop1');
261     my $add2 = $query->param('add2');
262     my $every2 = $query->param('every2');
263     my $whenmorethan2 = $query->param('whenmorethan2');
264     my $setto2 = $query->param('setto2');
265     my $lastvalue2 = $query->param('lastvalue2');
266     my $innerloop2 = $query->param('innerloop2');
267     my $add3 = $query->param('add3');
268     my $every3 = $query->param('every3');
269     my $whenmorethan3 = $query->param('whenmorethan3');
270     my $setto3 = $query->param('setto3');
271     my $lastvalue3 = $query->param('lastvalue3');
272     my $innerloop3 = $query->param('innerloop3');
273     my $numberingmethod = $query->param('numberingmethod');
274     my $status = 1;
275     my $callnumber = $query->param('callnumber');
276     my $notes = $query->param('notes');
277     my $internalnotes = $query->param('internalnotes');
278     my $hemisphere = $query->param('hemisphere');
279     my $letter = $query->param('letter');
280     my $manualhistory = $query->param('manualhist');
281     my $enddate = $query->param('enddate');
282     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
283     my $recievedlist = $query->param('recievedlist');
284     my $missinglist = $query->param('missinglist');
285     my $opacnote = $query->param('opacnote');
286     my $librariannote = $query->param('librariannote');
287     &ModSubscription(
288         $auser,           $branchcode,   $aqbooksellerid, $cost,
289         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
290         $dow,             $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         $subscriptionid);
299
300     ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
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                  weekarrayjs => $weekarrayjs,
316                  weekno => $weekno,
317         );
318         output_html_with_http_headers $query, $cookie, $template->output;
319 }