From d02e2789e567158b30ad237d5efd864247dd4ead Mon Sep 17 00:00:00 2001 From: Jared Camins-Esakov Date: Sun, 2 Jun 2013 07:50:43 -0400 Subject: [PATCH] Bug 10390: Add ability to delete empty invoices There is currently no way to delete unused invoices (for example, invoices created by mistake), and there really should be, since errors and absent-mindedness can result in numerous empty invoices over the course of years. To test: 1) Apply patch. 2) Create three invoices in the Acquisitions module. For one of them, receive at least one item. For the other two, do not receive any items. 3) View one of the invoices that does not have any items on it. 4) Try to delete it. This should succeed. 5) View the invoice that has an item. There should not be any option to delete it. 6) Do an invoice search that brings up the other invoice with no items on it. Try to delete it from the results page. This should succeed. 7) Run the unit test: > prove t/Acquisition/Invoice.t 8) Sign off. Signed-off-by: Srdjan Signed-off-by: Katrin Fischer All tests and QA script pass. I also did another test: I cancelled all receipts from an existing invoice and then could successfully delete it in the last step. Signed-off-by: Galen Charlton --- C4/Acquisition.pm | 34 +++++++++++++++++++ acqui/invoice.pl | 8 +++++ .../prog/en/modules/acqui/invoice.tt | 3 ++ .../prog/en/modules/acqui/invoices.tt | 3 ++ t/Acquisition/Invoice.t | 23 ++++++++++++- 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/C4/Acquisition.pm b/C4/Acquisition.pm index e406d81618..b7ba2e181e 100644 --- a/C4/Acquisition.pm +++ b/C4/Acquisition.pm @@ -71,6 +71,7 @@ BEGIN { &ModInvoice &CloseInvoice &ReopenInvoice + &DelInvoice &GetItemnumbersFromOrder @@ -2550,6 +2551,39 @@ sub ReopenInvoice { $sth->execute($invoiceid); } +=head3 DelInvoice + + DelInvoice($invoiceid); + +Delete an invoice if there are no items attached to it. + +=cut + +sub DelInvoice { + my ($invoiceid) = @_; + + return unless $invoiceid; + + my $dbh = C4::Context->dbh; + my $query = qq{ + SELECT COUNT(*) + FROM aqorders + WHERE invoiceid = ? + }; + my $sth = $dbh->prepare($query); + $sth->execute($invoiceid); + my $res = $sth->fetchrow_arrayref; + if ( $res && $res->[0] == 0 ) { + $query = qq{ + DELETE FROM aqinvoices + WHERE invoiceid = ? + }; + my $sth = $dbh->prepare($query); + return ( $sth->execute($invoiceid) > 0 ); + } + return; +} + 1; __END__ diff --git a/acqui/invoice.pl b/acqui/invoice.pl index 1b6d97c72f..e38e02db88 100755 --- a/acqui/invoice.pl +++ b/acqui/invoice.pl @@ -86,6 +86,14 @@ elsif ( $op && $op eq 'mod' ) { } $template->param( modified => 1 ); } +elsif ( $op && $op eq 'delete' ) { + DelInvoice($invoiceid); + my $referer = $input->param('referer') || 'invoices.pl'; + if ($referer) { + print $input->redirect($referer); + exit 0; + } +} my $details = GetInvoiceDetails($invoiceid); my $bookseller = GetBookSellerFromId( $details->{booksellerid} ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt index 96df8d1315..1ce4cd8712 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt @@ -84,6 +84,9 @@
+ [% UNLESS orders_loop.size %] + Delete + [% END %]

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt index 52d2ecc1dd..16c1b6121f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt @@ -80,6 +80,9 @@ $(document).ready(function() { [% ELSE %] Close [% END %] + [% UNLESS invoice.receivedbiblios || invoice.receiveditems %] + / Delete + [% END %] [% END %] diff --git a/t/Acquisition/Invoice.t b/t/Acquisition/Invoice.t index a88796505f..0a37f44070 100755 --- a/t/Acquisition/Invoice.t +++ b/t/Acquisition/Invoice.t @@ -3,7 +3,7 @@ use Modern::Perl; use C4::Context; -use Test::More tests => 47; +use Test::More tests => 49; use Test::MockModule; use_ok('C4::Acquisition'); @@ -129,3 +129,24 @@ is(scalar(@$history), 1); @bound_params = @{ $history->[0]->{bound_params} }; is(scalar(@bound_params), 1); is($bound_params[0], 42); +my $checkordersrs = [ + [qw(COUNT)], + [2] +]; + +$dbh->{mock_add_resultset} = $checkordersrs; +is(DelInvoice(42), undef, "Invoices with items don't get deleted"); + +$checkordersrs = [ + [qw(COUNT)], + [0] +]; + +my $deleters = [ + [qw(COUNT)], + [1] +]; + +$dbh->{mock_add_resultset} = $checkordersrs; +$dbh->{mock_add_resultset} = $deleters; +ok(DelInvoice(42), "Invoices with items do get deleted"); -- 2.39.5