Bug 5187: Show place of publication (MARC21, 260$c), XSLT
[koha.git] / reserve / request.pl
blob859a65b5376c2fd20ebc1b0e0a649a6dc563bd6e
1 #!/usr/bin/perl
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
12 # version.
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
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 =head1 request.pl
24 script to place reserves/requests
26 =cut
28 use strict;
29 use warnings;
30 use C4::Branch; # GetBranches get_branchinfos_of
31 use CGI;
32 use List::MoreUtils qw/uniq/;
33 use Date::Calc qw/Date_to_Days/;
34 use C4::Output;
35 use C4::Auth;
36 use C4::Reserves;
37 use C4::Biblio;
38 use C4::Items;
39 use C4::Koha;
40 use C4::Circulation;
41 use C4::Dates qw/format_date/;
42 use C4::Members;
43 use C4::Search; # enabled_staff_search_views
45 my $dbh = C4::Context->dbh;
46 my $sth;
47 my $input = new CGI;
48 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
50 template_name => "reserve/request.tmpl",
51 query => $input,
52 type => "intranet",
53 authnotrequired => 0,
54 flagsrequired => { reserveforothers => 'place_holds' },
58 my $multihold = $input->param('multi_hold');
59 $template->param(multi_hold => $multihold);
61 # get Branches and Itemtypes
62 my $branches = GetBranches();
63 my $itemtypes = GetItemTypes();
65 my $default = C4::Context->userenv->{branch};
66 my @values;
67 my %label_of;
69 foreach my $branchcode (sort keys %{$branches} ) {
70 push @values, $branchcode;
71 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
73 my $CGIbranch = CGI::scrolling_list(
74 -name => 'pickup',
75 -id => 'pickup',
76 -values => \@values,
77 -default => $default,
78 -labels => \%label_of,
79 -size => 1,
80 -multiple => 0,
83 # Select borrowers infos
84 my $findborrower = $input->param('findborrower');
85 $findborrower = '' unless defined $findborrower;
86 $findborrower =~ s|,| |g;
87 my $cardnumber = $input->param('cardnumber') || '';
88 my $borrowerslist;
89 my $messageborrower;
90 my $warnings;
91 my $messages;
93 my $date = C4::Dates->today('iso');
94 my $action = $input->param('action');
96 if ( $action eq 'move' ) {
97 my $where = $input->param('where');
98 my $borrowernumber = $input->param('borrowernumber');
99 my $biblionumber = $input->param('biblionumber');
101 AlterPriority( $where, $borrowernumber, $biblionumber );
103 } elsif ( $action eq 'cancel' ) {
104 my $borrowernumber = $input->param('borrowernumber');
105 my $biblionumber = $input->param('biblionumber');
106 CancelReserve( $biblionumber, '', $borrowernumber );
107 } elsif ( $action eq 'setLowestPriority' ) {
108 my $borrowernumber = $input->param('borrowernumber');
109 my $biblionumber = $input->param('biblionumber');
110 ToggleLowestPriority( $borrowernumber, $biblionumber );
113 if ($findborrower) {
114 my ( $count, $borrowers ) =
115 SearchMember($findborrower, 'cardnumber', 'web' );
117 my @borrowers = @$borrowers;
119 if ( $#borrowers == -1 ) {
120 $input->param( 'findborrower', '' );
121 $messageborrower = "'$findborrower'";
123 elsif ( $#borrowers == 0 ) {
124 $input->param( 'cardnumber', $borrowers[0]->{'cardnumber'} );
125 $cardnumber = $borrowers[0]->{'cardnumber'};
127 else {
128 $borrowerslist = \@borrowers;
132 if ($cardnumber) {
133 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
134 my $diffbranch;
135 my @getreservloop;
136 my $count_reserv = 0;
137 my $maxreserves;
139 # we check the reserves of the borrower, and if he can reserv a document
140 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
142 my $number_reserves =
143 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
145 if ( $number_reserves > C4::Context->preference('maxreserves') ) {
146 $warnings = 1;
147 $maxreserves = 1;
150 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
151 my $expiry_date = $borrowerinfo->{dateexpiry};
152 my $expiry = 0; # flag set if patron account has expired
153 if ($expiry_date and $expiry_date ne '0000-00-00' and
154 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
155 $messages = $expiry = 1;
159 # check if the borrower make the reserv in a different branch
160 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
161 $messages = 1;
162 $diffbranch = 1;
165 $template->param(
166 borrowernumber => $borrowerinfo->{'borrowernumber'},
167 borrowersurname => $borrowerinfo->{'surname'},
168 borrowerfirstname => $borrowerinfo->{'firstname'},
169 borrowerstreetaddress => $borrowerinfo->{'address'},
170 borrowercity => $borrowerinfo->{'city'},
171 borrowerphone => $borrowerinfo->{'phone'},
172 borrowermobile => $borrowerinfo->{'mobile'},
173 borrowerfax => $borrowerinfo->{'fax'},
174 borrowerphonepro => $borrowerinfo->{'phonepro'},
175 borroweremail => $borrowerinfo->{'email'},
176 borroweremailpro => $borrowerinfo->{'emailpro'},
177 borrowercategory => $borrowerinfo->{'category'},
178 borrowerreservs => $count_reserv,
179 maxreserves => $maxreserves,
180 expiry => $expiry,
181 diffbranch => $diffbranch,
182 messages => $messages,
183 warnings => $warnings
187 $template->param( messageborrower => $messageborrower );
189 my $CGIselectborrower;
190 if ($borrowerslist) {
191 my @values;
192 my %labels;
194 foreach my $borrower (
195 sort {
196 $a->{surname}
197 . $a->{firstname} cmp $b->{surname}
198 . $b->{firstname}
199 } @{$borrowerslist}
202 push @values, $borrower->{cardnumber};
204 $labels{ $borrower->{cardnumber} } = sprintf(
205 '%s, %s ... (%s - %s) ... %s',
206 $borrower->{surname}, $borrower->{firstname},
207 $borrower->{cardnumber}, $borrower->{categorycode},
208 $borrower->{address},
212 $CGIselectborrower = CGI::scrolling_list(
213 -name => 'cardnumber',
214 -values => \@values,
215 -labels => \%labels,
216 -size => 7,
217 -multiple => 0,
221 # FIXME launch another time GetMemberDetails perhaps until
222 my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
224 my @biblionumbers = ();
225 my $biblionumbers = $input->param('biblionumbers');
226 if ($multihold) {
227 @biblionumbers = split '/', $biblionumbers;
228 } else {
229 push @biblionumbers, $input->param('biblionumber');
232 my @biblioloop = ();
233 foreach my $biblionumber (@biblionumbers) {
235 my %biblioloopiter = ();
236 my $maxreserves;
238 my $dat = GetBiblioData($biblionumber);
240 if ( not CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
241 $warnings = 1;
242 $maxreserves = 1;
244 # get existing reserves .....
245 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
246 my $totalcount = $count;
247 my $alreadyreserved;
249 foreach my $res (@$reserves) {
250 if ( defined $res->{found} && $res->{found} eq 'W' ) {
251 $count--;
254 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
255 $warnings = 1;
256 $alreadyreserved = 1;
257 $biblioloopiter{warn} = 1;
258 $biblioloopiter{alreadyres} = 1;
262 $template->param( alreadyreserved => $alreadyreserved,
263 messages => $messages,
264 warnings => $warnings,
265 maxreserves=>$maxreserves
269 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
270 # make priorities options
272 my @optionloop;
273 for ( 1 .. $count + 1 ) {
274 push(
275 @optionloop,
277 num => $_,
278 selected => ( $_ == $count + 1 ),
282 # adding a fixed value for priority options
283 my $fixedRank = $count+1;
285 my @branchcodes;
286 my %itemnumbers_of_biblioitem;
287 my @itemnumbers;
289 ## $items is array of 'item' table numbers
290 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
291 @itemnumbers = @$items;
293 else {
294 $template->param('noitems' => 1);
295 $biblioloopiter{noitems} = 1;
298 ## Hash of item number to 'item' table fields
299 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
301 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
302 ## when by definition all of the itemnumber have the same biblioitemnumber
303 foreach my $itemnumber (@itemnumbers) {
304 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
305 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
308 ## Should be same as biblionumber
309 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
311 my $notforloan_label_of = get_notforloan_label_of();
313 ## Hash of biblioitemnumber to 'biblioitem' table records
314 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
316 my @bibitemloop;
318 foreach my $biblioitemnumber (@biblioitemnumbers) {
319 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
320 my $num_available = 0;
321 my $num_override = 0;
323 $biblioitem->{description} =
324 $itemtypes->{ $biblioitem->{itemtype} }{description};
325 $biblioloopiter{description} = $biblioitem->{description};
326 $biblioloopiter{itypename} = $biblioitem->{description};
327 $biblioloopiter{imageurl} =
328 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
330 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
331 my $item = $iteminfos_of->{$itemnumber};
333 unless (C4::Context->preference('item-level_itypes')) {
334 $item->{itype} = $biblioitem->{itemtype};
337 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
338 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
339 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
341 # if the holdingbranch is different than the homebranch, we show the
342 # holdingbranch of the document too
343 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
344 $item->{holdingbranchname} =
345 $branches->{ $item->{holdingbranch} }{branchname};
348 # add information
349 $item->{itemcallnumber} = $item->{itemcallnumber};
351 # if the item is currently on loan, we display its return date and
352 # change the background color
353 my $issues= GetItemIssue($itemnumber);
354 if ( $issues->{'date_due'} ) {
355 $item->{date_due} = format_date($issues->{'date_due'});
356 $item->{backgroundcolor} = 'onloan';
359 # checking reserve
360 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
361 my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
363 if ( defined $reservedate ) {
364 $item->{backgroundcolor} = 'reserved';
365 $item->{reservedate} = format_date($reservedate);
366 $item->{ReservedForBorrowernumber} = $reservedfor;
367 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
368 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
369 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
373 # Management of the notforloan document
374 if ( $item->{notforloan} ) {
375 $item->{backgroundcolor} = 'other';
376 $item->{notforloanvalue} =
377 $notforloan_label_of->{ $item->{notforloan} };
380 # Management of lost or long overdue items
381 if ( $item->{itemlost} ) {
383 # FIXME localized strings should never be in Perl code
384 $item->{message} =
385 $item->{itemlost} == 1 ? "(lost)"
386 : $item->{itemlost} == 2 ? "(long overdue)"
387 : "";
388 $item->{backgroundcolor} = 'other';
391 # Check the transit status
392 my ( $transfertwhen, $transfertfrom, $transfertto ) =
393 GetTransfers($itemnumber);
395 if ( defined $transfertwhen && $transfertwhen ne '' ) {
396 $item->{transfertwhen} = format_date($transfertwhen);
397 $item->{transfertfrom} =
398 $branches->{$transfertfrom}{branchname};
399 $item->{transfertto} = $branches->{$transfertto}{branchname};
400 $item->{nocancel} = 1;
403 # If there is no loan, return and transfer, we show a checkbox.
404 $item->{notforloan} = $item->{notforloan} || 0;
406 # if independent branches is on we need to check if the person can reserve
407 # for branches they arent logged in to
408 if ( C4::Context->preference("IndependantBranches") ) {
409 if (! C4::Context->preference("canreservefromotherbranches")){
410 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
411 my $userenv = C4::Context->userenv;
412 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
413 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
418 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
420 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
421 my $policy_holdallowed = 1;
423 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
425 if ( $branchitemrule->{'holdallowed'} == 0 ||
426 ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
427 $policy_holdallowed = 0;
430 if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve} and CanItemBeReserved($borrowerinfo->{borrowernumber}, $itemnumber) ) {
431 if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
432 $item->{override} = 1;
433 $num_override++;
434 } elsif ( $policy_holdallowed ) {
435 $item->{available} = 1;
436 $num_available++;
439 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
441 # FIXME: move this to a pm
442 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
443 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
444 while (my $wait_hashref = $sth2->fetchrow_hashref) {
445 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
447 push @{ $biblioitem->{itemloop} }, $item;
450 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
451 $template->param( override_required => 1 );
452 } elsif ( $num_available == 0 ) {
453 $template->param( none_available => 1 );
454 $template->param( warnings => 1 );
455 $biblioloopiter{warn} = 1;
456 $biblioloopiter{none_avail} = 1;
459 push @bibitemloop, $biblioitem;
462 # existingreserves building
463 my @reserveloop;
464 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
465 foreach my $res ( sort {
466 my $a_found = $a->{found} || '';
467 my $b_found = $a->{found} || '';
468 $a_found cmp $b_found;
469 } @$reserves ) {
470 my %reserve;
471 my @optionloop;
472 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
473 push(
474 @optionloop,
476 num => $i,
477 selected => ( $i == $res->{priority} ),
482 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
483 my $item = $res->{'itemnumber'};
484 $item = GetBiblioFromItemNumber($item,undef);
485 $reserve{'wait'}= 1;
486 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
487 $reserve{'biblionumber'}=$item->{'biblionumber'};
488 $reserve{'barcodenumber'} = $item->{'barcode'};
489 $reserve{'wbrcode'} = $res->{'branchcode'};
490 $reserve{'itemnumber'} = $res->{'itemnumber'};
491 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
492 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
493 $reserve{'atdestination'} = 1;
495 # set found to 1 if reserve is waiting for patron pickup
496 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
497 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
498 } elsif ($res->{priority} > 0) {
499 if (defined($res->{itemnumber})) {
500 my $item = GetItem($res->{itemnumber});
501 $reserve{'itemnumber'} = $res->{'itemnumber'};
502 $reserve{'barcodenumber'} = $item->{'barcode'};
503 $reserve{'item_level_hold'} = 1;
507 # get borrowers reserve info
508 my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
509 if (C4::Context->preference('HidePatronName')){
510 $reserve{'hidename'} = 1;
511 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
513 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
514 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
515 $reserve{'date'} = format_date( $res->{'reservedate'} );
516 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
517 $reserve{'biblionumber'} = $res->{'biblionumber'};
518 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
519 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
520 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
521 $reserve{'notes'} = $res->{'reservenotes'};
522 $reserve{'wait'} =
523 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
524 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
525 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
526 $reserve{'voldesc'} = $res->{'volumeddesc'};
527 $reserve{'ccode'} = $res->{'ccode'};
528 $reserve{'barcode'} = $res->{'barcode'};
529 $reserve{'priority'} = $res->{'priority'};
530 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
531 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
532 $reserve{'optionloop'} = \@optionloop;
534 push( @reserveloop, \%reserve );
537 # get the time for the form name...
538 my $time = time();
540 $template->param(
541 CGIbranch => $CGIbranch,
543 time => $time,
544 fixedRank => $fixedRank,
547 # display infos
548 $template->param(
549 optionloop => \@optionloop,
550 bibitemloop => \@bibitemloop,
551 date => $date,
552 biblionumber => $biblionumber,
553 findborrower => $findborrower,
554 cardnumber => $cardnumber,
555 title => $dat->{title},
556 author => $dat->{author},
557 holdsview => 1,
558 C4::Search::enabled_staff_search_views,
560 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
561 $template->param(
562 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
563 borrower_branchcode => $borrowerinfo->{'branchcode'},
566 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
568 $biblioloopiter{biblionumber} = $biblionumber;
569 $biblioloopiter{title} = $dat->{title};
570 $biblioloopiter{rank} = $fixedRank;
571 $biblioloopiter{reserveloop} = \@reserveloop;
573 if (@reserveloop) {
574 $template->param( reserveloop => \@reserveloop );
578 push @biblioloop, \%biblioloopiter;
581 $template->param( biblioloop => \@biblioloop );
582 $template->param( biblionumbers => $biblionumbers );
583 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
585 if ($multihold) {
586 $template->param( multi_hold => 1 );
589 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
590 $template->param( reserve_in_future => 1 );
593 # printout the page
594 output_html_with_http_headers $input, $cookie, $template->output;