From 5dd6d6609cb1042756e6b32f1253133144fb770a Mon Sep 17 00:00:00 2001
From: Lucas Gass <lucas@bywatersolutions.com>
Date: Fri, 25 Aug 2023 15:49:09 +0000
Subject: [PATCH] Bug 34620: Do not throw expection if payment type is writeoff
Content-Type: text/plain; charset=utf-8

To test:
1. Turn on RequirePaymentType
2. Create a manual invoice and then attempt to write it off.
3. 500 error
4. Turn of RequirePaymentType, no error.
5. Apply patch, restart_all
6. Try step 2 again, you should not get an error
7. prove t/db_dependent/Koha/Account.t
8. Make sure tests pass

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 Koha/Account.pm               | 18 +++++++++---------
 t/db_dependent/Koha/Account.t | 12 +++++++++++-
 2 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/Koha/Account.pm b/Koha/Account.pm
index 09cf0ad8ce..ad08a6df4b 100644
--- a/Koha/Account.pm
+++ b/Koha/Account.pm
@@ -87,18 +87,18 @@ sub pay {
 
     my $userenv = C4::Context->userenv;
 
-    Koha::Exceptions::Account::PaymentTypeRequired->throw()
-      if ( C4::Context->preference("RequirePaymentType")
-        && !defined($payment_type) );
+    unless ( $type eq 'WRITEOFF' ) {
+        Koha::Exceptions::Account::PaymentTypeRequired->throw()
+            if ( C4::Context->preference("RequirePaymentType")
+            && !defined($payment_type) );
 
-    my $av = Koha::AuthorisedValues->search_with_library_limits({ category => 'PAYMENT_TYPE', authorised_value => $payment_type });
+        my $av = Koha::AuthorisedValues->search_with_library_limits(
+            { category => 'PAYMENT_TYPE', authorised_value => $payment_type } );
 
-    if ( !$av->count && C4::Context->preference("RequirePaymentType")) {
-        Koha::Exceptions::Account::InvalidPaymentType->throw(
-            error => 'Invalid payment type'
-        );
+        if ( !$av->count && C4::Context->preference("RequirePaymentType") ) {
+            Koha::Exceptions::Account::InvalidPaymentType->throw( error => 'Invalid payment type' );
+        }
     }
-
     my $manager_id = $userenv ? $userenv->{number} : undef;
     my $interface = $params ? ( $params->{interface} || C4::Context->interface ) : C4::Context->interface;
     my $payment = $self->payin_amount(
diff --git a/t/db_dependent/Koha/Account.t b/t/db_dependent/Koha/Account.t
index 46f57f1358..57ecd741aa 100755
--- a/t/db_dependent/Koha/Account.t
+++ b/t/db_dependent/Koha/Account.t
@@ -691,7 +691,7 @@ subtest 'reconcile_balance' => sub {
 
 subtest 'pay() tests' => sub {
 
-    plan tests => 8;
+    plan tests => 9;
 
     $schema->storage->txn_begin;
 
@@ -727,6 +727,16 @@ subtest 'pay() tests' => sub {
     'Koha::Exceptions::Account::InvalidPaymentType',
       'Exception thrown for InvalidPaymentType:1 + payment_type:FOOBAR';
 
+    my $writeoff_id = $account->pay(
+        {
+            amount    => 10,
+            interface => 'intranet',
+            type      => 'WRITEOFF',
+        }
+    )->{payment_id};
+    my $writeoff = Koha::Account::Lines->find($writeoff_id);
+    is( $writeoff->payment_type, undef, "Writeoff should not have a payment_type " );
+
     t::lib::Mocks::mock_preference( 'RequirePaymentType', 0 );
     my $context = Test::MockModule->new('C4::Context');
     $context->mock( 'userenv', { branch => $library->id } );
-- 
2.30.2