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/;
35 use C4::Interface::CGI::Output;
40 use C4::Circulation::Circ2;
45 my $dbh = C4::Context->dbh;
48 my ($template, $borrowernumber, $cookie)
49 = get_template_and_user({
50 template_name => "reserve/request.tmpl",
54 flagsrequired => {reserveforothers => 1},
57 # get biblio information....
58 my $bib = $input->param('bib');
59 my $dat = bibdata($bib);
61 # Select borrowers infos
62 my $findborrower = $input->param('findborrower');
63 $findborrower =~ s|,| |g;
64 my $cardnumber = $input->param('cardnumber');
72 my ($count,$borrowers) = BornameSearch(\%env, $findborrower, 'cardnumber', 'web');
74 my @borrowers = @$borrowers;
76 if ($#borrowers == -1) {
77 $input->param('findborrower', '');
78 $messageborrower = "'$findborrower'";
80 elsif ($#borrowers == 0) {
81 $input->param('cardnumber', $borrowers[0]->{'cardnumber'});
82 $cardnumber = $borrowers[0]->{'cardnumber'};
85 $borrowerslist = \@borrowers;
90 my $borrowerinfo = getpatroninformation (\%env,0,$cardnumber);
97 # we check the reserves of the borrower, and if he can reserv a document
98 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
100 my $number_reserves =
101 GetNumberReservesFromBorrower($borrowerinfo->{'borrowernumber'});
103 if ($number_reserves > C4::Context->preference('maxreserves')) {
107 # we check the date expiricy of the borrower
108 my $warning=Date_Cmp(ParseDate("today"),format_date($borrowerinfo->{'dateexpiry'}));
113 # check if the borrower make the reserv in a different branch
114 if ($borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'}) {
119 borrowersurname => $borrowerinfo->{'surname'},
120 borrowerfirstname => $borrowerinfo->{'firstname'},
121 borrowerreservs => $count_reserv,
122 maxreserves => $maxreserves,
124 diffbranch => $diffbranch
129 messageborrower => $messageborrower
132 my $CGIselectborrower;
133 if ($borrowerslist) {
137 foreach my $borrower (
138 sort {$a->{surname}.$a->{firstname} cmp $b->{surname}.$b->{firstname}}
141 push @values, $borrower->{cardnumber};
143 $labels{ $borrower->{cardnumber} } = sprintf(
144 '%s, %s ... (%s - %s) ... %s',
145 $borrower->{surname},
146 $borrower->{firstname},
147 $borrower->{cardnumber},
148 $borrower->{categorycode},
149 $borrower->{streetaddress},
153 $CGIselectborrower = CGI::scrolling_list(
154 -name => 'cardnumber',
162 # get existing reserves .....
163 my ($count, $reserves) = FindReserves($bib, undef);
164 my $totalcount = $count;
167 # FIXME launch another time getpatroninformation perhaps until
168 my $borrowerinfo = getpatroninformation (\%env,0,$cardnumber);
170 foreach my $res (@$reserves) {
171 if (($res->{found} eq 'W') or ($res->{priority} == 0)) {
175 if ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) {
176 $alreadyreserved = 1;
179 $template->param(alreadyreserved => $alreadyreserved);
181 # make priorities options
183 for (1 .. $count + 1) {
188 selected => ($_ == $count + 1),
194 my %itemnumbers_of_biblioitem;
195 my @itemnumbers = @{get_itemnumbers_of($bib)->{$bib}};
196 my $iteminfos_of = get_iteminfos_of(@itemnumbers);
198 foreach my $itemnumber (@itemnumbers) {
201 $iteminfos_of->{$itemnumber}->{homebranch},
202 $iteminfos_of->{$itemnumber}->{holdingbranch}
205 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
207 @{ $itemnumbers_of_biblioitem{$biblioitemnumber} },
212 @branchcodes = uniq @branchcodes;
214 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
216 my $branchinfos_of = get_branchinfos_of(@branchcodes);
217 my $notforloan_label_of = get_notforloan_label_of();
218 my $biblioiteminfos_of = get_biblioiteminfos_of(@biblioitemnumbers);
221 foreach my $biblioitemnumber (@biblioitemnumbers) {
222 push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
225 my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
227 my $return_date_of = get_current_return_date_of(@itemnumbers);
231 foreach my $biblioitemnumber (@biblioitemnumbers) {
232 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
234 $biblioitem->{description} =
235 $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
237 foreach my $itemnumber (@{$itemnumbers_of_biblioitem{$biblioitemnumber}}) {
238 my $item = $iteminfos_of->{$itemnumber};
240 $item->{homebranchname} =
241 $branchinfos_of->{ $item->{homebranch} }{branchname};
243 # if the holdingbranch is different than the homebranch, we show the
244 # holdingbranch of the document too
245 if ($item->{homebranch} ne $item->{holdingbranch}) {
246 $item->{holdingbranchname} =
247 $branchinfos_of->{ $item->{holdingbranch} }{branchname};
250 # if the item is currently on loan, we display its return date and
251 # change the background color
254 if (defined $return_date_of->{$itemnumber}) {
255 $date_due = format_date($return_date_of->{$itemnumber});
256 $item->{date_due} = $date_due;
257 $item->{backgroundcolor} = 'onloan';
261 my $reservedate = GetFirstReserveDateFromItem($itemnumber);
263 if (defined $reservedate) {
264 $item->{backgroundcolor} = 'reserved';
265 $item->{reservedate} = format_date($reservedate);
268 # Management of the notforloan document
269 if ($item->{notforloan}) {
270 $item->{backgroundcolor} = 'other';
271 $item->{notforloanvalue} =
272 $notforloan_label_of->{ $item->{notforloan} };
275 # Management of lost or long overdue items
276 if ($item->{itemlost}) {
278 # FIXME localized strings should never be in Perl code
279 $item->{message} = $item->{itemlost} == 1
281 : $item->{itemlost} == 2
284 $item->{backgroundcolor} = 'other';
287 # Check of the transfered documents
288 my ($transfertwhen,$transfertfrom,$transfertto) =
289 get_transfert_infos($itemnumber);
291 if ($transfertwhen ne '') {
292 $item->{transfertwhen} = format_date($transfertwhen);
293 $item->{transfertfrom} =
294 $branchinfos_of->{$transfertfrom}{branchname};
295 $item->{transfertto} =
296 $branchinfos_of->{$transfertto}{branchname};
299 # If there is no loan, return and transfer, we show a checkbox.
300 $item->{notforloan} = $item->{notforloan} || 0;
302 # An item is available only if:
303 if (not defined $reservedate # not reserved yet
304 and $date_due eq '' # not currently on loan
305 and not $item->{itemlost} # not lost
306 and not $item->{notforloan} # not forbidden to loan
307 and $transfertwhen eq '' # not currently on transfert
309 $item->{available} = 1;
312 push @{$biblioitem->{itemloop}}, $item;
315 push @bibitemloop, $biblioitem;
319 # existingreserves building
321 my $branches = getbranches();
322 foreach my $res (sort {$a->{found} cmp $b->{found}} @$reserves){
325 for (my $i=1; $i <= $totalcount; $i++) {
330 selected => ($i == $res->{priority}),
335 foreach my $br (keys %$branches) {
337 $abranch{'selected'}=($br eq $res->{'branchcode'});
338 $abranch{'branch'}=$br;
339 $abranch{'branchname'}=$branches->{$br}->{'branchname'};
340 push(@branchloop,\%abranch);
343 if (($res->{'found'} eq 'W') or ($res->{'priority'} eq '0')) {
345 my $item = $res->{'itemnumber'};
346 $item = getiteminformation(\%env,$item);
347 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
348 $reserve{'barcode'}=$item->{'barcode'};
349 $reserve{'biblionumber'}=$item->{'biblionumber'};
350 $reserve{'wbrcode'} = $res->{'branchcode'};
351 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
352 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
353 $reserve{'atdestination'} = 1;
357 $reserve{'date'} = format_date($res->{'reservedate'});
358 $reserve{'borrowernumber'}=$res->{'borrowernumber'};
359 $reserve{'biblionumber'}=$res->{'biblionumber'};
360 $reserve{'bornum'}=$res->{'borrowernumber'};
361 $reserve{'firstname'}=$res->{'firstname'};
362 $reserve{'surname'}=$res->{'surname'};
363 $reserve{'bornum'}=$res->{'borrowernumber'};
364 $reserve{'notes'}=$res->{'reservenotes'};
365 $reserve{'wait'}=(($res->{'found'} eq 'W') or ($res->{'priority'} eq '0'));
366 $reserve{'constrainttypea'}=($res->{'constrainttype'} eq 'a');
367 $reserve{'constrainttypeo'}=($res->{'constrainttype'} eq 'o');
368 $reserve{'voldesc'}=$res->{'volumeddesc'};
369 $reserve{'itemtype'}=$res->{'itemtype'};
370 $reserve{'branchloop'}=\@branchloop;
371 $reserve{'optionloop'}=\@optionloop;
373 push(@reserveloop,\%reserve);
376 my $default = C4::Context->userenv->{branch};
380 foreach my $branchcode (keys %{$branches}) {
381 push @values, $branchcode;
382 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
385 my $CGIbranch = CGI::scrolling_list(
388 -default => $default,
389 -labels => \%label_of,
394 # get the time for the form name...
398 CGIbranch => $CGIbranch,
399 reserveloop => \@reserveloop,
405 optionloop =>\@optionloop,
406 bibitemloop => \@bibitemloop,
409 findborrower => $findborrower,
410 cardnumber => $cardnumber,
411 CGIselectborrower => $CGIselectborrower,
412 title =>$dat->{title},
416 print $input->header(
417 -type => C4::Interface::CGI::Output::guesstype($template->output),
419 ), $template->output;