From 13f628d5a0970aff15a71e2f876dd2401ce14a14 Mon Sep 17 00:00:00 2001
From: Josef Moravec <josef.moravec@gmail.com>
Date: Fri, 26 Oct 2018 08:43:43 +0000
Subject: [PATCH] Bug 21678: Create account offsets for debits of reserve fee
 and rental renewal

Test plan:
1) Make a reservation to create a reserve fee
2) Have an item type with rental fee and renew it to create rental
renewal fe

-- without patch there are no entries in table account_offsets for
creating a fee
-- with patch the appropriate account_offsets are created
---
 C4/Circulation.pm | 27 +++++++--------------------
 C4/Reserves.pm    | 17 +++--------------
 2 files changed, 10 insertions(+), 34 deletions(-)

diff --git a/C4/Circulation.pm b/C4/Circulation.pm
index ed3930f..479990e 100644
--- a/C4/Circulation.pm
+++ b/C4/Circulation.pm
@@ -2852,26 +2852,13 @@ sub AddRenewal {
     # Charge a new rental fee, if applicable?
     my ( $charge, $type ) = GetIssuingCharges( $itemnumber, $borrowernumber );
     if ( $charge > 0 ) {
-        my $accountno = C4::Accounts::getnextacctno( $borrowernumber );
-        my $manager_id = 0;
-        $manager_id = C4::Context->userenv->{'number'} if C4::Context->userenv; 
-        my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
-        Koha::Account::Line->new(
-            {
-                date              => dt_from_string(),
-                borrowernumber    => $borrowernumber,
-                accountno         => $accountno,
-                amount            => $charge,
-                manager_id        => $manager_id,
-                accounttype       => 'Rent',
-                amountoutstanding => $charge,
-                itemnumber        => $itemnumber,
-                branchcode        => $branchcode,
-                description       => 'Renewal of Rental Item '
-                  . $biblio->title
-                  . " $item->{'barcode'}",
-            }
-        )->store();
+        my $description = 'Renewal of Rental Item '
+            . $biblio->title
+            . " $item->{'barcode'}";
+
+        C4::Accounts::manualinvoice(
+            $borrowernumber, $itemnumber, $description, 'Rent', $charge
+        );
     }
 
     # Send a renewal slip according to checkout alert preferencei
diff --git a/C4/Reserves.pm b/C4/Reserves.pm
index 0fbe310..464a315 100644
--- a/C4/Reserves.pm
+++ b/C4/Reserves.pm
@@ -570,20 +570,9 @@ sub ChargeReserveFee {
 
     return if !$fee || $fee == 0;    # the last test is needed to include 0.00
 
-    my $branchcode = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
-    my $nextacctno = C4::Accounts::getnextacctno($borrowernumber);
-
-    Koha::Account::Line->new(
-        {
-            borrowernumber    => $borrowernumber,
-            accountno         => $nextacctno,
-            date              => dt_from_string(),
-            amount            => $fee,
-            description       => "Reserve Charge - $title",
-            accounttype       => 'Res',
-            amountoutstanding => $fee,
-        }
-    )->store();
+    C4::Accounts::manualinvoice(
+        $borrowernumber, undef, "Reserve Charge - $title", 'Res', $fee
+    );
 }
 
 =head2 GetReserveFee
-- 
2.1.4