5 #script to place reserves/requests
6 #writen 2/1/00 by chris@katipo.oc.nz
9 # Copyright 2000-2002 Katipo Communications
11 # This file is part of Koha.
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
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.
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
30 use List::MoreUtils qw/uniq/;
32 use Date::Calc qw/Today/;
36 use C4::Interface::CGI::Output;
41 use C4::Circulation::Circ2;
42 # FIXME Is there utility to use Acquisition.pm ????
43 # use C4::Acquisition;
47 my $dbh = C4::Context->dbh;
50 my ($template, $borrowernumber, $cookie)
51 = get_template_and_user({
52 template_name => "reserve/request.tmpl",
56 flagsrequired => {reserveforothers => 1},
59 # get biblio information....
60 my $bib = $input->param('bib');
61 my $dat = bibdata($bib);
63 # Select borrowers infos
64 my $findborrower = $input->param('findborrower');
65 $findborrower =~ s|,| |g;
66 my $cardnumber = $input->param('cardnumber');
72 my $date= sprintf('%04d-%02d-%02d', Today());
75 my ($count,$borrowers) = BornameSearch(\%env, $findborrower, 'cardnumber', 'web');
77 my @borrowers = @$borrowers;
79 if ($#borrowers == -1) {
80 $input->param('findborrower', '');
81 $messageborrower = "'$findborrower'";
83 elsif ($#borrowers == 0) {
84 $input->param('cardnumber', $borrowers[0]->{'cardnumber'});
85 $cardnumber = $borrowers[0]->{'cardnumber'};
88 $borrowerslist = \@borrowers;
93 my $borrowerinfo = getpatroninformation (\%env,0,$cardnumber);
100 # we check the reserves of the borrower, and if he can reserv a document
101 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
103 my $number_reserves =
104 GetNumberReservesFromBorrower($borrowerinfo->{'borrowernumber'});
106 if ($number_reserves > C4::Context->preference('maxreserves')) {
110 # we check the date expiricy of the borrower
111 my $warning=Date_Cmp(ParseDate("today"),format_date($borrowerinfo->{'dateexpiry'}));
116 # check if the borrower make the reserv in a different branch
117 if ($borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'}) {
122 borrowersurname => $borrowerinfo->{'surname'},
123 borrowerfirstname => $borrowerinfo->{'firstname'},
124 borrowerreservs => $count_reserv,
125 maxreserves => $maxreserves,
127 diffbranch => $diffbranch
132 messageborrower => $messageborrower
135 my $CGIselectborrower;
136 if ($borrowerslist) {
140 foreach my $borrower (
141 sort {$a->{surname}.$a->{firstname} cmp $b->{surname}.$b->{firstname}}
144 push @values, $borrower->{cardnumber};
146 $labels{ $borrower->{cardnumber} } = sprintf(
147 '%s, %s ... (%s - %s) ... %s',
148 $borrower->{surname},
149 $borrower->{firstname},
150 $borrower->{cardnumber},
151 $borrower->{categorycode},
152 $borrower->{streetaddress},
156 $CGIselectborrower = CGI::scrolling_list(
157 -name => 'cardnumber',
165 # get existing reserves .....
166 my ($count, $reserves) = FindReserves($bib, undef);
167 my $totalcount = $count;
170 # FIXME launch another time getpatroninformation perhaps until
171 my $borrowerinfo = getpatroninformation (\%env,0,$cardnumber);
173 foreach my $res (@$reserves) {
174 if (($res->{found} eq 'W') or ($res->{priority} == 0)) {
178 if ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) {
179 $alreadyreserved = 1;
182 $template->param(alreadyreserved => $alreadyreserved);
184 # make priorities options
186 for (1 .. $count + 1) {
191 selected => ($_ == $count + 1),
197 my %itemnumbers_of_biblioitem;
198 my @itemnumbers = @{get_itemnumbers_of($bib)->{$bib}};
199 my $iteminfos_of = get_iteminfos_of(@itemnumbers);
201 foreach my $itemnumber (@itemnumbers) {
204 $iteminfos_of->{$itemnumber}->{homebranch},
205 $iteminfos_of->{$itemnumber}->{holdingbranch}
208 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
210 @{ $itemnumbers_of_biblioitem{$biblioitemnumber} },
215 @branchcodes = uniq @branchcodes;
217 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
219 my $branchinfos_of = get_branchinfos_of(@branchcodes);
220 my $notforloan_label_of = get_notforloan_label_of();
221 my $biblioiteminfos_of = get_biblioiteminfos_of(@biblioitemnumbers);
224 foreach my $biblioitemnumber (@biblioitemnumbers) {
225 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
228 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
230 my $return_date_of = get_current_return_date_of(@itemnumbers);
234 foreach my $biblioitemnumber (@biblioitemnumbers) {
235 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
237 $biblioitem->{description} =
238 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
240 foreach my $itemnumber (@{$itemnumbers_of_biblioitem{$biblioitemnumber}}) {
241 my $item = $iteminfos_of->{$itemnumber};
243 $item->{homebranchname} =
244 $branchinfos_of->{ $item->{homebranch} }{branchname};
246 # if the holdingbranch is different than the homebranch, we show the
247 # holdingbranch of the document too
248 if ($item->{homebranch} ne $item->{holdingbranch}) {
249 $item->{holdingbranchname} =
250 $branchinfos_of->{ $item->{holdingbranch} }{branchname};
253 # if the item is currently on loan, we display its return date and
254 # change the background color
257 if (defined $return_date_of->{$itemnumber}) {
258 $date_due = format_date($return_date_of->{$itemnumber});
259 $item->{date_due} = $date_due;
260 $item->{backgroundcolor} = 'onloan';
264 my $reservedate = GetFirstReserveDateFromItem($itemnumber);
266 if (defined $reservedate) {
267 $item->{backgroundcolor} = 'reserved';
268 $item->{reservedate} = format_date($reservedate);
271 # Management of the notforloan document
272 if ($item->{notforloan}) {
273 $item->{backgroundcolor} = 'other';
274 $item->{notforloanvalue} =
275 $notforloan_label_of->{ $item->{notforloan} };
278 # Management of lost or long overdue items
279 if ($item->{itemlost}) {
281 # FIXME localized strings should never be in Perl code
282 $item->{message} = $item->{itemlost} == 1
284 : $item->{itemlost} == 2
287 $item->{backgroundcolor} = 'other';
290 # Check of the transfered documents
291 my ($transfertwhen,$transfertfrom,$transfertto) =
292 get_transfert_infos($itemnumber);
294 if ($transfertwhen ne '') {
295 $item->{transfertwhen} = format_date($transfertwhen);
296 $item->{transfertfrom} =
297 $branchinfos_of->{$transfertfrom}{branchname};
298 $item->{transfertto} =
299 $branchinfos_of->{$transfertto}{branchname};
302 # If there is no loan, return and transfer, we show a checkbox.
303 $item->{notforloan} = $item->{notforloan} || 0;
305 # An item is available only if:
306 if (not defined $reservedate # not reserved yet
307 and $date_due eq '' # not currently on loan
308 and not $item->{itemlost} # not lost
309 and not $item->{notforloan} # not forbidden to loan
310 and $transfertwhen eq '' # not currently on transfert
312 $item->{available} = 1;
315 push @{$biblioitem->{itemloop}}, $item;
318 push @bibitemloop, $biblioitem;
322 # existingreserves building
324 my $branches = getbranches('RE');
325 foreach my $res (sort {$a->{found} cmp $b->{found}} @$reserves){
328 for (my $i=1; $i <= $totalcount; $i++) {
333 selected => ($i == $res->{priority}),
338 foreach my $br (keys %$branches) {
340 $abranch{'selected'}=($br eq $res->{'branchcode'});
341 $abranch{'branch'}=$br;
342 $abranch{'branchname'}=$branches->{$br}->{'branchname'};
343 push(@branchloop,\%abranch);
346 if (($res->{'found'} eq 'W') or ($res->{'priority'} eq '0')) {
348 my $item = $res->{'itemnumber'};
349 $item = getiteminformation(\%env,$item);
350 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
351 $reserve{'barcode'}=$item->{'barcode'};
352 $reserve{'biblionumber'}=$item->{'biblionumber'};
353 $reserve{'wbrcode'} = $res->{'branchcode'};
354 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
355 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
356 $reserve{'atdestination'} = 1;
360 $reserve{'date'} = format_date($res->{'reservedate'});
361 $reserve{'borrowernumber'}=$res->{'borrowernumber'};
362 $reserve{'biblionumber'}=$res->{'biblionumber'};
363 $reserve{'bornum'}=$res->{'borrowernumber'};
364 $reserve{'firstname'}=$res->{'firstname'};
365 $reserve{'surname'}=$res->{'surname'};
366 $reserve{'bornum'}=$res->{'borrowernumber'};
367 $reserve{'notes'}=$res->{'reservenotes'};
368 $reserve{'wait'}=(($res->{'found'} eq 'W') or ($res->{'priority'} eq '0'));
369 $reserve{'constrainttypea'}=($res->{'constrainttype'} eq 'a');
370 $reserve{'constrainttypeo'}=($res->{'constrainttype'} eq 'o');
371 $reserve{'voldesc'}=$res->{'volumeddesc'};
372 $reserve{'itemtype'}=$res->{'itemtype'};
373 $reserve{'branchloop'}=\@branchloop;
374 $reserve{'optionloop'}=\@optionloop;
376 push(@reserveloop,\%reserve);
379 my $default = C4::Context->userenv->{branch};
383 foreach my $branchcode (keys %{$branches}) {
384 push @values, $branchcode;
385 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
387 my $CGIbranch = CGI::scrolling_list(
390 -default => $default,
391 -labels => \%label_of,
396 # get the time for the form name...
400 CGIbranch => $CGIbranch,
401 reserveloop => \@reserveloop,
407 optionloop =>\@optionloop,
408 bibitemloop => \@bibitemloop,
411 findborrower => $findborrower,
412 cardnumber => $cardnumber,
413 CGIselectborrower => $CGIselectborrower,
414 title =>$dat->{title},
418 print $input->header(
419 -type => C4::Interface::CGI::Output::guesstype($template->output),
421 ), $template->output;