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
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 with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
24 script to place reserves/requests
30 use C4::Branch; # GetBranches get_branchinfos_of
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
41 use C4::Dates qw/format_date/;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
54 flagsrequired => { reserveforothers => 1 },
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};
69 foreach my $branchcode (sort keys %{$branches} ) {
70 push @values, $branchcode;
71 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
73 my $CGIbranch = CGI::scrolling_list(
78 -labels => \%label_of,
83 # Select borrowers infos
84 my $findborrower = $input->param('findborrower');
85 $findborrower =~ s|,| |g;
86 my $cardnumber = $input->param('cardnumber');
92 my $date = C4::Dates->today('iso');
95 my ( $count, $borrowers ) =
96 SearchMember($findborrower, 'cardnumber', 'web' );
98 my @borrowers = @$borrowers;
100 if ( $#borrowers == -1 ) {
101 $input->param( 'findborrower', '' );
102 $messageborrower = "'$findborrower'";
104 elsif ( $#borrowers == 0 ) {
105 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
106 $cardnumber = $borrowers[0]->{'cardnumber'};
109 $borrowerslist = \@borrowers;
114 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
117 my $count_reserv = 0;
120 # we check the reserves of the borrower, and if he can reserv a document
121 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
123 my $number_reserves =
124 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
126 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
131 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
132 my $expiry_date = $borrowerinfo->{dateexpiry};
133 my $expiry = 0; # flag set if patron account has expired
134 if ($expiry_date and $expiry_date ne '0000-00-00' and
135 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
136 $messages = $expiry = 1;
140 # check if the borrower make the reserv in a different branch
141 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
147 borrowernumber => $borrowerinfo->{'borrowernumber'},
148 borrowersurname => $borrowerinfo->{'surname'},
149 borrowerfirstname => $borrowerinfo->{'firstname'},
150 borrowerstreetaddress => $borrowerinfo->{'address'},
151 borrowercity => $borrowerinfo->{'city'},
152 borrowerphone => $borrowerinfo->{'phone'},
153 borrowermobile => $borrowerinfo->{'mobile'},
154 borrowerfax => $borrowerinfo->{'fax'},
155 borrowerphonepro => $borrowerinfo->{'phonepro'},
156 borroweremail => $borrowerinfo->{'email'},
157 borroweremailpro => $borrowerinfo->{'emailpro'},
158 borrowercategory => $borrowerinfo->{'category'},
159 borrowerreservs => $count_reserv,
160 maxreserves => $maxreserves,
162 diffbranch => $diffbranch,
163 messages => $messages,
164 warnings => $warnings
168 $template->param( messageborrower => $messageborrower );
170 my $CGIselectborrower;
171 if ($borrowerslist) {
175 foreach my $borrower (
178 . $a->{firstname} cmp $b->{surname}
183 push @values, $borrower->{cardnumber};
185 $labels{ $borrower->{cardnumber} } = sprintf(
186 '%s, %s ... (%s - %s) ... %s',
187 $borrower->{surname}, $borrower->{firstname},
188 $borrower->{cardnumber}, $borrower->{categorycode},
189 $borrower->{address},
193 $CGIselectborrower = CGI::scrolling_list(
194 -name => 'cardnumber',
202 # FIXME launch another time GetMemberDetails perhaps until
203 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
205 my @biblionumbers = ();
206 my $biblionumbers = $input->param('biblionumbers');
208 @biblionumbers = split '/', $biblionumbers;
210 push @biblionumbers, $input->param('biblionumber');
214 foreach my $biblionumber (@biblionumbers) {
216 my %biblioloopiter = ();
218 my $dat = GetBiblioData($biblionumber);
220 # get existing reserves .....
221 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
222 my $totalcount = $count;
225 foreach my $res (@$reserves) {
226 if ( ( $res->{found} eq 'W' ) ) {
230 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
232 $alreadyreserved = 1;
233 $biblioloopiter{warn} = 1;
234 $biblioloopiter{alreadyres} = 1;
238 $template->param( alreadyreserved => $alreadyreserved,
239 messages => $messages,
240 warnings => $warnings );
243 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
244 # make priorities options
247 for ( 1 .. $count + 1 ) {
252 selected => ( $_ == $count + 1 ),
256 # adding a fixed value for priority options
257 my $fixedRank = $count+1;
260 my %itemnumbers_of_biblioitem;
263 ## $items is array of 'item' table numbers
264 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
265 @itemnumbers = @$items;
268 $template->param('noitems' => 1);
269 $biblioloopiter{noitems} = 1;
272 ## Hash of item number to 'item' table fields
273 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
275 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
276 ## when by definition all of the itemnumber have the same biblioitemnumber
277 foreach my $itemnumber (@itemnumbers) {
278 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
279 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
282 ## Should be same as biblionumber
283 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
285 my $notforloan_label_of = get_notforloan_label_of();
287 ## Hash of biblioitemnumber to 'biblioitem' table records
288 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
292 foreach my $biblioitemnumber (@biblioitemnumbers) {
293 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
297 $biblioitem->{description} =
298 $itemtypes->{ $biblioitem->{itemtype} }{description};
299 $biblioloopiter{description} = $biblioitem->{description};
300 $biblioloopiter{itypename} = $biblioitem->{description};
301 $biblioloopiter{imageurl} =
302 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
304 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
305 my $item = $iteminfos_of->{$itemnumber};
307 unless (C4::Context->preference('item-level_itypes')) {
308 $item->{itype} = $biblioitem->{itemtype};
311 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
312 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
313 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
315 # if the holdingbranch is different than the homebranch, we show the
316 # holdingbranch of the document too
317 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
318 $item->{holdingbranchname} =
319 $branches->{ $item->{holdingbranch} }{branchname};
323 $item->{itemcallnumber} = $item->{itemcallnumber};
325 # if the item is currently on loan, we display its return date and
326 # change the background color
327 my $issues= GetItemIssue($itemnumber);
328 if ( $issues->{'date_due'} ) {
329 $item->{date_due} = format_date($issues->{'date_due'});
330 $item->{backgroundcolor} = 'onloan';
334 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
335 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
337 if ( defined $reservedate ) {
338 $item->{backgroundcolor} = 'reserved';
339 $item->{reservedate} = format_date($reservedate);
340 $item->{ReservedForBorrowernumber} = $reservedfor;
341 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
342 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
343 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
347 # Management of the notforloan document
348 if ( $item->{notforloan} ) {
349 $item->{backgroundcolor} = 'other';
350 $item->{notforloanvalue} =
351 $notforloan_label_of->{ $item->{notforloan} };
354 # Management of lost or long overdue items
355 if ( $item->{itemlost} ) {
357 # FIXME localized strings should never be in Perl code
359 $item->{itemlost} == 1 ? "(lost)"
360 : $item->{itemlost} == 2 ? "(long overdue)"
362 $item->{backgroundcolor} = 'other';
365 # Check the transit status
366 my ( $transfertwhen, $transfertfrom, $transfertto ) =
367 GetTransfers($itemnumber);
369 if ( $transfertwhen ne '' ) {
370 $item->{transfertwhen} = format_date($transfertwhen);
371 $item->{transfertfrom} =
372 $branches->{$transfertfrom}{branchname};
373 $item->{transfertto} = $branches->{$transfertto}{branchname};
374 $item->{nocancel} = 1;
377 # If there is no loan, return and transfer, we show a checkbox.
378 $item->{notforloan} = $item->{notforloan} || 0;
380 # if independent branches is on we need to check if the person can reserve
381 # for branches they arent logged in to
382 if ( C4::Context->preference("IndependantBranches") ) {
383 if (! C4::Context->preference("canreservefromotherbranches")){
384 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
385 my $userenv = C4::Context->userenv;
386 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
387 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
392 my $branchitemrule = GetBranchItemRule( $item->{'homebranch'}, $item->{'itype'} );
393 my $policy_holdallowed = 1;
395 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
397 if ( $branchitemrule->{'holdallowed'} == 0 ||
398 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
399 $policy_holdallowed = 0;
402 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
403 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
404 $item->{override} = 1;
406 } elsif ( $policy_holdallowed ) {
407 $item->{available} = 1;
411 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
413 # FIXME: move this to a pm
414 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
415 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
416 while (my $wait_hashref = $sth2->fetchrow_hashref) {
417 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
419 push @{ $biblioitem->{itemloop} }, $item;
422 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
423 $template->param( override_required => 1 );
424 } elsif ( $num_available == 0 ) {
425 $template->param( none_available => 1 );
426 $template->param( warnings => 1 );
427 $biblioloopiter{warn} = 1;
428 $biblioloopiter{none_avail} = 1;
431 push @bibitemloop, $biblioitem;
434 # existingreserves building
436 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
437 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
440 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
445 selected => ( $i == $res->{priority} ),
450 if ( ( $res->{'found'} eq 'W' ) ) {
451 my $item = $res->{'itemnumber'};
452 $item = GetBiblioFromItemNumber($item,undef);
454 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
455 $reserve{'biblionumber'}=$item->{'biblionumber'};
456 $reserve{'barcodenumber'} = $item->{'barcode'};
457 $reserve{'wbrcode'} = $res->{'branchcode'};
458 $reserve{'itemnumber'} = $res->{'itemnumber'};
459 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
460 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
461 $reserve{'atdestination'} = 1;
463 # set found to 1 if reserve is waiting for patron pickup
464 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
465 } elsif ($res->{priority} > 0) {
466 if (defined($res->{itemnumber})) {
467 my $item = GetItem($res->{itemnumber});
468 $reserve{'itemnumber'} = $res->{'itemnumber'};
469 $reserve{'barcodenumber'} = $item->{'barcode'};
470 $reserve{'item_level_hold'} = 1;
474 # get borrowers reserve info
475 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
477 $reserve{'date'} = format_date( $res->{'reservedate'} );
478 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
479 $reserve{'biblionumber'} = $res->{'biblionumber'};
480 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
481 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
482 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
483 $reserve{'notes'} = $res->{'reservenotes'};
485 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
486 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
487 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
488 $reserve{'voldesc'} = $res->{'volumeddesc'};
489 $reserve{'ccode'} = $res->{'ccode'};
490 $reserve{'barcode'} = $res->{'barcode'};
491 $reserve{'priority'} = $res->{'priority'};
492 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
493 $reserve{'optionloop'} = \@optionloop;
495 push( @reserveloop, \%reserve );
498 # get the time for the form name...
502 CGIbranch => $CGIbranch,
505 fixedRank => $fixedRank,
510 optionloop => \@optionloop,
511 bibitemloop => \@bibitemloop,
513 biblionumber => $biblionumber,
514 findborrower => $findborrower,
515 cardnumber => $cardnumber,
516 CGIselectborrower => $CGIselectborrower,
517 title => $dat->{title},
518 author => $dat->{author},
520 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
521 borrower_branchcode => $borrowerinfo->{'branchcode'},
522 C4::Search::enabled_staff_search_views,
525 $biblioloopiter{biblionumber} = $biblionumber;
526 $biblioloopiter{title} = $dat->{title};
527 $biblioloopiter{rank} = $fixedRank;
528 $biblioloopiter{reserveloop} = \@reserveloop;
531 $template->param( reserveloop => \@reserveloop );
535 push @biblioloop, \%biblioloopiter;
538 $template->param( biblioloop => \@biblioloop );
539 $template->param( biblionumbers => $biblionumbers );
542 $template->param( multi_hold => 1 );
546 output_html_with_http_headers $input, $cookie, $template->output;