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
/Today 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 = sprintf( '%04d-%02d-%02d', Today
() );
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 );
102 # we check the reserves of the borrower, and if he can reserv a document
103 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
105 my $number_reserves =
106 GetReserveCount
( $borrowerinfo->{'borrowernumber'} );
108 if ( $number_reserves > C4
::Context
->preference('maxreserves') ) {
113 # we check the date expiricy of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
114 if ($borrowerinfo->{'dateexpiry'} ne '0000-00-00') {
115 my $warning = (Date_to_Days
(split /-/,$date) > Date_to_Days
( split /-/,$borrowerinfo->{'dateexpiry'}));
116 if ( $warning > 0 ) {
126 # check if the borrower make the reserv in a different branch
127 if ( $borrowerinfo->{'branchcode'} ne C4
::Context
->userenv->{'branch'} ) {
133 borrowernumber
=> $borrowerinfo->{'borrowernumber'},
134 borrowersurname
=> $borrowerinfo->{'surname'},
135 borrowerfirstname
=> $borrowerinfo->{'firstname'},
136 borrowerstreetaddress
=> $borrowerinfo->{'address'},
137 borrowercity
=> $borrowerinfo->{'city'},
138 borrowerphone
=> $borrowerinfo->{'phone'},
139 borrowermobile
=> $borrowerinfo->{'mobile'},
140 borrowerfax
=> $borrowerinfo->{'fax'},
141 borrowerphonepro
=> $borrowerinfo->{'phonepro'},
142 borroweremail
=> $borrowerinfo->{'email'},
143 borroweremailpro
=> $borrowerinfo->{'emailpro'},
144 borrowercategory
=> $borrowerinfo->{'category'},
145 borrowerreservs
=> $count_reserv,
146 maxreserves
=> $maxreserves,
148 diffbranch
=> $diffbranch,
149 messages
=> $messages,
150 warnings
=> $warnings
154 $template->param( messageborrower
=> $messageborrower );
156 my $CGIselectborrower;
157 if ($borrowerslist) {
161 foreach my $borrower (
164 . $a->{firstname
} cmp $b->{surname
}
169 push @values, $borrower->{cardnumber
};
171 $labels{ $borrower->{cardnumber
} } = sprintf(
172 '%s, %s ... (%s - %s) ... %s',
173 $borrower->{surname
}, $borrower->{firstname
},
174 $borrower->{cardnumber
}, $borrower->{categorycode
},
175 $borrower->{streetaddress
},
179 $CGIselectborrower = CGI
::scrolling_list
(
180 -name
=> 'cardnumber',
188 # get existing reserves .....
189 my ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
190 my $totalcount = $count;
193 # FIXME launch another time GetMemberDetails perhaps until
194 my $borrowerinfo = GetMemberDetails
( 0, $cardnumber );
196 foreach my $res (@
$reserves) {
197 if ( ( $res->{found
} eq 'W' ) ) {
201 if ( $borrowerinfo->{borrowernumber
} eq $res->{borrowernumber
} ) {
203 $alreadyreserved = 1;
207 $template->param( alreadyreserved
=> $alreadyreserved,
208 messages
=> $messages,
209 warnings
=> $warnings );
211 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
212 # make priorities options
215 for ( 1 .. $count + 1 ) {
220 selected
=> ( $_ == $count + 1 ),
224 # adding a fixed value for priority options
225 my $fixedRank = $count+1;
228 my %itemnumbers_of_biblioitem;
231 if (my $items = get_itemnumbers_of
($biblionumber)->{$biblionumber}){
232 @itemnumbers = @
$items;
235 $template->param('noitems' => 1);
238 my $iteminfos_of = GetItemInfosOf
(@itemnumbers);
240 foreach my $itemnumber (@itemnumbers) {
241 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber
};
242 push( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
245 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
247 my $notforloan_label_of = get_notforloan_label_of
();
248 my $biblioiteminfos_of = GetBiblioItemInfosOf
(@biblioitemnumbers);
252 foreach my $biblioitemnumber (@biblioitemnumbers) {
253 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
255 $biblioitem->{description
} =
256 $itemtypes->{ $biblioitem->{itemtype
} }{description
};
259 my $itemnumber ( @
{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
261 my $item = $iteminfos_of->{$itemnumber};
262 $item->{itypename
} = $itemtypes->{ $item->{itype
} }{description
};
263 $item->{imageurl
} = getitemtypeimagesrc
() . "/".$itemtypes->{ $item->{itype
} }{imageurl
};
264 $item->{homebranchname
} =
265 $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 # FIXME: every library will define this differently
345 # An item is available only if:
347 not defined $reservedate # not reserved yet
348 and $issues->{'date_due'} eq '' # not currently on loan
349 and not $item->{itemlost
} # not lost
350 and not $item->{notforloan
} # not forbidden to loan
351 and not $item->{cantreserve
}
352 and $transfertwhen eq '' # not currently on transfert
355 $item->{available
} = 1;
358 # FIXME: move this to a pm
359 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W' AND cancellationdate IS NULL");
360 $sth2->execute($item->{ReservedForBorrowernumber
},$item->{itemnumber
});
361 while (my $wait_hashref = $sth2->fetchrow_hashref) {
362 $item->{waitingdate
} = format_date
($wait_hashref->{waitingdate
});
364 push @
{ $biblioitem->{itemloop
} }, $item;
367 push @bibitemloop, $biblioitem;
370 # existingreserves building
372 ( $count, $reserves ) = GetReservesFromBiblionumber
($biblionumber);
373 foreach my $res ( sort { $a->{found
} cmp $b->{found
} } @
$reserves ) {
376 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
381 selected
=> ( $i == $res->{priority
} ),
386 foreach my $br ( keys %$branches ) {
388 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
389 $abranch{'branch'} = $br;
390 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
391 push( @branchloop, \
%abranch );
394 if ( ( $res->{'found'} eq 'W' ) ) {
395 my $item = $res->{'itemnumber'};
396 $item = GetBiblioFromItemNumber
($item,undef);
398 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
399 $reserve{'biblionumber'}=$item->{'biblionumber'};
400 $reserve{'barcodenumber'} = $item->{'barcode'};
401 $reserve{'wbrcode'} = $res->{'branchcode'};
402 $reserve{'itemnumber'} = $res->{'itemnumber'};
403 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
404 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
405 $reserve{'atdestination'} = 1;
407 # set found to 1 if reserve is waiting for patron pickup
408 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
411 # get borrowers reserve info
412 my $reserveborrowerinfo = GetMemberDetails
( $res->{'borrowernumber'}, 0);
414 $reserve{'date'} = format_date
( $res->{'reservedate'} );
415 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
416 $reserve{'biblionumber'} = $res->{'biblionumber'};
417 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
418 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
419 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
420 $reserve{'notes'} = $res->{'reservenotes'};
422 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
423 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
424 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
425 $reserve{'voldesc'} = $res->{'volumeddesc'};
426 $reserve{'ccode'} = $res->{'ccode'};
427 $reserve{'barcode'} = $res->{'barcode'};
428 $reserve{'priority'} = $res->{'priority'};
429 $reserve{'branchloop'} = \
@branchloop;
430 $reserve{'optionloop'} = \
@optionloop;
432 push( @reserveloop, \
%reserve );
435 my $default = C4
::Context
->userenv->{branch
};
439 foreach my $branchcode (sort keys %{$branches} ) {
440 push @values, $branchcode;
441 $label_of{$branchcode} = $branches->{$branchcode}->{branchname
};
443 my $CGIbranch = CGI
::scrolling_list
(
447 -default => $default,
448 -labels
=> \
%label_of,
453 # get the time for the form name...
457 CGIbranch
=> $CGIbranch,
458 reserveloop
=> \
@reserveloop,
460 fixedRank
=> $fixedRank,
465 optionloop
=> \
@optionloop,
466 bibitemloop
=> \
@bibitemloop,
468 biblionumber
=> $biblionumber,
469 findborrower
=> $findborrower,
470 cardnumber
=> $cardnumber,
471 CGIselectborrower
=> $CGIselectborrower,
472 title
=> $dat->{title
},
473 author
=> $dat->{author
},
478 output_html_with_http_headers
$input, $cookie, $template->output;