added inital 'delete' lines, so running 'mysql koha3 < marc21_framework_DEFAULT.sql...
[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::Date;
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 => get_date_format_string_for_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     $subs->{'startdate'}=format_date($subs->{'startdate'});
118     $subs->{'firstacquidate'}=format_date($subs->{'firstacquidate'});
119     $subs->{'histstartdate'}=format_date($subs->{'histstartdate'});
120     $subs->{'enddate'}=format_date($subs->{enddate});
121     $subs->{'letter'}='' unless($subs->{'letter'});
122
123     if($subs->{numberlength} > 0){
124         $sublength = $subs->{numberlength};
125         $sub_on = $subscription_types[0];
126     } elsif ($subs->{weeklength}>0){
127         $sublength = $weeklength;
128         $sub_on = $subscription_types[1];
129     } else {
130         $sublength = $subs->{monthlength};
131         $sub_on = $subscription_types[2];
132     }
133     while (@subscription_types) {
134         my $sub_type = shift @subscription_types;
135         my %row = ( 'name' => $sub_type );
136         if ( $sub_on eq $sub_type ) {
137             $row{'selected'} = ' selected';
138         } else {
139             $row{'selected'} = '';
140         }
141         push( @sub_type_data, \%row );
142     }
143
144     $template->param($subs);
145     $template->param(
146             $op => 1,
147             subtype => \@sub_type_data,
148             sublength =>$sublength,
149             history => ($op eq 'mod' && ($subs->{recievedlist}||$subs->{missinglist}||$subs->{opacnote}||$subs->{librariannote}))
150             );
151     $template->param(
152                 "periodicity".$subs->{'periodicity'} => 1,
153                 "dow".$subs->{'periodicity'} => 1,
154                 "numberpattern".$subs->{'periodicity'} => 1,
155                 );
156 }
157
158 if ($op eq 'addsubscription') {
159     my @irregular = $query->param('irregular');
160     my $irregular_count = scalar(@irregular);
161     for(my $i =0;$i<$irregular_count;$i++){
162         $irregularity .=$irregular[$i].",";
163     }
164     $irregularity =~ s/\,$//;
165
166     my $auser = $query->param('user');
167     my $branchcode = $query->param('branchcode');
168     my $aqbooksellerid = $query->param('aqbooksellerid');
169     my $cost = $query->param('cost');
170     my $aqbudgetid = $query->param('aqbudgetid'); 
171     my $startdate = $query->param('startdate');
172     my $firstacquidate = $query->param('firstacquidate');    
173     my $periodicity = $query->param('periodicity');
174     my $dow = $query->param('dow');
175     my @irregularity = $query->param('irregular');
176     my $numberlength = 0;
177     my $weeklength = 0;
178     my $monthlength = 0;
179     my $numberpattern = $query->param('numbering_pattern');
180     my $sublength = $query->param('sublength');
181     my $subtype = $query->param('subtype');
182     if ($subtype eq 'months'){
183         $monthlength = $sublength;
184     } elsif ($subtype eq 'weeks'){
185         $weeklength = $sublength;
186     } else {
187         $numberlength = $sublength;
188     }
189     my $add1 = $query->param('add1');
190     my $every1 = $query->param('every1');
191     my $whenmorethan1 = $query->param('whenmorethan1');
192     my $setto1 = $query->param('setto1');
193     my $lastvalue1 = $query->param('lastvalue1');
194     my $innerloop1 =$query->param('innerloop1');
195     my $add2 = $query->param('add2');
196     my $every2 = $query->param('every2');
197     my $whenmorethan2 = $query->param('whenmorethan2');
198     my $setto2 = $query->param('setto2');
199     my $innerloop2 =$query->param('innerloop2');
200     my $lastvalue2 = $query->param('lastvalue2');
201     my $add3 = $query->param('add3');
202     my $every3 = $query->param('every3');
203     my $whenmorethan3 = $query->param('whenmorethan3');
204     my $setto3 = $query->param('setto3');
205     my $lastvalue3 = $query->param('lastvalue3');
206     my $innerloop3 =$query->param('innerloop3');
207     my $numberingmethod = $query->param('numberingmethod');
208     my $status = 1;
209     my $biblionumber = $query->param('biblionumber');
210     my $callnumber = $query->param('callnumber');
211     my $notes = $query->param('notes');
212     my $internalnotes = $query->param('internalnotes');
213     my $hemisphere = $query->param('hemisphere') || 1;
214         my $letter = $query->param('letter');
215     ### BugFIX : hdl doesnot know what innerloops or letter stand for but it seems necessary. So he adds them.
216     my $manualhistory = $query->param('manualhist');
217         my $subscriptionid = NewSubscription($auser,$branchcode,$aqbooksellerid,$cost,$aqbudgetid,$biblionumber,
218                                         $startdate,$periodicity,$dow,$numberlength,$weeklength,$monthlength,
219                                         $add1,$every1,$whenmorethan1,$setto1,$lastvalue1,$innerloop1,
220                                         $add2,$every2,$whenmorethan2,$setto2,$lastvalue2,$innerloop2,
221                                         $add3,$every3,$whenmorethan3,$setto3,$lastvalue3,$innerloop3,
222                                         $numberingmethod, $status, $notes,$letter,$firstacquidate,join(",",@irregularity),
223                     $numberpattern, $callnumber, $hemisphere,($manualhistory?$manualhistory:0),$internalnotes
224                                 );
225
226     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
227 } elsif ($op eq 'modsubscription') {
228     my $subscriptionid = $query->param('subscriptionid');
229     my @irregular = $query->param('irregular');
230     my $irregular_count = @irregular;
231     for(my $i =0;$i<$irregular_count;$i++){
232       $irregularity .=$irregular[$i].",";
233       warn "irregular : $irregular[$i] string :$irregularity";
234     }
235     $irregularity =~ s/\,$//;
236
237     my $auser = $query->param('user');
238     my $librarian => $query->param('librarian'),
239     my $branchcode = $query->param('branchcode');
240     my $cost = $query->param('cost');
241     my $aqbooksellerid = $query->param('aqbooksellerid');
242     my $biblionumber = $query->param('biblionumber');
243     my $aqbudgetid = $query->param('aqbudgetid');
244     my $startdate = format_date_in_iso($query->param('startdate'));
245     my $firstacquidate = format_date_in_iso($query->param('firstacquidate'));    
246     my $periodicity = $query->param('periodicity');
247     my $dow = $query->param('dow');
248     my $sublength = $query->param('sublength');
249     my $subtype = $query->param('subtype');
250
251     if($subtype eq 'months'){
252         $monthlength = $sublength;
253     } elsif ($subtype eq 'weeks'){
254         $weeklength = $sublength;
255     } else {
256         $numberlength = $sublength;
257     }
258     my $numberpattern = $query->param('numbering_pattern');
259     my $add1 = $query->param('add1');
260     my $every1 = $query->param('every1');
261     my $whenmorethan1 = $query->param('whenmorethan1');
262     my $setto1 = $query->param('setto1');
263     my $lastvalue1 = $query->param('lastvalue1');
264     my $innerloop1 = $query->param('innerloop1');
265     my $add2 = $query->param('add2');
266     my $every2 = $query->param('every2');
267     my $whenmorethan2 = $query->param('whenmorethan2');
268     my $setto2 = $query->param('setto2');
269     my $lastvalue2 = $query->param('lastvalue2');
270     my $innerloop2 = $query->param('innerloop2');
271     my $add3 = $query->param('add3');
272     my $every3 = $query->param('every3');
273     my $whenmorethan3 = $query->param('whenmorethan3');
274     my $setto3 = $query->param('setto3');
275     my $lastvalue3 = $query->param('lastvalue3');
276     my $innerloop3 = $query->param('innerloop3');
277     my $numberingmethod = $query->param('numberingmethod');
278     my $status = 1;
279     my $callnumber = $query->param('callnumber');
280     my $notes = $query->param('notes');
281     my $internalnotes = $query->param('internalnotes');
282     my $hemisphere = $query->param('hemisphere');
283     my $letter = $query->param('letter');
284     my $manualhistory = $query->param('manualhist');
285     my $enddate = $query->param('enddate');
286     my $histstartdate = format_date_in_iso($query->param('histstartdate'));
287     my $recievedlist = $query->param('recievedlist');
288     my $missinglist = $query->param('missinglist');
289     my $opacnote = $query->param('opacnote');
290     my $librariannote = $query->param('librariannote');
291     &ModSubscription(
292         $auser,           $branchcode,   $aqbooksellerid, $cost,
293         $aqbudgetid,      $startdate,    $periodicity,    $firstacquidate,
294         $dow,             $irregularity, $numberpattern,  $numberlength,
295         $weeklength,      $monthlength,  $add1,           $every1,
296         $whenmorethan1,   $setto1,       $lastvalue1,     $innerloop1,
297         $add2,            $every2,       $whenmorethan2,  $setto2,
298         $lastvalue2,      $innerloop2,   $add3,           $every3,
299         $whenmorethan3,   $setto3,       $lastvalue3,     $innerloop3,
300         $numberingmethod, $status,       $biblionumber,   $callnumber,
301         $notes,           $letter,       $hemisphere,     $manualhistory,$internalnotes,
302         $subscriptionid);
303
304     ModSubscriptionHistory ($subscriptionid,$histstartdate,$enddate,$recievedlist,$missinglist,$opacnote,$librariannote);
305     print $query->redirect("/cgi-bin/koha/serials/subscription-detail.pl?subscriptionid=$subscriptionid");
306 } else {
307
308         while (@subscription_types) {
309            my $sub_type = shift @subscription_types;
310            my %row = ( 'name' => $sub_type );
311            if ( $sub_on eq $sub_type ) {
312              $row{'selected'} = ' selected';
313            } else {
314              $row{'selected'} = '';
315            }
316            push( @sub_type_data, \%row );
317         }    
318     $template->param(subtype => \@sub_type_data,
319                  weekarrayjs => $weekarrayjs,
320                  weekno => $weekno,
321         );
322         output_html_with_http_headers $query, $cookie, $template->output;
323 }