Merge remote-tracking branch 'kc/new/bug_5995' into kcmaster
[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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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;
24 use C4::Koha;
25 use C4::Auth;
26 use C4::Dates;
27 use C4::Output;
28 use C4::Acquisition;
29 use C4::Reserves;
30 use C4::Circulation;
31 use C4::Context;
32 use C4::Members;
33 use C4::Biblio;
34 use C4::Items;
35 use C4::Serials;
36 use URI::Escape;
37 use C4::Branch;
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 ($routing, @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 = GetBranchName($branch);
71
72         if (C4::Context->preference('RoutingListAddReserves')){
73                 # get existing reserves .....
74                 my ($count,$reserves) = GetReservesFromBiblionumber($biblio);
75                 my $totalcount = $count;
76                 foreach my $res (@$reserves) {
77                         if ($res->{'found'} eq 'W') {
78                                 $count--;
79                         }
80                 }
81                 my $const = 'o';
82                 my $notes;
83                 my $title = $subs->{'bibliotitle'};
84                 for(my $i=0;$i<$routing;$i++){
85                         my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ?");
86                                 $sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
87                                 my $data = $sth->fetchrow_hashref;
88
89                 #       warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
90                         if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
91                                 ModReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
92                                 } else {
93                                 AddReserve($branch,$routinglist[$i]->{'borrowernumber'},$biblio,$const,\@bibitems,$routinglist[$i]->{'ranking'},'',$notes,$title);
94                         }
95         }
96         }
97
98     ($template, $loggedinuser, $cookie)
99 = get_template_and_user({template_name => "serials/routing-preview-slip.tmpl",
100                                 query => $query,
101                                 type => "intranet",
102                                 authnotrequired => 0,
103                                 flagsrequired => {serials => 'routing'},
104                                 debug => 1,
105                                 });
106     $template->param("libraryname"=>$branchname);
107 } else {
108     ($template, $loggedinuser, $cookie)
109 = get_template_and_user({template_name => "serials/routing-preview.tmpl",
110                                 query => $query,
111                                 type => "intranet",
112                                 authnotrequired => 0,
113                                 flagsrequired => {serials => 'routing'},
114                                 debug => 1,
115                                 });
116 }
117
118 my @results;
119 my $data;
120 for(my $i=0;$i<$routing;$i++){
121     $data=GetMember('borrowernumber' => $routinglist[$i]->{'borrowernumber'});
122     $data->{'location'}=$data->{'branchcode'};
123     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
124     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
125     $data->{'subscriptionid'}=$subscriptionid;
126     push(@results, $data);
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($issue),
136     subscriptionid => $subscriptionid,
137     memberloop => \@results,
138     routingnotes => $routingnotes,
139     hasRouting => check_routing($subscriptionid),
140     );
141
142 output_html_with_http_headers $query, $cookie, $template->output;