From 1c82c600682f92c235e75d1d792fbec9cd194df7 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Tue, 12 Nov 2019 14:47:44 +0100 Subject: [PATCH] Bug 22393: (follow-up) Handle deleted items --- members/maninvoice.pl | 53 ++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/members/maninvoice.pl b/members/maninvoice.pl index 6681da4016..05fe86ced7 100755 --- a/members/maninvoice.pl +++ b/members/maninvoice.pl @@ -91,35 +91,51 @@ if ($add) { my $note = $input->param('note'); my $debit_type = $input->param('type'); - # If LOST try to match barcode to Item and then try to match Item + Borrower to an Issue. + # If barcode is passed, attempt to find the associated item my $failed; my $item_id; + my $issue_id; my $barcode = $input->param('barcode'); if ($barcode) { + my $old_item_id; my $item = Koha::Items->find( { barcode => $barcode } ); if ($item) { $item_id = $item->itemnumber; } else { - $template->param( error => 'itemnumber' ); - $failed = 1; + my $old_itemnumber = $dbh->selectrow_array( + "SELECT itemnumber FROM deleted_items WHERE barcode=?", + undef, $barcode ); + unless ($old_itemnumber) { + $template->param( error => 'itemnumber' ); + $failed = 1; + } + } + + my $issue_id; + if ( $debit_type eq 'LOST' && ( $item_id || $old_itemnumber ) ) { + my $itemnumber = $item_id // $old_itemnumber; + my $checkouts = Koha::Checkouts->search( + { + itemnumber => $itemnumber, + borrowernumber => $borrowernumber + } + ); + my $checkout = + $checkouts->count + ? $checkouts->next + : Koha::Old::Checkouts->search( + { + itemnumber => $itemnumber, + borrowernumber => $borrowernumber + }, + { order_by => { -desc => 'returndate' }, rows => 1 } + )->next; + $issue_id = $checkout ? $checkout->issue_id : undef; } - } - my $issue_id; - if ( $debit_type eq 'LOST' && $item_id ) { - my $checkouts = Koha::Checkouts->search( - { itemnumber => $item_id, borrowernumber => $borrowernumber } ); - my $checkout = - $checkouts->count - ? $checkouts->next - : Koha::Old::Checkouts->search( - { itemnumber => $item_id, borrowernumber => $borrowernumber }, - { order_by => { -desc => 'returndate' }, rows => 1 } - )->next; - $issue_id = $checkout ? $checkout->issue_id : undef; } - unless ($failed) { + if ( !$failed ) { try { $patron->account->add_debit( { @@ -148,7 +164,8 @@ if ($add) { my $error = $_; if ( ref($error) eq 'Koha::Exceptions::Object::FKConstraint' ) { $template->param( error => $error->broken_fk ); - } else { + } + else { $template->param( error => '1' ); } } -- 2.20.1