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;
123 # check if the borrower make the reserv in a different branch
124 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
130 borrowernumber => $borrowerinfo->{'borrowernumber'},
131 borrowersurname => $borrowerinfo->{'surname'},
132 borrowerfirstname => $borrowerinfo->{'firstname'},
133 borrowerstreetaddress => $borrowerinfo->{'address'},
134 borrowercity => $borrowerinfo->{'city'},
135 borrowerphone => $borrowerinfo->{'phone'},
136 borrowermobile => $borrowerinfo->{'mobile'},
137 borrowerfax => $borrowerinfo->{'fax'},
138 borrowerphonepro => $borrowerinfo->{'phonepro'},
139 borroweremail => $borrowerinfo->{'email'},
140 borroweremailpro => $borrowerinfo->{'emailpro'},
141 borrowercategory => $borrowerinfo->{'category'},
142 borrowerreservs => $count_reserv,
143 maxreserves => $maxreserves,
145 diffbranch => $diffbranch,
146 messages => $messages,
147 warnings => $warnings
151 $template->param( messageborrower => $messageborrower );
153 my $CGIselectborrower;
154 if ($borrowerslist) {
158 foreach my $borrower (
161 . $a->{firstname} cmp $b->{surname}
166 push @values, $borrower->{cardnumber};
168 $labels{ $borrower->{cardnumber} } = sprintf(
169 '%s, %s ... (%s - %s) ... %s',
170 $borrower->{surname}, $borrower->{firstname},
171 $borrower->{cardnumber}, $borrower->{categorycode},
172 $borrower->{address},
176 $CGIselectborrower = CGI::scrolling_list(
177 -name => 'cardnumber',
185 # get existing reserves .....
186 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
187 my $totalcount = $count;
190 # FIXME launch another time GetMemberDetails perhaps until
191 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
193 foreach my $res (@$reserves) {
194 if ( ( $res->{found} eq 'W' ) ) {
198 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
200 $alreadyreserved = 1;
204 $template->param( alreadyreserved => $alreadyreserved,
205 messages => $messages,
206 warnings => $warnings );
208 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
209 # make priorities options
212 for ( 1 .. $count + 1 ) {
217 selected => ( $_ == $count + 1 ),
221 # adding a fixed value for priority options
222 my $fixedRank = $count+1;
225 my %itemnumbers_of_biblioitem;
228 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
229 @itemnumbers = @$items;
232 $template->param('noitems' => 1);
235 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
237 foreach my $itemnumber (@itemnumbers) {
238 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
239 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
242 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
244 my $notforloan_label_of = get_notforloan_label_of();
245 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
249 foreach my $biblioitemnumber (@biblioitemnumbers) {
250 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
252 $biblioitem->{description} =
253 $itemtypes->{ $biblioitem->{itemtype} }{description};
256 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
258 my $item = $iteminfos_of->{$itemnumber};
259 unless (C4::Context->preference('item-level_itypes')) {
260 $item->{itype} = $biblioitem->{itemtype};
263 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
264 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
265 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
267 # if the holdingbranch is different than the homebranch, we show the
268 # holdingbranch of the document too
269 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
270 $item->{holdingbranchname} =
271 $branches->{ $item->{holdingbranch} }{branchname};
275 $item->{itemcallnumber} = $item->{itemcallnumber};
277 # if the item is currently on loan, we display its return date and
278 # change the background color
279 my $issues= GetItemIssue($itemnumber);
280 if ( $issues->{'date_due'} ) {
281 $item->{date_due} = format_date($issues->{'date_due'});
282 $item->{backgroundcolor} = 'onloan';
286 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
287 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
289 if ( defined $reservedate ) {
290 $item->{backgroundcolor} = 'reserved';
291 $item->{reservedate} = format_date($reservedate);
292 $item->{ReservedForBorrowernumber} = $reservedfor;
293 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
294 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
295 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
299 # Management of the notforloan document
300 if ( $item->{notforloan} ) {
301 $item->{backgroundcolor} = 'other';
302 $item->{notforloanvalue} =
303 $notforloan_label_of->{ $item->{notforloan} };
306 # Management of lost or long overdue items
307 if ( $item->{itemlost} ) {
309 # FIXME localized strings should never be in Perl code
311 $item->{itemlost} == 1 ? "(lost)"
312 : $item->{itemlost} == 2 ? "(long overdue)"
314 $item->{backgroundcolor} = 'other';
317 # Check the transit status
318 my ( $transfertwhen, $transfertfrom, $transfertto ) =
319 GetTransfers($itemnumber);
321 if ( $transfertwhen ne '' ) {
322 $item->{transfertwhen} = format_date($transfertwhen);
323 $item->{transfertfrom} =
324 $branches->{$transfertfrom}{branchname};
325 $item->{transfertto} = $branches->{$transfertto}{branchname};
326 $item->{nocancel} = 1;
329 # If there is no loan, return and transfer, we show a checkbox.
330 $item->{notforloan} = $item->{notforloan} || 0;
332 # if independent branches is on we need to check if the person can reserve
333 # for branches they arent logged in to
334 if ( C4::Context->preference("IndependantBranches") ) {
335 if (! C4::Context->preference("canreservefromotherbranches")){
336 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
337 my $userenv = C4::Context->userenv;
338 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
339 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
344 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
345 $item->{available} = 1;
348 # FIXME: move this to a pm
349 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
350 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
351 while (my $wait_hashref = $sth2->fetchrow_hashref) {
352 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
354 push @{ $biblioitem->{itemloop} }, $item;
357 push @bibitemloop, $biblioitem;
360 # existingreserves building
362 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
363 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
366 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
371 selected => ( $i == $res->{priority} ),
376 foreach my $br ( keys %$branches ) {
378 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
379 $abranch{'branch'} = $br;
380 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
381 push( @branchloop, \%abranch );
384 if ( ( $res->{'found'} eq 'W' ) ) {
385 my $item = $res->{'itemnumber'};
386 $item = GetBiblioFromItemNumber($item,undef);
388 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
389 $reserve{'biblionumber'}=$item->{'biblionumber'};
390 $reserve{'barcodenumber'} = $item->{'barcode'};
391 $reserve{'wbrcode'} = $res->{'branchcode'};
392 $reserve{'itemnumber'} = $res->{'itemnumber'};
393 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
394 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
395 $reserve{'atdestination'} = 1;
397 # set found to 1 if reserve is waiting for patron pickup
398 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
399 } elsif ($res->{priority} > 0) {
400 if (defined($res->{itemnumber})) {
401 my $item = GetItem($res->{itemnumber});
402 $reserve{'itemnumber'} = $res->{'itemnumber'};
403 $reserve{'barcodenumber'} = $item->{'barcode'};
404 $reserve{'item_level_hold'} = 1;
408 # get borrowers reserve info
409 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
411 $reserve{'date'} = format_date( $res->{'reservedate'} );
412 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
413 $reserve{'biblionumber'} = $res->{'biblionumber'};
414 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
415 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
416 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
417 $reserve{'notes'} = $res->{'reservenotes'};
419 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
420 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
421 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
422 $reserve{'voldesc'} = $res->{'volumeddesc'};
423 $reserve{'ccode'} = $res->{'ccode'};
424 $reserve{'barcode'} = $res->{'barcode'};
425 $reserve{'priority'} = $res->{'priority'};
426 $reserve{'branchloop'} = \@branchloop;
427 $reserve{'optionloop'} = \@optionloop;
429 push( @reserveloop, \%reserve );
432 my $default = C4::Context->userenv->{branch};
436 foreach my $branchcode (sort keys %{$branches} ) {
437 push @values, $branchcode;
438 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
440 my $CGIbranch = CGI::scrolling_list(
444 -default => $default,
445 -labels => \%label_of,
450 # get the time for the form name...
454 CGIbranch => $CGIbranch,
455 reserveloop => \@reserveloop,
457 fixedRank => $fixedRank,
462 optionloop => \@optionloop,
463 bibitemloop => \@bibitemloop,
465 biblionumber => $biblionumber,
466 findborrower => $findborrower,
467 cardnumber => $cardnumber,
468 CGIselectborrower => $CGIselectborrower,
469 title => $dat->{title},
470 author => $dat->{author},
472 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
473 borrower_branchcode => $borrowerinfo->{'branchcode'},
477 output_html_with_http_headers $input, $cookie, $template->output;