Bug 16154: Fix some other occurrences
[koha.git] / circ / circulation.pl
index f16475e..4a38f71 100755 (executable)
@@ -43,13 +43,17 @@ use Koha::Holds;
 use C4::Context;
 use CGI::Session;
 use C4::Members::Attributes qw(GetBorrowerAttributes);
-use Koha::Borrower::Debarments qw(GetDebarments IsDebarred);
+use Koha::Patron;
+use Koha::Patron::Debarments qw(GetDebarments IsDebarred);
 use Koha::DateUtils;
 use Koha::Database;
+use Koha::Patron::Messages;
+use Koha::Patron::Images;
+use Koha::SearchEngine;
+use Koha::SearchEngine::Search;
 
 use Date::Calc qw(
   Today
-  Add_Delta_YM
   Add_Delta_Days
   Date_to_Days
 );
@@ -88,7 +92,9 @@ if (!C4::Context->userenv && !$branch){
 }
 
 my $barcodes = [];
-if ( my $barcode = $query->param('barcode') ) {
+my $barcode =  $query->param('barcode');
+# Barcode given by user could be '0'
+if ( $barcode || $barcode eq '0' ) {
     $barcodes = [ $barcode ];
 } else {
     my $filefh = $query->upload('uploadfile');
@@ -101,7 +107,7 @@ if ( my $barcode = $query->param('barcode') ) {
         push @$barcodes, split( /\s\n/, $list );
         $barcodes = [ map { $_ =~ /^\s*$/ ? () : $_ } @$barcodes ];
     } else {
-        @$barcodes = $query->param('barcodes');
+        @$barcodes = $query->multi_param('barcodes');
     }
 }
 
@@ -141,11 +147,11 @@ if (!C4::Auth::haspermission( C4::Context->userenv->{id} , { circulate => 'force
 
 my $onsite_checkout = $query->param('onsite_checkout');
 
-my @failedrenews = $query->param('failedrenew');    # expected to be itemnumbers
+my @failedrenews = $query->multi_param('failedrenew');    # expected to be itemnumbers
 our %renew_failed = ();
 for (@failedrenews) { $renew_failed{$_} = 1; }
 
-my @failedreturns = $query->param('failedreturn');
+my @failedreturns = $query->multi_param('failedreturn');
 our %return_failed = ();
 for (@failedreturns) { $return_failed{$_} = 1; }
 
@@ -277,13 +283,6 @@ if ($borrowernumber) {
     my (  $today_year,   $today_month,   $today_day) = Today();
     my ($warning_year, $warning_month, $warning_day) = split /-/, $borrower->{'dateexpiry'};
     my (  $enrol_year,   $enrol_month,   $enrol_day) = split /-/, $borrower->{'dateenrolled'};
-    # Renew day is calculated by adding the enrolment period to today
-    my (  $renew_year,   $renew_month,   $renew_day);
-    if ($enrol_year*$enrol_month*$enrol_day>0) {
-        (  $renew_year,   $renew_month,   $renew_day) =
-        Add_Delta_YM( $enrol_year, $enrol_month, $enrol_day,
-            0 , $borrower->{'enrolmentperiod'});
-    }
     # if the expiry date is before today ie they have expired
     if ( !$borrower->{'dateexpiry'} || $warning_year*$warning_month*$warning_day==0
         || Date_to_Days($today_year,     $today_month, $today_day  ) 
@@ -291,11 +290,9 @@ if ($borrowernumber) {
     {
         #borrowercard expired, no issues
         $template->param(
-            flagged  => "1",
             noissues => ($force_allow_issue) ? 0 : "1",
             forceallow => $force_allow_issue,
             expired => "1",
-            renewaldate => "$renew_year-$renew_month-$renew_day",
         );
     }
     # check for NotifyBorrowerDeparture
@@ -305,7 +302,6 @@ if ($borrowernumber) {
     {
         # borrower card soon to expire warn librarian
         $template->param( "warndeparture" => $borrower->{dateexpiry} ,
-                          flagged         => "1"
                         );
         if (C4::Context->preference('ReturnBeforeExpiry')){
             $template->param("returnbeforeexpiry" => 1);
@@ -359,16 +355,16 @@ if (@$barcodes) {
     {
      $template_params->{FALLBACK} = 1;
 
+        my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
         my $query = "kw=" . $barcode;
-        my ( $searcherror, $results, $total_hits ) = SimpleSearch($query);
+        my ( $searcherror, $results, $total_hits ) = $searcher->simple_search_compat($query, 0, 10);
 
         # if multiple hits, offer options to librarian
         if ( $total_hits > 0 ) {
             my @options = ();
             foreach my $hit ( @{$results} ) {
                 my $chosen =
-                  TransformMarcToKoha( C4::Context->dbh,
-                    C4::Search::new_record_from_zebra('biblioserver',$hit) );
+                  TransformMarcToKoha( C4::Search::new_record_from_zebra('biblioserver',$hit) );
 
                 # offer all barcodes individually
                 if ( $chosen->{barcode} ) {
@@ -396,7 +392,12 @@ if (@$barcodes) {
         my $confirm_required = 0;
         unless($issueconfirmed){
             #  Get the item title for more information
-            $template_params->{additional_materials} = $iteminfo->{'materials'};
+            my $materials = $iteminfo->{'materials'};
+            my $avcode = GetAuthValCode('items.materials');
+            if ($avcode) {
+                $materials = GetKohaAuthorisedValueLib($avcode, $materials);
+            }
+            $template_params->{additional_materials} = $materials;
             $template_params->{itemhomebranch} = $iteminfo->{'homebranch'};
 
             # pass needsconfirmation to template if issuing is possible and user hasn't yet confirmed.
@@ -422,11 +423,16 @@ if (@$barcodes) {
 
     if ($question->{RESERVE_WAITING} or $question->{RESERVED}){
         $template->param(
-            reserveborrowernumber => $question->{'resborrowernumber'},
-            itembiblionumber => $getmessageiteminfo->{'biblionumber'}
+            reserveborrowernumber => $question->{'resborrowernumber'}
         );
     }
 
+    $template->param(
+        itembiblionumber => $getmessageiteminfo->{'biblionumber'}
+    );
+
+
+
     $template_params->{issuecount} = $issue;
 
     if ( $iteminfo ) {
@@ -457,9 +463,10 @@ if ($borrowernumber) {
 # show all reserves of this borrower, and the position of the reservation ....
 if ($borrowernumber) {
     my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } );
+    my $waiting_holds = $holds->waiting;
     $template->param(
         holds_count  => $holds->count(),
-        WaitingHolds => scalar $holds->waiting(),
+        WaitingHolds => $waiting_holds,
     );
 
     $template->param( adultborrower => 1 ) if ( $borrower->{category_type} eq 'A' || $borrower->{category_type} eq 'I' );
@@ -468,7 +475,6 @@ if ($borrowernumber) {
 #title
 my $flags = $borrower->{'flags'};
 foreach my $flag ( sort keys %$flags ) {
-    $template->param( flagged=> 1);
     $flags->{$flag}->{'message'} =~ s#\n#<br />#g;
     if ( $flags->{$flag}->{'noissues'} ) {
         $template->param(
@@ -535,7 +541,7 @@ foreach my $flag ( sort keys %$flags ) {
     }
 }
 
-my $amountold = $borrower->{flags}->{'CHARGES'}->{'message'} || 0;
+my $amountold = $borrower->{flags} ? $borrower->{flags}->{'CHARGES'}->{'message'} || 0 : 0;
 $amountold =~ s/^.*\$//;    # remove upto the $, if any
 
 my ( $total, $accts, $numaccts) = GetMemberAccountRecords( $borrowernumber );
@@ -547,11 +553,19 @@ if ( $borrowernumber && $borrower->{'category_type'} eq 'C') {
     $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
 }
 
-my $lib_messages_loop = GetMessages( $borrowernumber, 'L', $branch );
-if($lib_messages_loop){ $template->param(flagged => 1 ); }
+my $librarian_messages = Koha::Patron::Messages->search(
+    {
+        borrowernumber => $borrowernumber,
+        message_type => 'L',
+    }
+);
 
-my $bor_messages_loop = GetMessages( $borrowernumber, 'B', $branch );
-if($bor_messages_loop){ $template->param(flagged => 1 ); }
+my $patron_messages = Koha::Patron::Messages->search(
+    {
+        borrowernumber => $borrowernumber,
+        message_type => 'B',
+    }
+);
 
 my $fast_cataloging = 0;
 if (defined getframeworkinfo('FA')) {
@@ -569,7 +583,17 @@ my $view = $batch
     ?'batch_checkout_view'
     : 'circview';
 
-my @relatives = GetMemberRelatives( $borrower->{'borrowernumber'} );
+my @relatives;
+if ( $borrowernumber ) {
+    if ( my $patron = Koha::Patrons->find( $borrower->{borrowernumber} ) ) {
+        if ( my $guarantor = $patron->guarantor ) {
+            push @relatives, $guarantor->borrowernumber;
+            push @relatives, $_->borrowernumber for $patron->siblings;
+        } else {
+            push @relatives, $_->borrowernumber for $patron->guarantees;
+        }
+    }
+}
 my $relatives_issues_count =
   Koha::Database->new()->schema()->resultset('Issue')
   ->count( { borrowernumber => \@relatives } );
@@ -590,17 +614,17 @@ if ($restoreduedatespec || $stickyduedate) {
 }
 
 $template->param(
-    lib_messages_loop => $lib_messages_loop,
-    bor_messages_loop => $bor_messages_loop,
-    all_messages_del  => C4::Context->preference('AllowAllMessageDeletion'),
+    librarian_messages => $librarian_messages,
+    patron_messages   => $patron_messages,
     findborrower      => $findborrower,
     borrower          => $borrower,
     borrowernumber    => $borrowernumber,
+    categoryname      => $borrower->{'description'},
     branch            => $branch,
     branchname        => GetBranchName($borrower->{'branchcode'}),
     printer           => $printer,
     printername       => $printer,
-    was_renewed       => $query->param('was_renewed') ? 1 : 0,
+    was_renewed       => scalar $query->param('was_renewed') ? 1 : 0,
     expiry            => $borrower->{'dateexpiry'},
     roadtype          => $roadtype,
     amountold         => $amountold,
@@ -625,8 +649,8 @@ $template->param(
     relatives_borrowernumbers => \@relatives,
 );
 
-my ($picture, $dberror) = GetPatronImage($borrower->{'borrowernumber'});
-$template->param( picture => 1 ) if $picture;
+my $patron_image = Koha::Patron::Images->find($borrower->{borrowernumber});
+$template->param( picture => 1 ) if $patron_image;
 
 # get authorised values with type of BOR_NOTES
 
@@ -636,7 +660,6 @@ $template->param(
     debt_confirmed            => $debt_confirmed,
     SpecifyDueDate            => $duedatespec_allow,
     CircAutocompl             => C4::Context->preference("CircAutocompl"),
-    AllowRenewalLimitOverride => C4::Context->preference("AllowRenewalLimitOverride"),
     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' } ),