Clean up before final commits
[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 CGI;
8 use C4::Koha;
9 use C4::Auth;
10 use C4::Date;
11 use C4::Output;
12 use C4::Acquisition;
13 use C4::Reserves2;
14 use C4::Circulation::Circ2;
15 use C4::Interface::CGI::Output;
16 use C4::Context;
17 use C4::Search;
18 use C4::Serials;
19
20 my $query = new CGI;
21 my $subscriptionid = $query->param('subscriptionid');
22 my $issue = $query->param('issue');
23 my $routingid;
24 my $ok = $query->param('ok');
25 my $edit = $query->param('edit');
26 my $delete = $query->param('delete');
27 my $dbh = C4::Context->dbh;
28
29 if($delete){
30     delroutingmember($routingid,$subscriptionid);
31     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
32     $sth->execute($subscriptionid);    
33     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");    
34 }
35
36 if($edit){
37     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
38 }
39     
40 my ($routing, @routinglist) = getroutinglist($subscriptionid);
41 my $subs = GetSubscription($subscriptionid);
42 my ($count,@serials) = GetSerials($subscriptionid);
43 my ($template, $loggedinuser, $cookie);
44
45 if($ok){
46     my $env;
47     # get biblio information....
48     my $biblio = $subs->{'biblionumber'};
49     
50     # get existing reserves .....
51     my ($count,$reserves) = FindReserves($biblio);
52     my $totalcount = $count;
53     foreach my $res (@$reserves) {
54         if ($res->{'found'} eq 'W') {
55             $count--;
56         }
57     }
58     my ($count2,@bibitems) = bibitems($biblio);
59     my @itemresults = ItemInfo($env, $subs->{'biblionumber'}, 'intra');    
60     my $branch = $itemresults[0]->{'holdingbranch'};
61     my $const = 'o';
62     my $notes;
63     my $title = $subs->{'bibliotitle'};
64     for(my $i=0;$i<$routing;$i++){
65         my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ? 
66                                  AND cancellationdate is NULL AND (found <> 'F' or found is NULL)");
67         $sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
68         my $data = $sth->fetchrow_hashref;
69 #       warn Dumper($data);
70 #       warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
71         if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
72             UpdateReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
73         } else {
74             CreateReserve(\$env,$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 => {catalogue => 1},
85                                 debug => 1,
86                                 });    
87 } else {
88     ($template, $loggedinuser, $cookie)
89 = get_template_and_user({template_name => "serials/routing-preview.tmpl",
90                                 query => $query,
91                                 type => "intranet",
92                                 authnotrequired => 0,
93                                 flagsrequired => {catalogue => 1},
94                                 debug => 1,
95                                 });
96 }    
97
98 # my $firstdate = "$serials[0]->{'serialseq'} ($serials[0]->{'planneddate'})";
99 my @results;
100 my $data;
101 for(my $i=0;$i<$routing;$i++){
102     $data=borrdata('',$routinglist[$i]->{'borrowernumber'});
103     $data->{'location'}=$data->{'streetaddress'};
104     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
105     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
106     $data->{'subscriptionid'}=$subscriptionid;
107     push(@results, $data);
108 }
109
110 my $routingnotes = $serials[0]->{'routingnotes'};
111 $routingnotes =~ s/\n/\<br \/\>/g;
112   
113 $template->param(
114     title => $subs->{'bibliotitle'},
115     issue => $issue,
116     subscriptionid => $subscriptionid,
117     memberloop => \@results,    
118     routingnotes => $routingnotes,
119     );
120
121         output_html_with_http_headers $query, $cookie, $template->output;