Bug 8851 - strange blue bar above serials results
[koha.git] / reserve / request.pl
blobc34a61482096b370affdc62a4a0877d955bf8221
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 ) = 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 $default = C4::Context->userenv->{branch};
69 my @values;
70 my %label_of;
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(
77 -name => 'pickup',
78 -id => 'pickup',
79 -values => \@values,
80 -default => $default,
81 -labels => \%label_of,
82 -size => 1,
83 -multiple => 0,
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') || '';
91 my $borrowerslist;
92 my $messageborrower;
93 my $warnings;
94 my $messages;
96 my $date = C4::Dates->today('iso');
97 my $action = $input->param('action');
98 $action ||= q{};
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 );
120 if ($findborrower) {
121 my $borrowers = Search($findborrower, 'cardnumber');
123 if ($borrowers && @$borrowers) {
124 if ( @$borrowers == 1 ) {
125 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
127 else {
128 $borrowerslist = $borrowers;
130 } else {
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 );
139 my $diffbranch;
140 my @getreservloop;
141 my $count_reserv = 0;
142 my $maxreserves;
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')) ) {
151 $warnings = 1;
152 $maxreserves = 1;
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'} ) {
165 $messages = 1;
166 $diffbranch = 1;
169 $template->param(
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,
185 expiry => $expiry,
186 diffbranch => $diffbranch,
187 messages => $messages,
188 warnings => $warnings
192 $template->param( messageborrower => $messageborrower );
194 my $CGIselectborrower;
195 if ($borrowerslist) {
196 my @values;
197 my %labels;
199 foreach my $borrower (
200 sort {
201 uc($a->{surname}
202 . $a->{firstname}) cmp uc($b->{surname}
203 . $b->{firstname})
204 } @{$borrowerslist}
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',
219 -values => \@values,
220 -labels => \%labels,
221 -size => 7,
222 -multiple => 0,
226 # FIXME launch another time GetMember perhaps until
227 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
229 my @biblionumbers = ();
230 my $biblionumbers = $input->param('biblionumbers');
231 if ($multihold) {
232 @biblionumbers = split '/', $biblionumbers;
233 } else {
234 push @biblionumbers, $input->param('biblionumber');
237 my $itemdata_enumchron = 0;
238 my @biblioloop = ();
239 foreach my $biblionumber (@biblionumbers) {
241 my %biblioloopiter = ();
242 my $maxreserves;
244 my $dat = GetBiblioData($biblionumber);
246 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
247 $warnings = 1;
248 $maxreserves = 1;
250 # get existing reserves .....
251 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
252 my $totalcount = $count;
253 my $alreadyreserved;
255 foreach my $res (@$reserves) {
256 if ( defined $res->{found} && $res->{found} eq 'W' ) {
257 $count--;
260 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
261 $warnings = 1;
262 $alreadyreserved = 1;
263 $biblioloopiter{warn} = 1;
264 $biblioloopiter{alreadyres} = 1;
268 $template->param( alreadyreserved => $alreadyreserved,
269 messages => $messages,
270 warnings => $warnings,
271 maxreserves=>$maxreserves
275 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
276 # make priorities options
278 my @optionloop;
279 for ( 1 .. $count + 1 ) {
280 push(
281 @optionloop,
283 num => $_,
284 selected => ( $_ == $count + 1 ),
288 # adding a fixed value for priority options
289 my $fixedRank = $count+1;
291 my @branchcodes;
292 my %itemnumbers_of_biblioitem;
293 my @itemnumbers;
295 ## $items is array of 'item' table numbers
296 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
297 @itemnumbers = @$items;
299 my @hostitems = get_hostitemnumbers_of($biblionumber);
300 if (@hostitems){
301 $template->param('hostitemsflag' => 1);
302 push(@itemnumbers, @hostitems);
305 if (!@itemnumbers) {
306 $template->param('noitems' => 1);
307 $biblioloopiter{noitems} = 1;
310 ## Hash of item number to 'item' table fields
311 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
313 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
314 ## when by definition all of the itemnumber have the same biblioitemnumber
315 foreach my $itemnumber (@itemnumbers) {
316 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
317 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
320 ## Should be same as biblionumber
321 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
323 my $notforloan_label_of = get_notforloan_label_of();
325 ## Hash of biblioitemnumber to 'biblioitem' table records
326 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
328 my @bibitemloop;
330 foreach my $biblioitemnumber (@biblioitemnumbers) {
331 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
332 my $num_available = 0;
333 my $num_override = 0;
334 my $hiddencount = 0;
336 $biblioitem->{description} =
337 $itemtypes->{ $biblioitem->{itemtype} }{description};
338 if($biblioitem->{biblioitemnumber} ne $biblionumber){
339 $biblioitem->{hostitemsflag}=1;
341 $biblioloopiter{description} = $biblioitem->{description};
342 $biblioloopiter{itypename} = $biblioitem->{description};
343 $biblioloopiter{imageurl} =
344 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
346 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
347 my $item = $iteminfos_of->{$itemnumber};
349 unless (C4::Context->preference('item-level_itypes')) {
350 $item->{itype} = $biblioitem->{itemtype};
353 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
354 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
355 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
357 # if the holdingbranch is different than the homebranch, we show the
358 # holdingbranch of the document too
359 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
360 $item->{holdingbranchname} =
361 $branches->{ $item->{holdingbranch} }{branchname};
364 if($item->{biblionumber} ne $biblionumber){
365 $item->{hostitemsflag}=1;
366 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
369 # add information
370 $item->{itemcallnumber} = $item->{itemcallnumber};
372 # if the item is currently on loan, we display its return date and
373 # change the background color
374 my $issues= GetItemIssue($itemnumber);
375 if ( $issues->{'date_due'} ) {
376 $item->{date_due} = format_sqldatetime($issues->{date_due});
377 $item->{backgroundcolor} = 'onloan';
380 # checking reserve
381 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
382 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
384 if ( defined $reservedate ) {
385 $item->{backgroundcolor} = 'reserved';
386 $item->{reservedate} = format_date($reservedate);
387 $item->{ReservedForBorrowernumber} = $reservedfor;
388 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
389 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
390 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
394 # Management of the notforloan document
395 if ( $item->{notforloan} ) {
396 $item->{backgroundcolor} = 'other';
397 $item->{notforloanvalue} =
398 $notforloan_label_of->{ $item->{notforloan} };
401 # Management of lost or long overdue items
402 if ( $item->{itemlost} ) {
404 # FIXME localized strings should never be in Perl code
405 $item->{message} =
406 $item->{itemlost} == 1 ? "(lost)"
407 : $item->{itemlost} == 2 ? "(long overdue)"
408 : "";
409 $item->{backgroundcolor} = 'other';
410 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
411 $item->{hide} = 1;
412 $hiddencount++;
416 # Check the transit status
417 my ( $transfertwhen, $transfertfrom, $transfertto ) =
418 GetTransfers($itemnumber);
420 if ( defined $transfertwhen && $transfertwhen ne '' ) {
421 $item->{transfertwhen} = format_date($transfertwhen);
422 $item->{transfertfrom} =
423 $branches->{$transfertfrom}{branchname};
424 $item->{transfertto} = $branches->{$transfertto}{branchname};
425 $item->{nocancel} = 1;
428 # If there is no loan, return and transfer, we show a checkbox.
429 $item->{notforloan} = $item->{notforloan} || 0;
431 # if independent branches is on we need to check if the person can reserve
432 # for branches they arent logged in to
433 if ( C4::Context->preference("IndependantBranches") ) {
434 if (! C4::Context->preference("canreservefromotherbranches")){
435 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
436 my $userenv = C4::Context->userenv;
437 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
438 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
443 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
445 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
446 my $policy_holdallowed = 1;
448 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
450 if ( $branchitemrule->{'holdallowed'} == 0 ||
451 ( $branchitemrule->{'holdallowed'} == 1 &&
452 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
453 $policy_holdallowed = 0;
456 if (
457 $policy_holdallowed
458 && !$item->{cantreserve}
459 && IsAvailableForItemLevelRequest($itemnumber)
460 && CanItemBeReserved(
461 $borrowerinfo->{borrowernumber}, $itemnumber
465 $item->{available} = 1;
466 $num_available++;
468 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
470 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
471 $item->{override} = 1;
472 $num_override++;
475 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
477 # FIXME: move this to a pm
478 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
479 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
480 while (my $wait_hashref = $sth2->fetchrow_hashref) {
481 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
484 # Show serial enumeration when needed
485 if ($item->{enumchron}) {
486 $itemdata_enumchron = 1;
489 push @{ $biblioitem->{itemloop} }, $item;
492 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
493 $template->param( override_required => 1 );
494 } elsif ( $num_available == 0 ) {
495 $template->param( none_available => 1 );
496 $template->param( warnings => 1 );
497 $biblioloopiter{warn} = 1;
498 $biblioloopiter{none_avail} = 1;
500 $template->param( hiddencount => $hiddencount);
502 push @bibitemloop, $biblioitem;
505 # existingreserves building
506 my @reserveloop;
507 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
508 foreach my $res ( sort {
509 my $a_found = $a->{found} || '';
510 my $b_found = $a->{found} || '';
511 $a_found cmp $b_found;
512 } @$reserves ) {
513 my %reserve;
514 my @optionloop;
515 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
516 push(
517 @optionloop,
519 num => $i,
520 selected => ( $i == $res->{priority} ),
525 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
526 my $item = $res->{'itemnumber'};
527 $item = GetBiblioFromItemNumber($item,undef);
528 $reserve{'wait'}= 1;
529 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
530 $reserve{'biblionumber'}=$item->{'biblionumber'};
531 $reserve{'barcodenumber'} = $item->{'barcode'};
532 $reserve{'wbrcode'} = $res->{'branchcode'};
533 $reserve{'itemnumber'} = $res->{'itemnumber'};
534 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
535 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
536 $reserve{'atdestination'} = 1;
538 # set found to 1 if reserve is waiting for patron pickup
539 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
540 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
541 } elsif ($res->{priority} > 0) {
542 if (defined($res->{itemnumber})) {
543 my $item = GetItem($res->{itemnumber});
544 $reserve{'itemnumber'} = $res->{'itemnumber'};
545 $reserve{'barcodenumber'} = $item->{'barcode'};
546 $reserve{'item_level_hold'} = 1;
550 # get borrowers reserve info
551 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
552 if (C4::Context->preference('HidePatronName')){
553 $reserve{'hidename'} = 1;
554 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
556 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
557 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
558 $reserve{'date'} = format_date( $res->{'reservedate'} );
559 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
560 $reserve{'biblionumber'} = $res->{'biblionumber'};
561 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
562 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
563 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
564 $reserve{'notes'} = $res->{'reservenotes'};
565 $reserve{'wait'} =
566 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
567 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
568 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
569 $reserve{'voldesc'} = $res->{'volumeddesc'};
570 $reserve{'ccode'} = $res->{'ccode'};
571 $reserve{'barcode'} = $res->{'barcode'};
572 $reserve{'priority'} = $res->{'priority'};
573 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
574 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
575 $reserve{'optionloop'} = \@optionloop;
576 $reserve{'suspend'} = $res->{'suspend'};
577 $reserve{'suspend_until'} = $res->{'suspend_until'};
578 push( @reserveloop, \%reserve );
581 # get the time for the form name...
582 my $time = time();
584 $template->param(
585 CGIbranch => $CGIbranch,
587 time => $time,
588 fixedRank => $fixedRank,
591 # display infos
592 $template->param(
593 optionloop => \@optionloop,
594 bibitemloop => \@bibitemloop,
595 itemdata_enumchron => $itemdata_enumchron,
596 date => $date,
597 biblionumber => $biblionumber,
598 findborrower => $findborrower,
599 title => $dat->{title},
600 author => $dat->{author},
601 holdsview => 1,
602 C4::Search::enabled_staff_search_views,
604 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
605 $template->param(
606 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
607 borrower_branchcode => $borrowerinfo->{'branchcode'},
610 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
612 $biblioloopiter{biblionumber} = $biblionumber;
613 $biblioloopiter{title} = $dat->{title};
614 $biblioloopiter{rank} = $fixedRank;
615 $biblioloopiter{reserveloop} = \@reserveloop;
617 if (@reserveloop) {
618 $template->param( reserveloop => \@reserveloop );
621 push @biblioloop, \%biblioloopiter;
624 $template->param( biblioloop => \@biblioloop );
625 $template->param( biblionumbers => $biblionumbers );
626 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
628 if ($multihold) {
629 $template->param( multi_hold => 1 );
632 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
633 $template->param( reserve_in_future => 1 );
636 $template->param(
637 SuspendHoldsIntranet => C4::Context->preference('SuspendHoldsIntranet'),
638 AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds'),
641 # printout the page
642 output_html_with_http_headers $input, $cookie, $template->output;