Fix unmerged routing-preview.pl
[koha.git] / serials / routing-preview.pl
1 #!/usr/bin/perl
2
3 # Routing Preview.pl script used to view a routing list after creation
4 # lets one print out routing slip and create (in this instance) the heirarchy
5 # of reserves for the serial
6 use strict;
7 use warnings;
8 use CGI;
9 use C4::Koha;
10 use C4::Auth;
11 use C4::Dates;
12 use C4::Output;
13 use C4::Acquisition;
14 use C4::Reserves;
15 use C4::Circulation;
16 use C4::Context;
17 use C4::Members;
18 use C4::Biblio;
19 use C4::Items;
20 use C4::Serials;
21 use URI::Escape;
22 use C4::Branch;
23
24 my $query = new CGI;
25 my $subscriptionid = $query->param('subscriptionid');
26 my $issue = $query->param('issue');
27 my $routingid;
28 my $ok = $query->param('ok');
29 my $edit = $query->param('edit');
30 my $delete = $query->param('delete');
31 my $dbh = C4::Context->dbh;
32
33 if($delete){
34     delroutingmember($routingid,$subscriptionid);
35     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
36     $sth->execute($subscriptionid);
37     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
38 }
39
40 if($edit){
41     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
42 }
43
44 my ($routing, @routinglist) = getroutinglist($subscriptionid);
45 my $subs = GetSubscription($subscriptionid);
46 my ($count,@serials) = GetSerials($subscriptionid);
47 my ($template, $loggedinuser, $cookie);
48
49 if($ok){
50     # get biblio information....
51     my $biblio = $subs->{'biblionumber'};
52
53     # get existing reserves .....
54     my ($count,$reserves) = GetReservesFromBiblionumber($biblio);
55     my $totalcount = $count;
56     foreach my $res (@$reserves) {
57         if ($res->{'found'} eq 'W') {
58             $count--;
59         }
60     }
61     my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblio);
62     my @itemresults = GetItemsInfo($subs->{'biblionumber'}, 'intra');
63     my $branch = $itemresults[0]->{'holdingbranch'};
64     my $branchname = GetBranchName($branch);
65     my $const = 'o';
66     my $notes;
67     my $title = $subs->{'bibliotitle'};
68     for(my $i=0;$i<$routing;$i++){
69         my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ?");
70         $sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
71         my $data = $sth->fetchrow_hashref;
72
73 #       warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
74         if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
75             ModReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
76         } else {
77         AddReserve($branch,$routinglist[$i]->{'borrowernumber'},$biblio,$const,\@bibitems,$routinglist[$i]->{'ranking'},'',$notes,$title);
78         }
79     }
80
81
82     ($template, $loggedinuser, $cookie)
83 = get_template_and_user({template_name => "serials/routing-preview-slip.tmpl",
84                                 query => $query,
85                                 type => "intranet",
86                                 authnotrequired => 0,
87                                 flagsrequired => {serials => 1},
88                                 debug => 1,
89                                 });
90     $template->param("libraryname"=>$branchname);
91 } else {
92     ($template, $loggedinuser, $cookie)
93 = get_template_and_user({template_name => "serials/routing-preview.tmpl",
94                                 query => $query,
95                                 type => "intranet",
96                                 authnotrequired => 0,
97                                 flagsrequired => {serials => 1},
98                                 debug => 1,
99                                 });
100 }
101
102 my @results;
103 my $data;
104 for(my $i=0;$i<$routing;$i++){
105     $data=GetMember($routinglist[$i]->{'borrowernumber'},'borrowernumber');
106     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
107     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
108     $data->{'subscriptionid'}=$subscriptionid;
109     push(@results, $data);
110 }
111
112 my $routingnotes = $serials[0]->{'routingnotes'};
113 $routingnotes =~ s/\n/\<br \/\>/g;
114
115 $template->param(
116     title => $subs->{'bibliotitle'},
117     issue => $issue,
118     issue_escaped => URI::Escape::uri_escape($issue),
119     subscriptionid => $subscriptionid,
120     memberloop => \@results,
121     routingnotes => $routingnotes,
122     );
123
124         output_html_with_http_headers $query, $cookie, $template->output;