Bug 16522: (follow-up) MARC display templates and get_marc_host fixes
[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
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
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 Modern::Perl;
29 use CGI qw ( -utf8 );
30 use C4::Koha;
31 use C4::Auth qw( get_template_and_user );
32 use C4::Output qw( output_and_exit output_html_with_http_headers );
33 use C4::Context;
34
35 use C4::Serials qw( GetSubscription delroutingmember addroutingmember getroutinglist GetSerials GetLatestSerials check_routing );
36 use Koha::Patrons;
37
38 use URI::Escape;
39
40 my $query = CGI->new;
41 my $subscriptionid = $query->param('subscriptionid');
42 my $serialseq = $query->param('serialseq');
43 my $routingid = $query->param('routingid');
44 my $borrowernumber = $query->param('borrowernumber');
45 my $notes = $query->param('notes');
46 my $op = $query->param('op') || q{};
47 my $date_selected = $query->param('date_selected');
48 $date_selected ||= q{};
49 my $dbh = C4::Context->dbh;
50
51 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
52     {
53         template_name   => 'serials/routing.tt',
54         query           => $query,
55         type            => 'intranet',
56         flagsrequired   => { serials => 'routing' },
57     }
58 );
59
60 my $subs = GetSubscription($subscriptionid);
61
62 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
63     unless $subs;
64
65 if($op eq 'delete'){
66     delroutingmember($routingid,$subscriptionid);
67 }
68
69 if($op eq 'add'){
70     addroutingmember($borrowernumber,$subscriptionid);
71 }
72 if($op eq 'save'){
73     my $sth = $dbh->prepare('UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?');
74     $sth->execute($notes,$subscriptionid);
75     my $urldate = URI::Escape::uri_escape_utf8($date_selected);
76     print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$urldate");
77 }
78
79 my @routinglist = getroutinglist($subscriptionid);
80
81 my ($count,@serials) = GetSerials($subscriptionid);
82 my $serialdates = GetLatestSerials($subscriptionid,$count);
83
84 my $dates = [];
85 foreach my $dateseq (@{$serialdates}) {
86     my $d = {};
87     $d->{publisheddate} = $dateseq->{publisheddate};
88     $d->{serialseq} = $dateseq->{serialseq};
89     $d->{serialid} = $dateseq->{serialid};
90     if($date_selected eq $dateseq->{serialid}){
91         $d->{selected} = ' selected';
92     } else {
93         $d->{selected} = q{};
94     }
95     push @{$dates}, $d;
96 }
97
98 my $member_loop = [];
99 for my $routing ( @routinglist ) {
100     my $member = Koha::Patrons->find( $routing->{borrowernumber} )->unblessed;
101     $member->{location} = $member->{branchcode};
102     if ($member->{firstname} ) {
103         $member->{name} = $member->{firstname} . q| |;
104     }
105     else {
106         $member->{name} = q{};
107     }
108     if ($member->{surname} ) {
109         $member->{name} .= $member->{surname};
110     }
111     $member->{routingid}=$routing->{routingid} || q{};
112     $member->{ranking} = $routing->{ranking} || q{};
113
114     push(@{$member_loop}, $member);
115 }
116
117 $template->param(
118     title => $subs->{bibliotitle},
119     subscriptionid => $subscriptionid,
120     memberloop => $member_loop,
121     op => $op eq 'new',
122     dates => $dates,
123     routingnotes => $serials[0]->{'routingnotes'},
124     hasRouting => check_routing($subscriptionid),
125     (uc(C4::Context->preference("marcflavour"))) => 1
126
127     );
128
129 output_html_with_http_headers $query, $cookie, $template->output;