Modifying Members : Add Mod and GetMember
[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
23 my $query = new CGI;
24 my $subscriptionid = $query->param('subscriptionid');
25 my $issue = $query->param('issue');
26 my $routingid;
27 my $ok = $query->param('ok');
28 my $edit = $query->param('edit');
29 my $delete = $query->param('delete');
30 my $dbh = C4::Context->dbh;
31
32 if($delete){
33     delroutingmember($routingid,$subscriptionid);
34     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = NULL WHERE subscriptionid = ?");
35     $sth->execute($subscriptionid);
36     print $query->redirect("routing.pl?subscriptionid=$subscriptionid&op=new");
37 }
38
39 if($edit){
40     print $query->redirect("routing.pl?subscriptionid=$subscriptionid");
41 }
42
43 my ($routing, @routinglist) = getroutinglist($subscriptionid);
44 my $subs = GetSubscription($subscriptionid);
45 my ($count,@serials) = GetSerials($subscriptionid);
46 my ($template, $loggedinuser, $cookie);
47
48 if($ok){
49     # get biblio information....
50     my $biblio = $subs->{'biblionumber'};
51
52     # get existing reserves .....
53     my ($count,$reserves) = GetReservesFromBiblionumber($biblio);
54     my $totalcount = $count;
55     foreach my $res (@$reserves) {
56         if ($res->{'found'} eq 'W') {
57             $count--;
58         }
59     }
60     my ($count2,@bibitems) = GetBiblioItemByBiblioNumber($biblio);
61     my @itemresults = GetItemsInfo($subs->{'biblionumber'}, 'intra');
62     my $branch = $itemresults[0]->{'holdingbranch'};
63     my $const = 'o';
64     my $notes;
65     my $title = $subs->{'bibliotitle'};
66     for(my $i=0;$i<$routing;$i++){
67         my $sth = $dbh->prepare("SELECT * FROM reserves WHERE biblionumber = ? AND borrowernumber = ?");
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             ModReserve($routinglist[$i]->{'ranking'},$biblio,$routinglist[$i]->{'borrowernumber'},$branch);
74         } else {
75         AddReserve($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 @results;
101 my $data;
102 for(my $i=0;$i<$routing;$i++){
103     $data=GetMember('borrowernumber' => $routinglist[$i]->{'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;