From c1f3cb0330c6627c1962a3d9c957195a6e73c764 Mon Sep 17 00:00:00 2001
From: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Date: Wed, 20 Nov 2019 07:53:05 +0000
Subject: [PATCH] Bug 23354: (follow-up) Update for bugs 23049 and 23805

Remove all mentions of accounttype and replace with debit_type_code and
credit_type_code as appropriate.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
---
 Koha/Charges/Sales.pm                              |  5 ++--
 installer/data/mysql/account_credit_types.sql      |  3 +-
 installer/data/mysql/atomicupdate/bug_23354.perl   | 10 +++++--
 .../data/mysql/en/mandatory/sample_notices.sql     |  5 ++--
 t/db_dependent/Koha/Charges/Sales.pm               | 32 +++++++++-------------
 5 files changed, 29 insertions(+), 26 deletions(-)

diff --git a/Koha/Charges/Sales.pm b/Koha/Charges/Sales.pm
index bd88423d6a..71e3d9138d 100644
--- a/Koha/Charges/Sales.pm
+++ b/Koha/Charges/Sales.pm
@@ -20,6 +20,7 @@ package Koha::Charges::Sales;
 use Modern::Perl;
 
 use Koha::Account::Lines;
+use Koha::Account::DebitTypes;
 use Koha::Account::Offsets;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Exceptions;
@@ -210,7 +211,7 @@ sub purchase {
                 my $debit = Koha::Account::Line->new(
                     {
                         amount            => $amount,
-                        accounttype       => $item->{code},
+                        debit_type_code   => $item->{code},
                         amountoutstanding => 0,
                         note              => $item->{quantity},
                         manager_id        => $self->{staff_id},
@@ -235,7 +236,7 @@ sub purchase {
             $credit = Koha::Account::Line->new(
                 {
                     amount            => 0 - $total_owed,
-                    accounttype       => 'Purchase',
+                    credit_type_code  => 'PURCHASE',
                     payment_type      => $self->{payment_type},
                     amountoutstanding => 0,
                     manager_id        => $self->{staff_id},
diff --git a/installer/data/mysql/account_credit_types.sql b/installer/data/mysql/account_credit_types.sql
index 20e15ebbc7..5cf04abb15 100644
--- a/installer/data/mysql/account_credit_types.sql
+++ b/installer/data/mysql/account_credit_types.sql
@@ -4,6 +4,7 @@ INSERT INTO account_credit_types ( code, description, can_be_added_manually, is_
 ('FORGIVEN', 'Forgiven', 1, 1),
 ('CREDIT', 'Credit', 1, 1),
 ('REFUND', 'A refund applied to a patrons fine', 0, 1),
-('LOST_RETURN', 'Lost item fee refund', 0, 1);
+('LOST_RETURN', 'Lost item fee refund', 0, 1),
+('PURCHASE', 'Purchase', 0, 1);
 
 INSERT INTO authorised_values (category,authorised_value,lib) VALUES ('PAYMENT_TYPE','CASH','Cash');
diff --git a/installer/data/mysql/atomicupdate/bug_23354.perl b/installer/data/mysql/atomicupdate/bug_23354.perl
index 2e920c459d..717eaa5e42 100644
--- a/installer/data/mysql/atomicupdate/bug_23354.perl
+++ b/installer/data/mysql/atomicupdate/bug_23354.perl
@@ -5,6 +5,11 @@ if( CheckVersion( $DBversion ) ) {
         INSERT IGNORE INTO account_offset_types ( type ) VALUES ( 'Purchase' );
     });
 
+    $dbh->do(q{
+        INSERT INTO account_credit_types ( code, description, can_be_added_manually, is_system )
+        VALUES ('PURCHASE', 'Purchase', 0, 1);
+    });
+
     my $sth = $dbh->prepare(q{
         SELECT COUNT(*) FROM authorised_values WHERE category = 'PAYMENT_TYPE' AND authorised_value = 'CASH'
     });
@@ -40,7 +45,8 @@ if( CheckVersion( $DBversion ) ) {
 
     $dbh->do(q{
 INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title`, `content`, `message_transport_type`, `lang`) VALUES
-('pos', 'RECEIPT', '', 'Point of sale receipt', 0, 'Receipt', '<table>
+('pos', 'RECEIPT', '', 'Point of sale receipt', 0, 'Receipt', '[% PROCESS "accounts.inc" %]
+<table>
 [% IF ( LibraryName ) %]
  <tr>
     <th colspan="2" class="centerednames">
@@ -83,7 +89,7 @@ INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`,
 
   [% FOREACH offset IN offsets %]
     <tr>
-        <td>[% offset.debit.accounttype %]</td>
+        <td>[% PROCESS account_type_description account=offset.debit %]</td>
         <td>[% offset.amount * -1 | $Price %]</td>
     </tr>
   [% END %]
diff --git a/installer/data/mysql/en/mandatory/sample_notices.sql b/installer/data/mysql/en/mandatory/sample_notices.sql
index a970969bfd..1fea0039ad 100644
--- a/installer/data/mysql/en/mandatory/sample_notices.sql
+++ b/installer/data/mysql/en/mandatory/sample_notices.sql
@@ -318,7 +318,8 @@ INSERT INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title`
 ('circulation', 'SR_SLIP', '', 'Stock rotation slip', 0, 'Stock rotation report', 'Stock rotation report for [% branch.name %]:\r\n\r\n[% IF branch.items.size %][% branch.items.size %] items to be processed for this branch.\r\n[% ELSE %]No items to be processed for this branch\r\n[% END %][% FOREACH item IN branch.items %][% IF item.reason != \'in-demand\' %]Title: [% item.title %]\r\nAuthor: [% item.author %]\r\nCallnumber: [% item.callnumber %]\r\nLocation: [% item.location %]\r\nBarcode: [% item.barcode %]\r\nOn loan?: [% item.onloan %]\r\nStatus: [% item.reason %]\r\nCurrent library: [% item.branch.branchname %] [% item.branch.branchcode %]\r\n\r\n[% END %][% END %]', 'email');
 
 INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, `title`, `content`, `message_transport_type`, `lang`) VALUES
-('pos', 'RECEIPT', '', 'Point of sale receipt', 0, 'Receipt', '<table>
+('pos', 'RECEIPT', '', 'Point of sale receipt', 0, 'Receipt', '[% PROCESS "accounts.inc" %]
+<table>
 [% IF ( LibraryName ) %]
  <tr>
     <th colspan="2" class="centerednames">
@@ -361,7 +362,7 @@ INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`,
 
   [% FOREACH offset IN offsets %]
     <tr>
-        <td>[% offset.debit.accounttype %]</td>
+        <td>[% PROCESS account_type_description account=offset.debit %]</td>
         <td>[% offset.amount * -1 | $Price %]</td>
     </tr>
   [% END %]
diff --git a/t/db_dependent/Koha/Charges/Sales.pm b/t/db_dependent/Koha/Charges/Sales.pm
index e4dbdd366e..1801867d1d 100644
--- a/t/db_dependent/Koha/Charges/Sales.pm
+++ b/t/db_dependent/Koha/Charges/Sales.pm
@@ -138,21 +138,19 @@ subtest 'add_item (_get_valid_items) tests' => sub {
       'Exception thrown if `code` parameter is missing';
 
     my $library2 = $builder->build_object( { class => 'Koha::Libraries' } );
-    my $av = Koha::AuthorisedValue->new(
+    my $dt = Koha::Account::DebitType->new(
         {
-            category         => 'MANUAL_INV',
-            authorised_value => 'BOBBYRANDOM',
-            lib              => 'Test bobbyrandom',
-            lib_opac         => 'Test bobbyrandom',
+            code        => 'BOBBYRANDOM',
+            description => 'Test bobbyrandom',
         }
     )->store;
-    $av->replace_library_limits( [ $library2->branchcode ] );
+    $dt->replace_library_limits( [ $library2->branchcode ] );
 
     throws_ok { $sale->add_item( { code => 'BOBBYRANDOM' } ) }
     'Koha::Exceptions::Account::UnrecognisedType',
 'Exception thrown if passed an item code that is not valid for the cash registers branch';
 
-    $av->replace_library_limits();
+    $dt->replace_library_limits();
     $sale->{valid_items} = undef;    # Flush object cache for 'valid_items'
 
     throws_ok {
@@ -205,20 +203,16 @@ subtest 'purchase tests' => sub {
         }
     )->store;
 
-    my $item1 = Koha::AuthorisedValue->new(
+    my $item1 = Koha::Account::DebitType->new(
         {
-            category         => 'MANUAL_INV',
-            authorised_value => 'COPYRANDOM',
-            lib              => 'Copier fee',
-            lib_opac         => 'Copier fee',
+            code        => 'COPYRANDOM',
+            description => 'Copier fee',
         }
     )->store;
-    my $item2 = Koha::AuthorisedValue->new(
+    my $item2 = Koha::Account::DebitType->new(
         {
-            category         => 'MANUAL_INV',
-            authorised_value => 'CARDRANDOM',
-            lib              => 'New card fee',
-            lib_opac         => 'New card fee',
+            code        => 'CARDRANDOM',
+            description => 'New card fee',
         }
     )->store;
 
@@ -250,7 +244,7 @@ subtest 'purchase tests' => sub {
 
     is(ref($credit), 'Koha::Account::Line', "Koha::Account::Line returned");
     ok($credit->is_credit, "return is a credit for payment");
-    is($credit->accounttype, 'Purchase', "accounttype set correctly to 'Purchase' for payment");
+    is($credit->credit_type_code, 'PURCHASE', "credit_type_code set correctly to 'PURCHASE' for payment");
     is($credit->amount, -5.00, "amount is calculated correctly for payment");
     is($credit->amountoutstanding, 0.00, "amountoutstanding is set to zero for payment");
     is($credit->manager_id, $staff->borrowernumber, "manager_id set correctionly for payment");
@@ -258,7 +252,7 @@ subtest 'purchase tests' => sub {
     is($credit->payment_type, 'CASH', "payment_type set correctly for payment");
 
     my $offsets = Koha::Account::Offsets->search({credit_id => $credit->accountlines_id});
-    is($offsets->count, 2, "One offset was added for each item added");
+    is($offsets->count, 3, "One offset was added for each item added"); # 2 items + 1 purchase
 
 #ensure relevant fields are set
 #ensure register_id is only ever set with a corresponding payment_type having been set
-- 
2.11.0