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 { $branches->{$a}->{branchname
} cmp $branches->{$b}->{branchname
} } 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 );
113 } elsif ( $action eq 'toggleSuspend' ) {
114 my $borrowernumber = $input->param('borrowernumber');
115 my $biblionumber = $input->param('biblionumber');
116 my $suspend_until = $input->param('suspend_until');
117 ToggleSuspend
( $borrowernumber, $biblionumber, $suspend_until );
121 my $borrowers = Search
($findborrower, 'cardnumber');
123 if ($borrowers && @
$borrowers) {
124 if ( @
$borrowers == 1 ) {
125 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
128 $borrowerslist = $borrowers;
131 $messageborrower = "'$findborrower'";
135 # If we have the borrowernumber because we've performed an action, then we
136 # don't want to try to place another reserve.
137 if ($borrowernumber_hold && !$action) {
138 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
141 my $count_reserv = 0;
144 # we check the reserves of the borrower, and if he can reserv a document
145 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
147 my $number_reserves =
148 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
150 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
155 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
156 my $expiry_date = $borrowerinfo->{dateexpiry
};
157 my $expiry = 0; # flag set if patron account has expired
158 if ($expiry_date and $expiry_date ne '0000-00-00' and
159 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
160 $messages = $expiry = 1;
163 # check if the borrower make the reserv in a different branch
164 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
170 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
171 borrowersurname
=> $borrowerinfo->{'surname'},
172 borrowerfirstname
=> $borrowerinfo->{'firstname'},
173 borrowerstreetaddress
=> $borrowerinfo->{'address'},
174 borrowercity
=> $borrowerinfo->{'city'},
175 borrowerphone
=> $borrowerinfo->{'phone'},
176 borrowermobile
=> $borrowerinfo->{'mobile'},
177 borrowerfax
=> $borrowerinfo->{'fax'},
178 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
179 borroweremail
=> $borrowerinfo->{'email'},
180 borroweremailpro
=> $borrowerinfo->{'emailpro'},
181 borrowercategory
=> $borrowerinfo->{'category'},
182 borrowerreservs
=> $count_reserv,
183 cardnumber
=> $borrowerinfo->{'cardnumber'},
184 maxreserves
=> $maxreserves,
186 diffbranch
=> $diffbranch,
187 messages
=> $messages,
188 warnings
=> $warnings
192 $template->param( messageborrower
=> $messageborrower );
194 my $CGIselectborrower;
195 if ($borrowerslist) {
199 foreach my $borrower (
202 . $a->{firstname
}) cmp uc($b->{surname
}
207 push @values, $borrower->{borrowernumber
};
209 $labels{ $borrower->{borrowernumber
} } = sprintf(
210 '%s, %s ... (%s - %s) ... %s',
211 $borrower->{surname
} ||'', $borrower->{firstname
} || '',
212 $borrower->{cardnumber
} || '', $borrower->{categorycode
} || '',
213 $borrower->{address
} || '',
217 $CGIselectborrower = CGI
::scrolling_list
(
218 -name
=> 'borrowernumber',
226 # FIXME launch another time GetMember perhaps until
227 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
229 my @biblionumbers = ();
230 my $biblionumbers = $input->param('biblionumbers');
232 @biblionumbers = split '/', $biblionumbers;
234 push @biblionumbers, $input->param('biblionumber');
237 my $itemdata_enumchron = 0;
239 foreach my $biblionumber (@biblionumbers) {
241 my %biblioloopiter = ();
244 my $dat = GetBiblioData
($biblionumber);
246 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
251 my $alreadypossession;
252 if (not C4
::Context
->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron
($borrowerinfo->{borrowernumber
},$biblionumber)) {
254 $alreadypossession = 1;
257 # get existing reserves .....
258 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
259 my $totalcount = $count;
262 foreach my $res (@
$reserves) {
263 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
267 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
269 $alreadyreserved = 1;
270 $biblioloopiter{warn} = 1;
271 $biblioloopiter{alreadyres
} = 1;
275 $template->param( alreadyreserved
=> $alreadyreserved,
276 messages
=> $messages,
277 warnings
=> $warnings,
278 maxreserves
=>$maxreserves,
279 alreadypossession
=> $alreadypossession,
283 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
284 # make priorities options
287 for ( 1 .. $count + 1 ) {
292 selected
=> ( $_ == $count + 1 ),
296 # adding a fixed value for priority options
297 my $fixedRank = $count+1;
300 my %itemnumbers_of_biblioitem;
303 ## $items is array of 'item' table numbers
304 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
305 @itemnumbers = @
$items;
307 my @hostitems = get_hostitemnumbers_of
($biblionumber);
309 $template->param('hostitemsflag' => 1);
310 push(@itemnumbers, @hostitems);
314 $template->param('noitems' => 1);
315 $biblioloopiter{noitems
} = 1;
318 ## Hash of item number to 'item' table fields
319 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
321 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
322 ## when by definition all of the itemnumber have the same biblioitemnumber
323 foreach my $itemnumber (@itemnumbers) {
324 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
325 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
328 ## Should be same as biblionumber
329 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
331 my $notforloan_label_of = get_notforloan_label_of
();
333 ## Hash of biblioitemnumber to 'biblioitem' table records
334 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
338 foreach my $biblioitemnumber (@biblioitemnumbers) {
339 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
340 my $num_available = 0;
341 my $num_override = 0;
344 $biblioitem->{description
} =
345 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
346 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
347 $biblioitem->{hostitemsflag
}=1;
349 $biblioloopiter{description
} = $biblioitem->{description
};
350 $biblioloopiter{itypename
} = $biblioitem->{description
};
351 $biblioloopiter{imageurl
} =
352 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
354 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
355 my $item = $iteminfos_of->{$itemnumber};
357 unless (C4
::Context
->preference('item-level_itypes')) {
358 $item->{itype
} = $biblioitem->{itemtype
};
361 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
362 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
363 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
365 # if the holdingbranch is different than the homebranch, we show the
366 # holdingbranch of the document too
367 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
368 $item->{holdingbranchname
} =
369 $branches->{ $item->{holdingbranch
} }{branchname
};
372 if($item->{biblionumber
} ne $biblionumber){
373 $item->{hostitemsflag
}=1;
374 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
378 $item->{itemcallnumber
} = $item->{itemcallnumber
};
380 # if the item is currently on loan, we display its return date and
381 # change the background color
382 my $issues= GetItemIssue
($itemnumber);
383 if ( $issues->{'date_due'} ) {
384 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
385 $item->{backgroundcolor
} = 'onloan';
389 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
390 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
392 if ( defined $reservedate ) {
393 $item->{backgroundcolor
} = 'reserved';
394 $item->{reservedate
} = format_date
($reservedate);
395 $item->{ReservedForBorrowernumber
} = $reservedfor;
396 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
397 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
398 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
402 # Management of the notforloan document
403 if ( $item->{notforloan
} ) {
404 $item->{backgroundcolor
} = 'other';
405 $item->{notforloanvalue
} =
406 $notforloan_label_of->{ $item->{notforloan
} };
409 # Management of lost or long overdue items
410 if ( $item->{itemlost
} ) {
412 # FIXME localized strings should never be in Perl code
414 $item->{itemlost
} == 1 ?
"(lost)"
415 : $item->{itemlost
} == 2 ?
"(long overdue)"
417 $item->{backgroundcolor
} = 'other';
418 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
424 # Check the transit status
425 my ( $transfertwhen, $transfertfrom, $transfertto ) =
426 GetTransfers
($itemnumber);
428 if ( defined $transfertwhen && $transfertwhen ne '' ) {
429 $item->{transfertwhen
} = format_date
($transfertwhen);
430 $item->{transfertfrom
} =
431 $branches->{$transfertfrom}{branchname
};
432 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
433 $item->{nocancel
} = 1;
436 # If there is no loan, return and transfer, we show a checkbox.
437 $item->{notforloan
} = $item->{notforloan
} || 0;
439 # if independent branches is on we need to check if the person can reserve
440 # for branches they arent logged in to
441 if ( C4
::Context
->preference("IndependantBranches") ) {
442 if (! C4
::Context
->preference("canreservefromotherbranches")){
443 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
444 my $userenv = C4
::Context
->userenv;
445 if ( ($userenv) && ( $userenv->{flags
} % 2 != 1 ) ) {
446 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
451 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
453 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
454 my $policy_holdallowed = 1;
456 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
458 if ( $branchitemrule->{'holdallowed'} == 0 ||
459 ( $branchitemrule->{'holdallowed'} == 1 &&
460 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
461 $policy_holdallowed = 0;
466 && !$item->{cantreserve
}
467 && IsAvailableForItemLevelRequest
($itemnumber)
468 && CanItemBeReserved
(
469 $borrowerinfo->{borrowernumber
}, $itemnumber
473 $item->{available
} = 1;
476 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
478 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
479 $item->{override
} = 1;
483 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
485 # FIXME: move this to a pm
486 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
487 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
488 while (my $wait_hashref = $sth2->fetchrow_hashref) {
489 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
492 # Show serial enumeration when needed
493 if ($item->{enumchron
}) {
494 $itemdata_enumchron = 1;
497 push @
{ $biblioitem->{itemloop
} }, $item;
500 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
501 $template->param( override_required
=> 1 );
502 } elsif ( $num_available == 0 ) {
503 $template->param( none_available
=> 1 );
504 $template->param( warnings
=> 1 );
505 $biblioloopiter{warn} = 1;
506 $biblioloopiter{none_avail
} = 1;
508 $template->param( hiddencount
=> $hiddencount);
510 push @bibitemloop, $biblioitem;
513 # existingreserves building
515 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
516 foreach my $res ( sort {
517 my $a_found = $a->{found
} || '';
518 my $b_found = $a->{found
} || '';
519 $a_found cmp $b_found;
523 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
528 selected
=> ( $i == $res->{priority
} ),
533 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
534 my $item = $res->{'itemnumber'};
535 $item = GetBiblioFromItemNumber
($item,undef);
537 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
538 $reserve{'biblionumber'}=$item->{'biblionumber'};
539 $reserve{'barcodenumber'} = $item->{'barcode'};
540 $reserve{'wbrcode'} = $res->{'branchcode'};
541 $reserve{'itemnumber'} = $res->{'itemnumber'};
542 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
543 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
544 $reserve{'atdestination'} = 1;
546 # set found to 1 if reserve is waiting for patron pickup
547 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
548 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
549 } elsif ($res->{priority
} > 0) {
550 if (defined($res->{itemnumber
})) {
551 my $item = GetItem
($res->{itemnumber
});
552 $reserve{'itemnumber'} = $res->{'itemnumber'};
553 $reserve{'barcodenumber'} = $item->{'barcode'};
554 $reserve{'item_level_hold'} = 1;
558 # get borrowers reserve info
559 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
560 if (C4
::Context
->preference('HidePatronName')){
561 $reserve{'hidename'} = 1;
562 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
564 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
565 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
566 $reserve{'date'} = format_date
( $res->{'reservedate'} );
567 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
568 $reserve{'biblionumber'} = $res->{'biblionumber'};
569 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
570 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
571 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
572 $reserve{'notes'} = $res->{'reservenotes'};
574 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
575 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
576 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
577 $reserve{'voldesc'} = $res->{'volumeddesc'};
578 $reserve{'ccode'} = $res->{'ccode'};
579 $reserve{'barcode'} = $res->{'barcode'};
580 $reserve{'priority'} = $res->{'priority'};
581 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
582 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
583 $reserve{'optionloop'} = \
@optionloop;
584 $reserve{'suspend'} = $res->{'suspend'};
585 $reserve{'suspend_until'} = $res->{'suspend_until'};
586 push( @reserveloop, \
%reserve );
589 # get the time for the form name...
593 CGIbranch
=> $CGIbranch,
596 fixedRank
=> $fixedRank,
601 optionloop
=> \
@optionloop,
602 bibitemloop
=> \
@bibitemloop,
603 itemdata_enumchron
=> $itemdata_enumchron,
605 biblionumber
=> $biblionumber,
606 findborrower
=> $findborrower,
607 title
=> $dat->{title
},
608 author
=> $dat->{author
},
610 C4
::Search
::enabled_staff_search_views
,
612 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
614 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
615 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
618 $template->param(CGIselectborrower
=> $CGIselectborrower) if defined $CGIselectborrower;
620 $biblioloopiter{biblionumber
} = $biblionumber;
621 $biblioloopiter{title
} = $dat->{title
};
622 $biblioloopiter{rank
} = $fixedRank;
623 $biblioloopiter{reserveloop
} = \
@reserveloop;
626 $template->param( reserveloop
=> \
@reserveloop );
629 push @biblioloop, \
%biblioloopiter;
632 $template->param( biblioloop
=> \
@biblioloop );
633 $template->param( biblionumbers
=> $biblionumbers );
636 $template->param( multi_hold
=> 1 );
639 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
640 $template->param( reserve_in_future
=> 1 );
644 SuspendHoldsIntranet
=> C4
::Context
->preference('SuspendHoldsIntranet'),
645 AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds'),
649 output_html_with_http_headers
$input, $cookie, $template->output;