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/;
44 my $dbh = C4
::Context
->dbh;
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
49 template_name
=> "reserve/request.tmpl",
53 flagsrequired
=> { reserveforothers
=> 1 },
57 my $multihold = $input->param('multi_hold');
58 $template->param(multi_hold
=> $multihold);
60 # get Branches and Itemtypes
61 my $branches = GetBranches
();
62 my $itemtypes = GetItemTypes
();
64 my $default = C4
::Context
->userenv->{branch
};
68 foreach my $branchcode (sort keys %{$branches} ) {
69 push @values, $branchcode;
70 $label_of{$branchcode} = $branches->{$branchcode}->{branchname
};
72 my $CGIbranch = CGI
::scrolling_list
(
77 -labels
=> \
%label_of,
82 # Select borrowers infos
83 my $findborrower = $input->param('findborrower');
84 $findborrower =~ s
|,| |g
;
85 my $cardnumber = $input->param('cardnumber');
91 my $date = C4
::Dates
->today('iso');
94 my ( $count, $borrowers ) =
95 SearchMember
($findborrower, 'cardnumber', 'web' );
97 my @borrowers = @
$borrowers;
99 if ( $#borrowers == -1 ) {
100 $input->param( 'findborrower', '' );
101 $messageborrower = "'$findborrower'";
103 elsif ( $#borrowers == 0 ) {
104 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
105 $cardnumber = $borrowers[0]->{'cardnumber'};
108 $borrowerslist = \
@borrowers;
113 my $borrowerinfo = GetMemberDetails
( 0, $cardnumber );
116 my $count_reserv = 0;
119 # we check the reserves of the borrower, and if he can reserv a document
120 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
122 my $number_reserves =
123 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
125 if ( $number_reserves > C4
::Context
->preference('maxreserves') ) {
130 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
131 my $expiry_date = $borrowerinfo->{dateexpiry
};
132 my $expiry = 0; # flag set if patron account has expired
133 if ($expiry_date and $expiry_date ne '0000-00-00' and
134 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
135 $messages = $expiry = 1;
139 # check if the borrower make the reserv in a different branch
140 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
146 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
147 borrowersurname
=> $borrowerinfo->{'surname'},
148 borrowerfirstname
=> $borrowerinfo->{'firstname'},
149 borrowerstreetaddress
=> $borrowerinfo->{'address'},
150 borrowercity
=> $borrowerinfo->{'city'},
151 borrowerphone
=> $borrowerinfo->{'phone'},
152 borrowermobile
=> $borrowerinfo->{'mobile'},
153 borrowerfax
=> $borrowerinfo->{'fax'},
154 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
155 borroweremail
=> $borrowerinfo->{'email'},
156 borroweremailpro
=> $borrowerinfo->{'emailpro'},
157 borrowercategory
=> $borrowerinfo->{'category'},
158 borrowerreservs
=> $count_reserv,
159 maxreserves
=> $maxreserves,
161 diffbranch
=> $diffbranch,
162 messages
=> $messages,
163 warnings
=> $warnings
167 $template->param( messageborrower
=> $messageborrower );
169 my $CGIselectborrower;
170 if ($borrowerslist) {
174 foreach my $borrower (
177 . $a->{firstname
} cmp $b->{surname
}
182 push @values, $borrower->{cardnumber
};
184 $labels{ $borrower->{cardnumber
} } = sprintf(
185 '%s, %s ... (%s - %s) ... %s',
186 $borrower->{surname
}, $borrower->{firstname
},
187 $borrower->{cardnumber
}, $borrower->{categorycode
},
188 $borrower->{address
},
192 $CGIselectborrower = CGI
::scrolling_list
(
193 -name
=> 'cardnumber',
201 # FIXME launch another time GetMemberDetails perhaps until
202 my $borrowerinfo = GetMemberDetails
( 0, $cardnumber );
204 my @biblionumbers = ();
205 my $biblionumbers = $input->param('biblionumbers');
207 @biblionumbers = split '/', $biblionumbers;
209 push @biblionumbers, $input->param('biblionumber');
213 foreach my $biblionumber (@biblionumbers) {
215 my %biblioloopiter = ();
217 my $dat = GetBiblioData
($biblionumber);
219 # get existing reserves .....
220 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
221 my $totalcount = $count;
224 foreach my $res (@
$reserves) {
225 if ( ( $res->{found
} eq 'W' ) ) {
229 if ( $borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
} ) {
231 $alreadyreserved = 1;
232 $biblioloopiter{warn} = 1;
233 $biblioloopiter{alreadyres
} = 1;
237 $template->param( alreadyreserved
=> $alreadyreserved,
238 messages
=> $messages,
239 warnings
=> $warnings );
242 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
243 # make priorities options
246 for ( 1 .. $count + 1 ) {
251 selected
=> ( $_ == $count + 1 ),
255 # adding a fixed value for priority options
256 my $fixedRank = $count+1;
259 my %itemnumbers_of_biblioitem;
262 ## $items is array of 'item' table numbers
263 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
264 @itemnumbers = @
$items;
267 $template->param('noitems' => 1);
268 $biblioloopiter{noitems
} = 1;
271 ## Hash of item number to 'item' table fields
272 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
274 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
275 ## when by definition all of the itemnumber have the same biblioitemnumber
276 foreach my $itemnumber (@itemnumbers) {
277 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
278 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
281 ## Should be same as biblionumber
282 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
284 my $notforloan_label_of = get_notforloan_label_of
();
286 ## Hash of biblioitemnumber to 'biblioitem' table records
287 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
291 foreach my $biblioitemnumber (@biblioitemnumbers) {
292 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
296 $biblioitem->{description
} =
297 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
298 $biblioloopiter{description
} = $biblioitem->{description
};
299 $biblioloopiter{itypename
} = $biblioitem->{description
};
300 $biblioloopiter{imageurl
} =
301 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
303 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
304 my $item = $iteminfos_of->{$itemnumber};
306 unless (C4
::Context
->preference('item-level_itypes')) {
307 $item->{itype
} = $biblioitem->{itemtype
};
310 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
311 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
312 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
314 # if the holdingbranch is different than the homebranch, we show the
315 # holdingbranch of the document too
316 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
317 $item->{holdingbranchname
} =
318 $branches->{ $item->{holdingbranch
} }{branchname
};
322 $item->{itemcallnumber
} = $item->{itemcallnumber
};
324 # if the item is currently on loan, we display its return date and
325 # change the background color
326 my $issues= GetItemIssue
($itemnumber);
327 if ( $issues->{'date_due'} ) {
328 $item->{date_due
} = format_date
($issues->{'date_due'});
329 $item->{backgroundcolor
} = 'onloan';
333 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
334 my $ItemBorrowerReserveInfo = GetMemberDetails
( $reservedfor, 0);
336 if ( defined $reservedate ) {
337 $item->{backgroundcolor
} = 'reserved';
338 $item->{reservedate
} = format_date
($reservedate);
339 $item->{ReservedForBorrowernumber
} = $reservedfor;
340 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
341 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
342 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
346 # Management of the notforloan document
347 if ( $item->{notforloan
} ) {
348 $item->{backgroundcolor
} = 'other';
349 $item->{notforloanvalue
} =
350 $notforloan_label_of->{ $item->{notforloan
} };
353 # Management of lost or long overdue items
354 if ( $item->{itemlost
} ) {
356 # FIXME localized strings should never be in Perl code
358 $item->{itemlost
} == 1 ?
"(lost)"
359 : $item->{itemlost
} == 2 ?
"(long overdue)"
361 $item->{backgroundcolor
} = 'other';
364 # Check the transit status
365 my ( $transfertwhen, $transfertfrom, $transfertto ) =
366 GetTransfers
($itemnumber);
368 if ( $transfertwhen ne '' ) {
369 $item->{transfertwhen
} = format_date
($transfertwhen);
370 $item->{transfertfrom
} =
371 $branches->{$transfertfrom}{branchname
};
372 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
373 $item->{nocancel
} = 1;
376 # If there is no loan, return and transfer, we show a checkbox.
377 $item->{notforloan
} = $item->{notforloan
} || 0;
379 # if independent branches is on we need to check if the person can reserve
380 # for branches they arent logged in to
381 if ( C4
::Context
->preference("IndependantBranches") ) {
382 if (! C4
::Context
->preference("canreservefromotherbranches")){
383 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
384 my $userenv = C4
::Context
->userenv;
385 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
386 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
391 my $branchitemrule = GetBranchItemRule
( $item->{'homebranch'}, $item->{'itype'} );
392 my $policy_holdallowed = 1;
394 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
396 if ( $branchitemrule->{'holdallowed'} == 0 ||
397 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
398 $policy_holdallowed = 0;
401 if (IsAvailableForItemLevelRequest
($itemnumber) and not $item->{cantreserve
}) {
402 if ( not $policy_holdallowed and C4
::Context
->preference( 'AllowHoldPolicyOverride' ) ) {
403 $item->{override
} = 1;
405 } elsif ( $policy_holdallowed ) {
406 $item->{available
} = 1;
410 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
412 # FIXME: move this to a pm
413 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
414 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
415 while (my $wait_hashref = $sth2->fetchrow_hashref) {
416 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
418 push @
{ $biblioitem->{itemloop
} }, $item;
421 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
422 $template->param( override_required
=> 1 );
423 } elsif ( $num_available == 0 ) {
424 $template->param( none_available
=> 1 );
425 $template->param( warnings
=> 1 );
426 $biblioloopiter{warn} = 1;
427 $biblioloopiter{none_avail
} = 1;
430 push @bibitemloop, $biblioitem;
433 # existingreserves building
435 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
436 foreach my $res ( sort { $a->{found
} cmp $b->{found
} } @
$reserves ) {
439 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
444 selected
=> ( $i == $res->{priority
} ),
449 foreach my $br ( keys %$branches ) {
451 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
452 $abranch{'branch'} = $br;
453 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
454 push( @branchloop, \
%abranch );
457 if ( ( $res->{'found'} eq 'W' ) ) {
458 my $item = $res->{'itemnumber'};
459 $item = GetBiblioFromItemNumber
($item,undef);
461 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
462 $reserve{'biblionumber'}=$item->{'biblionumber'};
463 $reserve{'barcodenumber'} = $item->{'barcode'};
464 $reserve{'wbrcode'} = $res->{'branchcode'};
465 $reserve{'itemnumber'} = $res->{'itemnumber'};
466 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
467 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
468 $reserve{'atdestination'} = 1;
470 # set found to 1 if reserve is waiting for patron pickup
471 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
472 } elsif ($res->{priority
} > 0) {
473 if (defined($res->{itemnumber
})) {
474 my $item = GetItem
($res->{itemnumber
});
475 $reserve{'itemnumber'} = $res->{'itemnumber'};
476 $reserve{'barcodenumber'} = $item->{'barcode'};
477 $reserve{'item_level_hold'} = 1;
481 # get borrowers reserve info
482 my $reserveborrowerinfo = GetMemberDetails
( $res->{'borrowernumber'}, 0);
484 $reserve{'date'} = format_date
( $res->{'reservedate'} );
485 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
486 $reserve{'biblionumber'} = $res->{'biblionumber'};
487 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
488 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
489 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
490 $reserve{'notes'} = $res->{'reservenotes'};
492 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
493 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
494 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
495 $reserve{'voldesc'} = $res->{'volumeddesc'};
496 $reserve{'ccode'} = $res->{'ccode'};
497 $reserve{'barcode'} = $res->{'barcode'};
498 $reserve{'priority'} = $res->{'priority'};
499 $reserve{'branchloop'} = \
@branchloop;
500 $reserve{'optionloop'} = \
@optionloop;
502 push( @reserveloop, \
%reserve );
505 # get the time for the form name...
509 CGIbranch
=> $CGIbranch,
512 fixedRank
=> $fixedRank,
517 optionloop
=> \
@optionloop,
518 bibitemloop
=> \
@bibitemloop,
520 biblionumber
=> $biblionumber,
521 findborrower
=> $findborrower,
522 cardnumber
=> $cardnumber,
523 CGIselectborrower
=> $CGIselectborrower,
524 title
=> $dat->{title
},
525 author
=> $dat->{author
},
527 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
528 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
531 $biblioloopiter{biblionumber
} = $biblionumber;
532 $biblioloopiter{title
} = $dat->{title
};
533 $biblioloopiter{rank
} = $fixedRank;
534 $biblioloopiter{reserveloop
} = \
@reserveloop;
537 $template->param( reserveloop
=> \
@reserveloop );
541 push @biblioloop, \
%biblioloopiter;
544 $template->param( biblioloop
=> \
@biblioloop );
545 $template->param( biblionumbers
=> $biblionumbers );
548 $template->param( multi_hold
=> 1 );
552 output_html_with_http_headers
$input, $cookie, $template->output;