Merge remote branch 'kc/master' into new/bug_3013
[koha.git] / serials / routing.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
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18
19 =head1 Routing.pl
20
21 script used to create a routing list for a serial subscription
22 In this instance it is in fact a setting up of a list of reserves for the item
23 where the hierarchical order can be changed on the fly and a routing list can be
24 printed out
25
26 =cut
27
28 use strict;
29 use warnings;
30 use CGI;
31 use C4::Koha;
32 use C4::Auth;
33 use C4::Dates;
34 use C4::Output;
35 use C4::Acquisition;
36 use C4::Output;
37 use C4::Context;
38
39 use C4::Members;
40 use C4::Serials;
41
42 use URI::Escape;
43
44 my $query = new CGI;
45 my $subscriptionid = $query->param('subscriptionid');
46 my $serialseq = $query->param('serialseq');
47 my $routingid = $query->param('routingid');
48 my $borrowernumber = $query->param('borrowernumber');
49 my $notes = $query->param('notes');
50 my $op = $query->param('op') || q{};
51 my $date_selected = $query->param('date_selected');
52 $date_selected ||= q{};
53 my $dbh = C4::Context->dbh;
54
55 if($op eq 'delete'){
56     delroutingmember($routingid,$subscriptionid);
57 }
58
59 if($op eq 'add'){
60     addroutingmember($borrowernumber,$subscriptionid);
61 }
62 if($op eq 'save'){
63     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?");
64     $sth->execute($notes,$subscriptionid);
65     my $urldate = URI::Escape::uri_escape($date_selected);
66     print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$urldate");
67 }
68
69 my ($routing, @routinglist) = getroutinglist($subscriptionid);
70 my $subs = GetSubscription($subscriptionid);
71 my ($count,@serials) = GetSerials($subscriptionid);
72 my $serialdates = GetLatestSerials($subscriptionid,$count);
73
74 my $dates = [];
75 foreach my $dateseq (@{$serialdates}) {
76     my $d = {};
77     $d->{planneddate} = $dateseq->{planneddate};
78     $d->{serialseq} = $dateseq->{serialseq};
79     $d->{serialid} = $dateseq->{serialid};
80     if($date_selected eq $dateseq->{serialid}){
81         $d->{selected} = ' selected';
82     } else {
83         $d->{selected} = q{};
84     }
85     push @{$dates}, $d;
86 }
87
88 my ($template, $loggedinuser, $cookie)
89 = get_template_and_user({template_name => "serials/routing.tmpl",
90                                 query => $query,
91                                 type => "intranet",
92                                 authnotrequired => 0,
93                                 flagsrequired => {serials => 'routing'},
94                                 debug => 1,
95                                 });
96
97 my @results;
98 my $data;
99 for(my $i=0;$i<$routing;$i++){
100     $data=GetMember('borrowernumber' => $routinglist[$i]->{'borrowernumber'});
101     $data->{'location'}=$data->{'branchcode'};
102     if ($data->{firstname} ) {
103         $data->{name} = $data->{firstname} . q| |;
104     }
105     else {
106         $data->{name} = q{};
107     }
108     if ($data->{surname} ) {
109         $data->{name} .= $data->{surname};
110     }
111     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
112     $data->{'subscriptionid'}=$subscriptionid;
113     if (! $routinglist[$i]->{routingid} ) {
114         $routinglist[$i]->{routingid} = q||;
115     }
116     my $rankingbox = '<select name="itemrank" onchange="reorder_item('
117     . $subscriptionid . ',' .$routinglist[$i]->{'routingid'} . ',this.options[this.selectedIndex].value)">';
118     for(my $j=1; $j <= $routing; $j++) {
119         $rankingbox .= "<option ";
120         if($routinglist[$i]->{ranking} && $routinglist[$i]->{ranking} == $j){
121             $rankingbox .= " selected=\"selected\"";
122         }
123         $rankingbox .= " value=\"$j\">$j</option>";
124     }
125     $rankingbox .= "</select>";
126     $data->{'routingbox'} = $rankingbox;
127
128     push(@results, $data);
129 }
130
131 # for adding routing list
132 my $new;
133 if ($op eq 'new') {
134     $new = 1;
135 } else {
136 # for modify routing list default
137     $new = 0;
138 }
139
140 $template->param(
141     title => $subs->{'bibliotitle'},
142     subscriptionid => $subscriptionid,
143     memberloop => \@results,
144     op => $new,
145     dates => $dates,
146     routingnotes => $serials[0]->{'routingnotes'},
147     hasRouting => check_routing($subscriptionid),
148     );
149
150         output_html_with_http_headers $query, $cookie, $template->output;