From eca56947e1677176e60dcca9587a1b240fcee47a Mon Sep 17 00:00:00 2001
From: Kyle M Hall <kyle@bywatetsolutions.com>
Date: Fri, 9 Mar 2018 10:43:41 -0500
Subject: [PATCH] Bug 15985: Add new method Koha::Account::Line::checkout

---
 Koha/Account/Line.pm                |  1 +
 Koha/Old/Checkout.pm                |  2 +-
 Koha/Old/Checkouts.pm               |  2 +-
 Koha/Schema/Result/OldIssue.pm      | 10 +++++++
 t/db_dependent/Koha/Account/Lines.t | 46 ++++++++++++++++++++++++++++-
 5 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/Koha/Account/Line.pm b/Koha/Account/Line.pm
index bd86317bb4..c8d8cbd47c 100644
--- a/Koha/Account/Line.pm
+++ b/Koha/Account/Line.pm
@@ -28,6 +28,7 @@ use Koha::Account::Offsets;
 use Koha::Database;
 use Koha::Exceptions::Account;
 use Koha::Items;
+use Koha::Checkouts;
 
 use base qw(Koha::Object);
 
diff --git a/Koha/Old/Checkout.pm b/Koha/Old/Checkout.pm
index f89feaefe0..8ac95de573 100644
--- a/Koha/Old/Checkout.pm
+++ b/Koha/Old/Checkout.pm
@@ -19,7 +19,7 @@ use Modern::Perl;
 
 use Koha::Database;
 
-use base qw(Koha::Object);
+use base qw(Koha::Checkout);
 
 =head1 NAME
 
diff --git a/Koha/Old/Checkouts.pm b/Koha/Old/Checkouts.pm
index c7f1a6a220..1b5e21843a 100644
--- a/Koha/Old/Checkouts.pm
+++ b/Koha/Old/Checkouts.pm
@@ -20,7 +20,7 @@ use Modern::Perl;
 use Koha::Database;
 use Koha::Old::Checkout;
 
-use base qw(Koha::Objects);
+use base qw(Koha::Checkouts);
 
 sub _type {
     return 'OldIssue';
diff --git a/Koha/Schema/Result/OldIssue.pm b/Koha/Schema/Result/OldIssue.pm
index f1ff5c2fa6..2ecbcab0d6 100644
--- a/Koha/Schema/Result/OldIssue.pm
+++ b/Koha/Schema/Result/OldIssue.pm
@@ -233,6 +233,16 @@ __PACKAGE__->belongs_to(
   },
 );
 
+__PACKAGE__->belongs_to(
+  "branch",
+  "Koha::Schema::Result::Branch",
+  { branchcode => "branchcode" },
+  {
+    is_deferrable => 1,
+    join_type     => "LEFT",
+  },
+);
+
 
 # Created by DBIx::Class::Schema::Loader v0.07046 @ 2019-04-10 19:55:44
 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:E2N2paWcCHg916100ry+2A
diff --git a/t/db_dependent/Koha/Account/Lines.t b/t/db_dependent/Koha/Account/Lines.t
index a2a12c16f0..2b881df1c5 100755
--- a/t/db_dependent/Koha/Account/Lines.t
+++ b/t/db_dependent/Koha/Account/Lines.t
@@ -19,7 +19,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 15;
+use Test::More tests => 16;
 use Test::Exception;
 
 use C4::Circulation qw/AddIssue AddReturn/;
@@ -1208,4 +1208,48 @@ subtest "reduce() tests" => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest 'checkout' => sub {
+    plan tests => 1;
+
+    $schema->storage->txn_begin;
+
+    my $library    = $builder->build( { source => 'Branch' } );
+    my $biblioitem = $builder->build( { source => 'Biblioitem' } );
+    my $patron     = $builder->build( { source => 'Borrower' } );
+    my $item       = Koha::Item->new(
+        {
+            biblionumber     => $biblioitem->{biblionumber},
+            biblioitemnumber => $biblioitem->{biblioitemnumber},
+            homebranch       => $library->{branchcode},
+            holdingbranch    => $library->{branchcode},
+            barcode          => 'some_barcode_13',
+            itype            => 'BK',
+        }
+    )->store;
+
+    my $checkout = Koha::Checkout->new(
+        {
+            borrowernumber => $patron->{borrowernumber},
+            itemnumber     => $item->itemnumber,
+            branchcode     => $library->{branchcode},
+        }
+    )->store;
+
+    my $line = Koha::Account::Line->new(
+        {
+            borrowernumber => $patron->{borrowernumber},
+            itemnumber     => $item->itemnumber,
+            issue_id       => $checkout->id,
+            accounttype    => "F",
+            amount         => 10,
+            interface      => 'commandline',
+        }
+    )->store;
+
+    is( $line->checkout->id, $checkout->id,
+        'Koha::Account::Line->checkout should return the correct checkout' );
+
+    $schema->storage->txn_rollback;
+};
+
 1;
-- 
2.20.1