Bug 19080: Handle non-existing patrons gratefully
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 10 Aug 2017 17:44:50 +0000 (14:44 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 25 Aug 2017 14:03:37 +0000 (11:03 -0300)
This is a recurrent bug we have over the last years. When a script is
called with non-existent borrowernumber it will crashes.
We need to handle this gracefully instead of letting the script crashes.

On bug 18403 a new subroutine is added to the codebase
(output_and_exit_if_error) to handle this kind of errors correctly.
Since it is not pushed yet, I propose to just redirect to a script that
handle it correctly (circulation.pl) instead of adding this message to
all these scripts.

Test plan:
Hit different scripts from the members module and pass a non-existent
borrowernumber.
You must be redirected to circulation.pl with a friendly message.

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Julian Maurice <julian.maurice@biblibre.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

23 files changed:
circ/circulation.pl
koha-tmpl/intranet-tmpl/prog/en/modules/members/statistics.tt
members/boraccount.pl
members/deletemem.pl
members/discharge.pl
members/files.pl
members/mancredit.pl
members/maninvoice.pl
members/member-flags.pl
members/member-password.pl
members/memberentry.pl
members/notices.pl
members/pay.pl
members/paycollect.pl
members/printfeercpt.pl
members/printinvoice.pl
members/purchase-suggestions.pl
members/readingrec.pl
members/routing-lists.pl
members/statistics.pl
members/summary-print.pl
members/update-child.pl
tools/viewlog.pl

index 6fd4006..130d350 100755 (executable)
@@ -261,8 +261,8 @@ if ($findborrower) {
 }
 
 # get the borrower information.....
-if ($borrowernumber) {
-    $patron = Koha::Patrons->find( $borrowernumber );
+$patron ||= Koha::Patrons->find( $borrowernumber ) if $borrowernumber;
+if ($patron) {
     my $overdues = $patron->get_overdues;
     my $issues = $patron->checkouts;
     my $balance = $patron->account->balance;
@@ -443,8 +443,8 @@ if (@$barcodes) {
 ##################################################################################
 # BUILD HTML
 # show all reserves of this borrower, and the position of the reservation ....
-if ($borrowernumber) {
-    my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } );
+if ($patron) {
+    my $holds = Koha::Holds->search( { borrowernumber => $borrowernumber } ); # FIXME must be Koha::Patron->holds
     my $waiting_holds = $holds->waiting;
     $template->param(
         holds_count  => $holds->count(),
index ae5c276..7397339 100644 (file)
@@ -3,11 +3,7 @@
 [% USE Branches %]
 [% INCLUDE 'doc-head-open.inc' %]
 <title>Koha &rsaquo; Patrons &rsaquo;
-[% IF ( unknowuser ) %]
-    Patron does not exist
-[% ELSE %]
-    Statistics for [% INCLUDE 'patron-title.inc' %]
-[% END %]
+Statistics for [% INCLUDE 'patron-title.inc' %]
 </title>
 [% INCLUDE 'doc-head-close.inc' %]
 <link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/datatables.css" />
@@ -30,7 +26,7 @@
 <div id="breadcrumbs">
          <a href="/cgi-bin/koha/mainpage.pl">Home</a>
 &rsaquo; <a href="/cgi-bin/koha/members/members-home.pl">Patrons</a>
-&rsaquo; [% IF ( unknowuser ) %]Patron does not exist[% ELSE %]Statistics for [% firstname %] [% surname %] ([% cardnumber %])[% END %]
+&rsaquo; Statistics for [% firstname %] [% surname %] ([% cardnumber %])
 </div>
 
 <div id="doc3" class="yui-t1">
index 8abb373..0cc9487 100755 (executable)
@@ -54,6 +54,10 @@ my $action = $input->param('action') || '';
 
 #get patron details
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 
 if ( $action eq 'reverse' ) {
   ReversePayment( $input->param('accountlines_id') );
index 5ea015a..b03b5c8 100755 (executable)
@@ -75,6 +75,10 @@ my $issues = GetPendingIssues($member);     # FIXME: wasteful call when really,
 my $countissues = scalar(@$issues);
 
 my $patron = Koha::Patrons->find( $member );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $flags = C4::Members::patronflags( $patron->unblessed );
 my $userenv = C4::Context->userenv;
 
index b24c41b..a0af828 100755 (executable)
@@ -58,85 +58,86 @@ unless ( C4::Context->preference('useDischarge') ) {
    exit;
 }
 
-if ( $input->param('borrowernumber') ) {
-    $borrowernumber = $input->param('borrowernumber');
+$borrowernumber = $input->param('borrowernumber');
 
-    # Getting member data
-    my $patron = Koha::Patrons->find( $borrowernumber );
+my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 
-    my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
-        borrowernumber => $borrowernumber
-    });
+my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged({
+    borrowernumber => $borrowernumber
+});
 
-    my $holds = $patron->holds;
-    my $has_reserves = $holds->count;
+my $holds = $patron->holds;
+my $has_reserves = $holds->count;
 
-    # Generating discharge if needed
-    if ( $input->param('discharge') and $can_be_discharged ) {
-        my $is_discharged = Koha::Patron::Discharge::is_discharged({
-            borrowernumber => $borrowernumber,
+# Generating discharge if needed
+if ( $input->param('discharge') and $can_be_discharged ) {
+    my $is_discharged = Koha::Patron::Discharge::is_discharged({
+        borrowernumber => $borrowernumber,
+    });
+    unless ($is_discharged) {
+        Koha::Patron::Discharge::discharge({
+            borrowernumber => $borrowernumber
         });
-        unless ($is_discharged) {
-            Koha::Patron::Discharge::discharge({
-                borrowernumber => $borrowernumber
-            });
-        }
-        eval {
-            my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
-                { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
-
-            binmode(STDOUT);
-            print $input->header(
-                -type       => 'application/pdf',
-                -charset    => 'utf-8',
-                -attachment => "discharge_$borrowernumber.pdf",
-            );
-            open my $fh, '<', $pdf_path;
-            my @lines = <$fh>;
-            close $fh;
-            print @lines;
-            exit;
-        };
-        if ( $@ ) {
-            carp $@;
-            $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
-        }
     }
+    eval {
+        my $pdf_path = Koha::Patron::Discharge::generate_as_pdf(
+            { borrowernumber => $borrowernumber, branchcode => $patron->branchcode } );
+
+        binmode(STDOUT);
+        print $input->header(
+            -type       => 'application/pdf',
+            -charset    => 'utf-8',
+            -attachment => "discharge_$borrowernumber.pdf",
+        );
+        open my $fh, '<', $pdf_path;
+        my @lines = <$fh>;
+        close $fh;
+        print @lines;
+        exit;
+    };
+    if ( $@ ) {
+        carp $@;
+        $template->param( messages => [ {type => 'error', code => 'unable_to_generate_pdf'} ] );
+    }
+}
 
-    # Already generated discharges
-    my $validated_discharges = Koha::Patron::Discharge::get_validated({
-        borrowernumber => $borrowernumber,
-    });
+# Already generated discharges
+my $validated_discharges = Koha::Patron::Discharge::get_validated({
+    borrowernumber => $borrowernumber,
+});
 
-    $template->param( picture => 1 ) if $patron->image;
-
-    $template->param(
-        # FIXME The patron object should be passed to the template
-        borrowernumber    => $borrowernumber,
-        title             => $patron->title,
-        initials          => $patron->initials,
-        surname           => $patron->surname,
-        borrowernumber    => $borrowernumber,
-        firstname         => $patron->firstname,
-        cardnumber        => $patron->cardnumber,
-        categorycode      => $patron->categorycode,
-        category_type     => $patron->category->category_type,
-        categoryname      => $patron->category->description,
-        address           => $patron->address,
-        streetnumber      => $patron->streetnumber,
-        streettype        => $patron->streettype,
-        address2          => $patron->address2,
-        city              => $patron->city,
-        zipcode           => $patron->zipcode,
-        country           => $patron->country,
-        phone             => $patron->phone,
-        email             => $patron->email,
-        branchcode        => $patron->branchcode,
-        has_reserves      => $has_reserves,
-        can_be_discharged => $can_be_discharged,
-        validated_discharges => $validated_discharges,
-    );
-}
+$template->param( picture => 1 ) if $patron->image;
+
+$template->param(
+    # FIXME The patron object should be passed to the template
+    borrowernumber    => $borrowernumber,
+    title             => $patron->title,
+    initials          => $patron->initials,
+    surname           => $patron->surname,
+    borrowernumber    => $borrowernumber,
+    firstname         => $patron->firstname,
+    cardnumber        => $patron->cardnumber,
+    categorycode      => $patron->categorycode,
+    category_type     => $patron->category->category_type,
+    categoryname      => $patron->category->description,
+    address           => $patron->address,
+    streetnumber      => $patron->streetnumber,
+    streettype        => $patron->streettype,
+    address2          => $patron->address2,
+    city              => $patron->city,
+    zipcode           => $patron->zipcode,
+    country           => $patron->country,
+    phone             => $patron->phone,
+    email             => $patron->email,
+    branchcode        => $patron->branchcode,
+    has_reserves      => $has_reserves,
+    can_be_discharged => $can_be_discharged,
+    validated_discharges => $validated_discharges,
+);
 
 $template->param( dischargeview => 1, );
 
index 0070fcf..8cc25a9 100755 (executable)
@@ -64,6 +64,11 @@ if ( $op eq 'download' ) {
 }
 else {
     my $patron = Koha::Patrons->find( $borrowernumber );
+    unless ( $patron ) {
+        print $cgi->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+        exit;
+    }
+
     my $patron_category = $patron->category;
     $template->param(%{ $patron->unblessed});
 
index 4855c24..5a93e95 100755 (executable)
@@ -43,6 +43,10 @@ my $flagsrequired = { borrowers => 1, updatecharges => 1 };
 my $borrowernumber=$input->param('borrowernumber');
 
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $add=$input->param('add');
 
 if ($add){
index 2c1c05f..bd24a10 100755 (executable)
@@ -43,6 +43,11 @@ my $flagsrequired = { borrowers => 1 };
 my $borrowernumber=$input->param('borrowernumber');
 
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
+
 my $add=$input->param('add');
 if ($add){
     if ( checkauth( $input, 0, $flagsrequired, 'intranet' ) ) {
index e7c24e9..158074a 100755 (executable)
@@ -27,6 +27,11 @@ my $input = new CGI;
 my $flagsrequired = { permissions => 1 };
 my $member=$input->param('member');
 my $patron = Koha::Patrons->find( $member );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
+    exit;
+}
+
 my $category_type = $patron->category->category_type;
 my $bor = $patron->unblessed;
 if( $category_type eq 'S' )  {
index 4cf00bf..cb922da 100755 (executable)
@@ -49,6 +49,11 @@ my $newpassword2 = $input->param('newpassword2');
 my @errors;
 
 my $patron = Koha::Patrons->find( $member );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
+    exit;
+}
+
 my $category_type = $patron->category->category_type;
 my $bor = $patron->unblessed;
 
index 0501534..c58b2a9 100755 (executable)
@@ -155,6 +155,11 @@ $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
 if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' ) {
     my $patron = Koha::Patrons->find( $borrowernumber );
+    unless ( $patron ) {
+        print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+        exit;
+    }
+
     $borrower_data = $patron->unblessed;
     $borrower_data->{category_type} = $patron->category->category_type;
 }
index 977c921..9f0ab11 100755 (executable)
@@ -34,6 +34,10 @@ my $input=new CGI;
 
 my $borrowernumber = $input->param('borrowernumber');
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $borrower = $patron->unblessed;
 
 my ($template, $loggedinuser, $cookie)
index e4d0a95..7c3f6ad 100755 (executable)
@@ -68,6 +68,10 @@ if ( !$borrowernumber ) {
 
 # get borrower details
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $category = $patron->category;
 our $borrower = $patron->unblessed;
 $borrower->{description} = $category->description;
index 3b8b922..3576d10 100755 (executable)
@@ -50,6 +50,10 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 # get borrower details
 my $borrowernumber = $input->param('borrowernumber');
 my $patron         = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $borrower       = $patron->unblessed;
 my $category       = $patron->category;
 $borrower->{description} = $category->description;
index 8863d1c..f5933f1 100755 (executable)
@@ -51,6 +51,10 @@ my $action = $input->param('action') || '';
 my $accountlines_id = $input->param('accountlines_id');
 
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $category = $patron->category;
 my $data = $patron->unblessed;
 $data->{description} = $category->description;
index ad823d5..ff2c0d1 100755 (executable)
@@ -50,6 +50,10 @@ my $action          = $input->param('action') || '';
 my $accountlines_id = $input->param('accountlines_id');
 
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $category = $patron->category;
 my $data = $patron->unblessed;
 $data->{description} = $category->description;
index 9e35259..5acc3cc 100755 (executable)
@@ -42,8 +42,11 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 
 my $borrowernumber = $input->param('borrowernumber');
 
-# Set informations for the patron
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $category = $patron->category;
 my $data = $patron->unblessed;
 $data->{description} = $category->description;
index b0b563c..ba5e174 100755 (executable)
@@ -55,15 +55,19 @@ my $patron;
 if ($input->param('cardnumber')) {
     $cardnumber = $input->param('cardnumber');
     $patron = Koha::Patrons->find( { cardnumber => $cardnumber } );
-    $data = $patron->unblessed;
-    $borrowernumber = $data->{'borrowernumber'}; # we must define this as it is used to retrieve other data about the patron
 }
 if ($input->param('borrowernumber')) {
     $borrowernumber = $input->param('borrowernumber');
     $patron = Koha::Patrons->find( $borrowernumber );
-    $data = $patron->unblessed;
 }
 
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
+$data = $patron->unblessed;
+$borrowernumber = $patron->borrowernumber;
+
 my $order = 'date_due desc';
 my $limit = 0;
 my $issues = ();
index 6b024bd..5726a20 100755 (executable)
@@ -48,37 +48,38 @@ my $borrowernumber = $query->param('borrowernumber');
 
 my $branch = C4::Context->userenv->{'branch'};
 
-# get the borrower information.....
-my ( $patron, $patron_info );
-if ($borrowernumber) {
-    $patron = Koha::Patrons->find( $borrowernumber );
-    my $category = $patron->category;
-    $patron_info = $patron->unblessed;
-    $patron_info->{description} = $category->description;
-    $patron_info->{category_type} = $category->category_type;
-
-  my $count;
-  my @borrowerSubscriptions;
-  ($count, @borrowerSubscriptions) = GetSubscriptionsFromBorrower($borrowernumber );
-  my @subscripLoop;
-
-    foreach my $num_res (@borrowerSubscriptions) {
-        my %getSubscrip;
-        $getSubscrip{subscriptionid}   = $num_res->{'subscriptionid'};
-        $getSubscrip{title}                    = $num_res->{'title'};
-        $getSubscrip{borrowernumber}           = $num_res->{'borrowernumber'};
-        push( @subscripLoop, \%getSubscrip );
-    }
-
-    $template->param(
-        countSubscrip => scalar @subscripLoop,
-        subscripLoop  => \@subscripLoop,
-        routinglistview => 1
-    );
+my $patron = Koha::Patrons->find( $borrowernumber ) if $borrowernumber;
+unless ( $patron ) {
+    print $query->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 
-    $template->param( adultborrower => 1 ) if ( $patron_info->{category_type} =~ /^(A|I)$/ );
+my $category = $patron->category;
+my $patron_info = $patron->unblessed;
+$patron_info->{description} = $category->description;
+$patron_info->{category_type} = $category->category_type;
+
+my $count;
+my @borrowerSubscriptions;
+($count, @borrowerSubscriptions) = GetSubscriptionsFromBorrower($borrowernumber );
+my @subscripLoop;
+
+foreach my $num_res (@borrowerSubscriptions) {
+    my %getSubscrip;
+    $getSubscrip{subscriptionid} = $num_res->{'subscriptionid'};
+    $getSubscrip{title}          = $num_res->{'title'};
+    $getSubscrip{borrowernumber} = $num_res->{'borrowernumber'};
+    push( @subscripLoop, \%getSubscrip );
 }
 
+$template->param(
+    countSubscrip => scalar @subscripLoop,
+    subscripLoop  => \@subscripLoop,
+    routinglistview => 1
+);
+
+$template->param( adultborrower => 1 ) if ( $patron_info->{category_type} =~ /^(A|I)$/ );
+
 ##################################################################################
 
 $template->param(%$patron_info);
index 5979e7f..55d5ed0 100755 (executable)
@@ -50,8 +50,7 @@ my $borrowernumber = $input->param('borrowernumber');
 # Set informations for the patron
 my $patron = Koha::Patrons->find( $borrowernumber );
 unless ( $patron ) {
-    $template->param (unknowuser => 1);
-    output_html_with_http_headers $input, $cookie, $template->output;
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
     exit;
 }
 
index 546e931..a6a0e36 100755 (executable)
@@ -44,6 +44,10 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 );
 
 my $patron = Koha::Patrons->find( $borrowernumber );
+unless ( $patron ) {
+    print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+    exit;
+}
 my $category = $patron->category;
 my $data = $patron->unblessed;
 $data->{description} = $category->description;
index bedf9b5..c14db87 100755 (executable)
@@ -73,6 +73,10 @@ if ( $op eq 'multi' ) {
 
 elsif ( $op eq 'update' ) {
     my $patron = Koha::Patrons->find( $borrowernumber );
+    unless ( $patron ) {
+        print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+        exit;
+    }
     my $member = $patron->unblessed;
     $member->{'guarantorid'}  = 0;
     $member->{'categorycode'} = $catcode;
index 9af64d5..00469ba 100755 (executable)
@@ -74,6 +74,10 @@ if ( $src eq 'circ' ) {
     use C4::Members::Attributes qw(GetBorrowerAttributes);
     my $borrowernumber = $object;
     my $patron = Koha::Patrons->find( $borrowernumber );
+    unless ( $patron ) {
+        print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
+        exit;
+    }
     $template->param( picture => 1 ) if $patron->image;
     my $data = $patron->unblessed;