diff --git a/src/Nacha/Batch.php b/src/Nacha/Batch.php index 6c32f76..0c3bafa 100644 --- a/src/Nacha/Batch.php +++ b/src/Nacha/Batch.php @@ -138,18 +138,17 @@ public function getEntryHash() { * @param Entry $entry * @return bool */ - private function isCredit(Entry $entry) { - $creditEntryTransactionCodes = [26,27,28,29,36,37,38,39,46,47,48,49,55,56]; - return in_array($entry->getTransactionCode()->getValue(),$creditEntryTransactionCodes); + private function isDebit(Entry $entry) { + return TransactionCode::isDebit($entry->getTransactionCode()->getValue()); + } /** * @param Entry $entry * @return bool */ - private function isDebit(Entry $entry) { - $debitEntryTransactionCodes = [21,22,23,24,31,32,33,34,41,42,43,44,51,52,53,54]; - return in_array($entry->getTransactionCode()->getValue(),$debitEntryTransactionCodes); + private function isCredit(Entry $entry) { + return TransactionCode::isCredit($entry->getTransactionCode()->getValue()); } /** diff --git a/src/Nacha/Field/TransactionCode.php b/src/Nacha/Field/TransactionCode.php index 5871710..f155ccf 100644 --- a/src/Nacha/Field/TransactionCode.php +++ b/src/Nacha/Field/TransactionCode.php @@ -4,57 +4,117 @@ class TransactionCode extends Number { - const CHECKING_DEPOSIT = 22; // Deposit destined for a Checking Account + const CHECKING_CREDIT_RETURN = 21; + const CHECKING_DEPOSIT = 22; // Deposit destined for a Checking Account const CHECKING_CREDIT_PRENOTIFICATION = 23; // Prenotification for a checking credit - const CHECKING_ZERO_DOLLAR = 24; // Zero dollar with remittance into Checking + const CHECKING_ZERO_DOLLAR = 24; // Zero dollar with remittance into Checking - const CHECKING_DEBIT = 27; // Debit destined for a Checking Account + const CHECKING_DEBIT_RETURN = 26; + const CHECKING_DEBIT = 27; // Debit destined for a Checking Account const CHECKING_DEBIT_PRENOTIFICATION = 28; // Prenotification for a checking debit - const CHECKING_DEBIT_ZERO_DOLLAR = 29; // Zero dollar with remittance into Checking - duplicate? + const CHECKING_DEBIT_ZERO_DOLLAR = 29; // Zero dollar with remittance into Checking - duplicate? - const SAVINGS_DEPOSIT = 32; // Deposit destined for a Savings Account + const SAVINGS_CREDIT_RETURN = 31; + const SAVINGS_DEPOSIT = 32; // Deposit destined for a Savings Account const SAVINGS_CREDIT_PRENOTIFICATION = 33; // Prenotification for a savings credit - const SAVINGS_CREDIT_ZERO_DOLLAR = 34; // Zero dollar with remittance into Savings + const SAVINGS_CREDIT_ZERO_DOLLAR = 34; // Zero dollar with remittance into Savings - const SAVINGS_DEBIT = 37; // Debit destined for a Savings Account - const SAVINGS_DEBIT_PRENOTIFICATION = 38; // Prenotification for a Savings debit - const SAVINGS_DEBIT_ZERO_DOLLAR = 39; // Zero dollar with remittance into Savings + const SAVINGS_DEBIT_RETURN = 36; + const SAVINGS_DEBIT = 37; // Debit destined for a Savings Account + const SAVINGS_DEBIT_PRENOTIFICATION = 38; // Prenotification for a Savings debit + const SAVINGS_DEBIT_ZERO_DOLLAR = 39; // Zero dollar with remittance into Savings - const GL_CREDIT = 42; // General ledger Deposit (Credit) - const GL_DEBIT = 47; // General ledger Withdrawal - const GL_CREDIT_PRENOTIFICATION = 48; // Prenotification for General ledger Deposit (Credit) - const LOAN_CREDIT = 52; // Loan Deposit (Credit) - const LOAN_REVERSAL = 55; // Loan Reversal (Debit) (used rarely; reverses code 52) - const LOAN_CREDIT_PRENOTIFICATION = 53; // Pre-Note: Loan Deposit (Credit) + const GL_CREDIT_RETURN = 41; + const GL_CREDIT = 42; // General ledger Deposit (Credit) + const GL_CREDIT_PRENOTIFICATION = 43; + const GL_CREDIT_ZERO_DOLLAR = 44; + + const GL_DEBIT_REVERSAL = 46; + const GL_DEBIT = 47; // General ledger Withdrawal + const GL_DEBIT_PRENOTIFICATION = 48; // Prenotification for General ledger Deposit (Credit) + const GL_DEBIT_ZERO_DOLLAR = 49; + + const LOAN_CREDIT_REVERSAL = 51; + const LOAN_CREDIT = 52; // Loan Deposit (Credit) + const LOAN_CREDIT_PRENOTIFICATION = 53; // Pre-Note: Loan Deposit (Credit) + const LOAN_CREDIT_ZERO_DOLLAR = 54; + + const LOAN_REVERSAL = self::LOAN_DEBIT; // Loan Reversal (Debit) (used rarely; reverses code 52) + const LOAN_DEBIT = 55; + const LOAN_DEBIT_RETURN = 56; public function __construct($value) { parent::__construct($value, 2); - $valid = [ + if (!self::isValid($value)) { + throw new InvalidFieldException('Invalid transaction code "' . $value . '".'); + } + } + + /** + * @param int $code + * @return bool + */ + public static function isCredit($code) { + return in_array($code, self::getCreditTransactionCodes()); + } + + /** + * @param int $code + * @return bool + */ + public static function isDebit($code) { + return in_array($code, self::getDebitTransactionCodes()); + } + + /** + * @param $code + * @return bool + */ + public static function isValid($code) { + return in_array($code, array_merge(self::getCreditTransactionCodes(), self::getDebitTransactionCodes())); + } + + private static function getCreditTransactionCodes() { + return [ + self::CHECKING_CREDIT_RETURN, self::CHECKING_DEPOSIT, self::CHECKING_CREDIT_PRENOTIFICATION, self::CHECKING_ZERO_DOLLAR, - self::CHECKING_DEBIT, - self::CHECKING_DEBIT_PRENOTIFICATION, - self::CHECKING_DEBIT_ZERO_DOLLAR, + self::SAVINGS_CREDIT_RETURN, self::SAVINGS_DEPOSIT, self::SAVINGS_CREDIT_PRENOTIFICATION, self::SAVINGS_CREDIT_ZERO_DOLLAR, - self::SAVINGS_DEBIT, - self::SAVINGS_DEBIT_PRENOTIFICATION, - self::SAVINGS_DEBIT_ZERO_DOLLAR, + self::GL_CREDIT_RETURN, self::GL_CREDIT, - self::GL_DEBIT, self::GL_CREDIT_PRENOTIFICATION, + self::GL_CREDIT_ZERO_DOLLAR, + self::LOAN_CREDIT_REVERSAL, self::LOAN_CREDIT, - self::LOAN_REVERSAL, self::LOAN_CREDIT_PRENOTIFICATION, + self::LOAN_CREDIT_ZERO_DOLLAR, ]; + } - if (!in_array($value, $valid)) { - throw new InvalidFieldException('Invalid transaction code "' . $value . '".'); - } + + private static function getDebitTransactionCodes() { + return [ + self::CHECKING_DEBIT_RETURN, + self::CHECKING_DEBIT, + self::CHECKING_DEBIT_PRENOTIFICATION, + self::CHECKING_DEBIT_ZERO_DOLLAR, + self::SAVINGS_DEBIT_RETURN, + self::SAVINGS_DEBIT, + self::SAVINGS_DEBIT_PRENOTIFICATION, + self::SAVINGS_DEBIT_ZERO_DOLLAR, + self::GL_DEBIT_REVERSAL, + self::GL_DEBIT, + self::GL_DEBIT_PRENOTIFICATION, + self::GL_DEBIT_ZERO_DOLLAR, + self::LOAN_DEBIT, + self::LOAN_DEBIT_RETURN, + ]; } } \ No newline at end of file diff --git a/test/Nacha/BatchTest.php b/test/Nacha/BatchTest.php index 60aeace..0b0d7f2 100644 --- a/test/Nacha/BatchTest.php +++ b/test/Nacha/BatchTest.php @@ -31,7 +31,7 @@ public function setup() { public function testDebitOnlyBatch() { // when $this->batch->addEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999') @@ -48,7 +48,7 @@ public function testDebitOnlyBatch() { $this->assertEquals((string)$this->batch->getHeader()->getServiceClassCode(), Batch::DEBITS_ONLY); $this->assertEquals( "5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001\n". - "62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015\n". + "62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015\n". "822500000100091012980000000550000000000000001419871234 010212340000001", $output ); @@ -57,7 +57,7 @@ public function testDebitOnlyBatch() { public function testCreditOnlyBatch() { // when $this->batch->addEntry((new CcdEntry) - ->setTransactionCode(27) + ->setTransactionCode(22) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setReceivingDFiAccountNumber('46479999') @@ -74,7 +74,7 @@ public function testCreditOnlyBatch() { $this->assertEquals((string)$this->batch->getHeader()->getServiceClassCode(), Batch::CREDITS_ONLY); $this->assertEquals( "5220MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001\n". - "62709101298746479999 0000060000Location 23 Best Co 23 S 0099363400000015\n". + "62209101298746479999 0000060000Location 23 Best Co 23 S 0099363400000015\n". "822000000100091012980000000000000000000600001419871234 010212340000001", $output ); @@ -83,7 +83,7 @@ public function testCreditOnlyBatch() { public function testMixedBatch() { // when $this->batch->addEntry((new CcdEntry) - ->setTransactionCode(27) + ->setTransactionCode(22) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setReceivingDFiAccountNumber('46479999') @@ -95,7 +95,7 @@ public function testMixedBatch() { ->setTraceNumber('09936340', 15)); $this->batch->addEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999') @@ -112,8 +112,8 @@ public function testMixedBatch() { $this->assertEquals((string)$this->batch->getHeader()->getServiceClassCode(), Batch::MIXED); $this->assertEquals( "5200MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001\n". - "62709101298746479999 0000060000Location 23 Best Co 23 S 0099363400000015\n". - "62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0099363400000015\n". + "62209101298746479999 0000060000Location 23 Best Co 23 S 0099363400000015\n". + "62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0099363400000015\n". "820000000200182025960000000550000000000600001419871234 010212340000001", $output ); diff --git a/test/Nacha/Field/TransactionCodeTest.php b/test/Nacha/Field/TransactionCodeTest.php index fc5c70c..ba3d2e3 100644 --- a/test/Nacha/Field/TransactionCodeTest.php +++ b/test/Nacha/Field/TransactionCodeTest.php @@ -4,8 +4,11 @@ class TransactionCodeTest extends \PHPUnit_Framework_TestCase { + private $creditCodes = [21,22,23,24,31,32,33,34,41,42,43,44,51,52,53,54]; + private $debitCodes = [26,27,28,29,36,37,38,39,46,47,48,49,55,56]; + /** - * @expectedException Nacha\Field\InvalidFieldException + * @expectedException \Nacha\Field\InvalidFieldException */ public function testInvalidType() { new TransactionCode(40); @@ -19,4 +22,35 @@ public function testValid() { $this->assertEquals(TransactionCode::SAVINGS_DEPOSIT, (string)$sec); } + public function testAllValidTransactionCodes() { + $validCodes = array_merge($this->creditCodes,$this->debitCodes); + + foreach($validCodes as $code) { + $this->assertTrue(TransactionCode::isValid($code)); + } + + $invalidCodes = [1,3,4,5,6,7,8,9,0,10,11,12,13,14,15,16,17,18,19,20,25,30,35,40,45,50,57,58,59,60]; + foreach($invalidCodes as $code) { + $this->assertFalse(TransactionCode::isValid($code)); + } + } + + + public function testAllCreditTransactionCodesAreRecognized() { + foreach($this->creditCodes as $code) { + $this->assertTrue(TransactionCode::isCredit($code)); + } + foreach($this->debitCodes as $code) { + $this->assertFalse(TransactionCode::isCredit($code)); + } + } + + public function testAllDebitTransactionCodesAreRecognized() { + foreach($this->creditCodes as $code) { + $this->assertFalse(TransactionCode::isDebit($code)); + } + foreach($this->debitCodes as $code) { + $this->assertTrue(TransactionCode::isDebit($code)); + } + } } \ No newline at end of file diff --git a/test/Nacha/FileTest.php b/test/Nacha/FileTest.php index 001da3e..9c933c2 100644 --- a/test/Nacha/FileTest.php +++ b/test/Nacha/FileTest.php @@ -44,10 +44,10 @@ public function testBatchesAndEntries() { // then $this->assertEquals("101 051000033 0599999970602102232A094101ImdDest Name ImdOriginName Referenc 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 822500000100091012980000000550000000000000001419871234 010212340000001 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDEXPENSES 0602 0112 2010212340000002 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 822500000100091012980000000550000000000000001419871234 010212340000002 9000002000001000000020018202596000000110000000000000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 @@ -72,7 +72,7 @@ public function testNoBlockFill() { $batchA = $this->getBatch(); $batchA->getHeader()->setCompanyEntryDescription('EXPENSES'); $batchA->addDebitEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999') @@ -84,7 +84,7 @@ public function testNoBlockFill() { ->setTraceNumber('99936340', 15)); $batchA->addDebitEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999') @@ -102,12 +102,12 @@ public function testNoBlockFill() { // then $this->assertEquals("101 051000033 0599999970602102232A094101ImdDest Name ImdOriginName Referenc 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 822500000100091012980000000550000000000000001419871234 010212340000001 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDEXPENSES 0602 0112 2010212340000002 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 -62209101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 -62209101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 822500000300273038940000001650000000000000001419871234 010212340000002 9000002000001000000040036405192000000220000000000000000 ", (string)$this->file); } @@ -117,7 +117,7 @@ public function testBlockWithEntries() { $batchA = $this->getBatch(); $batchA->getHeader()->setCompanyEntryDescription('EXPENSES'); $batchA->addDebitEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999') @@ -136,14 +136,14 @@ public function testBlockWithEntries() { // then $this->assertEquals("101 051000033 0599999970602102232A094101ImdDest Name ImdOriginName Referenc 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000001 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 822500000100091012980000000550000000000000001419871234 010212340000001 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDPAYROLL 0602 0112 2010212340000002 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 822500000100091012980000000550000000000000001419871234 010212340000002 5225MY BEST COMP INCLUDES OVERTIME 1419871234PPDEXPENSES 0602 0112 2010212340000003 -62209101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 -62209101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Alex Dubrovsky S 0999363400000015 +62709101298746479999 0000055000SomePerson1255 Philip Whitt S 0999363400000015 822500000200182025960000001100000000000000001419871234 010212340000003 9000003000002000000040036405192000000220000000000000000 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 @@ -170,7 +170,7 @@ private function getBatch() { ->setOriginatingDFiId('01021234'); $batch->addDebitEntry((new DebitEntry) - ->setTransactionCode(22) + ->setTransactionCode(27) ->setReceivingDfiId('09101298') ->setCheckDigit(7) ->setDFiAccountNumber('46479999')