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 );
113 } elsif ( $action eq 'toggleSuspend' ) {
114 my $borrowernumber = $input->param('borrowernumber');
115 my $biblionumber = $input->param('biblionumber');
116 ToggleSuspend
( $borrowernumber, $biblionumber );
120 my $borrowers = Search
($findborrower, 'cardnumber');
122 if ($borrowers && @
$borrowers) {
123 if ( @
$borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
127 $borrowerslist = $borrowers;
130 $messageborrower = "'$findborrower'";
134 # If we have the borrowernumber because we've performed an action, then we
135 # don't want to try to place another reserve.
136 if ($borrowernumber_hold && !$action) {
137 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
140 my $count_reserv = 0;
143 # we check the reserves of the borrower, and if he can reserv a document
144 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
146 my $number_reserves =
147 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
149 if ( C4
::Context
->preference('maxreserves') && ($number_reserves >= C4
::Context
->preference('maxreserves')) ) {
154 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
155 my $expiry_date = $borrowerinfo->{dateexpiry
};
156 my $expiry = 0; # flag set if patron account has expired
157 if ($expiry_date and $expiry_date ne '0000-00-00' and
158 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
159 $messages = $expiry = 1;
162 # check if the borrower make the reserv in a different branch
163 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
169 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
170 borrowersurname
=> $borrowerinfo->{'surname'},
171 borrowerfirstname
=> $borrowerinfo->{'firstname'},
172 borrowerstreetaddress
=> $borrowerinfo->{'address'},
173 borrowercity
=> $borrowerinfo->{'city'},
174 borrowerphone
=> $borrowerinfo->{'phone'},
175 borrowermobile
=> $borrowerinfo->{'mobile'},
176 borrowerfax
=> $borrowerinfo->{'fax'},
177 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
178 borroweremail
=> $borrowerinfo->{'email'},
179 borroweremailpro
=> $borrowerinfo->{'emailpro'},
180 borrowercategory
=> $borrowerinfo->{'category'},
181 borrowerreservs
=> $count_reserv,
182 cardnumber
=> $borrowerinfo->{'cardnumber'},
183 maxreserves
=> $maxreserves,
185 diffbranch
=> $diffbranch,
186 messages
=> $messages,
187 warnings
=> $warnings
191 $template->param( messageborrower
=> $messageborrower );
193 my $CGIselectborrower;
194 if ($borrowerslist) {
198 foreach my $borrower (
201 . $a->{firstname
}) cmp uc($b->{surname
}
206 push @values, $borrower->{borrowernumber
};
208 $labels{ $borrower->{borrowernumber
} } = sprintf(
209 '%s, %s ... (%s - %s) ... %s',
210 $borrower->{surname
} ||'', $borrower->{firstname
} || '',
211 $borrower->{cardnumber
} || '', $borrower->{categorycode
} || '',
212 $borrower->{address
} || '',
216 $CGIselectborrower = CGI
::scrolling_list
(
217 -name
=> 'borrowernumber',
225 # FIXME launch another time GetMember perhaps until
226 my $borrowerinfo = GetMember
( borrowernumber
=> $borrowernumber_hold );
228 my @biblionumbers = ();
229 my $biblionumbers = $input->param('biblionumbers');
231 @biblionumbers = split '/', $biblionumbers;
233 push @biblionumbers, $input->param('biblionumber');
236 my $itemdata_enumchron = 0;
238 foreach my $biblionumber (@biblionumbers) {
240 my %biblioloopiter = ();
243 my $dat = GetBiblioData
($biblionumber);
245 unless ( CanBookBeReserved
($borrowerinfo->{borrowernumber
}, $biblionumber) ) {
249 # get existing reserves .....
250 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
251 my $totalcount = $count;
254 foreach my $res (@
$reserves) {
255 if ( defined $res->{found
} && $res->{found
} eq 'W' ) {
259 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
}) ) {
261 $alreadyreserved = 1;
262 $biblioloopiter{warn} = 1;
263 $biblioloopiter{alreadyres
} = 1;
267 $template->param( alreadyreserved
=> $alreadyreserved,
268 messages
=> $messages,
269 warnings
=> $warnings,
270 maxreserves
=>$maxreserves
274 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
275 # make priorities options
278 for ( 1 .. $count + 1 ) {
283 selected
=> ( $_ == $count + 1 ),
287 # adding a fixed value for priority options
288 my $fixedRank = $count+1;
291 my %itemnumbers_of_biblioitem;
294 ## $items is array of 'item' table numbers
295 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
296 @itemnumbers = @
$items;
298 my @hostitems = get_hostitemnumbers_of
($biblionumber);
300 $template->param('hostitemsflag' => 1);
301 push(@itemnumbers, @hostitems);
305 $template->param('noitems' => 1);
306 $biblioloopiter{noitems
} = 1;
309 ## Hash of item number to 'item' table fields
310 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
312 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
313 ## when by definition all of the itemnumber have the same biblioitemnumber
314 foreach my $itemnumber (@itemnumbers) {
315 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
316 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
319 ## Should be same as biblionumber
320 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
322 my $notforloan_label_of = get_notforloan_label_of
();
324 ## Hash of biblioitemnumber to 'biblioitem' table records
325 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
329 foreach my $biblioitemnumber (@biblioitemnumbers) {
330 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
331 my $num_available = 0;
332 my $num_override = 0;
335 $biblioitem->{description
} =
336 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
337 if($biblioitem->{biblioitemnumber
} ne $biblionumber){
338 $biblioitem->{hostitemsflag
}=1;
340 $biblioloopiter{description
} = $biblioitem->{description
};
341 $biblioloopiter{itypename
} = $biblioitem->{description
};
342 $biblioloopiter{imageurl
} =
343 getitemtypeimagelocation
('intranet', $itemtypes->{$biblioitem->{itemtype
}}{imageurl
});
345 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
346 my $item = $iteminfos_of->{$itemnumber};
348 unless (C4
::Context
->preference('item-level_itypes')) {
349 $item->{itype
} = $biblioitem->{itemtype
};
352 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
353 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
354 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
356 # if the holdingbranch is different than the homebranch, we show the
357 # holdingbranch of the document too
358 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
359 $item->{holdingbranchname
} =
360 $branches->{ $item->{holdingbranch
} }{branchname
};
363 if($item->{biblionumber
} ne $biblionumber){
364 $item->{hostitemsflag
}=1;
365 $item->{hosttitle
} = GetBiblioData
($item->{biblionumber
})->{title
};
369 $item->{itemcallnumber
} = $item->{itemcallnumber
};
371 # if the item is currently on loan, we display its return date and
372 # change the background color
373 my $issues= GetItemIssue
($itemnumber);
374 if ( $issues->{'date_due'} ) {
375 $item->{date_due
} = format_sqldatetime
($issues->{date_due
});
376 $item->{backgroundcolor
} = 'onloan';
380 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
381 my $ItemBorrowerReserveInfo = GetMember
( borrowernumber
=> $reservedfor );
383 if ( defined $reservedate ) {
384 $item->{backgroundcolor
} = 'reserved';
385 $item->{reservedate
} = format_date
($reservedate);
386 $item->{ReservedForBorrowernumber
} = $reservedfor;
387 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
388 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
389 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
393 # Management of the notforloan document
394 if ( $item->{notforloan
} ) {
395 $item->{backgroundcolor
} = 'other';
396 $item->{notforloanvalue
} =
397 $notforloan_label_of->{ $item->{notforloan
} };
400 # Management of lost or long overdue items
401 if ( $item->{itemlost
} ) {
403 # FIXME localized strings should never be in Perl code
405 $item->{itemlost
} == 1 ?
"(lost)"
406 : $item->{itemlost
} == 2 ?
"(long overdue)"
408 $item->{backgroundcolor
} = 'other';
409 if (GetHideLostItemsPreference
($borrowernumber) && !$showallitems) {
415 # Check the transit status
416 my ( $transfertwhen, $transfertfrom, $transfertto ) =
417 GetTransfers
($itemnumber);
419 if ( defined $transfertwhen && $transfertwhen ne '' ) {
420 $item->{transfertwhen
} = format_date
($transfertwhen);
421 $item->{transfertfrom
} =
422 $branches->{$transfertfrom}{branchname
};
423 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
424 $item->{nocancel
} = 1;
427 # If there is no loan, return and transfer, we show a checkbox.
428 $item->{notforloan
} = $item->{notforloan
} || 0;
430 # if independent branches is on we need to check if the person can reserve
431 # for branches they arent logged in to
432 if ( C4
::Context
->preference("IndependantBranches") ) {
433 if (! C4
::Context
->preference("canreservefromotherbranches")){
434 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
435 my $userenv = C4
::Context
->userenv;
436 if ( ($userenv) && ( $userenv->{flags
} % 2 != 1 ) ) {
437 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
442 my $branch = C4
::Circulation
::_GetCircControlBranch
($item, $borrowerinfo);
444 my $branchitemrule = GetBranchItemRule
( $branch, $item->{'itype'} );
445 my $policy_holdallowed = 1;
447 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
449 if ( $branchitemrule->{'holdallowed'} == 0 ||
450 ( $branchitemrule->{'holdallowed'} == 1 &&
451 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
452 $policy_holdallowed = 0;
457 && !$item->{cantreserve
}
458 && IsAvailableForItemLevelRequest
($itemnumber)
459 && CanItemBeReserved
(
460 $borrowerinfo->{borrowernumber
}, $itemnumber
464 $item->{available
} = 1;
467 elsif ( C4
::Context
->preference('AllowHoldPolicyOverride') ) {
469 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
470 $item->{override
} = 1;
474 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
476 # FIXME: move this to a pm
477 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
478 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
479 while (my $wait_hashref = $sth2->fetchrow_hashref) {
480 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
483 # Show serial enumeration when needed
484 if ($item->{enumchron
}) {
485 $itemdata_enumchron = 1;
488 push @
{ $biblioitem->{itemloop
} }, $item;
491 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
492 $template->param( override_required
=> 1 );
493 } elsif ( $num_available == 0 ) {
494 $template->param( none_available
=> 1 );
495 $template->param( warnings
=> 1 );
496 $biblioloopiter{warn} = 1;
497 $biblioloopiter{none_avail
} = 1;
499 $template->param( hiddencount
=> $hiddencount);
501 push @bibitemloop, $biblioitem;
504 # existingreserves building
506 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber,1);
507 foreach my $res ( sort {
508 my $a_found = $a->{found
} || '';
509 my $b_found = $a->{found
} || '';
510 $a_found cmp $b_found;
514 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
519 selected
=> ( $i == $res->{priority
} ),
524 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
525 my $item = $res->{'itemnumber'};
526 $item = GetBiblioFromItemNumber
($item,undef);
528 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
529 $reserve{'biblionumber'}=$item->{'biblionumber'};
530 $reserve{'barcodenumber'} = $item->{'barcode'};
531 $reserve{'wbrcode'} = $res->{'branchcode'};
532 $reserve{'itemnumber'} = $res->{'itemnumber'};
533 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
534 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
535 $reserve{'atdestination'} = 1;
537 # set found to 1 if reserve is waiting for patron pickup
538 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
539 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
540 } elsif ($res->{priority
} > 0) {
541 if (defined($res->{itemnumber
})) {
542 my $item = GetItem
($res->{itemnumber
});
543 $reserve{'itemnumber'} = $res->{'itemnumber'};
544 $reserve{'barcodenumber'} = $item->{'barcode'};
545 $reserve{'item_level_hold'} = 1;
549 # get borrowers reserve info
550 my $reserveborrowerinfo = GetMember
( borrowernumber
=> $res->{'borrowernumber'} );
551 if (C4
::Context
->preference('HidePatronName')){
552 $reserve{'hidename'} = 1;
553 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
555 $reserve{'expirationdate'} = format_date
( $res->{'expirationdate'} )
556 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
557 $reserve{'date'} = format_date
( $res->{'reservedate'} );
558 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
559 $reserve{'biblionumber'} = $res->{'biblionumber'};
560 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
561 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
562 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
563 $reserve{'notes'} = $res->{'reservenotes'};
565 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
566 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
567 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
568 $reserve{'voldesc'} = $res->{'volumeddesc'};
569 $reserve{'ccode'} = $res->{'ccode'};
570 $reserve{'barcode'} = $res->{'barcode'};
571 $reserve{'priority'} = $res->{'priority'};
572 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
573 $reserve{'branchloop'} = GetBranchesLoop
($res->{'branchcode'});
574 $reserve{'optionloop'} = \
@optionloop;
575 $reserve{'suspend'} = $res->{'suspend'};
576 $reserve{'suspend_until'} = C4
::Dates
->new( $res->{'suspend_until'}, "iso")->output("syspref");
577 push( @reserveloop, \
%reserve );
580 # get the time for the form name...
584 CGIbranch
=> $CGIbranch,
587 fixedRank
=> $fixedRank,
592 optionloop
=> \
@optionloop,
593 bibitemloop
=> \
@bibitemloop,
594 itemdata_enumchron
=> $itemdata_enumchron,
596 biblionumber
=> $biblionumber,
597 findborrower
=> $findborrower,
598 title
=> $dat->{title
},
599 author
=> $dat->{author
},
601 C4
::Search
::enabled_staff_search_views
,
603 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
605 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
606 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
609 $template->param(CGIselectborrower
=> $CGIselectborrower) if defined $CGIselectborrower;
611 $biblioloopiter{biblionumber
} = $biblionumber;
612 $biblioloopiter{title
} = $dat->{title
};
613 $biblioloopiter{rank
} = $fixedRank;
614 $biblioloopiter{reserveloop
} = \
@reserveloop;
617 $template->param( reserveloop
=> \
@reserveloop );
620 push @biblioloop, \
%biblioloopiter;
623 $template->param( biblioloop
=> \
@biblioloop );
624 $template->param( biblionumbers
=> $biblionumbers );
625 $template->param( DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar() );
628 $template->param( multi_hold
=> 1 );
631 if ( C4
::Context
->preference( 'AllowHoldDateInFuture' ) ) {
632 $template->param( reserve_in_future
=> 1 );
635 $template->param( AutoResumeSuspendedHolds
=> C4
::Context
->preference('AutoResumeSuspendedHolds') );
638 output_html_with_http_headers
$input, $cookie, $template->output;