Merging in katipo changes for serials
[koha.git] / serials / routing.pl
1 #!/usr/bin/perl
2
3 # Routing.pl script used to create a routing list for a serial subscription
4 # In this instance it is in fact a setting up of a list of reserves for the item
5 # where the hierarchical order can be changed on the fly and a routing list can be
6 # printed out
7 use strict;
8 use CGI;
9 use C4::Koha;
10 use C4::Auth;
11 use C4::Date;
12 use C4::Output;
13 use C4::Acquisition;
14 use C4::Interface::CGI::Output;
15 use C4::Context;
16 use HTML::Template;
17 use C4::Search;
18 use C4::Serials;
19
20 my $query = new CGI;
21 my $subscriptionid = $query->param('subscriptionid');
22 my $serialseq = $query->param('serialseq');
23 my $routingid = $query->param('routingid');
24 my $bornum = $query->param('bornum');
25 my $notes = $query->param('notes');
26 my $op = $query->param('op');
27 my $date_selected = $query->param('date_selected');
28 my $dbh = C4::Context->dbh;
29
30 if($op eq 'delete'){
31     delroutingmember($routingid,$subscriptionid);
32 }
33
34 if($op eq 'add'){
35     addroutingmember($bornum,$subscriptionid);
36 }
37 if($op eq 'save'){
38     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?");
39     $sth->execute($notes,$subscriptionid);
40     print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$date_selected");
41 }
42     
43 my ($routing, @routinglist) = getroutinglist($subscriptionid);
44 my $subs = GetSubscription($subscriptionid);
45 my ($count,@serials) = old_getserials($subscriptionid);
46 my ($serialdates) = GetLatestSerials($subscriptionid,$count);
47
48 my @dates;
49 my $i=0;
50 foreach my $dateseq (@$serialdates) {
51         $dates[$i]->{'planneddate'} = $dateseq->{'planneddate'};
52         $dates[$i]->{'serialseq'} = $dateseq->{'serialseq'};
53         $dates[$i]->{'serialid'} = $dateseq->{'serialid'};
54         if($date_selected eq $dateseq->{'serialid'}){
55             $dates[$i]->{'selected'} = ' selected';
56         } else {
57             $dates[$i]->{'selected'} = '';
58         }
59         $i++;
60 }
61
62 my ($template, $loggedinuser, $cookie)
63 = get_template_and_user({template_name => "serials/routing.tmpl",
64                                 query => $query,
65                                 type => "intranet",
66                                 authnotrequired => 0,
67                                 flagsrequired => {catalogue => 1},
68                                 debug => 1,
69                                 });
70 # my $date;
71 # if($serialseq){
72 #    for(my $i = 0;$i<@serials; $i++){
73 #       if($serials[$i]->{'serialseq'} eq $serialseq){
74 #           $date = $serials[$i]->{'planneddate'}
75 #       }
76 #    }
77 # } else {
78 #    $serialseq = $serials[0]->{'serialseq'};
79 #    $date = $serials[0]->{'planneddate'};
80 # }
81
82 # my $issue = "$serialseq ($date)";
83   
84 my @results;
85 my $data;
86 for(my $i=0;$i<$routing;$i++){
87     $data=borrdata('',$routinglist[$i]->{'borrowernumber'});
88     $data->{'location'}=$data->{'streetaddress'};
89     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
90     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
91     $data->{'subscriptionid'}=$subscriptionid;
92     my $rankingbox = '<select name="itemrank" onchange="reorder_item('.$subscriptionid.','.$routinglist[$i]->{'routingid'}.',this.options[this.selectedIndex].value)">';
93     for(my $j=1; $j <= $routing; $j++) {
94         $rankingbox .= "<option ";
95         if($routinglist[$i]->{'ranking'} == $j){
96             $rankingbox .= " selected='SELECTED'";
97         }
98         $rankingbox .= " value='$j'>$j</option>";
99     }
100     $rankingbox .= "</select>";
101     $data->{'routingbox'} = $rankingbox;
102     
103     push(@results, $data);
104 }
105 # warn Dumper(@results);
106 # for adding routing list
107 my $new;
108 if ($op eq 'new') {
109     $new = 1;
110 } else {
111 # for modify routing list default
112     $new => 0;
113 }
114
115 $template->param(
116     title => $subs->{'bibliotitle'},
117     subscriptionid => $subscriptionid,
118     memberloop => \@results,    
119     op => $new,
120     dates => \@dates,
121     routingnotes => $serials[0]->{'routingnotes'},
122     );
123
124         output_html_with_http_headers $query, $cookie, $template->output;