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