From 58032399df02aa71ce3f40804ebdef1ccac825d7 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 7 May 2018 19:46:05 -0300
Subject: [PATCH] Bug 20726: Add new method Acquisition::Order->invoice

Can be moved to a separate bug report.
---
 Koha/Acquisition/Invoice.pm             | 40 ++++++++++++++++++++++++++
 Koha/Acquisition/Invoices.pm            | 50 +++++++++++++++++++++++++++++++++
 Koha/Acquisition/Order.pm               | 16 +++++++++++
 t/db_dependent/Koha/Acquisition/Order.t | 31 +++++++++++++++++++-
 4 files changed, 136 insertions(+), 1 deletion(-)
 create mode 100644 Koha/Acquisition/Invoice.pm
 create mode 100644 Koha/Acquisition/Invoices.pm

diff --git a/Koha/Acquisition/Invoice.pm b/Koha/Acquisition/Invoice.pm
new file mode 100644
index 0000000000..6117322226
--- /dev/null
+++ b/Koha/Acquisition/Invoice.pm
@@ -0,0 +1,40 @@
+package Koha::Acquisition::Invoice;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use base qw(Koha::Object);
+
+=head1 NAME
+
+Koha::Acquisition::Invoice object class
+
+=head1 API
+
+=head2 Internal methods
+
+=head3 _type
+
+=cut
+
+sub _type {
+    return 'Aqinvoice';
+}
+
+1;
diff --git a/Koha/Acquisition/Invoices.pm b/Koha/Acquisition/Invoices.pm
new file mode 100644
index 0000000000..134c309392
--- /dev/null
+++ b/Koha/Acquisition/Invoices.pm
@@ -0,0 +1,50 @@
+package Koha::Acquisition::Invoices;
+
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+use Modern::Perl;
+
+use Koha::Database;
+
+use Koha::Acquisition::Invoice;
+
+use base qw(Koha::Objects);
+
+=head1 NAME
+
+Koha::Acquisition::Invoices object set class
+
+=head1 API
+
+=head2 Internal methods
+
+=head3 _type (internal)
+
+=cut
+
+sub _type {
+    return 'Aqinvoice';
+}
+
+=head3 object_class (internal)
+
+=cut
+
+sub object_class {
+    return 'Koha::Acquisition::Invoice';
+}
+
+1;
diff --git a/Koha/Acquisition/Order.pm b/Koha/Acquisition/Order.pm
index 702e5b61b0..dee03ed588 100644
--- a/Koha/Acquisition/Order.pm
+++ b/Koha/Acquisition/Order.pm
@@ -21,6 +21,7 @@ use Carp qw( croak );
 
 use Koha::Acquisition::Baskets;
 use Koha::Acquisition::Funds;
+use Koha::Acquisition::Invoices;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 
@@ -128,6 +129,21 @@ sub fund {
     return Koha::Acquisition::Fund->_new_from_dbic( $fund_rs );
 }
 
+=head3 invoice
+
+    my $invoice = $order->invoice
+
+Returns the invoice associated to the order.
+
+=cut
+
+sub invoice {
+    my ( $self )  = @_;
+    my $invoice_rs = $self->_result->invoiceid;
+    return unless $invoice_rs;
+    return Koha::Acquisition::Invoice->_new_from_dbic( $invoice_rs );
+}
+
 =head2 Internal methods
 
 =head3 _type
diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t
index f5a20a808b..c662e798c5 100644
--- a/t/db_dependent/Koha/Acquisition/Order.t
+++ b/t/db_dependent/Koha/Acquisition/Order.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use Test::More tests => 4;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -114,3 +114,32 @@ subtest 'fund' => sub {
         '->fund should return a Koha::Acquisition::Fund object' );
     $schema->storage->txn_rollback;
 };
+
+subtest 'invoice' => sub {
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+    my $o = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Orders',
+            value => { cancellationreason => 'XXXXXXXX', invoiceid => undef }, # not received yet
+        }
+    );
+
+    my $order = Koha::Acquisition::Orders->find( $o->ordernumber );
+    is( $order->invoice, undef,
+        '->invoice should return undef if no invoice defined yet');
+
+    my $invoice = $builder->build_object(
+        {
+            class => 'Koha::Acquisition::Invoices',
+        },
+    );
+
+    $o->invoiceid( $invoice->invoiceid )->store;
+    $order = Koha::Acquisition::Orders->find( $o->ordernumber );
+    is( ref( $order->invoice ), 'Koha::Acquisition::Invoice',
+        '->invoice should return a Koha::Acquisition::Invoice object if an invoice is defined');
+
+    $schema->storage->txn_rollback;
+};
-- 
2.11.0