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/;
44 my $dbh = C4::Context->dbh;
47 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
49 template_name => "reserve/request.tmpl",
53 flagsrequired => { reserveforothers => 1 },
57 # get Branches and Itemtypes
58 my $branches = GetBranches();
59 my $itemtypes = GetItemTypes();
61 # get biblio information....
62 my $biblionumber = $input->param('biblionumber');
63 my $dat = GetBiblioData($biblionumber);
65 # Select borrowers infos
66 my $findborrower = $input->param('findborrower');
67 $findborrower =~ s|,| |g;
68 my $cardnumber = $input->param('cardnumber');
74 my $date = C4::Dates->today('iso');
77 my ( $count, $borrowers ) =
78 SearchMember($findborrower, 'cardnumber', 'web' );
80 my @borrowers = @$borrowers;
82 if ( $#borrowers == -1 ) {
83 $input->param( 'findborrower', '' );
84 $messageborrower = "'$findborrower'";
86 elsif ( $#borrowers == 0 ) {
87 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
88 $cardnumber = $borrowers[0]->{'cardnumber'};
91 $borrowerslist = \@borrowers;
96 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 expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
114 my $expiry_date = $borrowerinfo->{dateexpiry};
115 my $expiry = 0; # flag set if patron account has expired
116 if ($expiry_date and $expiry_date ne '0000-00-00' and
117 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
118 $messages = $expiry = 1;
124 # check if the borrower make the reserv in a different branch
125 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
131 borrowernumber => $borrowerinfo->{'borrowernumber'},
132 borrowersurname => $borrowerinfo->{'surname'},
133 borrowerfirstname => $borrowerinfo->{'firstname'},
134 borrowerstreetaddress => $borrowerinfo->{'address'},
135 borrowercity => $borrowerinfo->{'city'},
136 borrowerphone => $borrowerinfo->{'phone'},
137 borrowermobile => $borrowerinfo->{'mobile'},
138 borrowerfax => $borrowerinfo->{'fax'},
139 borrowerphonepro => $borrowerinfo->{'phonepro'},
140 borroweremail => $borrowerinfo->{'email'},
141 borroweremailpro => $borrowerinfo->{'emailpro'},
142 borrowercategory => $borrowerinfo->{'category'},
143 borrowerreservs => $count_reserv,
144 maxreserves => $maxreserves,
146 diffbranch => $diffbranch,
147 messages => $messages,
148 warnings => $warnings
152 $template->param( messageborrower => $messageborrower );
154 my $CGIselectborrower;
155 if ($borrowerslist) {
159 foreach my $borrower (
162 . $a->{firstname} cmp $b->{surname}
167 push @values, $borrower->{cardnumber};
169 $labels{ $borrower->{cardnumber} } = sprintf(
170 '%s, %s ... (%s - %s) ... %s',
171 $borrower->{surname}, $borrower->{firstname},
172 $borrower->{cardnumber}, $borrower->{categorycode},
173 $borrower->{address},
177 $CGIselectborrower = CGI::scrolling_list(
178 -name => 'cardnumber',
186 # get existing reserves .....
187 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
188 my $totalcount = $count;
191 # FIXME launch another time GetMemberDetails perhaps until
192 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
194 foreach my $res (@$reserves) {
195 if ( ( $res->{found} eq 'W' ) ) {
199 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
201 $alreadyreserved = 1;
205 $template->param( alreadyreserved => $alreadyreserved,
206 messages => $messages,
207 warnings => $warnings );
209 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
210 # make priorities options
213 for ( 1 .. $count + 1 ) {
218 selected => ( $_ == $count + 1 ),
222 # adding a fixed value for priority options
223 my $fixedRank = $count+1;
226 my %itemnumbers_of_biblioitem;
229 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
230 @itemnumbers = @$items;
233 $template->param('noitems' => 1);
236 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
238 foreach my $itemnumber (@itemnumbers) {
239 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
240 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
243 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
245 my $notforloan_label_of = get_notforloan_label_of();
246 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
250 foreach my $biblioitemnumber (@biblioitemnumbers) {
251 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
253 $biblioitem->{description} =
254 $itemtypes->{ $biblioitem->{itemtype} }{description};
257 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
259 my $item = $iteminfos_of->{$itemnumber};
260 unless (C4::Context->preference('item-level_itypes')) {
261 $item->{itype} = $biblioitem->{itemtype};
264 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
265 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
266 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
268 # if the holdingbranch is different than the homebranch, we show the
269 # holdingbranch of the document too
270 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
271 $item->{holdingbranchname} =
272 $branches->{ $item->{holdingbranch} }{branchname};
276 $item->{itemcallnumber} = $item->{itemcallnumber};
278 # if the item is currently on loan, we display its return date and
279 # change the background color
280 my $issues= GetItemIssue($itemnumber);
281 if ( $issues->{'date_due'} ) {
282 $item->{date_due} = format_date($issues->{'date_due'});
283 $item->{backgroundcolor} = 'onloan';
287 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
288 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
290 if ( defined $reservedate ) {
291 $item->{backgroundcolor} = 'reserved';
292 $item->{reservedate} = format_date($reservedate);
293 $item->{ReservedForBorrowernumber} = $reservedfor;
294 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
295 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
296 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
300 # Management of the notforloan document
301 if ( $item->{notforloan} ) {
302 $item->{backgroundcolor} = 'other';
303 $item->{notforloanvalue} =
304 $notforloan_label_of->{ $item->{notforloan} };
307 # Management of lost or long overdue items
308 if ( $item->{itemlost} ) {
310 # FIXME localized strings should never be in Perl code
312 $item->{itemlost} == 1 ? "(lost)"
313 : $item->{itemlost} == 2 ? "(long overdue)"
315 $item->{backgroundcolor} = 'other';
318 # Check the transit status
319 my ( $transfertwhen, $transfertfrom, $transfertto ) =
320 GetTransfers($itemnumber);
322 if ( $transfertwhen ne '' ) {
323 $item->{transfertwhen} = format_date($transfertwhen);
324 $item->{transfertfrom} =
325 $branches->{$transfertfrom}{branchname};
326 $item->{transfertto} = $branches->{$transfertto}{branchname};
327 $item->{nocancel} = 1;
330 # If there is no loan, return and transfer, we show a checkbox.
331 $item->{notforloan} = $item->{notforloan} || 0;
333 # if independent branches is on we need to check if the person can reserve
334 # for branches they arent logged in to
335 if ( C4::Context->preference("IndependantBranches") ) {
336 if (! C4::Context->preference("canreservefromotherbranches")){
337 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
338 my $userenv = C4::Context->userenv;
339 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
340 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
345 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
346 $item->{available} = 1;
349 # FIXME: move this to a pm
350 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
351 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
352 while (my $wait_hashref = $sth2->fetchrow_hashref) {
353 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
355 push @{ $biblioitem->{itemloop} }, $item;
358 push @bibitemloop, $biblioitem;
361 # existingreserves building
363 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
364 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
367 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
372 selected => ( $i == $res->{priority} ),
377 foreach my $br ( keys %$branches ) {
379 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
380 $abranch{'branch'} = $br;
381 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
382 push( @branchloop, \%abranch );
385 if ( ( $res->{'found'} eq 'W' ) ) {
386 my $item = $res->{'itemnumber'};
387 $item = GetBiblioFromItemNumber($item,undef);
389 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
390 $reserve{'biblionumber'}=$item->{'biblionumber'};
391 $reserve{'barcodenumber'} = $item->{'barcode'};
392 $reserve{'wbrcode'} = $res->{'branchcode'};
393 $reserve{'itemnumber'} = $res->{'itemnumber'};
394 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
395 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
396 $reserve{'atdestination'} = 1;
398 # set found to 1 if reserve is waiting for patron pickup
399 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
400 } elsif ($res->{priority} > 0) {
401 if (defined($res->{itemnumber})) {
402 my $item = GetItem($res->{itemnumber});
403 $reserve{'itemnumber'} = $res->{'itemnumber'};
404 $reserve{'barcodenumber'} = $item->{'barcode'};
405 $reserve{'item_level_hold'} = 1;
409 # get borrowers reserve info
410 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
412 $reserve{'date'} = format_date( $res->{'reservedate'} );
413 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
414 $reserve{'biblionumber'} = $res->{'biblionumber'};
415 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
416 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
417 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
418 $reserve{'notes'} = $res->{'reservenotes'};
420 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
421 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
422 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
423 $reserve{'voldesc'} = $res->{'volumeddesc'};
424 $reserve{'ccode'} = $res->{'ccode'};
425 $reserve{'barcode'} = $res->{'barcode'};
426 $reserve{'priority'} = $res->{'priority'};
427 $reserve{'branchloop'} = \@branchloop;
428 $reserve{'optionloop'} = \@optionloop;
430 push( @reserveloop, \%reserve );
433 my $default = C4::Context->userenv->{branch};
437 foreach my $branchcode (sort keys %{$branches} ) {
438 push @values, $branchcode;
439 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
441 my $CGIbranch = CGI::scrolling_list(
445 -default => $default,
446 -labels => \%label_of,
451 # get the time for the form name...
455 CGIbranch => $CGIbranch,
456 reserveloop => \@reserveloop,
458 fixedRank => $fixedRank,
463 optionloop => \@optionloop,
464 bibitemloop => \@bibitemloop,
466 biblionumber => $biblionumber,
467 findborrower => $findborrower,
468 cardnumber => $cardnumber,
469 CGIselectborrower => $CGIselectborrower,
470 title => $dat->{title},
471 author => $dat->{author},
473 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
474 borrower_branchcode => $borrowerinfo->{'branchcode'},
478 output_html_with_http_headers $input, $cookie, $template->output;