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};
250 $biblioitem->{description} =
251 $itemtypes->{ $biblioitem->{itemtype} }{description};
254 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
256 my $item = $iteminfos_of->{$itemnumber};
257 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
258 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
259 $item->{homebranchname} =
260 $branches->{ $item->{homebranch} }{branchname};
262 # if the holdingbranch is different than the homebranch, we show the
263 # holdingbranch of the document too
264 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
265 $item->{holdingbranchname} =
266 $branches->{ $item->{holdingbranch} }{branchname};
270 $item->{itemcallnumber} = $item->{itemcallnumber};
272 # if the item is currently on loan, we display its return date and
273 # change the background color
274 my $issues= GetItemIssue($itemnumber);
275 if ( $issues->{'date_due'} ) {
276 $item->{date_due} = format_date($issues->{'date_due'});
277 $item->{backgroundcolor} = 'onloan';
281 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
282 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
284 if ( defined $reservedate ) {
285 $item->{backgroundcolor} = 'reserved';
286 $item->{reservedate} = format_date($reservedate);
287 $item->{ReservedForBorrowernumber} = $reservedfor;
288 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
289 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
290 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
294 # Management of the notforloan document
295 if ( $item->{notforloan} ) {
296 $item->{backgroundcolor} = 'other';
297 $item->{notforloanvalue} =
298 $notforloan_label_of->{ $item->{notforloan} };
301 # Management of lost or long overdue items
302 if ( $item->{itemlost} ) {
304 # FIXME localized strings should never be in Perl code
306 $item->{itemlost} == 1 ? "(lost)"
307 : $item->{itemlost} == 2 ? "(long overdue)"
309 $item->{backgroundcolor} = 'other';
312 # Check the transit status
313 my ( $transfertwhen, $transfertfrom, $transfertto ) =
314 GetTransfers($itemnumber);
316 if ( $transfertwhen ne '' ) {
317 $item->{transfertwhen} = format_date($transfertwhen);
318 $item->{transfertfrom} =
319 $branches->{$transfertfrom}{branchname};
320 $item->{transfertto} = $branches->{$transfertto}{branchname};
321 $item->{nocancel} = 1;
324 # If there is no loan, return and transfer, we show a checkbox.
325 $item->{notforloan} = $item->{notforloan} || 0;
327 # if independent branches is on we need to check if the person can reserve
328 # for branches they arent logged in to
329 if ( C4::Context->preference("IndependantBranches") ) {
330 if (! C4::Context->preference("canreservefromotherbranches")){
331 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
332 my $userenv = C4::Context->userenv;
333 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
334 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
339 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
340 $item->{available} = 1;
343 # FIXME: move this to a pm
344 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
345 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
346 while (my $wait_hashref = $sth2->fetchrow_hashref) {
347 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
349 push @{ $biblioitem->{itemloop} }, $item;
352 push @bibitemloop, $biblioitem;
355 # existingreserves building
357 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
358 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
361 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
366 selected => ( $i == $res->{priority} ),
371 foreach my $br ( keys %$branches ) {
373 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
374 $abranch{'branch'} = $br;
375 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
376 push( @branchloop, \%abranch );
379 if ( ( $res->{'found'} eq 'W' ) ) {
380 my $item = $res->{'itemnumber'};
381 $item = GetBiblioFromItemNumber($item,undef);
383 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
384 $reserve{'biblionumber'}=$item->{'biblionumber'};
385 $reserve{'barcodenumber'} = $item->{'barcode'};
386 $reserve{'wbrcode'} = $res->{'branchcode'};
387 $reserve{'itemnumber'} = $res->{'itemnumber'};
388 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
389 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
390 $reserve{'atdestination'} = 1;
392 # set found to 1 if reserve is waiting for patron pickup
393 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
394 } elsif ($res->{priority} > 0) {
395 if (defined($res->{itemnumber})) {
396 my $item = GetItem($res->{itemnumber});
397 $reserve{'itemnumber'} = $res->{'itemnumber'};
398 $reserve{'barcodenumber'} = $item->{'barcode'};
399 $reserve{'item_level_hold'} = 1;
403 # get borrowers reserve info
404 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
406 $reserve{'date'} = format_date( $res->{'reservedate'} );
407 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
408 $reserve{'biblionumber'} = $res->{'biblionumber'};
409 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
410 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
411 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
412 $reserve{'notes'} = $res->{'reservenotes'};
414 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
415 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
416 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
417 $reserve{'voldesc'} = $res->{'volumeddesc'};
418 $reserve{'ccode'} = $res->{'ccode'};
419 $reserve{'barcode'} = $res->{'barcode'};
420 $reserve{'priority'} = $res->{'priority'};
421 $reserve{'branchloop'} = \@branchloop;
422 $reserve{'optionloop'} = \@optionloop;
424 push( @reserveloop, \%reserve );
427 my $default = C4::Context->userenv->{branch};
431 foreach my $branchcode (sort keys %{$branches} ) {
432 push @values, $branchcode;
433 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
435 my $CGIbranch = CGI::scrolling_list(
439 -default => $default,
440 -labels => \%label_of,
445 # get the time for the form name...
449 CGIbranch => $CGIbranch,
450 reserveloop => \@reserveloop,
452 fixedRank => $fixedRank,
457 optionloop => \@optionloop,
458 bibitemloop => \@bibitemloop,
460 biblionumber => $biblionumber,
461 findborrower => $findborrower,
462 cardnumber => $cardnumber,
463 CGIselectborrower => $CGIselectborrower,
464 title => $dat->{title},
465 author => $dat->{author},
467 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
468 borrower_branchcode => $borrowerinfo->{'branchcode'},
472 output_html_with_http_headers $input, $cookie, $template->output;