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