Bug 9247 - Add two more usage examples to the manpage for koha-mysql
[koha.git] / reserve / request.pl
blobc411ce9bdf918d1685aa74df4ad4af57d34e42b1
1 #!/usr/bin/perl
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
13 # version.
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.
23 =head1 request.pl
25 script to place reserves/requests
27 =cut
29 use strict;
30 use warnings;
31 use C4::Branch;
32 use CGI;
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
35 use C4::Output;
36 use C4::Auth;
37 use C4::Reserves;
38 use C4::Biblio;
39 use C4::Items;
40 use C4::Koha;
41 use C4::Circulation;
42 use C4::Dates qw/format_date/;
43 use C4::Members;
44 use C4::Search; # enabled_staff_search_views
45 use Koha::DateUtils;
47 my $dbh = C4::Context->dbh;
48 my $sth;
49 my $input = new CGI;
50 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user(
52 template_name => "reserve/request.tmpl",
53 query => $input,
54 type => "intranet",
55 authnotrequired => 0,
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 $userbranch = '';
69 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
70 $userbranch = C4::Context->userenv->{'branch'};
74 # Select borrowers infos
75 my $findborrower = $input->param('findborrower');
76 $findborrower = '' unless defined $findborrower;
77 $findborrower =~ s|,| |g;
78 my $borrowernumber_hold = $input->param('borrowernumber') || '';
79 my $messageborrower;
80 my $maxreserves;
82 my $date = C4::Dates->today('iso');
83 my $action = $input->param('action');
84 $action ||= q{};
86 if ( $action eq 'move' ) {
87 my $where = $input->param('where');
88 my $reserve_id = $input->param('reserve_id');
89 AlterPriority( $where, $reserve_id );
90 } elsif ( $action eq 'cancel' ) {
91 my $reserve_id = $input->param('reserve_id');
92 CancelReserve({ reserve_id => $reserve_id });
93 } elsif ( $action eq 'setLowestPriority' ) {
94 my $reserve_id = $input->param('reserve_id');
95 ToggleLowestPriority( $reserve_id );
96 } elsif ( $action eq 'toggleSuspend' ) {
97 my $reserve_id = $input->param('reserve_id');
98 my $suspend_until = $input->param('suspend_until');
99 ToggleSuspend( $reserve_id, $suspend_until );
102 if ($findborrower) {
103 my $borrowers = Search($findborrower, 'cardnumber');
105 if ($borrowers && @$borrowers) {
106 if ( @$borrowers == 1 ) {
107 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
109 else {
110 $template->param( borrower_list => sort_borrowerlist($borrowers));
112 } else {
113 $messageborrower = "'$findborrower'";
117 # If we have the borrowernumber because we've performed an action, then we
118 # don't want to try to place another reserve.
119 if ($borrowernumber_hold && !$action) {
120 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
121 my $diffbranch;
122 my @getreservloop;
123 my $count_reserv = 0;
125 # we check the reserves of the borrower, and if he can reserv a document
126 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
128 my $number_reserves =
129 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
131 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
132 $maxreserves = 1;
135 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
136 my $expiry_date = $borrowerinfo->{dateexpiry};
137 my $expiry = 0; # flag set if patron account has expired
138 if ($expiry_date and $expiry_date ne '0000-00-00' and
139 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
140 $expiry = 1;
143 # check if the borrower make the reserv in a different branch
144 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
145 $diffbranch = 1;
148 $template->param(
149 borrowernumber => $borrowerinfo->{'borrowernumber'},
150 borrowersurname => $borrowerinfo->{'surname'},
151 borrowerfirstname => $borrowerinfo->{'firstname'},
152 borrowerstreetaddress => $borrowerinfo->{'address'},
153 borrowercity => $borrowerinfo->{'city'},
154 borrowerphone => $borrowerinfo->{'phone'},
155 borrowermobile => $borrowerinfo->{'mobile'},
156 borrowerfax => $borrowerinfo->{'fax'},
157 borrowerphonepro => $borrowerinfo->{'phonepro'},
158 borroweremail => $borrowerinfo->{'email'},
159 borroweremailpro => $borrowerinfo->{'emailpro'},
160 borrowercategory => $borrowerinfo->{'category'},
161 borrowerreservs => $count_reserv,
162 cardnumber => $borrowerinfo->{'cardnumber'},
163 expiry => $expiry,
164 diffbranch => $diffbranch,
168 $template->param( messageborrower => $messageborrower );
170 # FIXME launch another time GetMember perhaps until
171 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
173 my @biblionumbers = ();
174 my $biblionumbers = $input->param('biblionumbers');
175 if ($multihold) {
176 @biblionumbers = split '/', $biblionumbers;
177 } else {
178 push @biblionumbers, $input->param('biblionumber');
181 my $itemdata_enumchron = 0;
182 my @biblioloop = ();
183 foreach my $biblionumber (@biblionumbers) {
185 my %biblioloopiter = ();
187 my $dat = GetBiblioData($biblionumber);
189 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
190 $maxreserves = 1;
193 my $alreadypossession;
194 if (not C4::Context->preference('AllowHoldsOnPatronsPossessions') and CheckIfIssuedToPatron($borrowerinfo->{borrowernumber},$biblionumber)) {
195 $alreadypossession = 1;
198 # get existing reserves .....
199 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
200 my $totalcount = $count;
201 my $holds_count = 0;
202 my $alreadyreserved = 0;
204 foreach my $res (@$reserves) {
205 if ( defined $res->{found} && $res->{found} eq 'W' ) {
206 $count--;
209 if ( defined $borrowerinfo && defined($borrowerinfo->{borrowernumber}) && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
210 $holds_count++;
214 if ( $holds_count ) {
215 $alreadyreserved = 1;
216 $biblioloopiter{warn} = 1;
217 $biblioloopiter{alreadyres} = 1;
220 $template->param(
221 alreadyreserved => $alreadyreserved,
222 alreadypossession => $alreadypossession,
225 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
226 # make priorities options
228 my @optionloop;
229 for ( 1 .. $count + 1 ) {
230 push(
231 @optionloop,
233 num => $_,
234 selected => ( $_ == $count + 1 ),
238 # adding a fixed value for priority options
239 my $fixedRank = $count+1;
241 my @branchcodes;
242 my %itemnumbers_of_biblioitem;
243 my @itemnumbers;
245 ## $items is array of 'item' table numbers
246 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
247 @itemnumbers = @$items;
249 my @hostitems = get_hostitemnumbers_of($biblionumber);
250 if (@hostitems){
251 $template->param('hostitemsflag' => 1);
252 push(@itemnumbers, @hostitems);
255 if (!@itemnumbers) {
256 $template->param('noitems' => 1);
257 $biblioloopiter{noitems} = 1;
260 ## Hash of item number to 'item' table fields
261 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
263 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
264 ## when by definition all of the itemnumber have the same biblioitemnumber
265 foreach my $itemnumber (@itemnumbers) {
266 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
267 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
270 ## Should be same as biblionumber
271 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
273 my $notforloan_label_of = get_notforloan_label_of();
275 ## Hash of biblioitemnumber to 'biblioitem' table records
276 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
278 my @bibitemloop;
280 foreach my $biblioitemnumber (@biblioitemnumbers) {
281 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
282 my $num_available = 0;
283 my $num_override = 0;
284 my $hiddencount = 0;
286 $biblioitem->{description} =
287 $itemtypes->{ $biblioitem->{itemtype} }{description};
288 if($biblioitem->{biblioitemnumber} ne $biblionumber){
289 $biblioitem->{hostitemsflag}=1;
291 $biblioloopiter{description} = $biblioitem->{description};
292 $biblioloopiter{itypename} = $biblioitem->{description};
293 $biblioloopiter{imageurl} =
294 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
296 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
297 my $item = $iteminfos_of->{$itemnumber};
299 unless (C4::Context->preference('item-level_itypes')) {
300 $item->{itype} = $biblioitem->{itemtype};
303 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
304 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
305 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
307 # if the holdingbranch is different than the homebranch, we show the
308 # holdingbranch of the document too
309 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
310 $item->{holdingbranchname} =
311 $branches->{ $item->{holdingbranch} }{branchname};
314 if($item->{biblionumber} ne $biblionumber){
315 $item->{hostitemsflag}=1;
316 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
319 # add information
320 $item->{itemcallnumber} = $item->{itemcallnumber};
322 # if the item is currently on loan, we display its return date and
323 # change the background color
324 my $issues= GetItemIssue($itemnumber);
325 if ( $issues->{'date_due'} ) {
326 $item->{date_due} = format_sqldatetime($issues->{date_due});
327 $item->{backgroundcolor} = 'onloan';
330 # checking reserve
331 my ($reservedate,$reservedfor,$expectedAt,$reserve_id) = GetReservesFromItemnumber($itemnumber);
332 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
334 if ( defined $reservedate ) {
335 $item->{backgroundcolor} = 'reserved';
336 $item->{reservedate} = format_date($reservedate);
337 $item->{ReservedForBorrowernumber} = $reservedfor;
338 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
339 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
340 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
344 # Management of the notforloan document
345 if ( $item->{notforloan} ) {
346 $item->{backgroundcolor} = 'other';
347 $item->{notforloanvalue} =
348 $notforloan_label_of->{ $item->{notforloan} };
351 # Management of lost or long overdue items
352 if ( $item->{itemlost} ) {
354 # FIXME localized strings should never be in Perl code
355 $item->{message} =
356 $item->{itemlost} == 1 ? "(lost)"
357 : $item->{itemlost} == 2 ? "(long overdue)"
358 : "";
359 $item->{backgroundcolor} = 'other';
360 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
361 $item->{hide} = 1;
362 $hiddencount++;
366 # Check the transit status
367 my ( $transfertwhen, $transfertfrom, $transfertto ) =
368 GetTransfers($itemnumber);
370 if ( defined $transfertwhen && $transfertwhen ne '' ) {
371 $item->{transfertwhen} = format_date($transfertwhen);
372 $item->{transfertfrom} =
373 $branches->{$transfertfrom}{branchname};
374 $item->{transfertto} = $branches->{$transfertto}{branchname};
375 $item->{nocancel} = 1;
378 # If there is no loan, return and transfer, we show a checkbox.
379 $item->{notforloan} = $item->{notforloan} || 0;
381 # if independent branches is on we need to check if the person can reserve
382 # for branches they arent logged in to
383 if ( C4::Context->preference("IndependentBranches") ) {
384 if (! C4::Context->preference("canreservefromotherbranches")){
385 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
386 my $userenv = C4::Context->userenv;
387 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
388 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
393 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
395 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
396 my $policy_holdallowed = 1;
398 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
400 if ( $branchitemrule->{'holdallowed'} == 0 ||
401 ( $branchitemrule->{'holdallowed'} == 1 &&
402 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
403 $policy_holdallowed = 0;
406 if (
407 $policy_holdallowed
408 && !$item->{cantreserve}
409 && IsAvailableForItemLevelRequest($itemnumber)
410 && CanItemBeReserved(
411 $borrowerinfo->{borrowernumber}, $itemnumber
415 $item->{available} = 1;
416 $num_available++;
418 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
420 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
421 $item->{override} = 1;
422 $num_override++;
425 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
427 # FIXME: move this to a pm
428 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
429 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
430 while (my $wait_hashref = $sth2->fetchrow_hashref) {
431 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
434 # Show serial enumeration when needed
435 if ($item->{enumchron}) {
436 $itemdata_enumchron = 1;
439 push @{ $biblioitem->{itemloop} }, $item;
442 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
443 $template->param( override_required => 1 );
444 } elsif ( $num_available == 0 ) {
445 $template->param( none_available => 1 );
446 $biblioloopiter{warn} = 1;
447 $biblioloopiter{none_avail} = 1;
449 $template->param( hiddencount => $hiddencount);
451 push @bibitemloop, $biblioitem;
454 # existingreserves building
455 my @reserveloop;
456 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
457 foreach my $res ( sort {
458 my $a_found = $a->{found} || '';
459 my $b_found = $a->{found} || '';
460 $a_found cmp $b_found;
461 } @$reserves ) {
462 my %reserve;
463 my @optionloop;
464 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
465 push(
466 @optionloop,
468 num => $i,
469 selected => ( $i == $res->{priority} ),
474 if ( defined $res->{'found'} && ($res->{'found'} eq 'W' || $res->{'found'} eq 'T' )) {
475 my $item = $res->{'itemnumber'};
476 $item = GetBiblioFromItemNumber($item,undef);
477 $reserve{'wait'}= 1;
478 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
479 $reserve{'biblionumber'}=$item->{'biblionumber'};
480 $reserve{'barcodenumber'} = $item->{'barcode'};
481 $reserve{'wbrcode'} = $res->{'branchcode'};
482 $reserve{'itemnumber'} = $res->{'itemnumber'};
483 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
484 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
485 $reserve{'atdestination'} = 1;
487 # set found to 1 if reserve is waiting for patron pickup
488 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
489 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
490 } elsif ($res->{priority} > 0) {
491 if (defined($res->{itemnumber})) {
492 my $item = GetItem($res->{itemnumber});
493 $reserve{'itemnumber'} = $res->{'itemnumber'};
494 $reserve{'barcodenumber'} = $item->{'barcode'};
495 $reserve{'item_level_hold'} = 1;
499 # get borrowers reserve info
500 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
501 if (C4::Context->preference('HidePatronName')){
502 $reserve{'hidename'} = 1;
503 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
505 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
506 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
507 $reserve{'date'} = format_date( $res->{'reservedate'} );
508 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
509 $reserve{'biblionumber'} = $res->{'biblionumber'};
510 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
511 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
512 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
513 $reserve{'notes'} = $res->{'reservenotes'};
514 $reserve{'wait'} =
515 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
516 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
517 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
518 $reserve{'voldesc'} = $res->{'volumeddesc'};
519 $reserve{'ccode'} = $res->{'ccode'};
520 $reserve{'barcode'} = $res->{'barcode'};
521 $reserve{'priority'} = $res->{'priority'};
522 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
523 $reserve{'optionloop'} = \@optionloop;
524 $reserve{'suspend'} = $res->{'suspend'};
525 $reserve{'suspend_until'} = $res->{'suspend_until'};
526 $reserve{'reserve_id'} = $res->{'reserve_id'};
528 if ( C4::Context->preference('IndependentBranches') && $flags->{'superlibrarian'} != 1 ) {
529 $reserve{'branchloop'} = [ GetBranchDetail($res->{'branchcode'}) ];
530 } else {
531 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
534 push( @reserveloop, \%reserve );
537 # get the time for the form name...
538 my $time = time();
540 $template->param(
541 branchloop => GetBranchesLoop($userbranch),
542 time => $time,
543 fixedRank => $fixedRank,
546 # display infos
547 $template->param(
548 optionloop => \@optionloop,
549 bibitemloop => \@bibitemloop,
550 itemdata_enumchron => $itemdata_enumchron,
551 date => $date,
552 biblionumber => $biblionumber,
553 findborrower => $findborrower,
554 title => $dat->{title},
555 author => $dat->{author},
556 holdsview => 1,
557 C4::Search::enabled_staff_search_views,
559 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
560 $template->param(
561 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
562 borrower_branchcode => $borrowerinfo->{'branchcode'},
566 $biblioloopiter{biblionumber} = $biblionumber;
567 $biblioloopiter{title} = $dat->{title};
568 $biblioloopiter{rank} = $fixedRank;
569 $biblioloopiter{reserveloop} = \@reserveloop;
571 if (@reserveloop) {
572 $template->param( reserveloop => \@reserveloop );
575 push @biblioloop, \%biblioloopiter;
578 $template->param( biblioloop => \@biblioloop );
579 $template->param( biblionumbers => $biblionumbers );
580 $template->param( maxreserves => $maxreserves );
582 if ($multihold) {
583 $template->param( multi_hold => 1 );
586 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
587 $template->param( reserve_in_future => 1 );
590 $template->param(
591 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
592 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
595 # printout the page
596 output_html_with_http_headers $input, $cookie, $template->output;
598 sub sort_borrowerlist {
599 my $borrowerslist = shift;
600 my $ref = [];
601 push @{$ref}, sort {
602 uc( $a->{surname} . $a->{firstname} ) cmp
603 uc( $b->{surname} . $b->{firstname} )
604 } @{$borrowerslist};
605 return $ref;