Repository: moov-io/ach
Pull request #1456 (2024-08-02 21:50:41)

fix: correctly reverse loan transaction codes, test GL credit/debit


Patch:
diff --git a/reversal.go b/reversal.go
index f5a7ba2a..9d2fe871 100644
--- a/reversal.go
+++ b/reversal.go
@@ -56,20 +56,28 @@ func (f *File) Reversal(effectiveEntryDate time.Time) error {
 			case
 				CheckingCredit, CheckingReturnNOCCredit, CheckingPrenoteCredit, CheckingZeroDollarRemittanceCredit,
 				GLCredit, GLPrenoteCredit, GLReturnNOCCredit, GLZeroDollarRemittanceCredit,
-				LoanCredit, LoanPrenoteCredit, LoanReturnNOCCredit, LoanZeroDollarRemittanceCredit,
+				LoanPrenoteCredit, LoanReturnNOCCredit, LoanZeroDollarRemittanceCredit,
 				SavingsCredit, SavingsPrenoteCredit, SavingsReturnNOCCredit, SavingsZeroDollarRemittanceCredit:
 				// Credit -> Debit
 				hasDebits = true
 				entries[j].TransactionCode += 5
 
+			case LoanCredit:
+				hasDebits = true
+				entries[j].TransactionCode += 3
+
 			case
 				CheckingDebit, CheckingPrenoteDebit, CheckingReturnNOCDebit, CheckingZeroDollarRemittanceDebit,
 				GLDebit, GLPrenoteDebit, GLReturnNOCDebit, GLZeroDollarRemittanceDebit,
-				LoanDebit, LoanReturnNOCDebit,
+				LoanReturnNOCDebit,
 				SavingsDebit, SavingsPrenoteDebit, SavingsReturnNOCDebit, SavingsZeroDollarRemittanceDebit:
 				// Debit -> Credit
 				hasCredits = true
 				entries[j].TransactionCode -= 5
+
+			case LoanDebit:
+				hasDebits = true
+				entries[j].TransactionCode -= 3
 			}
 		}
 


Test patch:
diff --git a/reversal_test.go b/reversal_test.go
index de81d05e..aca95163 100644
--- a/reversal_test.go
+++ b/reversal_test.go
@@ -56,3 +56,71 @@ func TestFileDebitReversal(t *testing.T) {
 	require.Len(t, entries, 1)
 	require.Equal(t, CheckingCredit, entries[0].TransactionCode)
 }
+
+func TestReversal_GL(t *testing.T) {
+	// Debit
+	file, err := ReadFile(filepath.Join("test", "testdata", "gl-debit.ach"))
+	require.NoError(t, err)
+
+	b1 := file.Batches[0]
+	entries := b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, GLDebit, entries[0].TransactionCode)
+
+	// Reverse
+	effectiveEntryDate := time.Now().In(time.UTC)
+	err = file.Reversal(effectiveEntryDate)
+	require.NoError(t, err)
+
+	b1 = file.Batches[0]
+	require.Equal(t, "REVERSAL", b1.GetHeader().CompanyEntryDescription)
+
+	entries = b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, GLCredit, entries[0].TransactionCode)
+
+	// Reverse the REVERSAL
+	err = file.Reversal(effectiveEntryDate)
+	require.NoError(t, err)
+
+	b1 = file.Batches[0]
+	require.Equal(t, "REVERSAL", b1.GetHeader().CompanyEntryDescription)
+
+	entries = b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, GLDebit, entries[0].TransactionCode)
+}
+
+func TestReversal_LoanCredit(t *testing.T) {
+	// Credit
+	file, err := ReadFile(filepath.Join("test", "testdata", "loan-credit.ach"))
+	require.NoError(t, err)
+
+	b1 := file.Batches[0]
+	entries := b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, LoanCredit, entries[0].TransactionCode)
+
+	// Reverse
+	effectiveEntryDate := time.Now().In(time.UTC)
+	err = file.Reversal(effectiveEntryDate)
+	require.NoError(t, err)
+
+	b1 = file.Batches[0]
+	require.Equal(t, "REVERSAL", b1.GetHeader().CompanyEntryDescription)
+
+	entries = b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, LoanDebit, entries[0].TransactionCode)
+
+	// Reverse the REVERSAL
+	err = file.Reversal(effectiveEntryDate)
+	require.NoError(t, err)
+
+	b1 = file.Batches[0]
+	require.Equal(t, "REVERSAL", b1.GetHeader().CompanyEntryDescription)
+
+	entries = b1.GetEntries()
+	require.Len(t, entries, 1)
+	require.Equal(t, LoanCredit, entries[0].TransactionCode)
+}
diff --git a/test/testdata/gl-debit.ach b/test/testdata/gl-debit.ach
new file mode 100644
index 00000000..7eb0697b
--- /dev/null
+++ b/test/testdata/gl-debit.ach
@@ -0,0 +1,11 @@
+101 231380104 1210428821906240000A094101Federal Reserve Bank   My Bank Name                   
+5225Name on Account                     121042882 PPDREG.SALARY      190625   1121042880000001
+64723138010412345678         0100000000               Receiver Account Name   0121042880000001
+82250000010023138010000100000000000000000000121042882                          121042880000001
+9000001000001000000010023138010000100000000000000000000                                       
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+
diff --git a/test/testdata/loan-credit.ach b/test/testdata/loan-credit.ach
new file mode 100644
index 00000000..511cbf4e
--- /dev/null
+++ b/test/testdata/loan-credit.ach
@@ -0,0 +1,11 @@
+101 231380104 1210428821906240000A094101Federal Reserve Bank   My Bank Name                   
+5220Name on Account                     121042882 PPDREG.SALARY      190625   1121042880000001
+65223138010412345678         0100000000               Receiver Account Name   0121042880000001
+82200000010023138010000000000000000100000000121042882                          121042880000001
+9000001000001000000010023138010000000000000000100000000                                       
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
+