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
29 use C4
::Branch
; # GetBranches get_branchinfos_of
31 use List
::MoreUtils qw
/uniq/;
32 use Date
::Calc qw
/Date_to_Days/;
40 use C4
::Dates qw
/format_date/;
43 my $dbh = C4
::Context
->dbh;
46 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
48 template_name
=> "reserve/request.tmpl",
52 flagsrequired
=> { reserveforothers
=> 1 },
56 # get Branches and Itemtypes
57 my $branches = GetBranches
();
58 my $itemtypes = GetItemTypes
();
60 # get biblio information....
61 my $biblionumber = $input->param('biblionumber');
62 my $dat = GetBiblioData
($biblionumber);
64 # Select borrowers infos
65 my $findborrower = $input->param('findborrower');
66 $findborrower =~ s
|,| |g
;
67 my $cardnumber = $input->param('cardnumber');
73 my $date = C4
::Dates
->today('iso');
76 my ( $count, $borrowers ) =
77 SearchMember
($findborrower, 'cardnumber', 'web' );
79 my @borrowers = @
$borrowers;
81 if ( $#borrowers == -1 ) {
82 $input->param( 'findborrower', '' );
83 $messageborrower = "'$findborrower'";
85 elsif ( $#borrowers == 0 ) {
86 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
87 $cardnumber = $borrowers[0]->{'cardnumber'};
90 $borrowerslist = \
@borrowers;
95 my $borrowerinfo = GetMemberDetails
( 0, $cardnumber );
101 # we check the reserves of the borrower, and if he can reserv a document
102 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
104 my $number_reserves =
105 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
107 if ( $number_reserves > C4
::Context
->preference('maxreserves') ) {
112 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
113 my $expiry_date = $borrowerinfo->{dateexpiry
};
114 my $expiry = 0; # flag set if patron account has expired
115 if ($expiry_date and $expiry_date ne '0000-00-00' and
116 Date_to_Days
(split /-/,$date) > Date_to_Days
(split /-/,$expiry_date)) {
117 $messages = $expiry = 1;
121 # check if the borrower make the reserv in a different branch
122 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
128 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
129 borrowersurname
=> $borrowerinfo->{'surname'},
130 borrowerfirstname
=> $borrowerinfo->{'firstname'},
131 borrowerstreetaddress
=> $borrowerinfo->{'address'},
132 borrowercity
=> $borrowerinfo->{'city'},
133 borrowerphone
=> $borrowerinfo->{'phone'},
134 borrowermobile
=> $borrowerinfo->{'mobile'},
135 borrowerfax
=> $borrowerinfo->{'fax'},
136 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
137 borroweremail
=> $borrowerinfo->{'email'},
138 borroweremailpro
=> $borrowerinfo->{'emailpro'},
139 borrowercategory
=> $borrowerinfo->{'category'},
140 borrowerreservs
=> $count_reserv,
141 maxreserves
=> $maxreserves,
143 diffbranch
=> $diffbranch,
144 messages
=> $messages,
145 warnings
=> $warnings
149 $template->param( messageborrower
=> $messageborrower );
151 my $CGIselectborrower;
152 if ($borrowerslist) {
156 foreach my $borrower (
159 . $a->{firstname
} cmp $b->{surname
}
164 push @values, $borrower->{cardnumber
};
166 $labels{ $borrower->{cardnumber
} } = sprintf(
167 '%s, %s ... (%s - %s) ... %s',
168 $borrower->{surname
}, $borrower->{firstname
},
169 $borrower->{cardnumber
}, $borrower->{categorycode
},
170 $borrower->{streetaddress
},
174 $CGIselectborrower = CGI
::scrolling_list
(
175 -name
=> 'cardnumber',
183 # get existing reserves .....
184 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
185 my $totalcount = $count;
188 # FIXME launch another time GetMemberDetails perhaps until
189 my $borrowerinfo = GetMemberDetails
( 0, $cardnumber );
191 foreach my $res (@
$reserves) {
192 if ( ( $res->{found
} eq 'W' ) ) {
196 if ( $borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
} ) {
198 $alreadyreserved = 1;
202 $template->param( alreadyreserved
=> $alreadyreserved,
203 messages
=> $messages,
204 warnings
=> $warnings );
206 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
207 # make priorities options
210 for ( 1 .. $count + 1 ) {
215 selected
=> ( $_ == $count + 1 ),
219 # adding a fixed value for priority options
220 my $fixedRank = $count+1;
223 my %itemnumbers_of_biblioitem;
226 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
227 @itemnumbers = @
$items;
230 $template->param('noitems' => 1);
233 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
235 foreach my $itemnumber (@itemnumbers) {
236 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
237 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
240 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
242 my $notforloan_label_of = get_notforloan_label_of
();
243 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
247 foreach my $biblioitemnumber (@biblioitemnumbers) {
248 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
252 $biblioitem->{description
} =
253 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
255 foreach my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
256 my $item = $iteminfos_of->{$itemnumber};
258 if (C4
::Context
->preference('item-level_itypes')) {
259 $item->{itype
} = $biblioitem->{itemtype
};
262 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
263 $item->{imageurl
} = getitemtypeimagelocation
( 'intranet', $itemtypes->{ $item->{itype
} }{imageurl
} );
264 $item->{homebranchname
} = $branches->{ $item->{homebranch
} }{branchname
};
266 # if the holdingbranch is different than the homebranch, we show the
267 # holdingbranch of the document too
268 if ( $item->{homebranch
} ne $item->{holdingbranch
} ) {
269 $item->{holdingbranchname
} =
270 $branches->{ $item->{holdingbranch
} }{branchname
};
274 $item->{itemcallnumber
} = $item->{itemcallnumber
};
276 # if the item is currently on loan, we display its return date and
277 # change the background color
278 my $issues= GetItemIssue
($itemnumber);
279 if ( $issues->{'date_due'} ) {
280 $item->{date_due
} = format_date
($issues->{'date_due'});
281 $item->{backgroundcolor
} = 'onloan';
285 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber
($itemnumber);
286 my $ItemBorrowerReserveInfo = GetMemberDetails
( $reservedfor, 0);
288 if ( defined $reservedate ) {
289 $item->{backgroundcolor
} = 'reserved';
290 $item->{reservedate
} = format_date
($reservedate);
291 $item->{ReservedForBorrowernumber
} = $reservedfor;
292 $item->{ReservedForSurname
} = $ItemBorrowerReserveInfo->{'surname'};
293 $item->{ReservedForFirstname
} = $ItemBorrowerReserveInfo->{'firstname'};
294 $item->{ExpectedAtLibrary
} = $branches->{$expectedAt}{branchname
};
298 # Management of the notforloan document
299 if ( $item->{notforloan
} ) {
300 $item->{backgroundcolor
} = 'other';
301 $item->{notforloanvalue
} =
302 $notforloan_label_of->{ $item->{notforloan
} };
305 # Management of lost or long overdue items
306 if ( $item->{itemlost
} ) {
308 # FIXME localized strings should never be in Perl code
310 $item->{itemlost
} == 1 ?
"(lost)"
311 : $item->{itemlost
} == 2 ?
"(long overdue)"
313 $item->{backgroundcolor
} = 'other';
316 # Check the transit status
317 my ( $transfertwhen, $transfertfrom, $transfertto ) =
318 GetTransfers
($itemnumber);
320 if ( $transfertwhen ne '' ) {
321 $item->{transfertwhen
} = format_date
($transfertwhen);
322 $item->{transfertfrom
} =
323 $branches->{$transfertfrom}{branchname
};
324 $item->{transfertto
} = $branches->{$transfertto}{branchname
};
325 $item->{nocancel
} = 1;
328 # If there is no loan, return and transfer, we show a checkbox.
329 $item->{notforloan
} = $item->{notforloan
} || 0;
331 # if independent branches is on we need to check if the person can reserve
332 # for branches they arent logged in to
333 if ( C4
::Context
->preference("IndependantBranches") ) {
334 if (! C4
::Context
->preference("canreservefromotherbranches")){
335 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
336 my $userenv = C4
::Context
->userenv;
337 if ( ($userenv) && ( $userenv->{flags
} != 1 ) ) {
338 $item->{cantreserve
} = 1 if ( $item->{homebranch
} ne $userenv->{branch
} );
343 my $branchitemrule = GetBranchItemRule
( $item->{'homebranch'}, $item->{'itype'} );
344 my $policy_holdallowed = 1;
346 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
348 if ( $branchitemrule->{'holdallowed'} == 0 ||
349 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
350 $policy_holdallowed = 0;
353 if (IsAvailableForItemLevelRequest
($itemnumber) and not $item->{cantreserve
}) {
354 if ( not $policy_holdallowed and C4
::Context
->preference( 'AllowHoldPolicyOverride' ) ) {
355 $item->{override
} = 1;
357 } elsif ( $policy_holdallowed ) {
358 $item->{available
} = 1;
362 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
364 # FIXME: move this to a pm
365 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
366 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
367 while (my $wait_hashref = $sth2->fetchrow_hashref) {
368 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
370 push @
{ $biblioitem->{itemloop
} }, $item;
373 if ( $num_override == scalar( @
{ $biblioitem->{itemloop
} } ) ) { # That is, if all items require an override
374 $template->param( override_required
=> 1 );
375 } elsif ( $num_available == 0 ) {
376 $template->param( none_available
=> 1 );
377 $template->param( warnings
=> 1 );
380 push @bibitemloop, $biblioitem;
383 # existingreserves building
385 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
386 foreach my $res ( sort { $a->{found
} cmp $b->{found
} } @
$reserves ) {
389 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
394 selected
=> ( $i == $res->{priority
} ),
399 foreach my $br ( keys %$branches ) {
401 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
402 $abranch{'branch'} = $br;
403 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
404 push( @branchloop, \
%abranch );
407 if ( ( $res->{'found'} eq 'W' ) ) {
408 my $item = $res->{'itemnumber'};
409 $item = GetBiblioFromItemNumber
($item,undef);
411 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
412 $reserve{'biblionumber'}=$item->{'biblionumber'};
413 $reserve{'barcodenumber'} = $item->{'barcode'};
414 $reserve{'wbrcode'} = $res->{'branchcode'};
415 $reserve{'itemnumber'} = $res->{'itemnumber'};
416 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
417 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
418 $reserve{'atdestination'} = 1;
420 # set found to 1 if reserve is waiting for patron pickup
421 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
422 } elsif ($res->{priority
} > 0) {
423 if (defined($res->{itemnumber
})) {
424 my $item = GetItem
($res->{itemnumber
});
425 $reserve{'itemnumber'} = $res->{'itemnumber'};
426 $reserve{'barcodenumber'} = $item->{'barcode'};
427 $reserve{'item_level_hold'} = 1;
431 # get borrowers reserve info
432 my $reserveborrowerinfo = GetMemberDetails
( $res->{'borrowernumber'}, 0);
434 $reserve{'date'} = format_date
( $res->{'reservedate'} );
435 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
436 $reserve{'biblionumber'} = $res->{'biblionumber'};
437 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
438 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
439 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
440 $reserve{'notes'} = $res->{'reservenotes'};
442 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
443 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
444 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
445 $reserve{'voldesc'} = $res->{'volumeddesc'};
446 $reserve{'ccode'} = $res->{'ccode'};
447 $reserve{'barcode'} = $res->{'barcode'};
448 $reserve{'priority'} = $res->{'priority'};
449 $reserve{'branchloop'} = \
@branchloop;
450 $reserve{'optionloop'} = \
@optionloop;
452 push( @reserveloop, \
%reserve );
455 my $default = C4
::Context
->userenv->{branch
};
459 foreach my $branchcode (sort keys %{$branches} ) {
460 push @values, $branchcode;
461 $label_of{$branchcode} = $branches->{$branchcode}->{branchname
};
463 my $CGIbranch = CGI
::scrolling_list
(
467 -default => $default,
468 -labels
=> \
%label_of,
473 # get the time for the form name...
477 CGIbranch
=> $CGIbranch,
478 reserveloop
=> \
@reserveloop,
480 fixedRank
=> $fixedRank,
485 optionloop
=> \
@optionloop,
486 bibitemloop
=> \
@bibitemloop,
488 biblionumber
=> $biblionumber,
489 findborrower
=> $findborrower,
490 cardnumber
=> $cardnumber,
491 CGIselectborrower
=> $CGIselectborrower,
492 title
=> $dat->{title
},
493 author
=> $dat->{author
},
495 borrower_branchname
=> $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
496 borrower_branchcode
=> $borrowerinfo->{'branchcode'},
500 output_html_with_http_headers
$input, $cookie, $template->output;