Bug 10390: Add ability to delete empty invoices
authorJared Camins-Esakov <jcamins@cpbibliography.com>
Sun, 2 Jun 2013 11:50:43 +0000 (07:50 -0400)
committerGalen Charlton <gmc@esilibrary.com>
Fri, 5 Jul 2013 16:51:18 +0000 (16:51 +0000)
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 <srdjan@catalyst.net.nz>
Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
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 <gmc@esilibrary.com>
C4/Acquisition.pm
acqui/invoice.pl
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoice.tt
koha-tmpl/intranet-tmpl/prog/en/modules/acqui/invoices.tt
t/Acquisition/Invoice.t

index e406d81..b7ba2e1 100644 (file)
@@ -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__
 
index 1b6d97c..e38e02d 100755 (executable)
@@ -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} );
index 96df8d1..1ce4cd8 100644 (file)
@@ -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>
index 52d2ecc..16c1b61 100644 (file)
@@ -80,6 +80,9 @@ $(document).ready(function() {
                     [% ELSE %]
                       <a href="invoice.pl?op=close&amp;invoiceid=[% invoice.invoiceid %]&amp;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&amp;invoiceid=[% invoice.invoiceid %]&amp;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 %]
index a887965..0a37f44 100755 (executable)
@@ -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");