4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 script to place reserves/requests
33 use List
::MoreUtils qw
/uniq/;
34 use Date
::Calc qw
/Date_to_Days/;
42 use C4
::Dates qw
/format_date/;
44 use C4
::Search
; # enabled_staff_search_views
47 my $dbh = C4
::Context
->dbh;
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
52 template_name
=> "reserve/request.tmpl",
56 flagsrequired
=> { reserveforothers
=> 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold
=> $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches
();
66 my $itemtypes = GetItemTypes
();
68 my $default = C4
::Context
->userenv->{branch
};
72 foreach my $branchcode (sort keys %{$branches} ) {
73 push @values, $branchcode;
74 $label_of{$branchcode} = $branches->{$branchcode}->{branchname
};
76 my $CGIbranch = CGI
::scrolling_list
(
81 -labels
=> \
%label_of,
86 # Select borrowers infos
87 my $findborrower = $input->param('findborrower');
88 $findborrower = '' unless defined $findborrower;
89 $findborrower =~ s
|,| |g
;
90 my $borrowernumber_hold = $input->param('borrowernumber') || '';
96 my $date = C4
::Dates
->today('iso');
97 my $action = $input->param('action');
100 if ( $action eq 'move' ) {
101 my $where = $input->param('where');
102 my $borrowernumber = $input->param('borrowernumber');
103 my $biblionumber = $input->param('biblionumber');
104 AlterPriority
( $where, $borrowernumber, $biblionumber );
105 } elsif ( $action eq 'cancel' ) {
106 my $borrowernumber = $input->param('borrowernumber');
107 my $biblionumber = $input->param('biblionumber');
108 CancelReserve
( $biblionumber, '', $borrowernumber );
109 } elsif ( $action eq 'setLowestPriority' ) {
110 my $borrowernumber = $input->param('borrowernumber');
111 my $biblionumber = $input->param('biblionumber');
112 ToggleLowestPriority
( $borrowernumber, $biblionumber );
116 my $borrowers = Search
($findborrower, 'cardnumber');
118 if ($borrowers && @
$borrowers) {
119 if ( @
$borrowers == 1 ) {
120 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
123 $borrowerslist = $borrowers;
126 $messageborrower = "'$findborrower'";
130 # If we have the borrowernumber because we've performed an action, then we
131 # don't want to try to place another reserve.
132 if ($borrowernumber_hold && !$action) {
133 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
136 my $count_reserv = 0;
139 # we check the reserves of the borrower, and if he can reserv a document
140 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
142 my $number_reserves =
143 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
145 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
150 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
151 my $expiry_date = $borrowerinfo->{dateexpiry
};
152 my $expiry = 0; # flag set if patron account has expired
153 if ($expiry_date and $expiry_date ne '0000-00-00' and
154 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
155 $messages = $expiry = 1;
158 # check if the borrower make the reserv in a different branch
159 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
165 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
166 borrowersurname
=> $borrowerinfo->{'surname'},
167 borrowerfirstname
=> $borrowerinfo->{'firstname'},
168 borrowerstreetaddress
=> $borrowerinfo->{'address'},
169 borrowercity
=> $borrowerinfo->{'city'},
170 borrowerphone
=> $borrowerinfo->{'phone'},
171 borrowermobile
=> $borrowerinfo->{'mobile'},
172 borrowerfax
=> $borrowerinfo->{'fax'},
173 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
174 borroweremail
=> $borrowerinfo->{'email'},
175 borroweremailpro
=> $borrowerinfo->{'emailpro'},
176 borrowercategory
=> $borrowerinfo->{'category'},
177 borrowerreservs
=> $count_reserv,
178 cardnumber
=> $borrowerinfo->{'cardnumber'},
179 maxreserves
=> $maxreserves,
181 diffbranch
=> $diffbranch,
182 messages
=> $messages,
183 warnings
=> $warnings
187 $template->param( messageborrower
=> $messageborrower );
189 my $CGIselectborrower;
190 if ($borrowerslist) {
194 foreach my $borrower (
197 . $a->{firstname
}) cmp uc($b->{surname
}
202 push @values, $borrower->{borrowernumber
};
204 $labels{ $borrower->{borrowernumber
} } = sprintf(
205 '%s, %s ... (%s - %s) ... %s',
206 $borrower->{surname
} ||'', $borrower->{firstname
} || '',
207 $borrower->{cardnumber
} || '', $borrower->{categorycode
} || '',
208 $borrower->{address
} || '',
212 $CGIselectborrower = CGI
::scrolling_list
(
213 -name
=> 'borrowernumber',
221 # FIXME launch another time GetMember perhaps until
222 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
224 my @biblionumbers = ();
225 my $biblionumbers = $input->param('biblionumbers');
227 @biblionumbers = split '/', $biblionumbers;
229 push @biblionumbers, $input->param('biblionumber');
232 my $itemdata_enumchron = 0;
234 foreach my $biblionumber (@biblionumbers) {
236 my %biblioloopiter = ();
239 my $dat = GetBiblioData
($biblionumber);
241 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
245 # get existing reserves .....
246 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
247 my $totalcount = $count;
250 foreach my $res (@
$reserves) {
251 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
255 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
257 $alreadyreserved = 1;
258 $biblioloopiter{warn} = 1;
259 $biblioloopiter{alreadyres
} = 1;
263 $template->param( alreadyreserved
=> $alreadyreserved,
264 messages
=> $messages,
265 warnings
=> $warnings,
266 maxreserves
=>$maxreserves
270 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
271 # make priorities options
274 for ( 1 .. $count + 1 ) {
279 selected
=> ( $_ == $count + 1 ),
283 # adding a fixed value for priority options
284 my $fixedRank = $count+1;
287 my %itemnumbers_of_biblioitem;
290 ## $items is array of 'item' table numbers
291 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
292 @itemnumbers = @
$items;
294 my @hostitems = get_hostitemnumbers_of
($biblionumber);
296 $template->param('hostitemsflag' => 1);
297 push(@itemnumbers, @hostitems);
301 $template->param('noitems' => 1);
302 $biblioloopiter{noitems
} = 1;
305 ## Hash of item number to 'item' table fields
306 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
308 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
309 ## when by definition all of the itemnumber have the same biblioitemnumber
310 foreach my $itemnumber (@itemnumbers) {
311 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
312 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
315 ## Should be same as biblionumber
316 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
318 my $notforloan_label_of = get_notforloan_label_of
();
320 ## Hash of biblioitemnumber to 'biblioitem' table records
321 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
325 foreach my $biblioitemnumber (@biblioitemnumbers) {
326 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
327 my $num_available = 0;
328 my $num_override = 0;
331 $biblioitem->{description
} =
332 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
333 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
334 $biblioitem->{hostitemsflag
}=1;
336 $biblioloopiter{description
} = $biblioitem->{description
};
337 $biblioloopiter{itypename
} = $biblioitem->{description
};
338 $biblioloopiter{imageurl
} =
339 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
341 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
342 my $item = $iteminfos_of->{$itemnumber};
344 unless (C4
::Context
->preference('item-level_itypes')) {
345 $item->{itype
} = $biblioitem->{itemtype
};
348 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
349 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
350 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
352 # if the holdingbranch is different than the homebranch, we show the
353 # holdingbranch of the document too
354 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
355 $item->{holdingbranchname
} =
356 $branches->{ $item->{holdingbranch
} }{branchname
};
359 if($item->{biblionumber
} ne $biblionumber){
360 $item->{hostitemsflag
}=1;
361 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
365 $item->{itemcallnumber
} = $item->{itemcallnumber
};
367 # if the item is currently on loan, we display its return date and
368 # change the background color
369 my $issues= GetItemIssue
($itemnumber);
370 if ( $issues->{'date_due'} ) {
371 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
372 $item->{backgroundcolor
} = 'onloan';
376 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
377 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
379 if ( defined $reservedate ) {
380 $item->{backgroundcolor
} = 'reserved';
381 $item->{reservedate
} = format_date
($reservedate);
382 $item->{ReservedForBorrowernumber
} = $reservedfor;
383 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
384 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
385 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
389 # Management of the notforloan document
390 if ( $item->{notforloan
} ) {
391 $item->{backgroundcolor
} = 'other';
392 $item->{notforloanvalue
} =
393 $notforloan_label_of->{ $item->{notforloan
} };
396 # Management of lost or long overdue items
397 if ( $item->{itemlost
} ) {
399 # FIXME localized strings should never be in Perl code
401 $item->{itemlost
} == 1 ?
"(lost)"
402 : $item->{itemlost
} == 2 ?
"(long overdue)"
404 $item->{backgroundcolor
} = 'other';
405 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
411 # Check the transit status
412 my ( $transfertwhen, $transfertfrom, $transfertto ) =
413 GetTransfers
($itemnumber);
415 if ( defined $transfertwhen && $transfertwhen ne '' ) {
416 $item->{transfertwhen
} = format_date
($transfertwhen);
417 $item->{transfertfrom
} =
418 $branches->{$transfertfrom}{branchname
};
419 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
420 $item->{nocancel
} = 1;
423 # If there is no loan, return and transfer, we show a checkbox.
424 $item->{notforloan
} = $item->{notforloan
} || 0;
426 # if independent branches is on we need to check if the person can reserve
427 # for branches they arent logged in to
428 if ( C4
::Context
->preference("IndependantBranches") ) {
429 if (! C4
::Context
->preference("canreservefromotherbranches")){
430 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
431 my $userenv = C4
::Context
->userenv;
432 if ( ($userenv) && ( $userenv->{flags
} % 2 != 1 ) ) {
433 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
438 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
440 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
441 my $policy_holdallowed = 1;
443 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
445 if ( $branchitemrule->{'holdallowed'} == 0 ||
446 ( $branchitemrule->{'holdallowed'} == 1 &&
447 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
448 $policy_holdallowed = 0;
453 && !$item->{cantreserve
}
454 && IsAvailableForItemLevelRequest
($itemnumber)
455 && CanItemBeReserved
(
456 $borrowerinfo->{borrowernumber
}, $itemnumber
460 $item->{available
} = 1;
463 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
465 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
466 $item->{override
} = 1;
470 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
472 # FIXME: move this to a pm
473 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
474 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
475 while (my $wait_hashref = $sth2->fetchrow_hashref) {
476 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
479 # Show serial enumeration when needed
480 if ($item->{enumchron
}) {
481 $itemdata_enumchron = 1;
484 push @
{ $biblioitem->{itemloop
} }, $item;
487 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
488 $template->param( override_required
=> 1 );
489 } elsif ( $num_available == 0 ) {
490 $template->param( none_available
=> 1 );
491 $template->param( warnings
=> 1 );
492 $biblioloopiter{warn} = 1;
493 $biblioloopiter{none_avail
} = 1;
495 $template->param( hiddencount
=> $hiddencount);
497 push @bibitemloop, $biblioitem;
500 # existingreserves building
502 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
503 foreach my $res ( sort {
504 my $a_found = $a->{found
} || '';
505 my $b_found = $a->{found
} || '';
506 $a_found cmp $b_found;
510 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
515 selected
=> ( $i == $res->{priority
} ),
520 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
521 my $item = $res->{'itemnumber'};
522 $item = GetBiblioFromItemNumber
($item,undef);
524 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
525 $reserve{'biblionumber'}=$item->{'biblionumber'};
526 $reserve{'barcodenumber'} = $item->{'barcode'};
527 $reserve{'wbrcode'} = $res->{'branchcode'};
528 $reserve{'itemnumber'} = $res->{'itemnumber'};
529 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
530 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
531 $reserve{'atdestination'} = 1;
533 # set found to 1 if reserve is waiting for patron pickup
534 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
535 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
536 } elsif ($res->{priority
} > 0) {
537 if (defined($res->{itemnumber
})) {
538 my $item = GetItem
($res->{itemnumber
});
539 $reserve{'itemnumber'} = $res->{'itemnumber'};
540 $reserve{'barcodenumber'} = $item->{'barcode'};
541 $reserve{'item_level_hold'} = 1;
545 # get borrowers reserve info
546 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
547 if (C4
::Context
->preference('HidePatronName')){
548 $reserve{'hidename'} = 1;
549 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
551 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
552 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
553 $reserve{'date'} = format_date
( $res->{'reservedate'} );
554 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
555 $reserve{'biblionumber'} = $res->{'biblionumber'};
556 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
557 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
558 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
559 $reserve{'notes'} = $res->{'reservenotes'};
561 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
562 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
563 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
564 $reserve{'voldesc'} = $res->{'volumeddesc'};
565 $reserve{'ccode'} = $res->{'ccode'};
566 $reserve{'barcode'} = $res->{'barcode'};
567 $reserve{'priority'} = $res->{'priority'};
568 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
569 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
570 $reserve{'optionloop'} = \
@optionloop;
572 push( @reserveloop, \
%reserve );
575 # get the time for the form name...
579 CGIbranch
=> $CGIbranch,
582 fixedRank
=> $fixedRank,
587 optionloop
=> \
@optionloop,
588 bibitemloop
=> \
@bibitemloop,
589 itemdata_enumchron
=> $itemdata_enumchron,
591 biblionumber
=> $biblionumber,
592 findborrower
=> $findborrower,
593 title
=> $dat->{title
},
594 author
=> $dat->{author
},
596 C4
::Search
::enabled_staff_search_views
,
598 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
600 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
601 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
604 $template->param(CGIselectborrower
=> $CGIselectborrower) if defined $CGIselectborrower;
606 $biblioloopiter{biblionumber
} = $biblionumber;
607 $biblioloopiter{title
} = $dat->{title
};
608 $biblioloopiter{rank
} = $fixedRank;
609 $biblioloopiter{reserveloop
} = \
@reserveloop;
612 $template->param( reserveloop
=> \
@reserveloop );
615 push @biblioloop, \
%biblioloopiter;
618 $template->param( biblioloop
=> \
@biblioloop );
619 $template->param( biblionumbers
=> $biblionumbers );
620 $template->param( DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar() );
623 $template->param( multi_hold
=> 1 );
626 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
627 $template->param( reserve_in_future
=> 1 );
631 output_html_with_http_headers
$input, $cookie, $template->output;