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