Bug 7245 Tables population with mandatory data for italian installation
[koha.git] / reserve / request.pl
blob21183da4a2fb27c391f07431ded4ea60e79ff917
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
46 my $dbh = C4::Context->dbh;
47 my $sth;
48 my $input = new CGI;
49 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
51 template_name => "reserve/request.tmpl",
52 query => $input,
53 type => "intranet",
54 authnotrequired => 0,
55 flagsrequired => { reserveforothers => 'place_holds' },
59 my $multihold = $input->param('multi_hold');
60 $template->param(multi_hold => $multihold);
61 my $showallitems = $input->param('showallitems');
63 # get Branches and Itemtypes
64 my $branches = GetBranches();
65 my $itemtypes = GetItemTypes();
67 my $default = C4::Context->userenv->{branch};
68 my @values;
69 my %label_of;
71 foreach my $branchcode (sort keys %{$branches} ) {
72 push @values, $branchcode;
73 $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
75 my $CGIbranch = CGI::scrolling_list(
76 -name => 'pickup',
77 -id => 'pickup',
78 -values => \@values,
79 -default => $default,
80 -labels => \%label_of,
81 -size => 1,
82 -multiple => 0,
85 # Select borrowers infos
86 my $findborrower = $input->param('findborrower');
87 $findborrower = '' unless defined $findborrower;
88 $findborrower =~ s|,| |g;
89 my $borrowernumber_hold = $input->param('borrowernumber') || '';
90 my $borrowerslist;
91 my $messageborrower;
92 my $warnings;
93 my $messages;
95 my $date = C4::Dates->today('iso');
96 my $action = $input->param('action');
97 $action ||= q{};
99 if ( $action eq 'move' ) {
100 my $where = $input->param('where');
101 my $borrowernumber = $input->param('borrowernumber');
102 my $biblionumber = $input->param('biblionumber');
103 AlterPriority( $where, $borrowernumber, $biblionumber );
104 } elsif ( $action eq 'cancel' ) {
105 my $borrowernumber = $input->param('borrowernumber');
106 my $biblionumber = $input->param('biblionumber');
107 CancelReserve( $biblionumber, '', $borrowernumber );
108 } elsif ( $action eq 'setLowestPriority' ) {
109 my $borrowernumber = $input->param('borrowernumber');
110 my $biblionumber = $input->param('biblionumber');
111 ToggleLowestPriority( $borrowernumber, $biblionumber );
114 if ($findborrower) {
115 my $borrowers = Search($findborrower, 'cardnumber');
117 if ($borrowers && @$borrowers) {
118 if ( @$borrowers == 1 ) {
119 $borrowernumber_hold = $borrowers->[0]->{'borrowernumber'};
121 else {
122 $borrowerslist = $borrowers;
124 } else {
125 $messageborrower = "'$findborrower'";
129 # If we have the borrowernumber because we've performed an action, then we
130 # don't want to try to place another reserve.
131 if ($borrowernumber_hold && !$action) {
132 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
133 my $diffbranch;
134 my @getreservloop;
135 my $count_reserv = 0;
136 my $maxreserves;
138 # we check the reserves of the borrower, and if he can reserv a document
139 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
141 my $number_reserves =
142 GetReserveCount( $borrowerinfo->{'borrowernumber'} );
144 if ( C4::Context->preference('maxreserves') && ($number_reserves >= C4::Context->preference('maxreserves')) ) {
145 $warnings = 1;
146 $maxreserves = 1;
149 # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
150 my $expiry_date = $borrowerinfo->{dateexpiry};
151 my $expiry = 0; # flag set if patron account has expired
152 if ($expiry_date and $expiry_date ne '0000-00-00' and
153 Date_to_Days(split /-/,$date) > Date_to_Days(split /-/,$expiry_date)) {
154 $messages = $expiry = 1;
157 # check if the borrower make the reserv in a different branch
158 if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
159 $messages = 1;
160 $diffbranch = 1;
163 $template->param(
164 borrowernumber => $borrowerinfo->{'borrowernumber'},
165 borrowersurname => $borrowerinfo->{'surname'},
166 borrowerfirstname => $borrowerinfo->{'firstname'},
167 borrowerstreetaddress => $borrowerinfo->{'address'},
168 borrowercity => $borrowerinfo->{'city'},
169 borrowerphone => $borrowerinfo->{'phone'},
170 borrowermobile => $borrowerinfo->{'mobile'},
171 borrowerfax => $borrowerinfo->{'fax'},
172 borrowerphonepro => $borrowerinfo->{'phonepro'},
173 borroweremail => $borrowerinfo->{'email'},
174 borroweremailpro => $borrowerinfo->{'emailpro'},
175 borrowercategory => $borrowerinfo->{'category'},
176 borrowerreservs => $count_reserv,
177 cardnumber => $borrowerinfo->{'cardnumber'},
178 maxreserves => $maxreserves,
179 expiry => $expiry,
180 diffbranch => $diffbranch,
181 messages => $messages,
182 warnings => $warnings
186 $template->param( messageborrower => $messageborrower );
188 my $CGIselectborrower;
189 if ($borrowerslist) {
190 my @values;
191 my %labels;
193 foreach my $borrower (
194 sort {
195 $a->{surname}
196 . $a->{firstname} cmp $b->{surname}
197 . $b->{firstname}
198 } @{$borrowerslist}
201 push @values, $borrower->{borrowernumber};
203 $labels{ $borrower->{borrowernumber} } = sprintf(
204 '%s, %s ... (%s - %s) ... %s',
205 $borrower->{surname} ||'', $borrower->{firstname} || '',
206 $borrower->{cardnumber} || '', $borrower->{categorycode} || '',
207 $borrower->{address} || '',
211 $CGIselectborrower = CGI::scrolling_list(
212 -name => 'borrowernumber',
213 -values => \@values,
214 -labels => \%labels,
215 -size => 7,
216 -multiple => 0,
220 # FIXME launch another time GetMember perhaps until
221 my $borrowerinfo = GetMember( borrowernumber => $borrowernumber_hold );
223 my @biblionumbers = ();
224 my $biblionumbers = $input->param('biblionumbers');
225 if ($multihold) {
226 @biblionumbers = split '/', $biblionumbers;
227 } else {
228 push @biblionumbers, $input->param('biblionumber');
231 my $itemdata_enumchron = 0;
232 my @biblioloop = ();
233 foreach my $biblionumber (@biblionumbers) {
235 my %biblioloopiter = ();
236 my $maxreserves;
238 my $dat = GetBiblioData($biblionumber);
240 unless ( 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 my @hostitems = get_hostitemnumbers_of($biblionumber);
294 if (@hostitems){
295 $template->param('hostitemsflag' => 1);
296 push(@itemnumbers, @hostitems);
299 if (!@itemnumbers) {
300 $template->param('noitems' => 1);
301 $biblioloopiter{noitems} = 1;
304 ## Hash of item number to 'item' table fields
305 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
307 ## Here we go backwards again to create hash of biblioitemnumber to itemnumbers,
308 ## when by definition all of the itemnumber have the same biblioitemnumber
309 foreach my $itemnumber (@itemnumbers) {
310 my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
311 push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
314 ## Should be same as biblionumber
315 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
317 my $notforloan_label_of = get_notforloan_label_of();
319 ## Hash of biblioitemnumber to 'biblioitem' table records
320 my $biblioiteminfos_of = GetBiblioItemInfosOf(@biblioitemnumbers);
322 my @bibitemloop;
324 foreach my $biblioitemnumber (@biblioitemnumbers) {
325 my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
326 my $num_available = 0;
327 my $num_override = 0;
328 my $hiddencount = 0;
330 $biblioitem->{description} =
331 $itemtypes->{ $biblioitem->{itemtype} }{description};
332 if($biblioitem->{biblioitemnumber} ne $biblionumber){
333 $biblioitem->{hostitemsflag}=1;
335 $biblioloopiter{description} = $biblioitem->{description};
336 $biblioloopiter{itypename} = $biblioitem->{description};
337 $biblioloopiter{imageurl} =
338 getitemtypeimagelocation('intranet', $itemtypes->{$biblioitem->{itemtype}}{imageurl});
340 foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ) {
341 my $item = $iteminfos_of->{$itemnumber};
343 unless (C4::Context->preference('item-level_itypes')) {
344 $item->{itype} = $biblioitem->{itemtype};
347 $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
348 $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
349 $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
351 # if the holdingbranch is different than the homebranch, we show the
352 # holdingbranch of the document too
353 if ( $item->{homebranch} ne $item->{holdingbranch} ) {
354 $item->{holdingbranchname} =
355 $branches->{ $item->{holdingbranch} }{branchname};
358 if($item->{biblionumber} ne $biblionumber){
359 $item->{hostitemsflag}=1;
360 $item->{hosttitle} = GetBiblioData($item->{biblionumber})->{title};
363 # add information
364 $item->{itemcallnumber} = $item->{itemcallnumber};
366 # if the item is currently on loan, we display its return date and
367 # change the background color
368 my $issues= GetItemIssue($itemnumber);
369 if ( $issues->{'date_due'} ) {
370 $item->{date_due} = format_date($issues->{'date_due'});
371 $item->{backgroundcolor} = 'onloan';
374 # checking reserve
375 my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
376 my $ItemBorrowerReserveInfo = GetMember( borrowernumber => $reservedfor );
378 if ( defined $reservedate ) {
379 $item->{backgroundcolor} = 'reserved';
380 $item->{reservedate} = format_date($reservedate);
381 $item->{ReservedForBorrowernumber} = $reservedfor;
382 $item->{ReservedForSurname} = $ItemBorrowerReserveInfo->{'surname'};
383 $item->{ReservedForFirstname} = $ItemBorrowerReserveInfo->{'firstname'};
384 $item->{ExpectedAtLibrary} = $branches->{$expectedAt}{branchname};
388 # Management of the notforloan document
389 if ( $item->{notforloan} ) {
390 $item->{backgroundcolor} = 'other';
391 $item->{notforloanvalue} =
392 $notforloan_label_of->{ $item->{notforloan} };
395 # Management of lost or long overdue items
396 if ( $item->{itemlost} ) {
398 # FIXME localized strings should never be in Perl code
399 $item->{message} =
400 $item->{itemlost} == 1 ? "(lost)"
401 : $item->{itemlost} == 2 ? "(long overdue)"
402 : "";
403 $item->{backgroundcolor} = 'other';
404 if (GetHideLostItemsPreference($borrowernumber) && !$showallitems) {
405 $item->{hide} = 1;
406 $hiddencount++;
410 # Check the transit status
411 my ( $transfertwhen, $transfertfrom, $transfertto ) =
412 GetTransfers($itemnumber);
414 if ( defined $transfertwhen && $transfertwhen ne '' ) {
415 $item->{transfertwhen} = format_date($transfertwhen);
416 $item->{transfertfrom} =
417 $branches->{$transfertfrom}{branchname};
418 $item->{transfertto} = $branches->{$transfertto}{branchname};
419 $item->{nocancel} = 1;
422 # If there is no loan, return and transfer, we show a checkbox.
423 $item->{notforloan} = $item->{notforloan} || 0;
425 # if independent branches is on we need to check if the person can reserve
426 # for branches they arent logged in to
427 if ( C4::Context->preference("IndependantBranches") ) {
428 if (! C4::Context->preference("canreservefromotherbranches")){
429 # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
430 my $userenv = C4::Context->userenv;
431 if ( ($userenv) && ( $userenv->{flags} % 2 != 1 ) ) {
432 $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
437 my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
439 my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
440 my $policy_holdallowed = 1;
442 $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
444 if ( $branchitemrule->{'holdallowed'} == 0 ||
445 ( $branchitemrule->{'holdallowed'} == 1 &&
446 $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
447 $policy_holdallowed = 0;
450 if (
451 $policy_holdallowed
452 && !$item->{cantreserve}
453 && IsAvailableForItemLevelRequest($itemnumber)
454 && CanItemBeReserved(
455 $borrowerinfo->{borrowernumber}, $itemnumber
459 $item->{available} = 1;
460 $num_available++;
462 elsif ( C4::Context->preference('AllowHoldPolicyOverride') ) {
464 # If AllowHoldPolicyOverride is set, it should override EVERY restriction, not just branch item rules
465 $item->{override} = 1;
466 $num_override++;
469 # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
471 # FIXME: move this to a pm
472 my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
473 $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
474 while (my $wait_hashref = $sth2->fetchrow_hashref) {
475 $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
478 # Show serial enumeration when needed
479 if ($item->{enumchron}) {
480 $itemdata_enumchron = 1;
483 push @{ $biblioitem->{itemloop} }, $item;
486 if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
487 $template->param( override_required => 1 );
488 } elsif ( $num_available == 0 ) {
489 $template->param( none_available => 1 );
490 $template->param( warnings => 1 );
491 $biblioloopiter{warn} = 1;
492 $biblioloopiter{none_avail} = 1;
494 $template->param( hiddencount => $hiddencount);
496 push @bibitemloop, $biblioitem;
499 # existingreserves building
500 my @reserveloop;
501 ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber,1);
502 foreach my $res ( sort {
503 my $a_found = $a->{found} || '';
504 my $b_found = $a->{found} || '';
505 $a_found cmp $b_found;
506 } @$reserves ) {
507 my %reserve;
508 my @optionloop;
509 for ( my $i = 1 ; $i <= $totalcount ; $i++ ) {
510 push(
511 @optionloop,
513 num => $i,
514 selected => ( $i == $res->{priority} ),
519 if ( defined $res->{'found'} && $res->{'found'} eq 'W' || $res->{'found'} eq 'T' ) {
520 my $item = $res->{'itemnumber'};
521 $item = GetBiblioFromItemNumber($item,undef);
522 $reserve{'wait'}= 1;
523 $reserve{'holdingbranch'}=$item->{'holdingbranch'};
524 $reserve{'biblionumber'}=$item->{'biblionumber'};
525 $reserve{'barcodenumber'} = $item->{'barcode'};
526 $reserve{'wbrcode'} = $res->{'branchcode'};
527 $reserve{'itemnumber'} = $res->{'itemnumber'};
528 $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
529 if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
530 $reserve{'atdestination'} = 1;
532 # set found to 1 if reserve is waiting for patron pickup
533 $reserve{'found'} = 1 if $res->{'found'} eq 'W';
534 $reserve{'intransit'} = 1 if $res->{'found'} eq 'T';
535 } elsif ($res->{priority} > 0) {
536 if (defined($res->{itemnumber})) {
537 my $item = GetItem($res->{itemnumber});
538 $reserve{'itemnumber'} = $res->{'itemnumber'};
539 $reserve{'barcodenumber'} = $item->{'barcode'};
540 $reserve{'item_level_hold'} = 1;
544 # get borrowers reserve info
545 my $reserveborrowerinfo = GetMember( borrowernumber => $res->{'borrowernumber'} );
546 if (C4::Context->preference('HidePatronName')){
547 $reserve{'hidename'} = 1;
548 $reserve{'cardnumber'} = $reserveborrowerinfo->{'cardnumber'};
550 $reserve{'expirationdate'} = format_date( $res->{'expirationdate'} )
551 unless ( !defined($res->{'expirationdate'}) || $res->{'expirationdate'} eq '0000-00-00' );
552 $reserve{'date'} = format_date( $res->{'reservedate'} );
553 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
554 $reserve{'biblionumber'} = $res->{'biblionumber'};
555 $reserve{'borrowernumber'} = $res->{'borrowernumber'};
556 $reserve{'firstname'} = $reserveborrowerinfo->{'firstname'};
557 $reserve{'surname'} = $reserveborrowerinfo->{'surname'};
558 $reserve{'notes'} = $res->{'reservenotes'};
559 $reserve{'wait'} =
560 ( ( defined $res->{'found'} and $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) );
561 $reserve{'constrainttypea'} = ( $res->{'constrainttype'} eq 'a' );
562 $reserve{'constrainttypeo'} = ( $res->{'constrainttype'} eq 'o' );
563 $reserve{'voldesc'} = $res->{'volumeddesc'};
564 $reserve{'ccode'} = $res->{'ccode'};
565 $reserve{'barcode'} = $res->{'barcode'};
566 $reserve{'priority'} = $res->{'priority'};
567 $reserve{'lowestPriority'} = $res->{'lowestPriority'};
568 $reserve{'branchloop'} = GetBranchesLoop($res->{'branchcode'});
569 $reserve{'optionloop'} = \@optionloop;
571 push( @reserveloop, \%reserve );
574 # get the time for the form name...
575 my $time = time();
577 $template->param(
578 CGIbranch => $CGIbranch,
580 time => $time,
581 fixedRank => $fixedRank,
584 # display infos
585 $template->param(
586 optionloop => \@optionloop,
587 bibitemloop => \@bibitemloop,
588 itemdata_enumchron => $itemdata_enumchron,
589 date => $date,
590 biblionumber => $biblionumber,
591 findborrower => $findborrower,
592 title => $dat->{title},
593 author => $dat->{author},
594 holdsview => 1,
595 C4::Search::enabled_staff_search_views,
597 if (defined $borrowerinfo && exists $borrowerinfo->{'branchcode'}) {
598 $template->param(
599 borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
600 borrower_branchcode => $borrowerinfo->{'branchcode'},
603 $template->param(CGIselectborrower => $CGIselectborrower) if defined $CGIselectborrower;
605 $biblioloopiter{biblionumber} = $biblionumber;
606 $biblioloopiter{title} = $dat->{title};
607 $biblioloopiter{rank} = $fixedRank;
608 $biblioloopiter{reserveloop} = \@reserveloop;
610 if (@reserveloop) {
611 $template->param( reserveloop => \@reserveloop );
614 push @biblioloop, \%biblioloopiter;
617 $template->param( biblioloop => \@biblioloop );
618 $template->param( biblionumbers => $biblionumbers );
619 $template->param( DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar() );
621 if ($multihold) {
622 $template->param( multi_hold => 1 );
625 if ( C4::Context->preference( 'AllowHoldDateInFuture' ) ) {
626 $template->param( reserve_in_future => 1 );
629 # printout the page
630 output_html_with_http_headers $input, $cookie, $template->output;