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