Clean up before final commits
[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::Acquisition;
13 use C4::Interface::CGI::Output;
14 use C4::Context;
15 use C4::Search;
16 use C4::Serials;
17
18 my $query = new CGI;
19 my $subscriptionid = $query->param('subscriptionid');
20 my $serialseq = $query->param('serialseq');
21 my $routingid = $query->param('routingid');
22 my $bornum = $query->param('bornum');
23 my $notes = $query->param('notes');
24 my $op = $query->param('op');
25 my $date_selected = $query->param('date_selected');
26 my $dbh = C4::Context->dbh;
27
28 if($op eq 'delete'){
29     delroutingmember($routingid,$subscriptionid);
30 }
31
32 if($op eq 'add'){
33     addroutingmember($bornum,$subscriptionid);
34 }
35 if($op eq 'save'){
36     my $sth = $dbh->prepare("UPDATE serial SET routingnotes = ? WHERE subscriptionid = ?");
37     $sth->execute($notes,$subscriptionid);
38     print $query->redirect("routing-preview.pl?subscriptionid=$subscriptionid&issue=$date_selected");
39 }
40     
41 my ($routing, @routinglist) = getroutinglist($subscriptionid);
42 my $subs = GetSubscription($subscriptionid);
43 my ($count,@serials) = old_getserials($subscriptionid);
44 my ($serialdates) = GetLatestSerials($subscriptionid,$count);
45
46 my @dates;
47 my $i=0;
48 foreach my $dateseq (@$serialdates) {
49         $dates[$i]->{'planneddate'} = $dateseq->{'planneddate'};
50         $dates[$i]->{'serialseq'} = $dateseq->{'serialseq'};
51         $dates[$i]->{'serialid'} = $dateseq->{'serialid'};
52         if($date_selected eq $dateseq->{'serialid'}){
53             $dates[$i]->{'selected'} = ' selected';
54         } else {
55             $dates[$i]->{'selected'} = '';
56         }
57         $i++;
58 }
59
60 my ($template, $loggedinuser, $cookie)
61 = get_template_and_user({template_name => "serials/routing.tmpl",
62                                 query => $query,
63                                 type => "intranet",
64                                 authnotrequired => 0,
65                                 flagsrequired => {catalogue => 1},
66                                 debug => 1,
67                                 });
68 # my $date;
69 # if($serialseq){
70 #    for(my $i = 0;$i<@serials; $i++){
71 #       if($serials[$i]->{'serialseq'} eq $serialseq){
72 #           $date = $serials[$i]->{'planneddate'}
73 #       }
74 #    }
75 # } else {
76 #    $serialseq = $serials[0]->{'serialseq'};
77 #    $date = $serials[0]->{'planneddate'};
78 # }
79
80 # my $issue = "$serialseq ($date)";
81   
82 my @results;
83 my $data;
84 for(my $i=0;$i<$routing;$i++){
85     $data=borrdata('',$routinglist[$i]->{'borrowernumber'});
86     $data->{'location'}=$data->{'streetaddress'};
87     $data->{'name'}="$data->{'firstname'} $data->{'surname'}";
88     $data->{'routingid'}=$routinglist[$i]->{'routingid'};
89     $data->{'subscriptionid'}=$subscriptionid;
90     my $rankingbox = '<select name="itemrank" onchange="reorder_item('.$subscriptionid.','.$routinglist[$i]->{'routingid'}.',this.options[this.selectedIndex].value)">';
91     for(my $j=1; $j <= $routing; $j++) {
92         $rankingbox .= "<option ";
93         if($routinglist[$i]->{'ranking'} == $j){
94             $rankingbox .= " selected='SELECTED'";
95         }
96         $rankingbox .= " value='$j'>$j</option>";
97     }
98     $rankingbox .= "</select>";
99     $data->{'routingbox'} = $rankingbox;
100     
101     push(@results, $data);
102 }
103 # warn Dumper(@results);
104 # for adding routing list
105 my $new;
106 if ($op eq 'new') {
107     $new = 1;
108 } else {
109 # for modify routing list default
110     $new => 0;
111 }
112
113 $template->param(
114     title => $subs->{'bibliotitle'},
115     subscriptionid => $subscriptionid,
116     memberloop => \@results,    
117     op => $new,
118     dates => \@dates,
119     routingnotes => $serials[0]->{'routingnotes'},
120     );
121
122         output_html_with_http_headers $query, $cookie, $template->output;