Merge commit 'pianohacker-koha/prefs-submit' into master
[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         my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblio);
53         my @itemresults = GetItemsInfo($subs->{'biblionumber'}, 'intra');
54         my $branch = $itemresults[0]->{'holdingbranch'};
55         my $branchname = GetBranchName($branch);
56
57         if (C4::Context->preference('RoutingListAddReserves')){
58                 # get existing reserves .....
59                 my ($count,$reserves) = GetReservesFromBiblionumber($biblio);
60                 my $totalcount = $count;
61                 foreach my $res (@$reserves) {
62                         if ($res->{'found'} eq 'W') {
63                                 $count--;
64                         }
65                 }
66                 my $const = 'o';
67                 my $notes;
68                 my $title = $subs->{'bibliotitle'};
69                 for(my $i=0;$i<$routing;$i++){
70                         my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ?");
71                                 $sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
72                                 my $data = $sth->fetchrow_hashref;
73
74                 #       warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
75                         if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
76                                 ModReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
77                                 } else {
78                                 AddReserve($branch,$routinglist[$i]->{'borrowernumber'},$biblio,$const,\@bibitems,$routinglist[$i]->{'ranking'},'',$notes,$title);
79                         }
80         }
81         }
82
83     ($template, $loggedinuser, $cookie)
84 = get_template_and_user({template_name => "serials/routing-preview-slip.tmpl",
85                                 query => $query,
86                                 type => "intranet",
87                                 authnotrequired => 0,
88                                 flagsrequired => {serials => 1},
89                                 debug => 1,
90                                 });
91     $template->param("libraryname"=>$branchname);
92 } else {
93     ($template, $loggedinuser, $cookie)
94 = get_template_and_user({template_name => "serials/routing-preview.tmpl",
95                                 query => $query,
96                                 type => "intranet",
97                                 authnotrequired => 0,
98                                 flagsrequired => {serials => 1},
99                                 debug => 1,
100                                 });
101 }
102
103 my @results;
104 my $data;
105 for(my $i=0;$i<$routing;$i++){
106     $data=GetMember('borrowernumber' => $routinglist[$i]->{'borrowernumber'});
107     $data->{'location'}=$data->{'branchcode'};
108     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
109     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
110     $data->{'subscriptionid'}=$subscriptionid;
111     push(@results, $data);
112 }
113
114 my $routingnotes = $serials[0]->{'routingnotes'};
115 $routingnotes =~ s/\n/\<br \/\>/g;
116
117 $template->param(
118     title => $subs->{'bibliotitle'},
119     issue => $issue,
120     issue_escaped => URI::Escape::uri_escape($issue),
121     subscriptionid => $subscriptionid,
122     memberloop => \@results,
123     routingnotes => $routingnotes,
124     );
125
126 output_html_with_http_headers $query, $cookie, $template->output;