New XML API
[koha.git] / reserve / 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 C4::Acquisition;
36 use CGI;
37 use C4::Date;
38
39 my $input = new CGI;
40 my $dbh=C4::Context->dbh;
41 # get biblio information....
42 my $biblionumber = $input->param('biblionumber');
43 my ($record) = XMLgetbiblio($dbh,$biblionumber);
44 $record=XML_xml2hash_onerecord($record);
45 my $dat=XMLmarc2koha_onerecord($dbh,$record,"biblios");
46 # get existing reserves .....
47 my ($count,$reserves) = FindReserves($biblionumber);
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 my @bibitemloop;
74
75         my %abibitem;
76         my @barcodeloop;
77     my @barcodes = barcodes($biblionumber);
78 #warn @barcodes;
79     foreach my $num (@barcodes) {
80                 my %barcode;
81                 $barcode{'date_due'}=$num->{'date_due'} if $num->{'date_due'} gt "0000-00-00" ;
82                 $barcode{'barcode'}=$num->{'barcode'};
83                 $barcode{'itemnumber'}=$num->{'itemnumber'};
84                 $barcode{'message'}=$num->{'itemlost'} == 1 ? "(lost)" :
85                 $num->{'itemlost'} == 2 ? "(long overdue)" : "";
86                 $barcode{'class'}=$num->{'itemcallnumber'};
87                 $abibitem{'itemlost'}=(($num->{'notforloan'})|| ($num->{'itemlost'} == 1)) ;
88                 $abibitem{'itemlost'}=($num->{'wthdrawn'}) ;
89                 push(@barcodeloop, \%barcode);
90     }
91 #       
92         $abibitem{'class'}="$dat->{'classification'}$dat->{'dewey'}$dat->{'subclass'}";
93         $abibitem{'biblionumber'}=$dat->{'biblionumber'};
94         $abibitem{'description'}=$dat->{'itemtype'};
95         $abibitem{'volumeddesc'}=$dat->{'volumeddesc'};
96         $abibitem{'publicationyear'}=$dat->{'publicationyear'};
97
98         $abibitem{'barcodeloop'}=\@barcodeloop;
99         push(@bibitemloop,\%abibitem);
100
101
102
103
104 #existingreserves building
105 my @reserveloop;
106 my $branches = GetBranches();
107
108 foreach my $res (sort {$a->{'found'} cmp $b->{'found'}} @$reserves){
109         my %reserve;
110 if ($res->{'priority'}>$totalcount){$totalcount=$res->{'priority'};}
111 #    my $prioropt = priorityoptions($totalcount, $res->{'priority'});
112         my @optionloop;
113         for (my $i=1; $i<=$totalcount; $i++){
114                 my %option;
115                 $option{num}=$i;
116                 $option{selected}=($i==$res->{'priority'});
117                 push(@optionloop, \%option);
118         }
119         my @branchloop;
120
121         foreach my $br (keys %$branches) {
122
123
124
125                 (next) unless $branches->{$br}->{'IS'};
126
127                                 # Only branches with the 'IS' branchrelation
128                                 # can issue books
129                 my %abranch;
130                 $abranch{'selected'}=($br eq $res->{'branchcode'});
131                 $abranch{'branch'}=$br;
132                 $abranch{'branchname'}=$branches->{$br}->{'branchname'};
133                 push(@branchloop,\%abranch);
134         }
135 my %env;
136 #               my $item = $res->{'itemnumber'};
137 #               $item = getiteminformation(\%env,$item);
138
139     if ($res->{'found'} eq 'W') {
140                 
141                 $reserve{'holdingbranch'}=$res->{'holdingbranch'};
142                 $reserve{'barcode'}=$res->{'barcode'};
143                 $reserve{'biblionumber'}=$res->{'biblionumber'};
144                 $reserve{'wbrcode'} = $res->{'branchcode'};
145                 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
146                 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
147                         $reserve{'atdestination'} = 1;
148                 }
149     }
150         $reserve{'barcode'}=$res->{'barcode'};
151         $reserve{'date'} = format_date($res->{'reservedate'});
152         $reserve{'borrowernumber'}=$res->{'borrowernumber'};
153         $reserve{'biblionumber'}=$res->{'biblionumber'};
154         $reserve{'bornum'}=$res->{'borrowernumber'};
155         $reserve{'firstname'}=$res->{'firstname'};
156         $reserve{'surname'}=$res->{'surname'};
157         $reserve{'bornum'}=$res->{'borrowernumber'};
158         $reserve{'notes'}=$res->{'reservenotes'};
159         $reserve{'wait'}=($res->{'found'} eq 'W');
160         $reserve{'constrainttypea'}=($res->{'constrainttype'} eq 'a');
161         $reserve{'constrainttypeo'}=($res->{'constrainttype'} eq 'o');
162         $reserve{'voldesc'}=$res->{'volumeddesc'};
163         $reserve{'itemtype'}=$res->{'itemtype'};
164         $reserve{'branchloop'}=\@branchloop;
165         $reserve{'optionloop'}=\@optionloop;
166         push(@reserveloop,\%reserve);
167 }
168
169 my @branches;
170 my @select_branch;
171 my %select_branches;
172 my $branches=GetBranches;
173 #my $count2=scalar(@branches);
174 foreach my $br (keys %$branches) {
175
176         push @select_branch, $br;
177         $select_branches{$br}= $branches->{$br}->{'branchname'};
178 }
179 my $CGIbranch=CGI::scrolling_list( -name     => 'pickup',
180                         -values   => \@select_branch,
181                         -labels   => \%select_branches,
182                         -size     => 1,
183                         -multiple => 0 );
184
185 #get the time for the form name...
186 my $time = time();
187
188 #setup colours
189 my ($template, $borrowernumber, $cookie)
190     = get_template_and_user({template_name => "reserve/request.tmpl",
191                                                         query => $input,
192                             type => "intranet",
193                             authnotrequired => 0,
194                             flagsrequired => {borrowers => 1},
195                          });
196 $template->param(       optionloop =>\@optionloop,
197                                                                 CGIbranch => $CGIbranch,
198                                                                 reserveloop => \@reserveloop,
199                                                                 'time' => $time,
200                                                                 bibitemloop => \@bibitemloop,
201                                                                 date => $date,
202                                                                 biblionumber => $biblionumber,
203                                                                 title =>$dat->{title});
204 # printout the page
205 output_html_with_http_headers $input, $cookie, $template->output;