Bug 7213 - simple /svc/ HTTP example
[koha.git] / reserve / request.pl
blobff01850d3a0395b189e41e00ae81cae8a236c48c
1 #!/usr/bin/perl
4 #writen 2/1/00 by chris@katipo.oc.nz
5 # Copyright 2000-2002 Katipo Communications
6 # Parts Copyright 2011 Catalyst IT
8 # This file is part of Koha.
10 # Koha is free software; you can redistribute it and/or modify it under the
11 # terms of the GNU General Public License as published by the Free Software
12 # Foundation; either version 2 of the License, or (at your option) any later
13 # version.
15 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with Koha; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 =head1 request.pl
25 script to place reserves/requests
27 =cut
29 use strict;
30 use warnings;
31 use C4::Branch;
32 use CGI;
33 use List::MoreUtils qw/uniq/;
34 use Date::Calc qw/Date_to_Days/;
35 use C4::Output;
36 use C4::Auth;
37 use C4::Reserves;
38 use C4::Biblio;
39 use C4::Items;
40 use C4::Koha;
41 use C4::Circulation;
42 use C4::Dates qw/format_date/;
43 use C4::Members;
44 use C4::Search; # enabled_staff_search_views
45 use Koha::DateUtils;
47 my $dbh = C4::Context->dbh;
48 my $sth;
49 my $input = new CGI;
50 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52 template_name => "reserve/request.tmpl",
53 query => $input,
54 type => "intranet",
55 authnotrequired => 0,
56 flagsrequired => { reserveforothers => 'place_holds' },
60 my $multihold = $input->param('multi_hold');
61 $template->param(multi_hold => $multihold);
62 my $showallitems = $input->param('showallitems');
64 # get Branches and Itemtypes
65 my $branches = GetBranches();
66 my $itemtypes = GetItemTypes();
68 my $default = C4::Context->userenv->{branch};
69 my @values;
70 my %label_of;
72 foreach my $branchcode (sort keys %{$branches} ) {
73 push @values, $branchcode;
74 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
76 my $CGIbranch = CGI::scrolling_list(
77 -name => 'pickup',
78 -id => 'pickup',
79 -values => \@values,
80 -default => $default,
81 -labels => \%label_of,
82 -size => 1,
83 -multiple => 0,
86 # Select borrowers infos
87 my $findborrower = $input->param('findborrower');
88 $findborrower = '' unless defined $findborrower;
89 $findborrower =~ s|,| |g;
90 my $borrowernumber_hold = $input->param('borrowernumber') || '';
91 my $borrowerslist;
92 my $messageborrower;
93 my $warnings;
94 my $messages;
96 my $date = C4::Dates->today('iso');
97 my $action = $input->param('action');
98 $action ||= q{};
100 if ( $action eq 'move' ) {
101 my $where = $input->param('where');
102 my $borrowernumber = $input->param('borrowernumber');
103 my $biblionumber = $input->param('biblionumber');
104 AlterPriority( $where, $borrowernumber, $biblionumber );
105 } elsif ( $action eq 'cancel' ) {
106 my $borrowernumber = $input->param('borrowernumber');
107 my $biblionumber = $input->param('biblionumber');
108 CancelReserve( $biblionumber, '', $borrowernumber );
109 } elsif ( $action eq 'setLowestPriority' ) {
110 my $borrowernumber = $input->param('borrowernumber');
111 my $biblionumber = $input->param('biblionumber');
112 ToggleLowestPriority( $borrowernumber, $biblionumber );
113 } elsif ( $action eq 'toggleSuspend' ) {
114 my $borrowernumber = $input->param('borrowernumber');
115 my $biblionumber = $input->param('biblionumber');
116 ToggleSuspend( $borrowernumber, $biblionumber );
119 if ($findborrower) {
120 my $borrowers = Search($findborrower, 'cardnumber');
122 if ($borrowers && @$borrowers) {
123 if ( @$borrowers == 1 ) {
124 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
126 else {
127 $borrowerslist = $borrowers;
129 } else {
130 $messageborrower = "'$findborrower'";
134 # If we have the borrowernumber because we've performed an action, then we
135 # don't want to try to place another reserve.
136 if ($borrowernumber_hold && !$action) {
137 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
138 my $diffbranch;
139 my @getreservloop;
140 my $count_reserv = 0;
141 my $maxreserves;
143 # we check the reserves of the borrower, and if he can reserv a document
144 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
146 my $number_reserves =
147 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
149 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
150 $warnings = 1;
151 $maxreserves = 1;
154 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
155 my $expiry_date = $borrowerinfo->{dateexpiry};
156 my $expiry = 0; # flag set if patron account has expired
157 if ($expiry_date and $expiry_date ne '0000-00-00' and
158 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
159 $messages = $expiry = 1;
162 # check if the borrower make the reserv in a different branch
163 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
164 $messages = 1;
165 $diffbranch = 1;
168 $template->param(
169 borrowernumber => $borrowerinfo->{'borrowernumber'},
170 borrowersurname => $borrowerinfo->{'surname'},
171 borrowerfirstname => $borrowerinfo->{'firstname'},
172 borrowerstreetaddress => $borrowerinfo->{'address'},
173 borrowercity => $borrowerinfo->{'city'},
174 borrowerphone => $borrowerinfo->{'phone'},
175 borrowermobile => $borrowerinfo->{'mobile'},
176 borrowerfax => $borrowerinfo->{'fax'},
177 borrowerphonepro => $borrowerinfo->{'phonepro'},
178 borroweremail => $borrowerinfo->{'email'},
179 borroweremailpro => $borrowerinfo->{'emailpro'},
180 borrowercategory => $borrowerinfo->{'category'},
181 borrowerreservs => $count_reserv,
182 cardnumber => $borrowerinfo->{'cardnumber'},
183 maxreserves => $maxreserves,
184 expiry => $expiry,
185 diffbranch => $diffbranch,
186 messages => $messages,
187 warnings => $warnings
191 $template->param( messageborrower => $messageborrower );
193 my $CGIselectborrower;
194 if ($borrowerslist) {
195 my @values;
196 my %labels;
198 foreach my $borrower (
199 sort {
200 uc($a->{surname}
201 . $a->{firstname}) cmp uc($b->{surname}
202 . $b->{firstname})
203 } @{$borrowerslist}
206 push @values, $borrower->{borrowernumber};
208 $labels{ $borrower->{borrowernumber} } = sprintf(
209 '%s, %s ... (%s - %s) ... %s',
210 $borrower->{surname} ||'', $borrower->{firstname} || '',
211 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
212 $borrower->{address} || '',
216 $CGIselectborrower = CGI::scrolling_list(
217 -name => 'borrowernumber',
218 -values => \@values,
219 -labels => \%labels,
220 -size => 7,
221 -multiple => 0,
225 # FIXME launch another time GetMember perhaps until
226 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
228 my @biblionumbers = ();
229 my $biblionumbers = $input->param('biblionumbers');
230 if ($multihold) {
231 @biblionumbers = split '/', $biblionumbers;
232 } else {
233 push @biblionumbers, $input->param('biblionumber');
236 my $itemdata_enumchron = 0;
237 my @biblioloop = ();
238 foreach my $biblionumber (@biblionumbers) {
240 my %biblioloopiter = ();
241 my $maxreserves;
243 my $dat = GetBiblioData($biblionumber);
245 unless ( CanBookBeReserved($borrowerinfo->{borrowernumber}, $biblionumber) ) {
246 $warnings = 1;
247 $maxreserves = 1;
249 # get existing reserves .....
250 my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
251 my $totalcount = $count;
252 my $alreadyreserved;
254 foreach my $res (@$reserves) {
255 if ( defined $res->{found} && $res->{found} eq 'W' ) {
256 $count--;
259 if ( defined $borrowerinfo && ($borrowerinfo->{borrowernumber} eq $res->{borrowernumber}) ) {
260 $warnings = 1;
261 $alreadyreserved = 1;
262 $biblioloopiter{warn} = 1;
263 $biblioloopiter{alreadyres} = 1;
267 $template->param( alreadyreserved => $alreadyreserved,
268 messages => $messages,
269 warnings => $warnings,
270 maxreserves=>$maxreserves
274 # FIXME think @optionloop, is maybe obsolete, or must be switchable by a systeme preference fixed rank or not
275 # make priorities options
277 my @optionloop;
278 for ( 1 .. $count + 1 ) {
279 push(
280 @optionloop,
282 num => $_,
283 selected => ( $_ == $count + 1 ),
287 # adding a fixed value for priority options
288 my $fixedRank = $count+1;
290 my @branchcodes;
291 my %itemnumbers_of_biblioitem;
292 my @itemnumbers;
294 ## $items is array of 'item' table numbers
295 if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
296 @itemnumbers = @$items;
298 my @hostitems = get_hostitemnumbers_of($biblionumber);
299 if (@hostitems){
300 $template->param('hostitemsflag' => 1);
301 push(@itemnumbers, @hostitems);
304 if (!@itemnumbers) {
305 $template->param('noitems' => 1);
306 $biblioloopiter{noitems} = 1;
309 ## Hash of item number to 'item' table fields
310 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
312 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
313 ## when by definition all of the itemnumber have the same biblioitemnumber
314 foreach my $itemnumber (@itemnumbers) {
315 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
316 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
319 ## Should be same as biblionumber
320 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
322 my $notforloan_label_of = get_notforloan_label_of();
324 ## Hash of biblioitemnumber to 'biblioitem' table records
325 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
327 my @bibitemloop;
329 foreach my $biblioitemnumber (@biblioitemnumbers) {
330 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
331 my $num_available = 0;
332 my $num_override = 0;
333 my $hiddencount = 0;
335 $biblioitem->{description} =
336 $itemtypes->{ $biblioitem->{itemtype} }{description};
337 if($biblioitem->{biblioitemnumber} ne $biblionumber){
338 $biblioitem->{hostitemsflag}=1;
340 $biblioloopiter{description} = $biblioitem->{description};
341 $biblioloopiter{itypename} = $biblioitem->{description};
342 $biblioloopiter{imageurl} =
343 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
345 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
346 my $item = $iteminfos_of->{$itemnumber};
348 unless (C4::Context->preference('item-level_itypes')) {
349 $item->{itype} = $biblioitem->{itemtype};
352 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
353 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
354 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
356 # if the holdingbranch is different than the homebranch, we show the
357 # holdingbranch of the document too
358 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
359 $item->{holdingbranchname} =
360 $branches->{ $item->{holdingbranch} }{branchname};
363 if($item->{biblionumber} ne $biblionumber){
364 $item->{hostitemsflag}=1;
365 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
368 # add information
369 $item->{itemcallnumber} = $item->{itemcallnumber};
371 # if the item is currently on loan, we display its return date and
372 # change the background color
373 my $issues= GetItemIssue($itemnumber);
374 if ( $issues->{'date_due'} ) {
375 $item->{date_due} = format_sqldatetime($issues->{date_due});
376 $item->{backgroundcolor} = 'onloan';
379 # checking reserve
380 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
381 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
383 if ( defined $reservedate ) {
384 $item->{backgroundcolor} = 'reserved';
385 $item->{reservedate} = format_date($reservedate);
386 $item->{ReservedForBorrowernumber} = $reservedfor;
387 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
388 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
389 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
393 # Management of the notforloan document
394 if ( $item->{notforloan} ) {
395 $item->{backgroundcolor} = 'other';
396 $item->{notforloanvalue} =
397 $notforloan_label_of->{ $item->{notforloan} };
400 # Management of lost or long overdue items
401 if ( $item->{itemlost} ) {
403 # FIXME localized strings should never be in Perl code
404 $item->{message} =
405 $item->{itemlost} == 1 ? "(lost)"
406 : $item->{itemlost} == 2 ? "(long overdue)"
407 : "";
408 $item->{backgroundcolor} = 'other';
409 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
410 $item->{hide} = 1;
411 $hiddencount++;
415 # Check the transit status
416 my ( $transfertwhen, $transfertfrom, $transfertto ) =
417 GetTransfers($itemnumber);
419 if ( defined $transfertwhen && $transfertwhen ne '' ) {
420 $item->{transfertwhen} = format_date($transfertwhen);
421 $item->{transfertfrom} =
422 $branches->{$transfertfrom}{branchname};
423 $item->{transfertto} = $branches->{$transfertto}{branchname};
424 $item->{nocancel} = 1;
427 # If there is no loan, return and transfer, we show a checkbox.
428 $item->{notforloan} = $item->{notforloan} || 0;
430 # if independent branches is on we need to check if the person can reserve
431 # for branches they arent logged in to
432 if ( C4::Context->preference("IndependantBranches") ) {
433 if (! C4::Context->preference("canreservefromotherbranches")){
434 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
435 my $userenv = C4::Context->userenv;
436 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
437 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
442 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
444 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
445 my $policy_holdallowed = 1;
447 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
449 if ( $branchitemrule->{'holdallowed'} == 0 ||
450 ( $branchitemrule->{'holdallowed'} == 1 &&
451 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
452 $policy_holdallowed = 0;
455 if (
456 $policy_holdallowed
457 && !$item->{cantreserve}
458 && IsAvailableForItemLevelRequest($itemnumber)
459 && CanItemBeReserved(
460 $borrowerinfo->{borrowernumber}, $itemnumber
464 $item->{available} = 1;
465 $num_available++;
467 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
469 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
470 $item->{override} = 1;
471 $num_override++;
474 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
476 # FIXME: move this to a pm
477 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
478 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
479 while (my $wait_hashref = $sth2->fetchrow_hashref) {
480 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
483 # Show serial enumeration when needed
484 if ($item->{enumchron}) {
485 $itemdata_enumchron = 1;
488 push @{ $biblioitem->{itemloop} }, $item;
491 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
492 $template->param( override_required => 1 );
493 } elsif ( $num_available == 0 ) {
494 $template->param( none_available => 1 );
495 $template->param( warnings => 1 );
496 $biblioloopiter{warn} = 1;
497 $biblioloopiter{none_avail} = 1;
499 $template->param( hiddencount => $hiddencount);
501 push @bibitemloop, $biblioitem;
504 # existingreserves building
505 my @reserveloop;
506 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
507 foreach my $res ( sort {
508 my $a_found = $a->{found} || '';
509 my $b_found = $a->{found} || '';
510 $a_found cmp $b_found;
511 } @$reserves ) {
512 my %reserve;
513 my @optionloop;
514 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
515 push(
516 @optionloop,
518 num => $i,
519 selected => ( $i == $res->{priority} ),
524 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
525 my $item = $res->{'itemnumber'};
526 $item = GetBiblioFromItemNumber($item,undef);
527 $reserve{'wait'}= 1;
528 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
529 $reserve{'biblionumber'}=$item->{'biblionumber'};
530 $reserve{'barcodenumber'} = $item->{'barcode'};
531 $reserve{'wbrcode'} = $res->{'branchcode'};
532 $reserve{'itemnumber'} = $res->{'itemnumber'};
533 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
534 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
535 $reserve{'atdestination'} = 1;
537 # set found to 1 if reserve is waiting for patron pickup
538 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
539 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
540 } elsif ($res->{priority} > 0) {
541 if (defined($res->{itemnumber})) {
542 my $item = GetItem($res->{itemnumber});
543 $reserve{'itemnumber'} = $res->{'itemnumber'};
544 $reserve{'barcodenumber'} = $item->{'barcode'};
545 $reserve{'item_level_hold'} = 1;
549 # get borrowers reserve info
550 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
551 if (C4::Context->preference('HidePatronName')){
552 $reserve{'hidename'} = 1;
553 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
555 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
556 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
557 $reserve{'date'} = format_date( $res->{'reservedate'} );
558 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
559 $reserve{'biblionumber'} = $res->{'biblionumber'};
560 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
561 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
562 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
563 $reserve{'notes'} = $res->{'reservenotes'};
564 $reserve{'wait'} =
565 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
566 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
567 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
568 $reserve{'voldesc'} = $res->{'volumeddesc'};
569 $reserve{'ccode'} = $res->{'ccode'};
570 $reserve{'barcode'} = $res->{'barcode'};
571 $reserve{'priority'} = $res->{'priority'};
572 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
573 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
574 $reserve{'optionloop'} = \@optionloop;
575 $reserve{'suspend'} = $res->{'suspend'};
576 $reserve{'suspend_until'} = C4::Dates->new( $res->{'suspend_until'}, "iso")->output("syspref");
577 push( @reserveloop, \%reserve );
580 # get the time for the form name...
581 my $time = time();
583 $template->param(
584 CGIbranch => $CGIbranch,
586 time => $time,
587 fixedRank => $fixedRank,
590 # display infos
591 $template->param(
592 optionloop => \@optionloop,
593 bibitemloop => \@bibitemloop,
594 itemdata_enumchron => $itemdata_enumchron,
595 date => $date,
596 biblionumber => $biblionumber,
597 findborrower => $findborrower,
598 title => $dat->{title},
599 author => $dat->{author},
600 holdsview => 1,
601 C4::Search::enabled_staff_search_views,
603 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
604 $template->param(
605 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
606 borrower_branchcode => $borrowerinfo->{'branchcode'},
609 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
611 $biblioloopiter{biblionumber} = $biblionumber;
612 $biblioloopiter{title} = $dat->{title};
613 $biblioloopiter{rank} = $fixedRank;
614 $biblioloopiter{reserveloop} = \@reserveloop;
616 if (@reserveloop) {
617 $template->param( reserveloop => \@reserveloop );
620 push @biblioloop, \%biblioloopiter;
623 $template->param( biblioloop => \@biblioloop );
624 $template->param( biblionumbers => $biblionumbers );
625 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
627 if ($multihold) {
628 $template->param( multi_hold => 1 );
631 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
632 $template->param( reserve_in_future => 1 );
635 $template->param( AutoResumeSuspendedHolds => C4::Context->preference('AutoResumeSuspendedHolds') );
637 # printout the page
638 output_html_with_http_headers $input, $cookie, $template->output;