[followup](bug #4051) add borrower "title", and complete address in csv
[koha.git] / reserve / request.pl
index e26fc14..8c55e54 100755 (executable)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
 
-# $Id$
 
 #writen 2/1/00 by chris@katipo.oc.nz
 # Copyright 2000-2002 Katipo Communications
@@ -27,19 +26,21 @@ script to place reserves/requests
 =cut
 
 use strict;
+use warnings;
 use C4::Branch; # GetBranches get_branchinfos_of
 use CGI;
 use List::MoreUtils qw/uniq/;
-use Date::Calc qw/Today Date_to_Days/;
+use Date::Calc qw/Date_to_Days/;
 use C4::Output;
-use C4::Interface::CGI::Output;
 use C4::Auth;
-use C4::Reserves2;
+use C4::Reserves;
 use C4::Biblio;
+use C4::Items;
 use C4::Koha;
-use C4::Circulation::Circ2;
-use C4::Date;
+use C4::Circulation;
+use C4::Dates qw/format_date/;
 use C4::Members;
+use C4::Search;                # enabled_staff_search_views
 
 my $dbh = C4::Context->dbh;
 my $sth;
@@ -54,6 +55,10 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
     }
 );
 
+# get Branches and Itemtypes
+my $branches = GetBranches();
+my $itemtypes = GetItemTypes();
+
 # get biblio information....
 my $biblionumber = $input->param('biblionumber');
 my $dat          = GetBiblioData($biblionumber);
@@ -62,15 +67,16 @@ my $dat          = GetBiblioData($biblionumber);
 my $findborrower = $input->param('findborrower');
 $findborrower =~ s|,| |g;
 my $cardnumber = $input->param('cardnumber');
-my %env;
 my $borrowerslist;
 my $messageborrower;
+my $warnings;
+my $messages;
 
-my $date = sprintf( '%04d-%02d-%02d', Today() );
+my $date = C4::Dates->today('iso');
 
 if ($findborrower) {
     my ( $count, $borrowers ) =
-      BornameSearch( \%env, $findborrower, 'cardnumber', 'web' );
+      SearchMember($findborrower, 'cardnumber', 'web' );
 
     my @borrowers = @$borrowers;
 
@@ -88,42 +94,59 @@ if ($findborrower) {
 }
 
 if ($cardnumber) {
-    my $borrowerinfo = getpatroninformation( \%env, 0, $cardnumber );
-    my $expiry;
+    my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
     my $diffbranch;
     my @getreservloop;
     my $count_reserv = 0;
     my $maxreserves;
 
-#      we check the reserves of the borrower, and if he can reserv a document
+#   we check the reserves of the borrower, and if he can reserv a document
 # FIXME At this time we have a simple count of reservs, but, later, we could improve the infos "title" ...
 
     my $number_reserves =
-      CountReservesFromBorrower( $borrowerinfo->{'borrowernumber'} );
+      GetReserveCount( $borrowerinfo->{'borrowernumber'} );
 
     if ( $number_reserves > C4::Context->preference('maxreserves') ) {
+               $warnings = 1;
         $maxreserves = 1;
     }
 
-    # we check the date expiricy of the borrower
-    my $warning = (Date_to_Days(split /-/,$date) > Date_to_Days( split /-/,$borrowerinfo->{'dateexpiry'}));
-     
-    if ( $warning > 0 ) {
-        $expiry = 1;
+    # we check the date expiry of the borrower (only if there is an expiry date, otherwise, set to 1 (warn)
+    my $expiry_date = $borrowerinfo->{dateexpiry};
+    my $expiry = 0; # flag set if patron account has expired
+    if ($expiry_date and $expiry_date ne '0000-00-00' and
+            Date_to_Days(split (/-/,$date)) > Date_to_Days(split (/-/,$expiry_date))) {
+               $messages = $expiry = 1;
+    }else{
+        $expiry = 0;
     }
 
+
     # check if the borrower make the reserv in a different branch
     if ( $borrowerinfo->{'branchcode'} ne C4::Context->userenv->{'branch'} ) {
+               $messages = 1;
         $diffbranch = 1;
     }
 
     $template->param(
-        borrowersurname   => $borrowerinfo->{'surname'},
-        borrowerfirstname => $borrowerinfo->{'firstname'},
-        borrowerreservs   => $count_reserv,
-        maxreserves       => $maxreserves,
-        expiry            => $expiry,
-        diffbranch        => $diffbranch
+                borrowernumber => $borrowerinfo->{'borrowernumber'},
+                borrowersurname   => $borrowerinfo->{'surname'},
+                borrowerfirstname => $borrowerinfo->{'firstname'},
+                borrowerstreetaddress => $borrowerinfo->{'address'},
+                borrowercity => $borrowerinfo->{'city'},
+                borrowerphone => $borrowerinfo->{'phone'},
+                borrowermobile => $borrowerinfo->{'mobile'},
+                borrowerfax => $borrowerinfo->{'fax'},
+                borrowerphonepro => $borrowerinfo->{'phonepro'},
+                borroweremail => $borrowerinfo->{'email'},
+                borroweremailpro => $borrowerinfo->{'emailpro'},
+                borrowercategory => $borrowerinfo->{'category'},
+                borrowerreservs   => $count_reserv,
+                maxreserves       => $maxreserves,
+                expiry            => $expiry,
+                diffbranch        => $diffbranch,
+                               messages => $messages,
+                               warnings => $warnings
     );
 }
 
@@ -148,7 +171,7 @@ if ($borrowerslist) {
             '%s, %s ... (%s - %s) ... %s',
             $borrower->{surname},    $borrower->{firstname},
             $borrower->{cardnumber}, $borrower->{categorycode},
-            $borrower->{streetaddress},
+            $borrower->{address},
         );
     }
 
@@ -162,24 +185,27 @@ if ($borrowerslist) {
 }
 
 # get existing reserves .....
-my ( $count, $reserves ) = FindReserves($biblionumber);
+my ( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
 my $totalcount = $count;
 my $alreadyreserved;
 
-# FIXME launch another time getpatroninformation perhaps until
-my $borrowerinfo = getpatroninformation( \%env, 0, $cardnumber );
+# FIXME launch another time GetMemberDetails perhaps until
+my $borrowerinfo = GetMemberDetails( 0, $cardnumber );
 
 foreach my $res (@$reserves) {
-    if ( ( $res->{found} eq 'W' ) or ( $res->{priority} == 0 ) ) {
+    if ( ( $res->{found} eq 'W' ) ) {
         $count--;
     }
 
     if ( $borrowerinfo->{borrowernumber} eq $res->{borrowernumber} ) {
+               $warnings = 1;
         $alreadyreserved = 1;
     }
 }
 
-$template->param( alreadyreserved => $alreadyreserved );
+$template->param( alreadyreserved => $alreadyreserved,
+                               messages => $messages,
+                               warnings => $warnings );
 
 # FIXME think @optionloop, is maybe obsolete, or  must be switchable by a systeme preference fixed rank or not
 # make priorities options
@@ -199,74 +225,69 @@ my $fixedRank = $count+1;
 
 my @branchcodes;
 my %itemnumbers_of_biblioitem;
-my @itemnumbers  = @{ get_itemnumbers_of($biblionumber)->{$biblionumber} };
+my @itemnumbers;
+
+if (my $items = get_itemnumbers_of($biblionumber)->{$biblionumber}){
+       @itemnumbers  = @$items;
+}
+else {
+       $template->param('noitems' => 1);
+}
+       
 my $iteminfos_of = GetItemInfosOf(@itemnumbers);
 
 foreach my $itemnumber (@itemnumbers) {
-    push( @branchcodes,
-        $iteminfos_of->{$itemnumber}->{homebranch},
-        $iteminfos_of->{$itemnumber}->{holdingbranch} );
-
     my $biblioitemnumber = $iteminfos_of->{$itemnumber}->{biblioitemnumber};
     push( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} }, $itemnumber );
 }
 
-@branchcodes = uniq @branchcodes;
-
 my @biblioitemnumbers = keys %itemnumbers_of_biblioitem;
 
-my $branchinfos_of      = get_branchinfos_of(@branchcodes);
 my $notforloan_label_of = get_notforloan_label_of();
 my $biblioiteminfos_of  = GetBiblioItemInfosOf(@biblioitemnumbers);
 
-my @itemtypes;
-foreach my $biblioitemnumber (@biblioitemnumbers) {
-    push @itemtypes, $biblioiteminfos_of->{$biblioitemnumber}{itemtype};
-}
-
-my $itemtypeinfos_of = get_itemtypeinfos_of(@itemtypes);
-
-my $return_date_of = get_current_return_date_of(@itemnumbers);
-
 my @bibitemloop;
 
 foreach my $biblioitemnumber (@biblioitemnumbers) {
     my $biblioitem = $biblioiteminfos_of->{$biblioitemnumber};
 
+    my $num_available;
+    my $num_override;
+
     $biblioitem->{description} =
-      $itemtypeinfos_of->{ $biblioitem->{itemtype} }{description};
+      $itemtypes->{ $biblioitem->{itemtype} }{description};
 
-    foreach
-      my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } )
-    {
+    foreach my $itemnumber ( @{ $itemnumbers_of_biblioitem{$biblioitemnumber} } ){
         my $item = $iteminfos_of->{$itemnumber};
+        unless (C4::Context->preference('item-level_itypes')) {
+            $item->{itype} = $biblioitem->{itemtype};
+        }
 
-        $item->{homebranchname} =
-          $branchinfos_of->{ $item->{homebranch} }{branchname};
+        $item->{itypename} = $itemtypes->{ $item->{itype} }{description};
+        $item->{imageurl} = getitemtypeimagelocation( 'intranet', $itemtypes->{ $item->{itype} }{imageurl} );
+        $item->{homebranchname} = $branches->{ $item->{homebranch} }{branchname};
 
         # if the holdingbranch is different than the homebranch, we show the
         # holdingbranch of the document too
         if ( $item->{homebranch} ne $item->{holdingbranch} ) {
             $item->{holdingbranchname} =
-              $branchinfos_of->{ $item->{holdingbranch} }{branchname};
+              $branches->{ $item->{holdingbranch} }{branchname};
         }
         
-#      add inforrmation
-       $item->{itemcallnumber} = $item->{itemcallnumber};
-       
+        #   add information
+        $item->{itemcallnumber} = $item->{itemcallnumber};
+    
         # if the item is currently on loan, we display its return date and
         # change the background color
-        my $date_due;
-
-        if ( defined $return_date_of->{$itemnumber} ) {
-            $date_due = format_date( $return_date_of->{$itemnumber} );
-            $item->{date_due} = $date_due;
+        my $issues= GetItemIssue($itemnumber);
+        if ( $issues->{'date_due'} ) {
+            $item->{date_due} = format_date($issues->{'date_due'});
             $item->{backgroundcolor} = 'onloan';
         }
 
         # checking reserve
-        my ($reservedate,$reservedfor,$expectedAt) = GetFirstReserveDateFromItem($itemnumber);
-        my $ItemBorrowerReserveInfo = getpatroninformation( \%env, $reservedfor, 0);
+        my ($reservedate,$reservedfor,$expectedAt) = GetReservesFromItemnumber($itemnumber);
+        my $ItemBorrowerReserveInfo = GetMemberDetails( $reservedfor, 0);
 
         if ( defined $reservedate ) {
             $item->{backgroundcolor} = 'reserved';
@@ -274,7 +295,7 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
             $item->{ReservedForBorrowernumber}     = $reservedfor;
             $item->{ReservedForSurname}     = $ItemBorrowerReserveInfo->{'surname'};
             $item->{ReservedForFirstname}     = $ItemBorrowerReserveInfo->{'firstname'};
-            $item->{ExpectedAtLibrary}     = $expectedAt;
+            $item->{ExpectedAtLibrary}     = $branches->{$expectedAt}{branchname};
             
         }
 
@@ -296,43 +317,78 @@ foreach my $biblioitemnumber (@biblioitemnumbers) {
             $item->{backgroundcolor} = 'other';
         }
 
-        # Check of the transfered documents
+        # Check the transit status
         my ( $transfertwhen, $transfertfrom, $transfertto ) =
-          get_transfert_infos($itemnumber);
+          GetTransfers($itemnumber);
 
         if ( $transfertwhen ne '' ) {
             $item->{transfertwhen} = format_date($transfertwhen);
             $item->{transfertfrom} =
-              $branchinfos_of->{$transfertfrom}{branchname};
-            $item->{transfertto} = $branchinfos_of->{$transfertto}{branchname};
-               $item->{nocancel} = 1;
+              $branches->{$transfertfrom}{branchname};
+            $item->{transfertto} = $branches->{$transfertto}{branchname};
+        $item->{nocancel} = 1;
         }
 
         # If there is no loan, return and transfer, we show a checkbox.
         $item->{notforloan} = $item->{notforloan} || 0;
+    
+        # if independent branches is on we need to check if the person can reserve
+        # for branches they arent logged in to
+        if ( C4::Context->preference("IndependantBranches") ) { 
+            if (! C4::Context->preference("canreservefromotherbranches")){
+                # cant reserve items so need to check if item homebranch and userenv branch match if not we cant reserve
+                my $userenv = C4::Context->userenv; 
+                if ( ($userenv) && ( $userenv->{flags} %2 != 1 ) ) {
+                    $item->{cantreserve} = 1 if ( $item->{homebranch} ne $userenv->{branch} );
+                } 
+            }
+        }
 
-        # An item is available only if:
-        if (
-            not defined $reservedate    # not reserved yet
-            and $date_due eq ''         # not currently on loan
-            and not $item->{itemlost}   # not lost
-            and not $item->{notforloan} # not forbidden to loan
-            and $transfertwhen eq ''    # not currently on transfert
-          )
-        {
-            $item->{available} = 1;
+        my $branch = C4::Circulation::_GetCircControlBranch($item, $borrowerinfo);
+
+        my $branchitemrule = GetBranchItemRule( $branch, $item->{'itype'} );
+        my $policy_holdallowed = 1;
+
+        $item->{'holdallowed'} = $branchitemrule->{'holdallowed'};
+
+        if ( $branchitemrule->{'holdallowed'} == 0 ||
+                ( $branchitemrule->{'holdallowed'} == 1 && $borrowerinfo->{'branchcode'} ne $item->{'homebranch'} ) ) {
+            $policy_holdallowed = 0;
         }
 
+        if (IsAvailableForItemLevelRequest($itemnumber) and not $item->{cantreserve}) {
+            if ( not $policy_holdallowed and C4::Context->preference( 'AllowHoldPolicyOverride' ) ) {
+                $item->{override} = 1;
+                $num_override++;
+            } elsif ( $policy_holdallowed ) {
+                $item->{available} = 1;
+                $num_available++;
+            }
+        }
+        # If none of the conditions hold true, then neither override nor available is set and the item cannot be checked
+
+        # FIXME: move this to a pm
+        my $sth2 = $dbh->prepare("SELECT * FROM reserves WHERE borrowernumber=? AND itemnumber=? AND found='W'");
+        $sth2->execute($item->{ReservedForBorrowernumber},$item->{itemnumber});
+        while (my $wait_hashref = $sth2->fetchrow_hashref) {
+            $item->{waitingdate} = format_date($wait_hashref->{waitingdate});
+        }
         push @{ $biblioitem->{itemloop} }, $item;
     }
 
+    if ( $num_override == scalar( @{ $biblioitem->{itemloop} } ) ) { # That is, if all items require an override
+        $template->param( override_required => 1 );
+    } elsif ( $num_available == 0 ) {
+        $template->param( none_available => 1 );
+        $template->param( warnings => 1 );
+    }
+
     push @bibitemloop, $biblioitem;
 }
 
 # existingreserves building
 my @reserveloop;
-my $branches = GetBranches();
-my ( $count, $reserves ) = FindReservesInQueue($biblionumber);
+( $count, $reserves ) = GetReservesFromBiblionumber($biblionumber);
 foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
     my %reserve;
     my @optionloop;
@@ -354,24 +410,32 @@ foreach my $res ( sort { $a->{found} cmp $b->{found} } @$reserves ) {
         push( @branchloop, \%abranch );
     }
 
-    if ( ( $res->{'found'} eq 'W' ) or ( $res->{'priority'} eq '0' ) ) {
-        my %env;
+    if ( ( $res->{'found'} eq 'W' ) ) {
         my $item = $res->{'itemnumber'};
-        $item = getiteminformation($item,undef);
+        $item = GetBiblioFromItemNumber($item,undef);
         $reserve{'wait'}= 1; 
         $reserve{'holdingbranch'}=$item->{'holdingbranch'};
         $reserve{'biblionumber'}=$item->{'biblionumber'};
-        $reserve{'barcodenumber'}      = $item->{'barcode'};
+        $reserve{'barcodenumber'}   = $item->{'barcode'};
         $reserve{'wbrcode'} = $res->{'branchcode'};
-        $reserve{'itemnumber'} = $res->{'itemnumber'};
+        $reserve{'itemnumber'}  = $res->{'itemnumber'};
         $reserve{'wbrname'} = $branches->{$res->{'branchcode'}}->{'branchname'};
         if($reserve{'holdingbranch'} eq $reserve{'wbrcode'}){
             $reserve{'atdestination'} = 1;
         }
+        # set found to 1 if reserve is waiting for patron pickup
+        $reserve{'found'} = 1 if $res->{'found'} eq 'W';
+    } elsif ($res->{priority} > 0) {
+        if (defined($res->{itemnumber})) {
+            my $item = GetItem($res->{itemnumber});
+            $reserve{'itemnumber'}  = $res->{'itemnumber'};
+            $reserve{'barcodenumber'}   = $item->{'barcode'};
+            $reserve{'item_level_hold'} = 1;
+        }
     }
     
 #     get borrowers reserve info
-my $reserveborrowerinfo = getpatroninformation( \%env, $res->{'borrowernumber'}, 0);
+my $reserveborrowerinfo = GetMemberDetails( $res->{'borrowernumber'}, 0);
 
     $reserve{'date'}           = format_date( $res->{'reservedate'} );
     $reserve{'borrowernumber'} = $res->{'borrowernumber'};
@@ -387,7 +451,7 @@ my $reserveborrowerinfo = getpatroninformation( \%env, $res->{'borrowernumber'},
     $reserve{'voldesc'}         = $res->{'volumeddesc'};
     $reserve{'ccode'}           = $res->{'ccode'};
     $reserve{'barcode'}         = $res->{'barcode'};
-    $reserve{'priority'}       = $res->{'priority'};
+    $reserve{'priority'}    = $res->{'priority'};
     $reserve{'branchloop'} = \@branchloop;
     $reserve{'optionloop'} = \@optionloop;
 
@@ -398,12 +462,13 @@ my $default = C4::Context->userenv->{branch};
 my @values;
 my %label_of;
 
-foreach my $branchcode ( keys %{$branches} ) {
+foreach my $branchcode (sort keys %{$branches} ) {
     push @values, $branchcode;
     $label_of{$branchcode} = $branches->{$branchcode}->{branchname};
 }
 my $CGIbranch = CGI::scrolling_list(
     -name     => 'pickup',
+    -id          => 'pickup',
     -values   => \@values,
     -default  => $default,
     -labels   => \%label_of,
@@ -418,7 +483,7 @@ $template->param(
     CGIbranch   => $CGIbranch,
     reserveloop => \@reserveloop,
     time        => $time,
-    fixedRank  => $fixedRank,
+    fixedRank   => $fixedRank,
 );
 
 # display infos
@@ -431,7 +496,10 @@ $template->param(
     cardnumber        => $cardnumber,
     CGIselectborrower => $CGIselectborrower,
     title             => $dat->{title},
-    author            => $dat->{author}
+    author            => $dat->{author},
+       holdsview => 1,
+       borrower_branchname => $branches->{$borrowerinfo->{'branchcode'}}->{'branchname'},
+       borrower_branchcode => $borrowerinfo->{'branchcode'},
 );
 
 # printout the page