(MT #1654) followup : corrects tablesorting for authorised_values and itemtypes
[koha.git] / reserve / request.pl
blob7e513648ebc198670dfe57a4f54d8e8588518c1a
1 #!/usr/bin/perl
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
12 # version.
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
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =head1 request.pl
24 script to place reserves/requests
26 =cut
28 use strict;
29 use warnings;
30 use C4::Branch; # GetBranches get_branchinfos_of
31 use CGI;
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
34 use C4::Output;
35 use C4::Auth;
36 use C4::Reserves;
37 use C4::Biblio;
38 use C4::Items;
39 use C4::Koha;
40 use C4::Circulation;
41 use C4::Dates qw/format_date/;
42 use C4::Members;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
46 my $sth;
47 my $input = new CGI;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
51 query => $input,
52 type => "intranet",
53 authnotrequired => 0,
54 flagsrequired => { reserveforothers => 'place_holds' },
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};
66 my @values;
67 my %label_of;
69 foreach my $branchcode (sort keys %{$branches} ) {
70 push @values, $branchcode;
71 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
73 my $CGIbranch = CGI::scrolling_list(
74 -name => 'pickup',
75 -id => 'pickup',
76 -values => \@values,
77 -default => $default,
78 -labels => \%label_of,
79 -size => 1,
80 -multiple => 0,
83 # Select borrowers infos
84 my $findborrower = $input->param('findborrower');
85 $findborrower = '' unless defined $findborrower;
86 $findborrower =~ s|,| |g;
87 my $cardnumber = $input->param('cardnumber') || '';
88 my $borrowerslist;
89 my $messageborrower;
90 my $warnings;
91 my $messages;
93 my $date = C4::Dates->today('iso');
94 my $action = $input->param('action');
96 if ( $action eq 'move' ) {
97 my $where = $input->param('where');
98 my $borrowernumber = $input->param('borrowernumber');
99 my $biblionumber = $input->param('biblionumber');
101 AlterPriority( $where, $borrowernumber, $biblionumber );
103 } elsif ( $action eq 'cancel' ) {
104 my $borrowernumber = $input->param('borrowernumber');
105 my $biblionumber = $input->param('biblionumber');
106 CancelReserve( $biblionumber, '', $borrowernumber );
107 } elsif ( $action eq 'setLowestPriority' ) {
108 my $borrowernumber = $input->param('borrowernumber');
109 my $biblionumber = $input->param('biblionumber');
110 ToggleLowestPriority( $borrowernumber, $biblionumber );
113 if ($findborrower) {
114 my ( $count, $borrowers ) =
115 SearchMember($findborrower, 'cardnumber', 'web' );
117 my @borrowers = @$borrowers;
119 if ( $#borrowers == -1 ) {
120 $input->param( 'findborrower', '' );
121 $messageborrower = "'$findborrower'";
123 elsif ( $#borrowers == 0 ) {
124 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
125 $cardnumber = $borrowers[0]->{'cardnumber'};
127 else {
128 $borrowerslist = \@borrowers;
132 if ($cardnumber) {
133 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
134 my $diffbranch;
135 my @getreservloop;
136 my $count_reserv = 0;
137 my $maxreserves;
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 ( $number_reserves > C4::Context->preference('maxreserves') ) {
146 $warnings = 1;
147 $maxreserves = 1;
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;
159 # check if the borrower make the reserv in a different branch
160 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
161 $messages = 1;
162 $diffbranch = 1;
165 $template->param(
166 borrowernumber => $borrowerinfo->{'borrowernumber'},
167 borrowersurname => $borrowerinfo->{'surname'},
168 borrowerfirstname => $borrowerinfo->{'firstname'},
169 borrowerstreetaddress => $borrowerinfo->{'address'},
170 borrowercity => $borrowerinfo->{'city'},
171 borrowerphone => $borrowerinfo->{'phone'},
172 borrowermobile => $borrowerinfo->{'mobile'},
173 borrowerfax => $borrowerinfo->{'fax'},
174 borrowerphonepro => $borrowerinfo->{'phonepro'},
175 borroweremail => $borrowerinfo->{'email'},
176 borroweremailpro => $borrowerinfo->{'emailpro'},
177 borrowercategory => $borrowerinfo->{'category'},
178 borrowerreservs => $count_reserv,
179 maxreserves => $maxreserves,
180 expiry => $expiry,
181 diffbranch => $diffbranch,
182 messages => $messages,
183 warnings => $warnings
187 $template->param( messageborrower => $messageborrower );
189 my $CGIselectborrower;
190 if ($borrowerslist) {
191 my @values;
192 my %labels;
194 foreach my $borrower (
195 sort {
196 $a->{surname}
197 . $a->{firstname} cmp $b->{surname}
198 . $b->{firstname}
199 } @{$borrowerslist}
202 push @values, $borrower->{cardnumber};
204 $labels{ $borrower->{cardnumber} } = 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 => 'cardnumber',
214 -values => \@values,
215 -labels => \%labels,
216 -size => 7,
217 -multiple => 0,
221 # FIXME launch another time GetMemberDetails perhaps until
222 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
224 my @biblionumbers = ();
225 my $biblionumbers = $input->param('biblionumbers');
226 if ($multihold) {
227 @biblionumbers = split '/', $biblionumbers;
228 } else {
229 push @biblionumbers, $input->param('biblionumber');
232 my @biblioloop = ();
233 foreach my $biblionumber (@biblionumbers) {
235 my %biblioloopiter = ();
236 my $maxreserves;
238 my $dat = GetBiblioData($biblionumber);
240 if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
241 $warnings = 1;
242 $maxreserves = 1;
244 # get existing reserves .....
245 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
246 my $totalcount = $count;
247 my $alreadyreserved;
249 foreach my $res (@$reserves) {
250 if ( defined $res->{found} && $res->{found} eq 'W' ) {
251 $count--;
254 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
255 $warnings = 1;
256 $alreadyreserved = 1;
257 $biblioloopiter{warn} = 1;
258 $biblioloopiter{alreadyres} = 1;
262 $template->param( alreadyreserved => $alreadyreserved,
263 messages => $messages,
264 warnings => $warnings,
265 maxreserves=>$maxreserves
269 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
270 # make priorities options
272 my @optionloop;
273 for ( 1 .. $count + 1 ) {
274 push(
275 @optionloop,
277 num => $_,
278 selected => ( $_ == $count + 1 ),
282 # adding a fixed value for priority options
283 my $fixedRank = $count+1;
285 my @branchcodes;
286 my %itemnumbers_of_biblioitem;
287 my @itemnumbers;
289 ## $items is array of 'item' table numbers
290 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
291 @itemnumbers = @$items;
293 else {
294 $template->param('noitems' => 1);
295 $biblioloopiter{noitems} = 1;
298 ## Hash of item number to 'item' table fields
299 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
301 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
302 ## when by definition all of the itemnumber have the same biblioitemnumber
303 foreach my $itemnumber (@itemnumbers) {
304 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
305 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
308 ## Should be same as biblionumber
309 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
311 my $notforloan_label_of = get_notforloan_label_of();
313 ## Hash of biblioitemnumber to 'biblioitem' table records
314 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
316 my @bibitemloop;
318 foreach my $biblioitemnumber (@biblioitemnumbers) {
319 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
320 my $num_available = 0;
321 my $num_override = 0;
323 $biblioitem->{description} =
324 $itemtypes->{ $biblioitem->{itemtype} }{description};
325 $biblioloopiter{description} = $biblioitem->{description};
326 $biblioloopiter{itypename} = $biblioitem->{description};
327 $biblioloopiter{imageurl} =
328 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
330 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
331 my $item = $iteminfos_of->{$itemnumber};
333 unless (C4::Context->preference('item-level_itypes')) {
334 $item->{itype} = $biblioitem->{itemtype};
337 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
338 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
339 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
341 # if the holdingbranch is different than the homebranch, we show the
342 # holdingbranch of the document too
343 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
344 $item->{holdingbranchname} =
345 $branches->{ $item->{holdingbranch} }{branchname};
348 # add information
349 $item->{itemcallnumber} = $item->{itemcallnumber};
351 # if the item is currently on loan, we display its return date and
352 # change the background color
353 my $issues= GetItemIssue($itemnumber);
354 if ( $issues->{'date_due'} ) {
355 $item->{date_due} = format_date($issues->{'date_due'});
356 $item->{backgroundcolor} = 'onloan';
359 # checking reserve
360 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
361 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
363 if ( defined $reservedate ) {
364 $item->{backgroundcolor} = 'reserved';
365 $item->{reservedate} = format_date($reservedate);
366 $item->{ReservedForBorrowernumber} = $reservedfor;
367 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
368 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
369 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
373 # Management of the notforloan document
374 if ( $item->{notforloan} ) {
375 $item->{backgroundcolor} = 'other';
376 $item->{notforloanvalue} =
377 $notforloan_label_of->{ $item->{notforloan} };
380 # Management of lost or long overdue items
381 if ( $item->{itemlost} ) {
383 # FIXME localized strings should never be in Perl code
384 $item->{message} =
385 $item->{itemlost} == 1 ? "(lost)"
386 : $item->{itemlost} == 2 ? "(long overdue)"
387 : "";
388 $item->{backgroundcolor} = 'other';
391 # Check the transit status
392 my ( $transfertwhen, $transfertfrom, $transfertto ) =
393 GetTransfers($itemnumber);
395 if ( defined $transfertwhen && $transfertwhen ne '' ) {
396 $item->{transfertwhen} = format_date($transfertwhen);
397 $item->{transfertfrom} =
398 $branches->{$transfertfrom}{branchname};
399 $item->{transfertto} = $branches->{$transfertto}{branchname};
400 $item->{nocancel} = 1;
403 # If there is no loan, return and transfer, we show a checkbox.
404 $item->{notforloan} = $item->{notforloan} || 0;
406 # if independent branches is on we need to check if the person can reserve
407 # for branches they arent logged in to
408 if ( C4::Context->preference("IndependantBranches") ) {
409 if (! C4::Context->preference("canreservefromotherbranches")){
410 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
411 my $userenv = C4::Context->userenv;
412 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
413 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
418 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
420 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
421 my $policy_holdallowed = 1;
423 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
425 if ( $branchitemrule->{'holdallowed'} == 0 ||
426 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
427 $policy_holdallowed = 0;
430 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber) ) {
431 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
432 $item->{override} = 1;
433 $num_override++;
434 } elsif ( $policy_holdallowed ) {
435 $item->{available} = 1;
436 $num_available++;
438 } elsif (C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
439 $item->{override} = 1;
440 $num_override++;
442 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
444 # FIXME: move this to a pm
445 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
446 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
447 while (my $wait_hashref = $sth2->fetchrow_hashref) {
448 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
450 push @{ $biblioitem->{itemloop} }, $item;
453 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
454 $template->param( override_required => 1 );
455 } elsif ( $num_available == 0 ) {
456 $template->param( none_available => 1 );
457 $template->param( warnings => 1 );
458 $biblioloopiter{warn} = 1;
459 $biblioloopiter{none_avail} = 1;
462 push @bibitemloop, $biblioitem;
465 # existingreserves building
466 my @reserveloop;
467 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
468 foreach my $res ( sort {
469 my $a_found = $a->{found} || '';
470 my $b_found = $a->{found} || '';
471 $a_found cmp $b_found;
472 } @$reserves ) {
473 my %reserve;
474 my @optionloop;
475 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
476 push(
477 @optionloop,
479 num => $i,
480 selected => ( $i == $res->{priority} ),
485 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
486 my $item = $res->{'itemnumber'};
487 $item = GetBiblioFromItemNumber($item,undef);
488 $reserve{'wait'}= 1;
489 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
490 $reserve{'biblionumber'}=$item->{'biblionumber'};
491 $reserve{'barcodenumber'} = $item->{'barcode'};
492 $reserve{'wbrcode'} = $res->{'branchcode'};
493 $reserve{'itemnumber'} = $res->{'itemnumber'};
494 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
495 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
496 $reserve{'atdestination'} = 1;
498 # set found to 1 if reserve is waiting for patron pickup
499 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
500 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
501 } elsif ($res->{priority} > 0) {
502 if (defined($res->{itemnumber})) {
503 my $item = GetItem($res->{itemnumber});
504 $reserve{'itemnumber'} = $res->{'itemnumber'};
505 $reserve{'barcodenumber'} = $item->{'barcode'};
506 $reserve{'item_level_hold'} = 1;
510 # get borrowers reserve info
511 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
512 if (C4::Context->preference('HidePatronName')){
513 $reserve{'hidename'} = 1;
514 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
516 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
517 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
518 $reserve{'date'} = format_date( $res->{'reservedate'} );
519 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
520 $reserve{'biblionumber'} = $res->{'biblionumber'};
521 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
522 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
523 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
524 $reserve{'notes'} = $res->{'reservenotes'};
525 $reserve{'wait'} =
526 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
527 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
528 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
529 $reserve{'voldesc'} = $res->{'volumeddesc'};
530 $reserve{'ccode'} = $res->{'ccode'};
531 $reserve{'barcode'} = $res->{'barcode'};
532 $reserve{'priority'} = $res->{'priority'};
533 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
534 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
535 $reserve{'optionloop'} = \@optionloop;
537 push( @reserveloop, \%reserve );
540 # get the time for the form name...
541 my $time = time();
543 $template->param(
544 CGIbranch => $CGIbranch,
546 time => $time,
547 fixedRank => $fixedRank,
550 # display infos
551 $template->param(
552 optionloop => \@optionloop,
553 bibitemloop => \@bibitemloop,
554 date => $date,
555 biblionumber => $biblionumber,
556 findborrower => $findborrower,
557 cardnumber => $cardnumber,
558 title => $dat->{title},
559 author => $dat->{author},
560 holdsview => 1,
561 C4::Search::enabled_staff_search_views,
563 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
564 $template->param(
565 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
566 borrower_branchcode => $borrowerinfo->{'branchcode'},
569 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
571 $biblioloopiter{biblionumber} = $biblionumber;
572 $biblioloopiter{title} = $dat->{title};
573 $biblioloopiter{rank} = $fixedRank;
574 $biblioloopiter{reserveloop} = \@reserveloop;
576 if (@reserveloop) {
577 $template->param( reserveloop => \@reserveloop );
581 push @biblioloop, \%biblioloopiter;
584 $template->param( biblioloop => \@biblioloop );
585 $template->param( biblionumbers => $biblionumbers );
586 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
588 if ($multihold) {
589 $template->param( multi_hold => 1 );
592 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
593 $template->param( reserve_in_future => 1 );
596 # printout the page
597 output_html_with_http_headers $input, $cookie, $template->output;