Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / serials / routing-preview.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 # Routing Preview.pl script used to view a routing list after creation
19 # lets one print out routing slip and create (in this instance) the hierarchy
20 # of reserves for the serial
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use C4::Koha;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::Reserves qw( AddReserve ModReserve );
27 use C4::Context;
28 use C4::Items qw( GetItemsInfo );
29 use C4::Serials qw( delroutingmember getroutinglist GetSubscription GetSerials check_routing );
30 use URI::Escape;
31
32 use Koha::Biblios;
33 use Koha::Libraries;
34 use Koha::Patrons;
35
36 my $query = CGI->new;
37 my $subscriptionid = $query->param('subscriptionid');
38 my $issue = $query->param('issue');
39 my $routingid;
40 my $ok = $query->param('ok');
41 my $edit = $query->param('edit');
42 my $delete = $query->param('delete');
43 my $dbh = C4::Context->dbh;
44
45 if($delete){
46     delroutingmember($routingid,$subscriptionid);
47     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
48     $sth->execute($subscriptionid);
49     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
50 }
51
52 if($edit){
53     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
54 }
55
56 my @routinglist = getroutinglist($subscriptionid);
57 my $subs = GetSubscription($subscriptionid);
58 my ($tmp ,@serials) = GetSerials($subscriptionid);
59 my ($template, $loggedinuser, $cookie);
60
61 my $library;
62 if($ok){
63     # get biblio information....
64     my $biblionumber = $subs->{'bibnum'};
65     my @itemresults = GetItemsInfo( $biblionumber );
66     my $branch = @itemresults ? $itemresults[0]->{'holdingbranch'} : $subs->{branchcode};
67     $library = Koha::Libraries->find($branch);
68
69         if (C4::Context->preference('RoutingListAddReserves')){
70                 # get existing reserves .....
71
72         my $biblio = Koha::Biblios->find( $biblionumber );
73         my $holds = $biblio->current_holds;
74         my $count = $holds->count;
75         while ( my $hold = $holds->next ) {
76             $count-- if $hold->is_waiting;
77         }
78                 my $notes;
79                 my $title = $subs->{'bibliotitle'};
80         for my $routing ( @routinglist ) {
81             my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1');
82             $sth->execute($biblionumber,$routing->{borrowernumber});
83             my $reserve = $sth->fetchrow_hashref;
84
85             if($routing->{borrowernumber} == $reserve->{borrowernumber}){
86                 ModReserve({
87                     rank           => $routing->{ranking},
88                     biblionumber   => $biblionumber,
89                     borrowernumber => $routing->{borrowernumber},
90                     branchcode     => $branch
91                 });
92             } else {
93                 AddReserve(
94                     {
95                         branchcode     => $branch,
96                         borrowernumber => $routing->{borrowernumber},
97                         biblionumber   => $biblionumber,
98                         priority       => $routing->{ranking},
99                         notes          => $notes,
100                         title          => $title,
101                     }
102                 );
103         }
104     }
105         }
106
107     ($template, $loggedinuser, $cookie)
108 = get_template_and_user({template_name => "serials/routing-preview-slip.tt",
109                                 query => $query,
110                                 type => "intranet",
111                                 flagsrequired => {serials => '*'},
112                                 });
113 } else {
114     ($template, $loggedinuser, $cookie)
115 = get_template_and_user({template_name => "serials/routing-preview.tt",
116                                 query => $query,
117                                 type => "intranet",
118                                 flagsrequired => {serials => '*'},
119                                 });
120 }
121
122 $template->param( libraryname => $library->branchname ) if $library;
123
124 my $memberloop = [];
125 for my $routing (@routinglist) {
126     my $member = Koha::Patrons->find( $routing->{borrowernumber} )->unblessed;
127     $member->{name}           = "$member->{firstname} $member->{surname}";
128     push @{$memberloop}, $member;
129 }
130
131 my $routingnotes = $serials[0]->{'routingnotes'};
132 $routingnotes =~ s/\n/\<br \/\>/g;
133
134 $template->param(
135     title => $subs->{'bibliotitle'},
136     issue => $issue,
137     issue_escaped => URI::Escape::uri_escape_utf8($issue),
138     subscriptionid => $subscriptionid,
139     memberloop => $memberloop,
140     routingnotes => $routingnotes,
141     hasRouting => check_routing($subscriptionid),
142     (uc(C4::Context->preference("marcflavour"))) => 1
143     );
144
145 output_html_with_http_headers $query, $cookie, $template->output;