Bug 15758: Koha::Libraries - Remove GetBranches
[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 use C4::Branch;
37 use Koha::Libraries;
38
39 my $query = new CGI;
40 my $subscriptionid = $query->param('subscriptionid');
41 my $issue = $query->param('issue');
42 my $routingid;
43 my $ok = $query->param('ok');
44 my $edit = $query->param('edit');
45 my $delete = $query->param('delete');
46 my $dbh = C4::Context->dbh;
47
48 if($delete){
49     delroutingmember($routingid,$subscriptionid);
50     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
51     $sth->execute($subscriptionid);
52     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
53 }
54
55 if($edit){
56     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
57 }
58
59 my @routinglist = getroutinglist($subscriptionid);
60 my $subs = GetSubscription($subscriptionid);
61 my ($tmp ,@serials) = GetSerials($subscriptionid);
62 my ($template, $loggedinuser, $cookie);
63
64 if($ok){
65     # get biblio information....
66     my $biblio = $subs->{'biblionumber'};
67         my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblio);
68         my @itemresults = GetItemsInfo( $subs->{biblionumber} );
69         my $branch = $itemresults[0]->{'holdingbranch'};
70     my $branchname = Koha::Libraries->find($branch)->branchname;
71
72         if (C4::Context->preference('RoutingListAddReserves')){
73                 # get existing reserves .....
74         my $reserves = GetReservesFromBiblionumber({ biblionumber => $biblio });
75         my $count = scalar( @$reserves );
76         my $totalcount = $count;
77                 foreach my $res (@$reserves) {
78                         if ($res->{'found'} eq 'W') {
79                                 $count--;
80                         }
81                 }
82                 my $notes;
83                 my $title = $subs->{'bibliotitle'};
84         for my $routing ( @routinglist ) {
85             my $sth = $dbh->prepare('SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? LIMIT 1');
86             $sth->execute($biblio,$routing->{borrowernumber});
87             my $reserve = $sth->fetchrow_hashref;
88
89             if($routing->{borrowernumber} == $reserve->{borrowernumber}){
90                 ModReserve({
91                     rank           => $routing->{ranking},
92                     biblionumber   => $biblio,
93                     borrowernumber => $routing->{borrowernumber},
94                     branchcode     => $branch
95                 });
96             } else {
97                 AddReserve($branch,$routing->{borrowernumber},$biblio,\@bibitems,$routing->{ranking}, undef, undef, $notes,$title);
98         }
99     }
100         }
101
102     ($template, $loggedinuser, $cookie)
103 = get_template_and_user({template_name => "serials/routing-preview-slip.tt",
104                                 query => $query,
105                                 type => "intranet",
106                                 authnotrequired => 0,
107                                 flagsrequired => {serials => '*'},
108                                 debug => 1,
109                                 });
110     $template->param("libraryname"=>$branchname);
111 } else {
112     ($template, $loggedinuser, $cookie)
113 = get_template_and_user({template_name => "serials/routing-preview.tt",
114                                 query => $query,
115                                 type => "intranet",
116                                 authnotrequired => 0,
117                                 flagsrequired => {serials => '*'},
118                                 debug => 1,
119                                 });
120 }
121
122 my $memberloop = [];
123 for my $routing (@routinglist) {
124     my $member = GetMember( borrowernumber => $routing->{borrowernumber} );
125     $member->{name}           = "$member->{firstname} $member->{surname}";
126     push @{$memberloop}, $member;
127 }
128
129 my $routingnotes = $serials[0]->{'routingnotes'};
130 $routingnotes =~ s/\n/\<br \/\>/g;
131
132 $template->param(
133     title => $subs->{'bibliotitle'},
134     issue => $issue,
135     issue_escaped => URI::Escape::uri_escape_utf8($issue),
136     subscriptionid => $subscriptionid,
137     memberloop => $memberloop,
138     routingnotes => $routingnotes,
139     generalroutingnote => C4::Context->preference('RoutingListNote'),
140     hasRouting => check_routing($subscriptionid),
141     (uc(C4::Context->preference("marcflavour"))) => 1
142     );
143
144 output_html_with_http_headers $query, $cookie, $template->output;