rel_3_0 moved to HEAD
[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::Members;
18 use C4::Biblio;
19 use C4::Serials;
20
21 my $query = new CGI;
22 my $subscriptionid = $query->param('subscriptionid');
23 my $issue = $query->param('issue');
24 my $routingid;
25 my $ok = $query->param('ok');
26 my $edit = $query->param('edit');
27 my $delete = $query->param('delete');
28 my $dbh = C4::Context->dbh;
29
30 if($delete){
31     delroutingmember($routingid,$subscriptionid);
32     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
33     $sth->execute($subscriptionid);    
34     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");    
35 }
36
37 if($edit){
38     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
39 }
40     
41 my ($routing, @routinglist) = getroutinglist($subscriptionid);
42 my $subs = GetSubscription($subscriptionid);
43 my ($count,@serials) = GetSerials($subscriptionid);
44 my ($template, $loggedinuser, $cookie);
45
46 if($ok){
47     my $env;
48     # get biblio information....
49     my $biblio = $subs->{'biblionumber'};
50     
51     # get existing reserves .....
52     my ($count,$reserves) = FindReserves($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                                  AND cancellationdate is NULL AND (found <> 'F' or found is NULL)");
68         $sth->execute($biblio,$routinglist[$i]->{'borrowernumber'});
69         my $data = $sth->fetchrow_hashref;
70
71 #       warn "$routinglist[$i]->{'borrowernumber'} is the same as $data->{'borrowernumber'}";
72         if($routinglist[$i]->{'borrowernumber'} == $data->{'borrowernumber'}){
73             UpdateReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
74         } else {
75         CreateReserve(\$env,$branch,$routinglist[$i]->{'borrowernumber'},$biblio,$const,\@bibitems,$routinglist[$i]->{'ranking'},$notes,$title);
76         }
77     }
78     
79     
80     ($template, $loggedinuser, $cookie)
81 = get_template_and_user({template_name => "serials/routing-preview-slip.tmpl",
82                                 query => $query,
83                                 type => "intranet",
84                                 authnotrequired => 0,
85                                 flagsrequired => {serials => 1},
86                                 debug => 1,
87                                 });
88     $template->param("libraryname"=>C4::Context->preference("LibraryName"));
89 } else {
90     ($template, $loggedinuser, $cookie)
91 = get_template_and_user({template_name => "serials/routing-preview.tmpl",
92                                 query => $query,
93                                 type => "intranet",
94                                 authnotrequired => 0,
95                                 flagsrequired => {serials => 1},
96                                 debug => 1,
97                                 });
98 }    
99
100 # my $firstdate = "$serials[0]->{'serialseq'} ($serials[0]->{'planneddate'})";
101 my @results;
102 my $data;
103 for(my $i=0;$i<$routing;$i++){
104     $data=borrdata('',$routinglist[$i]->{'borrowernumber'});
105     $data->{'location'}=$data->{'streetaddress'};
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     subscriptionid => $subscriptionid,
119     memberloop => \@results,    
120     routingnotes => $routingnotes,
121     );
122
123         output_html_with_http_headers $query, $cookie, $template->output;