Bug 14842: Wrong call to GetBranchItemRule
[srvgit] / circ / circulation.pl
index 131c8b0..8b2d8d8 100755 (executable)
@@ -9,34 +9,38 @@
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
-use CGI;
+use CGI qw ( -utf8 );
+use DateTime;
+use DateTime::Duration;
 use C4::Output;
 use C4::Print;
-use C4::Auth qw/:DEFAULT get_session/;
+use C4::Auth qw/:DEFAULT get_session haspermission/;
 use C4::Dates qw/format_date/;
 use C4::Branch; # GetBranches
 use C4::Koha;   # GetPrinter
 use C4::Circulation;
+use C4::Utils::DataTables::Members;
 use C4::Members;
 use C4::Biblio;
 use C4::Search;
 use MARC::Record;
 use C4::Reserves;
+use Koha::Holds;
 use C4::Context;
 use CGI::Session;
 use C4::Members::Attributes qw(GetBorrowerAttributes);
@@ -98,6 +102,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user (
 my $branches = GetBranches();
 
 my $force_allow_issue = $query->param('forceallow') || 0;
+if (!C4::Auth::haspermission( C4::Context->userenv->{id} , { circulate => 'force_checkout' } )) {
+    $force_allow_issue = 0;
+}
+
+my $onsite_checkout = $query->param('onsite_checkout');
 
 my @failedrenews = $query->param('failedrenew');    # expected to be itemnumbers
 our %renew_failed = ();
@@ -153,7 +162,10 @@ else {
 my ($datedue,$invalidduedate);
 
 my $duedatespec_allow = C4::Context->preference('SpecifyDueDate');
-if($duedatespec_allow){
+if( $onsite_checkout && !$duedatespec_allow ) {
+    $datedue = output_pref({ dt => dt_from_string, dateonly => 1, dateformat => 'iso' });
+    $datedue .= ' 23:59:00';
+} elsif( $duedatespec_allow ) {
     if ($duedatespec) {
         if ($duedatespec =~ C4::Dates->regexp('syspref')) {
                 $datedue = dt_from_string($duedatespec);
@@ -192,32 +204,31 @@ if ( $print eq 'yes' && $borrowernumber ne '' ) {
 # STEP 2 : FIND BORROWER
 # if there is a list of find borrowers....
 #
-my $borrowerslist;
 my $message;
 if ($findborrower) {
-    my $borrowers = Search($findborrower, 'cardnumber') || [];
-    if (C4::Context->preference("AddPatronLists")) {
-        $template->param(
-            "AddPatronLists_".C4::Context->preference("AddPatronLists")=> "1",
+    my $borrower = C4::Members::GetMember( cardnumber => $findborrower );
+    if ( $borrower ) {
+        $borrowernumber = $borrower->{borrowernumber};
+    } else {
+        my $dt_params = { iDisplayLength => -1 };
+        my $results = C4::Utils::DataTables::Members::search(
+            {
+                searchmember => $findborrower,
+                dt_params => $dt_params,
+            }
         );
-        if (C4::Context->preference("AddPatronLists")=~/code/){
-            my $categories = GetBorrowercategoryList;
-            $categories->[0]->{'first'} = 1;
-            $template->param(categories=>$categories);
+        my $borrowers = $results->{patrons};
+        if ( scalar @$borrowers == 1 ) {
+            $borrowernumber = $borrowers->[0]->{borrowernumber};
+            $query->param( 'borrowernumber', $borrowernumber );
+            $query->param( 'barcode',           '' );
+        } elsif ( @$borrowers ) {
+            $template->param( borrowers => $borrowers );
+        } else {
+            $query->param( 'findborrower', '' );
+            $message = "'$findborrower'";
         }
     }
-    if ( @$borrowers == 0 ) {
-        $query->param( 'findborrower', '' );
-        $message = "'$findborrower'";
-    }
-    elsif ( @$borrowers == 1 ) {
-        $borrowernumber = $borrowers->[0]->{'borrowernumber'};
-        $query->param( 'borrowernumber', $borrowernumber );
-        $query->param( 'barcode',           '' );
-    }
-    else {
-        $borrowerslist = $borrowers;
-    }
 }
 
 # get the borrower information.....
@@ -331,13 +342,15 @@ if ($barcode) {
         }
     }
 
-    delete $question->{'DEBT'} if ($debt_confirmed);
-    foreach my $impossible ( keys %$error ) {
-        $template->param(
-            $impossible => $$error{$impossible},
-            IMPOSSIBLE  => 1
-        );
-        $blocker = 1;
+    unless( $onsite_checkout and C4::Context->preference("OnSiteCheckoutsForce") ) {
+        delete $question->{'DEBT'} if ($debt_confirmed);
+        foreach my $impossible ( keys %$error ) {
+            $template->param(
+                $impossible => $$error{$impossible},
+                IMPOSSIBLE  => 1
+            );
+            $blocker = 1;
+        }
     }
     if( !$blocker || $force_allow_issue ){
         my $confirm_required = 0;
@@ -353,13 +366,15 @@ if ($barcode) {
                     $needsconfirmation => $$question{$needsconfirmation},
                     getTitleMessageIteminfo => $getmessageiteminfo->{'title'},
                     getBarcodeMessageIteminfo => $getmessageiteminfo->{'barcode'},
-                    NEEDSCONFIRMATION  => 1
+                    NEEDSCONFIRMATION  => 1,
+                    onsite_checkout => $onsite_checkout,
                 );
                 $confirm_required = 1;
             }
         }
         unless($confirm_required) {
-            AddIssue( $borrower, $barcode, $datedue, $cancelreserve, undef, undef, $session->param('auto_renew') );
+            my $issue = AddIssue( $borrower, $barcode, $datedue, $cancelreserve, undef, undef, { onsite_checkout => $onsite_checkout, auto_renew => $session->param('auto_renew') } );
+            $template->param( issue => $issue );
             $session->clear('auto_renew');
             $inprocess = 1;
         }
@@ -367,6 +382,13 @@ if ($barcode) {
     
     my ( $od, $issue, $fines ) = GetMemberIssuesAndFines($borrowernumber);
     $template->param( issuecount => $issue );
+
+    if ($question->{RESERVE_WAITING} or $question->{RESERVED}){
+        $template->param(
+            reserveborrowernumber => $question->{'resborrowernumber'},
+            itembiblionumber => $getmessageiteminfo->{'biblionumber'}
+        );
+    }
 }
 
 # reload the borrower info for the sake of reseting the flags.....
@@ -378,30 +400,13 @@ if ($borrowernumber) {
 # BUILD HTML
 # show all reserves of this borrower, and the position of the reservation ....
 if ($borrowernumber) {
+    my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } );
     $template->param(
-        holds_count => Koha::Database->new()->schema()->resultset('Reserve')
-          ->count( { borrowernumber => $borrowernumber } ) );
-
-    $template->param( adultborrower => 1 ) if ( $borrower->{'category_type'} eq 'A' );
-}
+        holds_count  => $holds->count(),
+        WaitingHolds => scalar $holds->waiting(),
+    );
 
-my @values;
-my %labels;
-my $selectborrower;
-if ($borrowerslist) {
-    foreach (
-        sort {(lc $a->{'surname'} cmp lc $b->{'surname'} || lc $a->{'firstname'} cmp lc $b->{'firstname'})
-        } @$borrowerslist
-      )
-    {
-        push @values, $_->{'borrowernumber'};
-        $labels{ $_->{'borrowernumber'} } =
-"$_->{'surname'}, $_->{'firstname'} ... ($_->{'cardnumber'} - $_->{'categorycode'} - $_->{'branchcode'}) ...  $_->{'address'} ";
-    }
-    $selectborrower = {
-        values => \@values,
-        labels => \%labels,
-    };
+    $template->param( adultborrower => 1 ) if ( $borrower->{category_type} eq 'A' || $borrower->{category_type} eq 'I' );
 }
 
 #title
@@ -492,12 +497,6 @@ if($lib_messages_loop){ $template->param(flagged => 1 ); }
 my $bor_messages_loop = GetMessages( $borrowernumber, 'B', $branch );
 if($bor_messages_loop){ $template->param(flagged => 1 ); }
 
-# Computes full borrower address
-my @fulladdress;
-push @fulladdress, $borrower->{'streetnumber'} if ( $borrower->{'streetnumber'} );
-push @fulladdress, C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $borrower->{'streettype'} ) if ( $borrower->{'streettype'} );
-push @fulladdress, $borrower->{'address'} if ( $borrower->{'address'} );
-
 my $fast_cataloging = 0;
 if (defined getframeworkinfo('FA')) {
     $fast_cataloging = 1 
@@ -516,6 +515,10 @@ my $relatives_issues_count =
   Koha::Database->new()->schema()->resultset('Issue')
   ->count( { borrowernumber => \@relatives } );
 
+my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $borrower->{streettype} );
+
+$template->param(%$borrower);
+
 $template->param(
     lib_messages_loop => $lib_messages_loop,
     bor_messages_loop => $bor_messages_loop,
@@ -527,34 +530,14 @@ $template->param(
     branchname        => GetBranchName($borrower->{'branchcode'}),
     printer           => $printer,
     printername       => $printer,
-    firstname         => $borrower->{'firstname'},
-    surname           => $borrower->{'surname'},
-    showname          => $borrower->{'showname'},
-    category_type     => $borrower->{'category_type'},
     was_renewed       => $query->param('was_renewed') ? 1 : 0,
     expiry            => format_date($borrower->{'dateexpiry'}),
-    categorycode      => $borrower->{'categorycode'},
-    categoryname      => $borrower->{description},
-    address           => join(' ', @fulladdress),
-    address2          => $borrower->{'address2'},
-    email             => $borrower->{'email'},
-    emailpro          => $borrower->{'emailpro'},
-    borrowernotes     => $borrower->{'borrowernotes'},
-    city              => $borrower->{'city'},
-    state              => $borrower->{'state'},
-    zipcode           => $borrower->{'zipcode'},
-    country           => $borrower->{'country'},
-    phone             => $borrower->{'phone'},
-    mobile            => $borrower->{'mobile'},
-    phonepro          => $borrower->{'phonepro'},
-    cardnumber        => $borrower->{'cardnumber'},
-    othernames        => $borrower->{'othernames'},
+    roadtype          => $roadtype,
     amountold         => $amountold,
     barcode           => $barcode,
     stickyduedate     => $stickyduedate,
     duedatespec       => $duedatespec,
     message           => $message,
-    selectborrower    => $selectborrower,
     totaldue          => sprintf('%.2f', $total),
     inprocess         => $inprocess,
     is_child          => ($borrowernumber && $borrower->{'category_type'} eq 'C'),
@@ -587,10 +570,9 @@ $template->param(
     SpecifyDueDate            => $duedatespec_allow,
     CircAutocompl             => C4::Context->preference("CircAutocompl"),
     AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
-    export_remove_fields      => C4::Context->preference("ExportRemoveFields"),
-    export_with_csv_profile   => C4::Context->preference("ExportWithCsvProfile"),
     canned_bor_notes_loop     => $canned_notes,
     debarments                => GetDebarments({ borrowernumber => $borrowernumber }),
+    todaysdate                => output_pref( { dt => dt_from_string()->set(hour => 23)->set(minute => 59), dateformat => 'sql' } ),
 );
 
 output_html_with_http_headers $query, $cookie, $template->output;