Switching from building 'edit' link in the script to building it in the template...
[koha.git] / request.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 #script to place reserves/requests
6 #writen 2/1/00 by chris@katipo.oc.nz
7
8
9 # Copyright 2000-2002 Katipo Communications
10 #
11 # This file is part of Koha.
12 #
13 # Koha is free software; you can redistribute it and/or modify it under the
14 # terms of the GNU General Public License as published by the Free Software
15 # Foundation; either version 2 of the License, or (at your option) any later
16 # version.
17 #
18 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License along with
23 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
24 # Suite 330, Boston, MA  02111-1307 USA
25
26 use strict;
27 use C4::Search;
28 use C4::Output;
29 use C4::Interface::CGI::Output;
30 use C4::Auth;
31 use C4::Reserves2;
32 use C4::Biblio;
33 use C4::Koha;
34 use C4::Circulation::Circ2;
35 use HTML::Template;
36 use C4::Acquisition;
37 use CGI;
38 use C4::Date;
39
40 my $input = new CGI;
41
42 # get biblio information....
43 my $bib = $input->param('bib');
44 my $dat = bibdata($bib);
45
46 # get existing reserves .....
47 my ($count,$reserves) = FindReserves($bib);
48 my $totalcount = $count;
49 foreach my $res (@$reserves) {
50     if ($res->{'found'} eq 'W') {
51         $count--;
52     }
53 }
54
55 # make priorities options
56 my $num = $count + 1;
57
58 #priorityoptions building
59 my @optionloop;
60 for (my $i=1; $i<=$num; $i++){
61         my %option;
62         $option{num}=$i;
63         $option{selected}=($i==$num);
64         push(@optionloop, \%option);
65 }
66
67 # todays date
68 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =localtime(time);
69 $year=$year+1900;
70 $mon++;
71 my $date=format_date("$year-$mon-$mday");
72
73
74 # get biblioitem information and build rows for form
75 my ($count2,@data) = bibitems($bib);
76
77 my @bibitemloop;
78 foreach my $dat (sort {$b->{'dateaccessioned'} cmp $a->{'dateaccessioned'}} @data) {
79     $dat->{'dewey'}="" if ($dat->{'dewey'} == 0);
80     $dat->{'volumeddesc'} = "&nbsp;" unless $dat->{'volumeddesc'};
81     $dat->{'dewey'}=~ s/\.0000$//;
82     $dat->{'dewey'}=~ s/00$//;
83
84         my %abibitem;
85         my @barcodeloop;
86     my @barcodes = barcodes($dat->{'biblioitemnumber'});
87     foreach my $num (@barcodes) {
88                 my %barcode;
89                 $barcode{'barcode'}=$num->{'barcode'};
90                 $barcode{'message'}=$num->{'itemlost'} == 1 ? "(lost)" :
91             $num->{'itemlost'} == 2 ? "(long overdue)" : "";
92                 push(@barcodeloop, \%barcode);
93     }
94         $abibitem{'barcodeloop'}=\@barcodeloop;
95     $abibitem{'class'}="$dat->{'classification'}$dat->{'dewey'}$dat->{'subclass'}";
96     my $select;
97     $abibitem{'itemlost'}=(($dat->{'notforloan'})|| ($dat->{'itemlost'} == 1)) ;
98         $abibitem{'biblioitemnumber'}=$dat->{'biblioitemnumber'};
99         $abibitem{'description'}=$dat->{'description'};
100         $abibitem{'volumeddesc'}=$dat->{'volumeddesc'};
101         $abibitem{'publicationyear'}=$dat->{'publicationyear'};
102         push(@bibitemloop,\%abibitem);
103 }
104
105
106
107 #existingreserves building
108 my @reserveloop;
109 my $branches = getbranches();
110 foreach my $res (sort {$a->{'found'} cmp $b->{'found'}} @$reserves){
111         my %reserve;
112 #    my $prioropt = priorityoptions($totalcount, $res->{'priority'});
113         my @optionloop;
114         for (my $i=1; $i<=$totalcount; $i++){
115                 my %option;
116                 $option{num}=$i;
117                 $option{selected}=($i==$res->{'priority'});
118                 push(@optionloop, \%option);
119         }
120         my @branchloop;
121         foreach my $br (keys %$branches) {
122 #               (next) unless $branches->{$br}->{'IS'};
123                                 # Only branches with the 'IS' branchrelation
124                                 # can issue books
125                 my %abranch;
126                 $abranch{'selected'}=($br eq $res->{'branchcode'});
127                 $abranch{'branch'}=$br;
128                 $abranch{'branchname'}=$branches->{$br}->{'branchname'};
129                 push(@branchloop,\%abranch);
130         }
131
132     if ($res->{'found'} eq 'W') {
133                 my %env;
134                 my $item = $res->{'itemnumber'};
135                 $item = getiteminformation(\%env,$item);
136                 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
137                 $reserve{'barcode'}=$item->{'barcode'};
138                 $reserve{'biblionumber'}=$item->{'biblionumber'};
139                 $reserve{'wbrcode'} = $res->{'branchcode'};
140                 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
141                 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
142                         $reserve{'atdestination'} = 1;
143                 }
144     }
145     $reserve{'date'} = format_date($res->{'reservedate'});
146         $reserve{'borrowernumber'}=$res->{'borrowernumber'};
147         $reserve{'biblionumber'}=$res->{'biblionumber'};
148         $reserve{'bornum'}=$res->{'borrowernumber'};
149         $reserve{'firstname'}=$res->{'firstname'};
150         $reserve{'surname'}=$res->{'surname'};
151         $reserve{'bornum'}=$res->{'borrowernumber'};
152         $reserve{'notes'}=$res->{'reservenotes'};
153         $reserve{'wait'}=($res->{'found'} eq 'W');
154         $reserve{'constrainttypea'}=($res->{'constrainttype'} eq 'a');
155         $reserve{'constrainttypeo'}=($res->{'constrainttype'} eq 'o');
156         $reserve{'voldesc'}=$res->{'volumeddesc'};
157         $reserve{'itemtype'}=$res->{'itemtype'};
158         $reserve{'branchloop'}=\@branchloop;
159         $reserve{'optionloop'}=\@optionloop;
160         push(@reserveloop,\%reserve);
161 }
162
163 my @branches;
164 my @select_branch;
165 my %select_branches;
166 my ($count2,@branches)=branches();
167 for (my $i=0;$i<$count2;$i++){
168         push @select_branch, $branches[$i]->{'branchcode'};#
169         $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
170 }
171 my $CGIbranch=CGI::scrolling_list( -name     => 'pickup',
172                         -values   => \@select_branch,
173                         -labels   => \%select_branches,
174                         -size     => 1,
175                         -multiple => 0 );
176
177 #get the time for the form name...
178 my $time = time();
179
180 #setup colours
181 my ($template, $borrowernumber, $cookie)
182     = get_template_and_user({template_name => "request.tmpl",
183                                                         query => $input,
184                             type => "intranet",
185                             authnotrequired => 0,
186                             flagsrequired => {parameters => 1},
187                          });
188 $template->param(       optionloop =>\@optionloop,
189                                                                 CGIbranch => $CGIbranch,
190                                                                 reserveloop => \@reserveloop,
191                                                                 'time' => $time,
192                                                                 bibitemloop => \@bibitemloop,
193                                                                 date => $date,
194                                                                 bib => $bib,
195                                                                 title =>$dat->{title});
196 # printout the page
197 print $input->header(
198         -type => C4::Interface::CGI::Output::guesstype($template->output),
199         -expires=>'now'
200 ), $template->output;