Bug 33568: Remove debug_display
[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 Try::Tiny;
30 use CGI qw ( -utf8 );
31 use C4::Koha;
32 use C4::Auth qw( get_template_and_user );
33 use C4::Output qw( output_and_exit output_html_with_http_headers );
34 use C4::Context;
35
36 use C4::Serials qw( GetSubscription delroutingmember addroutingmember getroutinglist GetSerials GetLatestSerials check_routing );
37 use Koha::Patrons;
38
39 use URI::Escape;
40
41 my $query = CGI->new;
42 my $subscriptionid = $query->param('subscriptionid');
43 my $serialseq = $query->param('serialseq');
44 my $routingid = $query->param('routingid');
45 my $borrowernumbers = $query->param('borrowernumbers');
46 my $notes = $query->param('notes');
47 my $op = $query->param('op') || q{};
48 my $date_selected = $query->param('date_selected');
49 $date_selected ||= q{};
50 my $dbh = C4::Context->dbh;
51
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53     {
54         template_name   => 'serials/routing.tt',
55         query           => $query,
56         type            => 'intranet',
57         flagsrequired   => { serials => 'routing' },
58     }
59 );
60
61 my $subs = GetSubscription($subscriptionid);
62
63 output_and_exit( $query, $cookie, $template, 'unknown_subscription')
64     unless $subs;
65
66 if($op eq 'cud-delete'){
67     delroutingmember($routingid,$subscriptionid);
68 }
69
70 if ( $op eq 'cud-add_new_recipients' ) {
71     for my $borrowernumber ( split ':', $borrowernumbers ) {
72         try {
73             addroutingmember( $borrowernumber, $subscriptionid );
74         } catch {
75             if ( $_ !~ m{Duplicate entry .* for key 'subscriptionid'} ) {
76                 warn $_;
77             }
78         };
79     }
80 }
81 if($op eq 'cud-save'){
82     my $sth = $dbh->prepare('UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?');
83     $sth->execute($notes,$subscriptionid);
84     my $urldate = URI::Escape::uri_escape_utf8($date_selected);
85     print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$urldate");
86 }
87
88 my @routinglist = getroutinglist($subscriptionid);
89
90 my ($count,@serials) = GetSerials($subscriptionid);
91 my $serialdates = GetLatestSerials($subscriptionid,$count);
92
93 my $dates = [];
94 foreach my $dateseq (@{$serialdates}) {
95     my $d = {};
96     $d->{publisheddate} = $dateseq->{publisheddate};
97     $d->{serialseq} = $dateseq->{serialseq};
98     $d->{serialid} = $dateseq->{serialid};
99     if($date_selected eq $dateseq->{serialid}){
100         $d->{selected} = ' selected';
101     } else {
102         $d->{selected} = q{};
103     }
104     push @{$dates}, $d;
105 }
106
107 my $member_loop = [];
108 for my $routing ( @routinglist ) {
109     my $member = Koha::Patrons->find( $routing->{borrowernumber} )->unblessed;
110     $member->{location} = $member->{branchcode};
111     if ($member->{firstname} ) {
112         $member->{name} = $member->{firstname} . q| |;
113     }
114     else {
115         $member->{name} = q{};
116     }
117     if ($member->{surname} ) {
118         $member->{name} .= $member->{surname};
119     }
120     $member->{routingid}=$routing->{routingid} || q{};
121     $member->{ranking} = $routing->{ranking} || q{};
122
123     push(@{$member_loop}, $member);
124 }
125
126 $template->param(
127     title => $subs->{bibliotitle},
128     subscriptionid => $subscriptionid,
129     memberloop => $member_loop,
130     op => $op eq 'new',
131     dates => $dates,
132     routingnotes => $serials[0]->{'routingnotes'},
133     hasRouting => check_routing($subscriptionid),
134     (uc(C4::Context->preference("marcflavour"))) => 1
135
136     );
137
138 output_html_with_http_headers $query, $cookie, $template->output;