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/;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
54 flagsrequired => { reserveforothers => 1 },
58 # get Branches and Itemtypes
59 my $branches = GetBranches();
60 my $itemtypes = GetItemTypes();
62 # get biblio information....
63 my $biblionumber = $input->param('biblionumber');
64 my $dat = GetBiblioData($biblionumber);
66 # Select borrowers infos
67 my $findborrower = $input->param('findborrower');
68 $findborrower =~ s|,| |g;
69 my $cardnumber = $input->param('cardnumber');
75 my $date = C4::Dates->today('iso');
78 my ( $count, $borrowers ) =
79 SearchMember($findborrower, 'cardnumber', 'web' );
81 my @borrowers = @$borrowers;
83 if ( $#borrowers == -1 ) {
84 $input->param( 'findborrower', '' );
85 $messageborrower = "'$findborrower'";
87 elsif ( $#borrowers == 0 ) {
88 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
89 $cardnumber = $borrowers[0]->{'cardnumber'};
92 $borrowerslist = \@borrowers;
97 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
100 my $count_reserv = 0;
103 # we check the reserves of the borrower, and if he can reserv a document
104 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
106 my $number_reserves =
107 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
109 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
114 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
115 my $expiry_date = $borrowerinfo->{dateexpiry};
116 my $expiry = 0; # flag set if patron account has expired
117 if ($expiry_date and $expiry_date ne '0000-00-00' and
118 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
119 $messages = $expiry = 1;
125 # check if the borrower make the reserv in a different branch
126 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
132 borrowernumber => $borrowerinfo->{'borrowernumber'},
133 borrowersurname => $borrowerinfo->{'surname'},
134 borrowerfirstname => $borrowerinfo->{'firstname'},
135 borrowerstreetaddress => $borrowerinfo->{'address'},
136 borrowercity => $borrowerinfo->{'city'},
137 borrowerphone => $borrowerinfo->{'phone'},
138 borrowermobile => $borrowerinfo->{'mobile'},
139 borrowerfax => $borrowerinfo->{'fax'},
140 borrowerphonepro => $borrowerinfo->{'phonepro'},
141 borroweremail => $borrowerinfo->{'email'},
142 borroweremailpro => $borrowerinfo->{'emailpro'},
143 borrowercategory => $borrowerinfo->{'category'},
144 borrowerreservs => $count_reserv,
145 maxreserves => $maxreserves,
147 diffbranch => $diffbranch,
148 messages => $messages,
149 warnings => $warnings
153 $template->param( messageborrower => $messageborrower );
155 my $CGIselectborrower;
156 if ($borrowerslist) {
160 foreach my $borrower (
163 . $a->{firstname} cmp $b->{surname}
168 push @values, $borrower->{cardnumber};
170 $labels{ $borrower->{cardnumber} } = sprintf(
171 '%s, %s ... (%s - %s) ... %s',
172 $borrower->{surname}, $borrower->{firstname},
173 $borrower->{cardnumber}, $borrower->{categorycode},
174 $borrower->{address},
178 $CGIselectborrower = CGI::scrolling_list(
179 -name => 'cardnumber',
187 # get existing reserves .....
188 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
189 my $totalcount = $count;
192 # FIXME launch another time GetMemberDetails perhaps until
193 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
195 foreach my $res (@$reserves) {
196 if ( ( $res->{found} eq 'W' ) ) {
200 if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
202 $alreadyreserved = 1;
206 $template->param( alreadyreserved => $alreadyreserved,
207 messages => $messages,
208 warnings => $warnings );
210 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
211 # make priorities options
214 for ( 1 .. $count + 1 ) {
219 selected => ( $_ == $count + 1 ),
223 # adding a fixed value for priority options
224 my $fixedRank = $count+1;
227 my %itemnumbers_of_biblioitem;
230 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
231 @itemnumbers = @$items;
234 $template->param('noitems' => 1);
237 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
239 foreach my $itemnumber (@itemnumbers) {
240 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
241 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
244 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
246 my $notforloan_label_of = get_notforloan_label_of();
247 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
251 foreach my $biblioitemnumber (@biblioitemnumbers) {
252 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
254 $biblioitem->{description} =
255 $itemtypes->{ $biblioitem->{itemtype} }{description};
258 my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
260 my $item = $iteminfos_of->{$itemnumber};
261 unless (C4::Context->preference('item-level_itypes')) {
262 $item->{itype} = $biblioitem->{itemtype};
265 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
266 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
267 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
269 # if the holdingbranch is different than the homebranch, we show the
270 # holdingbranch of the document too
271 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
272 $item->{holdingbranchname} =
273 $branches->{ $item->{holdingbranch} }{branchname};
277 $item->{itemcallnumber} = $item->{itemcallnumber};
279 # if the item is currently on loan, we display its return date and
280 # change the background color
281 my $issues= GetItemIssue($itemnumber);
282 if ( $issues->{'date_due'} ) {
283 $item->{date_due} = format_date($issues->{'date_due'});
284 $item->{backgroundcolor} = 'onloan';
288 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
289 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
291 if ( defined $reservedate ) {
292 $item->{backgroundcolor} = 'reserved';
293 $item->{reservedate} = format_date($reservedate);
294 $item->{ReservedForBorrowernumber} = $reservedfor;
295 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
296 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
297 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
301 # Management of the notforloan document
302 if ( $item->{notforloan} ) {
303 $item->{backgroundcolor} = 'other';
304 $item->{notforloanvalue} =
305 $notforloan_label_of->{ $item->{notforloan} };
308 # Management of lost or long overdue items
309 if ( $item->{itemlost} ) {
311 # FIXME localized strings should never be in Perl code
313 $item->{itemlost} == 1 ? "(lost)"
314 : $item->{itemlost} == 2 ? "(long overdue)"
316 $item->{backgroundcolor} = 'other';
319 # Check the transit status
320 my ( $transfertwhen, $transfertfrom, $transfertto ) =
321 GetTransfers($itemnumber);
323 if ( $transfertwhen ne '' ) {
324 $item->{transfertwhen} = format_date($transfertwhen);
325 $item->{transfertfrom} =
326 $branches->{$transfertfrom}{branchname};
327 $item->{transfertto} = $branches->{$transfertto}{branchname};
328 $item->{nocancel} = 1;
331 # If there is no loan, return and transfer, we show a checkbox.
332 $item->{notforloan} = $item->{notforloan} || 0;
334 # if independent branches is on we need to check if the person can reserve
335 # for branches they arent logged in to
336 if ( C4::Context->preference("IndependantBranches") ) {
337 if (! C4::Context->preference("canreservefromotherbranches")){
338 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
339 my $userenv = C4::Context->userenv;
340 if ( ($userenv) && ( $userenv->{flags} %2 != 1 ) ) {
341 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
346 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
347 $item->{available} = 1;
350 # FIXME: move this to a pm
351 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
352 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
353 while (my $wait_hashref = $sth2->fetchrow_hashref) {
354 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
356 push @{ $biblioitem->{itemloop} }, $item;
359 push @bibitemloop, $biblioitem;
362 # existingreserves building
364 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
365 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
368 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
373 selected => ( $i == $res->{priority} ),
378 foreach my $br ( keys %$branches ) {
380 $abranch{'selected'} = ( $br eq $res->{'branchcode'} );
381 $abranch{'branch'} = $br;
382 $abranch{'branchname'} = $branches->{$br}->{'branchname'};
383 push( @branchloop, \%abranch );
386 if ( ( $res->{'found'} eq 'W' ) ) {
387 my $item = $res->{'itemnumber'};
388 $item = GetBiblioFromItemNumber($item,undef);
390 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
391 $reserve{'biblionumber'}=$item->{'biblionumber'};
392 $reserve{'barcodenumber'} = $item->{'barcode'};
393 $reserve{'wbrcode'} = $res->{'branchcode'};
394 $reserve{'itemnumber'} = $res->{'itemnumber'};
395 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
396 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
397 $reserve{'atdestination'} = 1;
399 # set found to 1 if reserve is waiting for patron pickup
400 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
401 } elsif ($res->{priority} > 0) {
402 if (defined($res->{itemnumber})) {
403 my $item = GetItem($res->{itemnumber});
404 $reserve{'itemnumber'} = $res->{'itemnumber'};
405 $reserve{'barcodenumber'} = $item->{'barcode'};
406 $reserve{'item_level_hold'} = 1;
410 # get borrowers reserve info
411 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
413 $reserve{'date'} = format_date( $res->{'reservedate'} );
414 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
415 $reserve{'biblionumber'} = $res->{'biblionumber'};
416 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
417 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
418 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
419 $reserve{'notes'} = $res->{'reservenotes'};
421 ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
422 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
423 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
424 $reserve{'voldesc'} = $res->{'volumeddesc'};
425 $reserve{'ccode'} = $res->{'ccode'};
426 $reserve{'barcode'} = $res->{'barcode'};
427 $reserve{'priority'} = $res->{'priority'};
428 $reserve{'branchloop'} = \@branchloop;
429 $reserve{'optionloop'} = \@optionloop;
431 push( @reserveloop, \%reserve );
434 my $default = C4::Context->userenv->{branch};
438 foreach my $branchcode (sort keys %{$branches} ) {
439 push @values, $branchcode;
440 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
442 my $CGIbranch = CGI::scrolling_list(
446 -default => $default,
447 -labels => \%label_of,
452 # get the time for the form name...
456 CGIbranch => $CGIbranch,
457 reserveloop => \@reserveloop,
459 fixedRank => $fixedRank,
464 optionloop => \@optionloop,
465 bibitemloop => \@bibitemloop,
467 biblionumber => $biblionumber,
468 findborrower => $findborrower,
469 cardnumber => $cardnumber,
470 CGIselectborrower => $CGIselectborrower,
471 title => $dat->{title},
472 author => $dat->{author},
474 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
475 borrower_branchcode => $borrowerinfo->{'branchcode'},
479 output_html_with_http_headers $input, $cookie, $template->output;