[Koha-patches] [PATCH] [SIGNED-OFF] Bug 10390: Add ability to delete empty invoices

Srdjan srdjan at catalyst.net.nz
Mon Jun 3 05:27:33 CEST 2013


From: Jared Camins-Esakov <jcamins at cpbibliography.com>

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.

Signed-off-by: Srdjan <srdjan at catalyst.net.nz>
---
 C4/Acquisition.pm                                  | 34 ++++++++++++++++++++++
 acqui/invoice.pl                                   |  8 +++++
 .../intranet-tmpl/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 b6fab5d..dd7069a 100644
--- a/C4/Acquisition.pm
+++ b/C4/Acquisition.pm
@@ -71,6 +71,7 @@ BEGIN {
         &ModInvoice
         &CloseInvoice
         &ReopenInvoice
+        &DelInvoice
 
         &GetItemnumbersFromOrder
 
@@ -2607,6 +2608,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 1b6d97c..e38e02d 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 96df8d1..1ce4cd8 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 @@
         </fieldset>
         <fieldset class="action">
             <input type="submit" value="Save" />
+            [% UNLESS orders_loop.size %]
+            <a href="invoice.pl?op=delete&invoiceid=[% invoiceid %]">Delete</a>
+            [% END %]
         </fieldset>
       </form>
       <p>
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 52d2ecc..16c1b61 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 %]
                       <a href="invoice.pl?op=close&invoiceid=[% invoice.invoiceid %]&referer=/cgi-bin/koha/acqui/invoices.pl%3Fop=do_search%26invoicenumber=[% invoicenumber %]%26supplier=[% booksellerid %]%26billingdatefrom=[% billingdatefrom %]%26billingdateto=[% billingdateto %]%26isbneanissn=[% isbneanissn %]%26title=[% title %]%26author=[% author %]%26publisher=[% publisher %]%26publicationyear=[% publicationyear %]%26branch=[% branch %]">Close</a>
                     [% END %]
+                    [% UNLESS invoice.receivedbiblios || invoice.receiveditems %]
+                      / <a href="invoice.pl?op=delete&invoiceid=[% invoice.invoiceid %]&referer=/cgi-bin/koha/acqui/invoices.pl%3Fop=do_search%26invoicenumber=[% invoicenumber %]%26supplier=[% booksellerid %]%26billingdatefrom=[% billingdatefrom %]%26billingdateto=[% billingdateto %]%26isbneanissn=[% isbneanissn %]%26title=[% title %]%26author=[% author %]%26publisher=[% publisher %]%26publicationyear=[% publicationyear %]%26branch=[% branch %]">Delete</a>
+                    [% END %]
                   </td>
                 </tr>
               [% END %]
diff --git a/t/Acquisition/Invoice.t b/t/Acquisition/Invoice.t
index a887965..0a37f44 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");
-- 
1.8.1.2



More information about the Koha-patches mailing list