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 my $multihold = $input->param('multi_hold');
59 $template->param(multi_hold => $multihold);
61 # get Branches and Itemtypes
62 my $branches = GetBranches();
63 my $itemtypes = GetItemTypes();
65 my $default = C4::Context->userenv->{branch};
69 foreach my $branchcode (sort keys %{$branches} ) {
70 push @values, $branchcode;
71 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
73 my $CGIbranch = CGI::scrolling_list(
78 -labels => \%label_of,
83 # Select borrowers infos
84 my $findborrower = $input->param('findborrower');
85 $findborrower =~ s|,| |g;
86 my $cardnumber = $input->param('cardnumber');
93 my $date = C4::Dates->today('iso');
96 my ( $count, $borrowers ) =
97 SearchMember($findborrower, 'cardnumber', 'web' );
99 my @borrowers = @$borrowers;
101 if ( $#borrowers == -1 ) {
102 $input->param( 'findborrower', '' );
103 $messageborrower = "'$findborrower'";
105 elsif ( $#borrowers == 0 ) {
106 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
107 $cardnumber = $borrowers[0]->{'cardnumber'};
110 $borrowerslist = \@borrowers;
115 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
118 my $count_reserv = 0;
120 # we check the reserves of the borrower, and if he can reserv a document
121 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
125 my $number_reserves =
126 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
128 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
129 my $expiry_date = $borrowerinfo->{dateexpiry};
130 my $expiry = 0; # flag set if patron account has expired
131 if ($expiry_date and $expiry_date ne '0000-00-00' and
132 Date_to_Days(split (/-/,$date)) > Date_to_Days(split (/-/,$expiry_date))) {
133 $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,
160 diffbranch => $diffbranch,
161 messages => $messages,
162 warnings => $warnings
166 $template->param( messageborrower => $messageborrower );
168 my $CGIselectborrower;
169 if ($borrowerslist) {
173 foreach my $borrower (
176 . $a->{firstname} cmp $b->{surname}
181 push @values, $borrower->{cardnumber};
183 $labels{ $borrower->{cardnumber} } = sprintf(
184 '%s, %s ... (%s - %s) ... %s',
185 $borrower->{surname}, $borrower->{firstname},
186 $borrower->{cardnumber}, $borrower->{categorycode},
187 $borrower->{address},
191 $CGIselectborrower = CGI::scrolling_list(
192 -name => 'cardnumber',
200 # FIXME launch another time GetMemberDetails perhaps until
201 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
203 my @biblionumbers = ();
204 my $biblionumbers = $input->param('biblionumbers');
206 @biblionumbers = split '/', $biblionumbers;
208 push @biblionumbers, $input->param('biblionumber');
212 foreach my $biblionumber (@biblionumbers) {
214 if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
219 my %biblioloopiter = ();
221 my $dat = GetBiblioData($biblionumber);
223 # get existing reserves .....
224 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
225 my $totalcount = $count;
228 foreach my $res (@$reserves) {
229 if ( ( $res->{found} eq 'W' ) ) {
233 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
235 $alreadyreserved = 1;
236 $biblioloopiter{warn} = 1;
237 $biblioloopiter{alreadyres} = 1;
241 $template->param( alreadyreserved => $alreadyreserved,
242 messages => $messages,
243 warnings => $warnings );
246 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
247 # make priorities options
250 for ( 1 .. $count + 1 ) {
255 selected => ( $_ == $count + 1 ),
259 # adding a fixed value for priority options
260 my $fixedRank = $count+1;
263 my %itemnumbers_of_biblioitem;
266 ## $items is array of 'item' table numbers
267 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
268 @itemnumbers = @$items;
271 $template->param('noitems' => 1);
272 $biblioloopiter{noitems} = 1;
275 ## Hash of item number to 'item' table fields
276 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
278 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
279 ## when by definition all of the itemnumber have the same biblioitemnumber
280 foreach my $itemnumber (@itemnumbers) {
281 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
282 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
285 ## Should be same as biblionumber
286 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
288 my $notforloan_label_of = get_notforloan_label_of();
290 ## Hash of biblioitemnumber to 'biblioitem' table records
291 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
295 foreach my $biblioitemnumber (@biblioitemnumbers) {
296 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
300 $biblioitem->{description} =
301 $itemtypes->{ $biblioitem->{itemtype} }{description};
302 $biblioloopiter{description} = $biblioitem->{description};
303 $biblioloopiter{itypename} = $biblioitem->{description};
304 $biblioloopiter{imageurl} =
305 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
307 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
308 my $item = $iteminfos_of->{$itemnumber};
310 unless (C4::Context->preference('item-level_itypes')) {
311 $item->{itype} = $biblioitem->{itemtype};
314 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
315 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
316 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
318 # if the holdingbranch is different than the homebranch, we show the
319 # holdingbranch of the document too
320 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
321 $item->{holdingbranchname} =
322 $branches->{ $item->{holdingbranch} }{branchname};
326 $item->{itemcallnumber} = $item->{itemcallnumber};
328 # if the item is currently on loan, we display its return date and
329 # change the background color
330 my $issues= GetItemIssue($itemnumber);
331 if ( $issues->{'date_due'} ) {
332 $item->{date_due} = format_date($issues->{'date_due'});
333 $item->{backgroundcolor} = 'onloan';
337 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
338 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
340 if ( defined $reservedate ) {
341 $item->{backgroundcolor} = 'reserved';
342 $item->{reservedate} = format_date($reservedate);
343 $item->{ReservedForBorrowernumber} = $reservedfor;
344 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
345 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
346 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
350 # Management of the notforloan document
351 if ( $item->{notforloan} ) {
352 $item->{backgroundcolor} = 'other';
353 $item->{notforloanvalue} =
354 $notforloan_label_of->{ $item->{notforloan} };
357 # Management of lost or long overdue items
358 if ( $item->{itemlost} ) {
360 # FIXME localized strings should never be in Perl code
362 $item->{itemlost} == 1 ? "(lost)"
363 : $item->{itemlost} == 2 ? "(long overdue)"
365 $item->{backgroundcolor} = 'other';
368 # Check the transit status
369 my ( $transfertwhen, $transfertfrom, $transfertto ) =
370 GetTransfers($itemnumber);
372 if ( $transfertwhen ne '' ) {
373 $item->{transfertwhen} = format_date($transfertwhen);
374 $item->{transfertfrom} =
375 $branches->{$transfertfrom}{branchname};
376 $item->{transfertto} = $branches->{$transfertto}{branchname};
377 $item->{nocancel} = 1;
380 # If there is no loan, return and transfer, we show a checkbox.
381 $item->{notforloan} = $item->{notforloan} || 0;
383 # if independent branches is on we need to check if the person can reserve
384 # for branches they arent logged in to
385 if ( C4::Context->preference("IndependantBranches") ) {
386 if (! C4::Context->preference("canreservefromotherbranches")){
387 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
388 my $userenv = C4::Context->userenv;
389 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
390 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
395 my $branchitemrule = GetBranchItemRule( $item->{'homebranch'}, $item->{'itype'} );
396 my $policy_holdallowed = 1;
398 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
400 if ( $branchitemrule->{'holdallowed'} == 0 ||
401 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
402 $policy_holdallowed = 0;
405 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber) ) {
406 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
407 $item->{override} = 1;
409 } elsif ( $policy_holdallowed ) {
410 $item->{available} = 1;
414 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
416 # FIXME: move this to a pm
417 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
418 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
419 while (my $wait_hashref = $sth2->fetchrow_hashref) {
420 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
422 push @{ $biblioitem->{itemloop} }, $item;
425 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
426 $template->param( override_required => 1 );
427 } elsif ( $num_available == 0 ) {
428 $template->param( none_available => 1 );
429 $template->param( warnings => 1 );
430 $biblioloopiter{warn} = 1;
431 $biblioloopiter{none_avail} = 1;
434 push @bibitemloop, $biblioitem;
437 # existingreserves building
439 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
440 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
443 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
448 selected => ( $i == $res->{priority} ),
453 foreach my $br ( keys %$branches ) {
455 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
456 $abranch{'branch'} = $br;
457 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
458 push( @branchloop, \%abranch );
461 if ( ( $res->{'found'} eq 'W' ) ) {
462 my $item = $res->{'itemnumber'};
463 $item = GetBiblioFromItemNumber($item,undef);
465 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
466 $reserve{'biblionumber'}=$item->{'biblionumber'};
467 $reserve{'barcodenumber'} = $item->{'barcode'};
468 $reserve{'wbrcode'} = $res->{'branchcode'};
469 $reserve{'itemnumber'} = $res->{'itemnumber'};
470 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
471 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
472 $reserve{'atdestination'} = 1;
474 # set found to 1 if reserve is waiting for patron pickup
475 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
476 } elsif ($res->{priority} > 0) {
477 if (defined($res->{itemnumber})) {
478 my $item = GetItem($res->{itemnumber});
479 $reserve{'itemnumber'} = $res->{'itemnumber'};
480 $reserve{'barcodenumber'} = $item->{'barcode'};
481 $reserve{'item_level_hold'} = 1;
485 # get borrowers reserve info
486 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
488 $reserve{'date'} = format_date( $res->{'reservedate'} );
489 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
490 $reserve{'biblionumber'} = $res->{'biblionumber'};
491 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
492 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
493 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
494 $reserve{'notes'} = $res->{'reservenotes'};
496 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
497 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
498 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
499 $reserve{'voldesc'} = $res->{'volumeddesc'};
500 $reserve{'ccode'} = $res->{'ccode'};
501 $reserve{'barcode'} = $res->{'barcode'};
502 $reserve{'priority'} = $res->{'priority'};
503 $reserve{'branchloop'} = \@branchloop;
504 $reserve{'optionloop'} = \@optionloop;
506 push( @reserveloop, \%reserve );
509 # get the time for the form name...
513 CGIbranch => $CGIbranch,
516 fixedRank => $fixedRank,
521 optionloop => \@optionloop,
522 bibitemloop => \@bibitemloop,
524 biblionumber => $biblionumber,
525 findborrower => $findborrower,
526 cardnumber => $cardnumber,
527 CGIselectborrower => $CGIselectborrower,
528 title => $dat->{title},
529 author => $dat->{author},
531 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
532 borrower_branchcode => $borrowerinfo->{'branchcode'},
533 C4::Search::enabled_staff_search_views,
536 $biblioloopiter{biblionumber} = $biblionumber;
537 $biblioloopiter{title} = $dat->{title};
538 $biblioloopiter{rank} = $fixedRank;
539 $biblioloopiter{reserveloop} = \@reserveloop;
542 $template->param( reserveloop => \@reserveloop );
546 push @biblioloop, \%biblioloopiter;
549 $template->param( biblioloop => \@biblioloop );
550 $template->param( biblionumbers => $biblionumbers );
551 $template->param( maxreserves => $maxreserves );
554 $template->param( multi_hold => 1 );
558 output_html_with_http_headers $input, $cookie, $template->output;