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 = $borrowerinfo->{dateexpiry};
114 unless ($expiry and $expiry ne '0000-00-00' and
115 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry)) {
116 $messages = $expiry = 1;
120 # check if the borrower make the reserv in a different branch
121 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
127 borrowernumber => $borrowerinfo->{'borrowernumber'},
128 borrowersurname => $borrowerinfo->{'surname'},
129 borrowerfirstname => $borrowerinfo->{'firstname'},
130 borrowerstreetaddress => $borrowerinfo->{'address'},
131 borrowercity => $borrowerinfo->{'city'},
132 borrowerphone => $borrowerinfo->{'phone'},
133 borrowermobile => $borrowerinfo->{'mobile'},
134 borrowerfax => $borrowerinfo->{'fax'},
135 borrowerphonepro => $borrowerinfo->{'phonepro'},
136 borroweremail => $borrowerinfo->{'email'},
137 borroweremailpro => $borrowerinfo->{'emailpro'},
138 borrowercategory => $borrowerinfo->{'category'},
139 borrowerreservs => $count_reserv,
140 maxreserves => $maxreserves,
142 diffbranch => $diffbranch,
143 messages => $messages,
144 warnings => $warnings
148 $template->param( messageborrower => $messageborrower );
150 my $CGIselectborrower;
151 if ($borrowerslist) {
155 foreach my $borrower (
158 . $a->{firstname} cmp $b->{surname}
163 push @values, $borrower->{cardnumber};
165 $labels{ $borrower->{cardnumber} } = sprintf(
166 '%s, %s ... (%s - %s) ... %s',
167 $borrower->{surname}, $borrower->{firstname},
168 $borrower->{cardnumber}, $borrower->{categorycode},
169 $borrower->{streetaddress},
173 $CGIselectborrower = CGI::scrolling_list(
174 -name => 'cardnumber',
182 # get existing reserves .....
183 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
184 my $totalcount = $count;
187 # FIXME launch another time GetMemberDetails perhaps until
188 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
190 foreach my $res (@$reserves) {
191 if ( ( $res->{found} eq 'W' ) ) {
195 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
197 $alreadyreserved = 1;
201 $template->param( alreadyreserved => $alreadyreserved,
202 messages => $messages,
203 warnings => $warnings );
205 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
206 # make priorities options
209 for ( 1 .. $count + 1 ) {
214 selected => ( $_ == $count + 1 ),
218 # adding a fixed value for priority options
219 my $fixedRank = $count+1;
222 my %itemnumbers_of_biblioitem;
225 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
226 @itemnumbers = @$items;
229 $template->param('noitems' => 1);
232 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
234 foreach my $itemnumber (@itemnumbers) {
235 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
236 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
239 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
241 my $notforloan_label_of = get_notforloan_label_of();
242 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
246 foreach my $biblioitemnumber (@biblioitemnumbers) {
247 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
249 $biblioitem->{description} =
250 $itemtypes->{ $biblioitem->{itemtype} }{description};
253 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
255 my $item = $iteminfos_of->{$itemnumber};
256 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
257 $item->{imageurl} = getitemtypeimagesrc() . "/".$itemtypes->{ $item->{itype} }{imageurl};
258 $item->{homebranchname} =
259 $branches->{ $item->{homebranch} }{branchname};
261 # if the holdingbranch is different than the homebranch, we show the
262 # holdingbranch of the document too
263 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
264 $item->{holdingbranchname} =
265 $branches->{ $item->{holdingbranch} }{branchname};
269 $item->{itemcallnumber} = $item->{itemcallnumber};
271 # if the item is currently on loan, we display its return date and
272 # change the background color
273 my $issues= GetItemIssue($itemnumber);
274 if ( $issues->{'date_due'} ) {
275 $item->{date_due} = format_date($issues->{'date_due'});
276 $item->{backgroundcolor} = 'onloan';
280 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
281 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
283 if ( defined $reservedate ) {
284 $item->{backgroundcolor} = 'reserved';
285 $item->{reservedate} = format_date($reservedate);
286 $item->{ReservedForBorrowernumber} = $reservedfor;
287 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
288 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
289 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
293 # Management of the notforloan document
294 if ( $item->{notforloan} ) {
295 $item->{backgroundcolor} = 'other';
296 $item->{notforloanvalue} =
297 $notforloan_label_of->{ $item->{notforloan} };
300 # Management of lost or long overdue items
301 if ( $item->{itemlost} ) {
303 # FIXME localized strings should never be in Perl code
305 $item->{itemlost} == 1 ? "(lost)"
306 : $item->{itemlost} == 2 ? "(long overdue)"
308 $item->{backgroundcolor} = 'other';
311 # Check the transit status
312 my ( $transfertwhen, $transfertfrom, $transfertto ) =
313 GetTransfers($itemnumber);
315 if ( $transfertwhen ne '' ) {
316 $item->{transfertwhen} = format_date($transfertwhen);
317 $item->{transfertfrom} =
318 $branches->{$transfertfrom}{branchname};
319 $item->{transfertto} = $branches->{$transfertto}{branchname};
320 $item->{nocancel} = 1;
323 # If there is no loan, return and transfer, we show a checkbox.
324 $item->{notforloan} = $item->{notforloan} || 0;
326 # if independent branches is on we need to check if the person can reserve
327 # for branches they arent logged in to
328 if ( C4::Context->preference("IndependantBranches") ) {
329 if (! C4::Context->preference("canreservefromotherbranches")){
330 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
331 my $userenv = C4::Context->userenv;
332 if ( ($userenv) && ( $userenv->{flags} != 1 ) ) {
333 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
338 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
339 $item->{available} = 1;
342 # FIXME: move this to a pm
343 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
344 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
345 while (my $wait_hashref = $sth2->fetchrow_hashref) {
346 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
348 push @{ $biblioitem->{itemloop} }, $item;
351 push @bibitemloop, $biblioitem;
354 # existingreserves building
356 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
357 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
360 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
365 selected => ( $i == $res->{priority} ),
370 foreach my $br ( keys %$branches ) {
372 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
373 $abranch{'branch'} = $br;
374 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
375 push( @branchloop, \%abranch );
378 if ( ( $res->{'found'} eq 'W' ) ) {
379 my $item = $res->{'itemnumber'};
380 $item = GetBiblioFromItemNumber($item,undef);
382 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
383 $reserve{'biblionumber'}=$item->{'biblionumber'};
384 $reserve{'barcodenumber'} = $item->{'barcode'};
385 $reserve{'wbrcode'} = $res->{'branchcode'};
386 $reserve{'itemnumber'} = $res->{'itemnumber'};
387 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
388 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
389 $reserve{'atdestination'} = 1;
391 # set found to 1 if reserve is waiting for patron pickup
392 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
393 } elsif ($res->{priority} > 0) {
394 if (defined($res->{itemnumber})) {
395 my $item = GetItem($res->{itemnumber});
396 $reserve{'itemnumber'} = $res->{'itemnumber'};
397 $reserve{'barcodenumber'} = $item->{'barcode'};
398 $reserve{'item_level_hold'} = 1;
402 # get borrowers reserve info
403 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
405 $reserve{'date'} = format_date( $res->{'reservedate'} );
406 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
407 $reserve{'biblionumber'} = $res->{'biblionumber'};
408 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
409 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
410 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
411 $reserve{'notes'} = $res->{'reservenotes'};
413 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
414 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
415 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
416 $reserve{'voldesc'} = $res->{'volumeddesc'};
417 $reserve{'ccode'} = $res->{'ccode'};
418 $reserve{'barcode'} = $res->{'barcode'};
419 $reserve{'priority'} = $res->{'priority'};
420 $reserve{'branchloop'} = \@branchloop;
421 $reserve{'optionloop'} = \@optionloop;
423 push( @reserveloop, \%reserve );
426 my $default = C4::Context->userenv->{branch};
430 foreach my $branchcode (sort keys %{$branches} ) {
431 push @values, $branchcode;
432 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
434 my $CGIbranch = CGI::scrolling_list(
438 -default => $default,
439 -labels => \%label_of,
444 # get the time for the form name...
448 CGIbranch => $CGIbranch,
449 reserveloop => \@reserveloop,
451 fixedRank => $fixedRank,
456 optionloop => \@optionloop,
457 bibitemloop => \@bibitemloop,
459 biblionumber => $biblionumber,
460 findborrower => $findborrower,
461 cardnumber => $cardnumber,
462 CGIselectborrower => $CGIselectborrower,
463 title => $dat->{title},
464 author => $dat->{author},
466 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
467 borrower_branchcode => $borrowerinfo->{'branchcode'},
471 output_html_with_http_headers $input, $cookie, $template->output;