4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 script to place reserves/requests
30 use C4::Branch; # GetBranches get_branchinfos_of
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
41 use C4::Dates qw/format_date/;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
54 flagsrequired => { reserveforothers => 1 },
58 # get Branches and Itemtypes
59 my $branches = GetBranches();
60 my $itemtypes = GetItemTypes();
62 # get biblio information....
63 my $biblionumber = $input->param('biblionumber');
64 my $dat = GetBiblioData($biblionumber);
66 # Select borrowers infos
67 my $findborrower = $input->param('findborrower');
68 $findborrower =~ s|,| |g;
69 my $cardnumber = $input->param('cardnumber');
75 my $date = C4::Dates->today('iso');
78 my ( $count, $borrowers ) =
79 SearchMember($findborrower, 'cardnumber', 'web' );
81 my @borrowers = @$borrowers;
83 if ( $#borrowers == -1 ) {
84 $input->param( 'findborrower', '' );
85 $messageborrower = "'$findborrower'";
87 elsif ( $#borrowers == 0 ) {
88 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
89 $cardnumber = $borrowers[0]->{'cardnumber'};
92 $borrowerslist = \@borrowers;
97 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
100 my $count_reserv = 0;
103 # we check the reserves of the borrower, and if he can reserv a document
104 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
106 my $number_reserves =
107 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
109 if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber)) {
114 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
115 my $expiry_date = $borrowerinfo->{dateexpiry};
116 my $expiry = 0; # flag set if patron account has expired
117 if ($expiry_date and $expiry_date ne '0000-00-00' and
118 Date_to_Days(split (/-/,$date)) > Date_to_Days(split (/-/,$expiry_date))) {
119 $messages = $expiry = 1;
125 # check if the borrower make the reserv in a different branch
126 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
132 borrowernumber => $borrowerinfo->{'borrowernumber'},
133 borrowersurname => $borrowerinfo->{'surname'},
134 borrowerfirstname => $borrowerinfo->{'firstname'},
135 borrowerstreetaddress => $borrowerinfo->{'address'},
136 borrowercity => $borrowerinfo->{'city'},
137 borrowerphone => $borrowerinfo->{'phone'},
138 borrowermobile => $borrowerinfo->{'mobile'},
139 borrowerfax => $borrowerinfo->{'fax'},
140 borrowerphonepro => $borrowerinfo->{'phonepro'},
141 borroweremail => $borrowerinfo->{'email'},
142 borroweremailpro => $borrowerinfo->{'emailpro'},
143 borrowercategory => $borrowerinfo->{'category'},
144 borrowerreservs => $count_reserv,
145 maxreserves => $maxreserves,
147 diffbranch => $diffbranch,
148 messages => $messages,
149 warnings => $warnings
153 $template->param( messageborrower => $messageborrower );
155 my $CGIselectborrower;
156 if ($borrowerslist) {
160 foreach my $borrower (
163 . $a->{firstname} cmp $b->{surname}
168 push @values, $borrower->{cardnumber};
170 $labels{ $borrower->{cardnumber} } = sprintf(
171 '%s, %s ... (%s - %s) ... %s',
172 $borrower->{surname}, $borrower->{firstname},
173 $borrower->{cardnumber}, $borrower->{categorycode},
174 $borrower->{address},
178 $CGIselectborrower = CGI::scrolling_list(
179 -name => 'cardnumber',
187 # get existing reserves .....
188 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
189 my $totalcount = $count;
192 # FIXME launch another time GetMemberDetails perhaps until
193 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
195 foreach my $res (@$reserves) {
196 if ( ( $res->{found} eq 'W' ) ) {
200 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
202 $alreadyreserved = 1;
206 $template->param( alreadyreserved => $alreadyreserved,
207 messages => $messages,
208 warnings => $warnings );
210 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
211 # make priorities options
214 for ( 1 .. $count + 1 ) {
219 selected => ( $_ == $count + 1 ),
223 # adding a fixed value for priority options
224 my $fixedRank = $count+1;
227 my %itemnumbers_of_biblioitem;
230 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
231 @itemnumbers = @$items;
234 $template->param('noitems' => 1);
237 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
239 foreach my $itemnumber (@itemnumbers) {
240 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
241 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
244 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
246 my $notforloan_label_of = get_notforloan_label_of();
247 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
251 foreach my $biblioitemnumber (@biblioitemnumbers) {
252 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
256 $biblioitem->{itemloop} = [];
258 $biblioitem->{description} =
259 $itemtypes->{ $biblioitem->{itemtype} }{description};
261 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ){
262 my $item = $iteminfos_of->{$itemnumber};
265 unless (C4::Context->preference('item-level_itypes')) {
266 $item->{itype} = $biblioitem->{itemtype};
269 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
270 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
271 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
273 # if the holdingbranch is different than the homebranch, we show the
274 # holdingbranch of the document too
275 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
276 $item->{holdingbranchname} =
277 $branches->{ $item->{holdingbranch} }{branchname};
281 $item->{itemcallnumber} = $item->{itemcallnumber};
283 # if the item is currently on loan, we display its return date and
284 # change the background color
285 my $issues= GetItemIssue($itemnumber);
286 if ( $issues->{'date_due'} ) {
287 $item->{date_due} = format_date($issues->{'date_due'});
288 $item->{backgroundcolor} = 'onloan';
292 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
293 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
295 if ( defined $reservedate ) {
296 $item->{backgroundcolor} = 'reserved';
297 $item->{reservedate} = format_date($reservedate);
298 $item->{ReservedForBorrowernumber} = $reservedfor;
299 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
300 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
301 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
305 # Management of the notforloan document
306 if ( $item->{notforloan} ) {
307 $item->{backgroundcolor} = 'other';
308 $item->{notforloanvalue} =
309 $notforloan_label_of->{ $item->{notforloan} };
312 # Management of lost or long overdue items
313 if ( $item->{itemlost} ) {
315 # FIXME localized strings should never be in Perl code
317 $item->{itemlost} == 1 ? "(lost)"
318 : $item->{itemlost} == 2 ? "(long overdue)"
320 $item->{backgroundcolor} = 'other';
323 # Check the transit status
324 my ( $transfertwhen, $transfertfrom, $transfertto ) =
325 GetTransfers($itemnumber);
327 if ( $transfertwhen ne '' ) {
328 $item->{transfertwhen} = format_date($transfertwhen);
329 $item->{transfertfrom} =
330 $branches->{$transfertfrom}{branchname};
331 $item->{transfertto} = $branches->{$transfertto}{branchname};
332 $item->{nocancel} = 1;
335 # If there is no loan, return and transfer, we show a checkbox.
336 $item->{notforloan} = $item->{notforloan} || 0;
338 # if independent branches is on we need to check if the person can reserve
339 # for branches they arent logged in to
340 if ( C4::Context->preference("IndependantBranches") ) {
341 if (! C4::Context->preference("canreservefromotherbranches")){
342 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
343 my $userenv = C4::Context->userenv;
344 if ( ($userenv) && ( $userenv->{flags} %2 != 1 ) ) {
345 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
350 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
352 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
353 my $policy_holdallowed = 1;
355 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
357 if ( $branchitemrule->{'holdallowed'} == 0 ||
358 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
359 $policy_holdallowed = 0;
362 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber)) {
363 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
364 $item->{override} = 1;
366 } elsif ( $policy_holdallowed ) {
367 $item->{available} = 1;
371 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
373 # FIXME: move this to a pm
374 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
375 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
376 while (my $wait_hashref = $sth2->fetchrow_hashref) {
377 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
379 push @{ $biblioitem->{itemloop} }, $item;
382 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
383 $template->param( override_required => 1 );
384 } elsif ( $num_available == 0 ) {
385 $template->param( none_available => 1 );
386 $template->param( warnings => 1 );
389 push @bibitemloop, $biblioitem;
392 # existingreserves building
394 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
395 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
398 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
403 selected => ( $i == $res->{priority} ),
408 foreach my $br ( keys %$branches ) {
410 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
411 $abranch{'branch'} = $br;
412 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
413 push( @branchloop, \%abranch );
416 if ( ( $res->{'found'} eq 'W' ) ) {
417 my $item = $res->{'itemnumber'};
418 $item = GetBiblioFromItemNumber($item,undef);
420 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
421 $reserve{'biblionumber'}=$item->{'biblionumber'};
422 $reserve{'barcodenumber'} = $item->{'barcode'};
423 $reserve{'wbrcode'} = $res->{'branchcode'};
424 $reserve{'itemnumber'} = $res->{'itemnumber'};
425 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
426 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
427 $reserve{'atdestination'} = 1;
429 # set found to 1 if reserve is waiting for patron pickup
430 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
431 } elsif ($res->{priority} > 0) {
432 if (defined($res->{itemnumber})) {
433 my $item = GetItem($res->{itemnumber});
434 $reserve{'itemnumber'} = $res->{'itemnumber'};
435 $reserve{'barcodenumber'} = $item->{'barcode'};
436 $reserve{'item_level_hold'} = 1;
440 # get borrowers reserve info
441 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
443 $reserve{'date'} = format_date( $res->{'reservedate'} );
444 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
445 $reserve{'biblionumber'} = $res->{'biblionumber'};
446 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
447 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
448 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
449 $reserve{'notes'} = $res->{'reservenotes'};
451 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
452 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
453 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
454 $reserve{'voldesc'} = $res->{'volumeddesc'};
455 $reserve{'ccode'} = $res->{'ccode'};
456 $reserve{'barcode'} = $res->{'barcode'};
457 $reserve{'priority'} = $res->{'priority'};
458 $reserve{'branchloop'} = \@branchloop;
459 $reserve{'optionloop'} = \@optionloop;
461 push( @reserveloop, \%reserve );
464 my $default = C4::Context->userenv->{branch};
468 foreach my $branchcode (sort keys %{$branches} ) {
469 push @values, $branchcode;
470 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
472 my $CGIbranch = CGI::scrolling_list(
476 -default => $default,
477 -labels => \%label_of,
482 # get the time for the form name...
486 CGIbranch => $CGIbranch,
487 reserveloop => \@reserveloop,
489 fixedRank => $fixedRank,
494 optionloop => \@optionloop,
495 bibitemloop => \@bibitemloop,
497 biblionumber => $biblionumber,
498 findborrower => $findborrower,
499 cardnumber => $cardnumber,
500 CGIselectborrower => $CGIselectborrower,
501 title => $dat->{title},
502 author => $dat->{author},
504 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
505 borrower_branchcode => $borrowerinfo->{'branchcode'},
509 output_html_with_http_headers $input, $cookie, $template->output;