blib/lib/ACH/Builder.pm untested

filter: Statementstmt 0.0% 0 / 191 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 49 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 33 totaltotal 0.0% 0 / 356 CC 24 SCAR 24.7
CC
24
Cov
0%
make_batch
56.0
make_file_control_record
20.0
fixedlength
12.0
CRAP 11.8
1 package ACH::Builder;
2  
3 use strict;
4 use warnings;
5  
6 use POSIX qw( ceil strftime );
7 use Carp qw( carp croak );
8  
9 our $VERSION = '0.21';
10  
11 =pod
12  
13 =head1 NAME
14  
15 ACH::Builder - Tools for building ACH (Automated Clearing House) files
16  
17 =head1 SYNOPSIS
18  
19 use ACH::Builder;
20  
21 my $ach = ACH::Builder->new( {
22 # Required
23 company_id => '11-111111',
24 company_name => 'MY COMPANY',
25 entry_description => 'TV-TELCOM',
26 destination => '123123123',
27 destination_name => 'COMMERCE BANK',
28 origination => '12312311',
29 origination_name => 'MYCOMPANY',
30  
31 # Optional
32 company_note => 'BILL',
33 effective_date => '130903',
34 allow_unbalanced_file => 1,
35 } );
36  
37 # load some sample records
38 my @samples = $ach->sample_detail_records;
39  
40 # build file header record
41 $ach->make_file_header_record;
42  
43 # build batch for web entries
44 $ach->set_entry_class_code( 'WEB' );
45 $ach->make_batch( \@samples );
46  
47 # build batch for telephone entries
48 $ach->set_entry_class_code( 'TEL' );
49 $ach->make_batch( \@samples );
50  
51 # build file control record
52 $ach->make_file_control_record;
53  
54 print $ach->to_string;
55  
56 =head1 DESCRIPTION
57  
58 This module is tool to help construct ACH files, which are fixed-width
59 formatted files accpected by most banks. ACH (Automated Clearing House)
60 is an electronic banking network operating system in the United States.
61 ACH processes large volumes of both credit and debit transactions which
62 are originated in batches. Rules and regulations governing the ACH network
63 are established by the National Automated Clearing House Association
64 (NACHA) and the Federal Reserve (Fed).
65  
66 ACH credit transfers include direct deposit payroll payments and payments
67 to contractors and vendors. ACH debit transfers include consumer payments
68 on insurance premiums, mortgage loans, and other kinds of bills.
69  
70 =head1 CONFIGURATION
71  
72 The parameters below can be passed to the constructor C<new> in a hashref.
73  
74 =head2 company_id
75  
76 Required. Your 10-digit company number; usually your federal tax ID.
77  
78 =head2 company_name
79  
80 Required. Your company name to appear on the receiver's statement; up to 16
81 characters.
82  
83 =head2 entry_description
84  
85 Required per batch. A brief description of the nature of the transactions.
86 This will appear on the receiver's bank statement. Maximum of 10 characters.
87  
88 =head2 destination
89  
90 Required per file. The 9-digit routing number for the destination bank.
91  
92 =head2 destination_name
93  
94 Optional per file. A 23-character string identifying the destination bank.
95  
96 =head2 origination
97  
98 Required per file. This will usually be the same as the C<company_id>.
99  
100 =head2 origination_name
101  
102 Required per file. This will usually be the same as the C<company_name>,
103 but note that it can be up to 23 characters long.
104  
105 =head2 originating_dfi
106  
107 Optional per file. This will usually be the first 8 positions of C<origination>,
108 but it can be set specifically if a certain originating Depository Financial
109 Institution is needed.
110  
111 =head2 company_note
112  
113 Optional per batch. For your own internal use. Maximum 20 characters.
114  
115 =head2 effective_date
116  
117 Optional per batch. Date in C<yymmdd> format that the transactions should be posted.
118  
119 =head2 creation_date
120  
121 Optional per batch. Date in C<yymmdd> format that the transactions were created on.
122 Useful when needing to re-create files exactly as they were originally created.
123 Defaults to current date.
124  
125 =head2 creation_time
126  
127 Optional per batch. Time in C<HHMM> format that the transactions were created at.
128 Useful when needing to re-create files exactly as they were originally created.
129 Defaults to current time.
130  
131 =head2 allow_unbalanced_file
132  
133 Optional per file. 1=True, 0=False. Allows for a file to contain debits and
134 credits that don't net to 0. The default is 0. 0 is useful for using the file
135 to transfer funds and 1 is useful when only doing debits (billing for services)
136 or credits (refunding transactions, distributing funds).
137  
138 =head1 DETAIL RECORD FORMAT
139  
140 The C<make_batch> function expects entry detail records in this format:
141  
142 {
143 customer_name => 'JOHN SMITH', # Maximum of 22 characters
144 customer_acct => '0000-0111111', # Maximum of 15 characters
145 amount => '2501', # In whole cents; this is $25.01
146 routing_number => '10010101', # 9 digits
147 bank_account => '103030030', # Maximum of 17 characters
148 transaction_code => '27',
149 entry_trace => 'ABCDE0000000001', # Optional trace number
150 }
151  
152 Only the following transaction codes are supported:
153  
154 22 - Deposit to checking account
155 27 - Debit from checking account
156 32 - Deposit to savings account
157 37 - Debit from savings account
158  
159 Rules for the C<entry_trace> may vary. An example institution requires the
160 first 8 characters be the destination bank's routing number (excluding the
161 final checksum digit), and the next 7 characters be a zero-filled number
162 incrementing sequentially for each record.
163  
164 =head1 METHODS
165  
166 =head2 new({ company_id => '...', company_note ... })
167  
168 See above for configuration details. Note that the configuration parameter
169 names do not always match the names of the setter methods below.
170  
171 =cut
172  
173 sub new {
174 my ( $class, $vars ) = @_;
175  
176 my $self = {};
177 bless( $self, $class );
178  
179 $self->{__BATCH_COUNT__} = 0;
180 $self->{__ENTRY_COUNT__} = 0;
181 $self->{__ENTRY_HASH__} = 0;
182 $self->{__FILE_TOTAL_DEBIT__} = 0;
183 $self->{__FILE_TOTAL_CREDIT__} = 0;
184 $self->{__ACH_DATA__} = [];
185  
186 # Collapse given and default values
187 $self->set_service_class_code( $vars->{service_class_code} || 200);
188 $self->set_immediate_dest_name( $vars->{destination_name});
189 $self->set_immediate_origin_name( $vars->{origination_name});
190 $self->set_immediate_dest( $vars->{destination});
191 $self->set_immediate_origin( $vars->{origination});
192 $self->set_originating_dfi( $vars->{originating_dfi});
193  
194 $self->set_entry_class_code( $vars->{entry_class_code} || 'PPD');
195 $self->set_entry_description( $vars->{entry_description});
196 $self->set_company_id( $vars->{company_id});
197 $self->set_company_name( $vars->{company_name});
198 $self->set_company_note( $vars->{company_note});
199 $self->set_effective_date( $vars->{effective_date} || strftime( "%y%m%d", localtime( time + 86400 ) ));
200 $self->set_creation_date( $vars->{creation_date} || strftime( "%y%m%d", localtime( time ) ));
201 $self->set_creation_time( $vars->{creation_time} || strftime( "%H%M", localtime( time ) ));
202 $self->set_allow_unbalanced_file( $vars->{allow_unbalanced_file} || 0);
203  
204 $self->set_origin_status_code( $vars->{origin_status_code} || 1);
205 $self->set_file_id_modifier( $vars->{file_id_modifier} || 'A');
206 $self->set_record_size( $vars->{record_size} || 94);
207 $self->set_blocking_factor( $vars->{blocking_factor} || 10);
208 $self->set_format_code( $vars->{format_code} || 1);
209  
210 return( $self );
211 }
212  
213 =pod
214  
215 =head2 make_file_header_record( )
216  
217 Adds the file header record. This can only be called once and must be
218 called before any batches are added with C<make_batch>.
219  
220 =cut
221  
222 sub make_file_header_record {
223 my( $self ) = @_;
224  
225 # ach file header definition
226 my @def = qw(
227 record_type
228 priority_code
229 immediate_dest
230 immediate_origin
231 date
232 time
233 file_id_modifier
234 record_size
235 blocking_factor
236 format_code
237 immediate_dest_name
238 immediate_origin_name
239 reference_code
240 );
241  
242 my $data = {
243 record_type => 1,
244 priority_code => 1,
245 immediate_dest => $self->{__IMMEDIATE_DEST__},
246 immediate_origin => $self->{__IMMEDIATE_ORIGIN__},
247 date => $self->{__CREATION_DATE__},
248 time => $self->{__CREATION_TIME__},
249 file_id_modifier => $self->{__FILE_ID_MODIFIER__},
250 record_size => $self->{__RECORD_SIZE__},
251 blocking_factor => $self->{__BLOCKING_FACTOR__},
252 format_code => $self->{__FORMAT_CODE__},
253 immediate_dest_name => $self->{__IMMEDIATE_DEST_NAME__},
254 immediate_origin_name => $self->{__IMMEDIATE_ORIGIN_NAME__},
255 reference_code => '',
256 };
257  
258 push( @{ $self->ach_data() },
259 fixedlength( $self->format_rules, $data, \@def )
260 );
261 }
262  
263 =pod
264  
265 =head2 sample_detail_records( )
266  
267 Returns a list of sample records ready for C<make_batch>. See above for format details.
268  
269 =cut
270  
271 sub sample_detail_records {
272 return {
273 customer_name => 'JOHN SMITH',
274 customer_acct => '1234-0123456',
275 amount => '2501',
276 routing_number => '010010101',
277 bank_account => '103030030',
278 transaction_code => '27',
279 },
280 {
281 customer_name => 'ALICE VERYLONGNAMEGETSTRUNCATED',
282 customer_acct => 'verylongaccountgetstruncated',
283 amount => '2501',
284 routing_number => '010010401',
285 bank_account => '440030030',
286 transaction_code => '32',
287 },
288 ;
289 }
290  
291 =pod
292  
293 =head2 make_batch( @$records )
294  
295 Adds a batch of records to the file. You must have called
296 C<make_file_header_record> before adding a batch to the file.
297  
298 =cut
299  
300 sub make_batch {
301 my ( $self, $records ) = @_;
302  
303 croak "Invalid or empty records for batch" unless ref $records eq 'ARRAY' && @$records;
304  
305 ++$self->{__BATCH_COUNT__};
306  
307 # reset the batch variables
308 $self->{__BATCH_TOTAL_DEBIT__} = 0;
309 $self->{__BATCH_TOTAL_CREDIT__} = 0;
310 $self->{__BATCH_ENTRY_COUNT__} = 0;
311 $self->{__BATCH_ENTRY_HASH__} = 0;
312  
313 $self->_make_batch_header_record();
314  
315 foreach my $record ( @$records ) {
316 croak 'Amount cannot be negative' if $record->{amount} < 0;
317  
318 if ($record->{transaction_code} =~ /^(27|37)$/) {
319 croak "Debits cannot be used with service_class_code 220" if $self->{__SERVICE_CLASS_CODE__} eq '220';
320 $self->{__BATCH_TOTAL_DEBIT__} += $record->{amount};
321 $self->{__FILE_TOTAL_DEBIT__} += $record->{amount};
322 } elsif ($record->{transaction_code} =~ /^(22|32)$/ ) {
323 croak "Credits cannot be used with service_class_code 220" if $self->{__SERVICE_CLASS_CODE__} eq '225';
324 $self->{__BATCH_TOTAL_CREDIT__} += $record->{amount};
325 $self->{__FILE_TOTAL_CREDIT__} += $record->{amount};
326 } else {
327 croak "Unsupported transaction_code '$record->{transaction_code}'";
328 }
329  
330 # modify batch values
331 # Hash is calculated without the checksum digit
332 $self->{__BATCH_ENTRY_HASH__} += substr $record->{routing_number}, 0, 8;
333 ++$self->{__BATCH_ENTRY_COUNT__};
334  
335 # modify file values
336 $self->{__ENTRY_HASH__} += substr $record->{routing_number}, 0, 8;
337 ++$self->{__ENTRY_COUNT__};
338  
339 $self->_make_detail_record( $record )
340 }
341  
342 $self->_make_batch_control_record();
343 }
344  
345 # For internal use only. Formats a detail record and adds it to the ACH data.
346 sub _make_detail_record {
347 my( $self, $record ) = @_;
348  
349 my @def = qw(
350 record_type
351 transaction_code
352 routing_number
353 bank_account
354 amount
355 customer_acct
356 customer_name
357 discretionary_data
358 addenda
359 entry_trace
360 );
361  
362 # default values for some fields
363 $record->{record_type} ||= 6;
364 $record->{discretionary_data} ||= '';
365 $record->{entry_trace} ||= substr($self->{__ORIGINATING_DFI__}, 0, 8)
366 . sprintf("%07s", $self->{__ENTRY_COUNT__}+1);
367 $record->{addenda} ||= 0;
368  
369 # stash detail record
370 push( @{ $self->ach_data() },
371 fixedlength( $self->format_rules(), $record, \@def )
372 );
373 }
374  
375 # For internal use only. Starts a batch of detail records.
376 sub _make_batch_header_record {
377 my( $self ) = @_;
378  
379 my @def = qw(
380 record_type
381 service_class_code
382 company_name
383 company_note
384 company_id
385 entry_class_code
386 entry_description
387 date
388 effective_date
389 settlement_date
390 origin_status_code
391 origin_dfi_id
392 batch_number
393 );
394  
395 my $data = {
396 record_type => 5,
397 service_class_code => $self->{__SERVICE_CLASS_CODE__},
398 company_name => $self->{__COMPANY_NAME__},
399 company_note => $self->{__COMPANY_NOTE__},
400 company_id => $self->{__COMPANY_ID__},
401 entry_class_code => $self->{__ENTRY_CLASS_CODE__},
402 entry_description => $self->{__ENTRY_DESCRIPTION__},
403 date => $self->{__EFFECTIVE_DATE__},
404 effective_date => $self->{__EFFECTIVE_DATE__},
405 settlement_date => '',
406 origin_status_code => $self->{__ORIGIN_STATUS_CODE__},
407 origin_dfi_id => $self->{__ORIGINATING_DFI__},
408 batch_number => $self->{__BATCH_COUNT__},
409 authen_code => '',
410 bank_6 => '',
411 };
412  
413 push( @{ $self->ach_data() },
414 fixedlength( $self->format_rules(), $data, \@def )
415 );
416 }
417  
418 # For internal use only. Closes out a batch of detail records.
419 sub _make_batch_control_record {
420 my( $self ) = @_;
421  
422 my @def = qw(
423 record_type
424 service_class_code
425 entry_count
426 entry_hash
427 total_debit_amount
428 total_credit_amount
429 company_id
430 authen_code
431 bank_6
432 origin_dfi_id
433 batch_number
434 );
435  
436 my $data = {
437 record_type => 8,
438 service_class_code => $self->{__SERVICE_CLASS_CODE__},
439 company_id => $self->{__COMPANY_ID__},
440 origin_dfi_id => $self->{__ORIGINATING_DFI__},
441 batch_number => $self->{__BATCH_COUNT__},
442 authen_code => '',
443 bank_6 => '',
444 entry_hash => $self->{__BATCH_ENTRY_HASH__},
445 entry_count => $self->{__BATCH_ENTRY_COUNT__},
446 total_debit_amount => $self->{__BATCH_TOTAL_DEBIT__},
447 total_credit_amount => $self->{__BATCH_TOTAL_CREDIT__},
448 };
449  
450 # Truncate leftmost digits of entry hash
451 $data->{entry_hash} = substr($data->{entry_hash}, length($data->{entry_hash}) - 10, 10) if length($data->{entry_hash}) > 10;
452  
453 push( @{ $self->ach_data() },
454 fixedlength( $self->format_rules(), $data, \@def )
455 );
456 }
457  
458 =pod
459  
460 =head2 make_file_control_record( )
461  
462 Adds the file control record, finishing the file. This can only be called
463 once. Afterward, the ACH file can be retrieved in its entirety with C<to_string>.
464  
465 =cut
466  
467 sub make_file_control_record {
468 my( $self ) = @_;
469  
470 unless ( $self->{__ALLOW_UNBALANCED_FILE__} ) {
471 croak "Detail records have unbalanced debits ($self->{__FILE_TOTAL_DEBIT__}) and credits ($self->{__FILE_TOTAL_CREDIT__})!"
472 unless $self->{__FILE_TOTAL_DEBIT__} eq $self->{__FILE_TOTAL_CREDIT__};
473 }
474  
475 my @def = qw(
476 record_type
477 batch_count
478 block_count
479 file_entry_count
480 entry_hash
481 total_debit_amount
482 total_credit_amount
483 bank_39
484 );
485  
486 my $data = {
487 record_type => 9,
488 batch_count => $self->{__BATCH_COUNT__},
489 block_count => ceil((scalar(@{ $self->{__ACH_DATA__} })+1)/$self->{__BLOCKING_FACTOR__}),
490 file_entry_count => $self->{__ENTRY_COUNT__},
491 entry_hash => $self->{__ENTRY_HASH__},
492 total_debit_amount => $self->{__FILE_TOTAL_DEBIT__},
493 total_credit_amount => $self->{__FILE_TOTAL_CREDIT__},
494 bank_39 => '',
495 };
496  
497 # Truncate leftmost digits of entry hash
498 $data->{entry_hash} = substr($data->{entry_hash}, length($data->{entry_hash}) - 10, 10) if length($data->{entry_hash}) > 10;
499  
500 push( @{ $self->ach_data() },
501 fixedlength( $self->format_rules(), $data, \@def )
502 );
503 }
504  
505 =pod
506  
507 =head2 format_rules( )
508  
509 Returns a hash of ACH format rules. Used internally to generate the
510 fixed-width format required for output.
511  
512 =cut
513  
514 sub format_rules {
515 my( $self ) = @_;
516  
517 return( {
518 customer_name => '%-22.22s',
519 customer_acct => '%-15.15s',
520 amount => '%010.10s',
521 discretionary_data => '%-2.2s',
522 entry_trace => '%-15.15s',
523 addenda => '%01.1s',
524 trace_num => '%-15.15s',
525 transaction_code => '%-2.2s',
526 record_type => '%1.1s',
527 bank_account => '%-17.17s',
528 routing_number => '%09.9s',
529  
530 record_type => '%1.1s',
531  
532 priority_code => '%02.2s',
533 immediate_dest => '%10.10s',
534 immediate_origin => '%10.10s',
535 date => '%-6.6s',
536 time => '%-4.4s',
537 file_id_modifier => '%1.1s',
538 record_size => '%03.3s',
539 blocking_factor => '%02.2s',
540 format_code => '%1.1s',
541 immediate_dest_name => '%-23.23s',
542 immediate_origin_name => '%-23.23s',
543 reference_code => '%-8.8s',
544  
545 service_class_code => '%-3.3s',
546 company_name => '%-16.16s',
547 company_note => '%-20.20s',
548 company_id => '%-10.10s',
549 entry_class_code => '%-3.3s',
550 entry_description => '%-10.10s',
551 effective_date => '%-6.6s',
552 settlement_date => '%-3.3s', # for bank
553 origin_status_code => '%-1.1s', # for bank
554 origin_dfi_id => '%-8.8s', # for bank
555 batch_number => '%07.7s',
556  
557 entry_count => '%06s',
558 entry_hash => '%010s',
559 total_debit_amount => '%012s',
560 total_credit_amount => '%012s',
561 authen_code => '%-19.19s',
562 bank_6 => '%-6.6s',
563  
564 batch_count => '%06s',
565 block_count => '%06s',
566 file_entry_count => '%08s',
567 bank_39 => '%-39.39s',
568 } );
569 }
570  
571 # For internal use only. Formats a record according to format_rules.
572 sub fixedlength {
573 my( $format, $data, $order ) = @_;
574  
575 my $fmt_string;
576 foreach my $field ( @$order ) {
577 croak "Format for the field $field was not defined"
578 unless defined $format->{$field};
579  
580 carp "data for $field is not defined"
581 unless defined $data->{$field};
582  
583 $data->{$field} ||= "";
584  
585 $fmt_string .= sprintf $format->{$field}, $data->{$field};
586 }
587  
588 return $fmt_string;
589 }
590  
591 =pod
592  
593 =head2 to_string( )
594  
595 Returns the built ACH file.
596  
597 =cut
598  
599 sub to_string {
600 my $self = shift;
601 return( join( "\n", @{ $self->{__ACH_DATA__} } ) );
602 }
603  
604 =pod
605  
606 =head2 ach_data( )
607  
608 Returns as an arrayref the formatted lines that will be turned into the ACH file.
609  
610 =cut
611  
612 sub ach_data {
613 my ( $self ) = shift;
614 $self->{__ACH_DATA__};
615 }
616  
617 =pod
618  
619 =head2 set_origin_status_code( $value )
620  
621 The code must be either:
622  
623 1 - Originator is a financial institution bound by the NACHA rules (the default)
624 2 - Originator is a federal agency not bound by the NACHA rules
625  
626 =cut
627  
628 sub set_origin_status_code {
629 my ( $self, $p ) = @_;
630 croak "Invalid origin_status_code" unless $p eq '1' || $p eq '2';
631 $self->{__ORIGIN_STATUS_CODE__} = $p;
632 }
633  
634 =pod
635  
636 =head2 set_format_code( $value )
637  
638 Of limited value. The only valid format code is I<1>, the default.
639  
640 =cut
641  
642 sub set_format_code {
643 my ( $self, $p ) = @_;
644 croak "format_code other than 1 is not supported" unless $p eq '1';
645 $self->{__FORMAT_CODE__} = $p;
646 }
647  
648 =pod
649  
650 =head2 set_blocking_factor( $value )
651  
652 Of limited value. The only valid blocking factor is I<10>, the default.
653  
654 =cut
655  
656 sub set_blocking_factor {
657 my ( $self, $p ) = @_;
658 croak "blocking_factor other than 10 is not supported" unless $p == 10;
659 $self->{__BLOCKING_FACTOR__} = $p;
660 }
661  
662 =pod
663  
664 =head2 set_record_size( $value )
665  
666 Of limited value. The only valid record size (characters per line) is I<94>, the default.
667  
668 =cut
669  
670 sub set_record_size {
671 my ( $self, $p ) = @_;
672 croak "record_size other than 94 is not supported" unless $p == 94;
673 $self->{__RECORD_SIZE__} = $p;
674 }
675  
676 =pod
677  
678 =head2 set_file_id_modifier( $value )
679  
680 A sequential alphanumeric value used to distinguish files submitted on the same day. The default is I<A>.
681  
682 =cut
683  
684 sub set_file_id_modifier {
685 my ( $self, $p ) = @_;
686 check_length($p, 'file_id_modifier');
687 $self->{__FILE_ID_MODIFIER__} = $p;
688 }
689  
690 =pod
691  
692 =head2 set_immediate_origin_name( $value )
693  
694 The same as the C<origination_name> described above. This will usually be
695 your company name and is limited to 23 characters.
696  
697 =cut
698  
699 sub set_immediate_origin_name {
700 my ( $self, $p ) = @_;
701 check_length($p, 'immediate_origin_name');
702 $self->{__IMMEDIATE_ORIGIN_NAME__} = $p;
703 }
704  
705 =pod
706  
707 =head2 set_immediate_origin( $value )
708  
709 The same as the C<origination> described above. This will usually be your
710 federal tax ID number, in I<##-#######> format.
711  
712 =cut
713  
714 sub set_immediate_origin {
715 my ( $self, $p ) = @_;
716 check_length($p, 'immediate_origin');
717 $self->{__IMMEDIATE_ORIGIN__} = $p;
718 $self->{__ORIGINATING_DFI__} = substr $p, 0, 8;
719 }
720  
721 =pod
722  
723 =head2 set_originating_dfi( $value )
724  
725 The same as the C<originating_dfi> described above, Originating DFI
726 Identification. This will default to the first 8 positions of
727 C<origination> unless otherwise specified.
728  
729 =cut
730  
731 sub set_originating_dfi {
732 my ( $self, $p ) = @_;
733 $p ||= substr $self->{__IMMEDIATE_ORIGIN__}, 0, 8;
734 check_length($p, 'origin_dfi_id');
735 $self->{__ORIGINATING_DFI__} = $p;
736 }
737  
738 =pod
739  
740 =head2 set_immediate_dest_name( $value )
741  
742 The same as the C<destination_name> described above. This identifies the
743 destination bank that will be processing this file. Limited to 23
744 characters.
745  
746 =cut
747  
748 sub set_immediate_dest_name {
749 my ( $self, $p ) = @_;
750 check_length($p, 'immediate_dest_name');
751 $self->{__IMMEDIATE_DEST_NAME__} = $p;
752 }
753  
754 =pod
755  
756 =head2 set_immediate_dest( $value )
757  
758 The same as the C<destination> described above. This is the 9-digit routing
759 number of the destination bank.
760  
761 =cut
762  
763 sub set_immediate_dest {
764 my ( $self, $p ) = @_;
765 check_length($p, 'immediate_dest');
766 $self->{__IMMEDIATE_DEST__} = $p;
767 }
768  
769 =pod
770  
771 =head2 set_entry_description( $value )
772  
773 A brief description of the nature of the transactions. This will appear on
774 the receiver's bank statement. Maximum of 10 characters. This can be set
775 separately for each batch before C<make_batch> is called.
776  
777 =cut
778  
779 sub set_entry_description {
780 my ( $self, $p ) = @_;
781 check_length($p, 'entry_description');
782 $self->{__ENTRY_DESCRIPTION__} = $p;
783 }
784  
785 =pod
786  
787 =head2 set_entry_class_code( $value )
788  
789 The code must be one of:
790  
791 PPD - Prearranged Payments and Deposit entries for consumer items (the default)
792 CCD - Cash Concentration and Disbursement entries
793 CTX - Corporate Trade Exchange entries for corporate transactions
794 TEL - Telephone initiated entries
795 WEB - Authorization received via the Internet
796  
797 =cut
798  
799 sub set_entry_class_code {
800 my ( $self, $p ) = @_;
801 check_length($p, 'entry_class_code');
802 $self->{__ENTRY_CLASS_CODE__} = $p;
803 }
804  
805 =pod
806  
807 =head2 set_company_id( $value )
808  
809 Your 10-digit company number; usually your federal tax ID. This can be set
810 separately for each batch.
811  
812 =cut
813  
814 sub set_company_id {
815 my ( $self, $p ) = @_;
816 check_length($p, 'company_id');
817 $self->{__COMPANY_ID__} = $p;
818 }
819  
820 =pod
821  
822 =head2 set_company_name( $value )
823  
824 Required. Your company name to appear on the receiver's statement; up to 16
825 characters.
826  
827 =cut
828  
829 sub set_company_name {
830 my ( $self, $p ) = @_;
831 check_length($p, 'company_name');
832 $self->{__COMPANY_NAME__} = $p;
833 }
834  
835 =pod
836  
837 =head2 set_company_note( $value )
838  
839 An optional parameter for your internal use, limited to 20 characters.
840  
841 =cut
842  
843 sub set_company_note {
844 my ( $self, $p ) = @_;
845 check_length($p, 'company_note');
846 $self->{__COMPANY_NOTE__} = $p;
847 }
848  
849 =pod
850  
851 =head2 set_effective_date( $value )
852  
853 The date that transactions in this batch will be posted. The date should be
854 in I<YYMMDD> format. Defaults to tomorrow.
855  
856 =cut
857  
858 sub set_effective_date {
859 my ( $self, $p ) = @_;
860 croak "Invalid effective_date" unless $p =~ /^\d{6}$/;
861 check_length($p, 'effective_date');
862 $self->{__EFFECTIVE_DATE__} = $p;
863 }
864  
865 =pod
866  
867 =head2 set_creation_date( $value )
868  
869 The date that transactions in this batch were created. The date should be
870 in I<YYMMDD> format. Defaults to today.
871  
872 =cut
873  
874 sub set_creation_date {
875 my ( $self, $p ) = @_;
876 croak "Invalid creation_date" unless $p =~ /^\d{6}$/;
877 check_length($p, 'date');
878 $self->{__CREATION_DATE__} = $p;
879 }
880  
881 =pod
882  
883 =head2 set_creation_time( $value )
884  
885 The time that transactions in this batch were created. The time should be
886 in I<HHMM> format. Defaults to now.
887  
888 =cut
889  
890 sub set_creation_time {
891 my ( $self, $p ) = @_;
892 croak "Invalid creation_time" unless $p =~ /^\d{4}$/;
893 check_length($p, 'time');
894 $self->{__CREATION_TIME__} = $p;
895 }
896  
897 =pod
898  
899 =head2 set_allow_unbalanced_file( $value )
900  
901 Allow for a file's credits and debits to be unbalanced. Possible values are
902 1 (true) and 0 (false). Default is 0.
903  
904 =cut
905  
906 sub set_allow_unbalanced_file {
907 my ( $self, $p ) = @_;
908 croak "Invalid allow_unbalanced_file" unless $p ~~ [qw(0 1)];
909 $self->{__ALLOW_UNBALANCED_FILE__} = $p;
910 }
911  
912 =pod
913  
914 =head2 set_service_class_code( $value )
915  
916 The code must be one of:
917  
918 200 - Mixed credits and debits (the default)
919 220 - Credits only
920 225 - Debits only
921  
922 =cut
923  
924 sub set_service_class_code {
925 my ( $self, $p ) = @_;
926 croak "Invalid service_class_code" unless $p =~ /^(200|220|225)$/;
927 $self->{__SERVICE_CLASS_CODE__} = $p;
928 }
929  
930 # For internal use only. Checks that the value for a field fits and warns if not.
931 sub check_length {
932 my ($p, $field) = @_;
933 my $rules = format_rules();
934 carp "Field '$field' not found in format rules!" and return unless $rules->{$field};
935 (my $length = $rules->{$field}) =~ s/^%-?0*(\d+).*/$1/;
936 carp "Value '$p' for field $field will be truncated to '".sprintf($rules->{$field}, $p)."'!"
937 and return 0 if length $p > $length;
938 return 1;
939 }
940  
941 =pod
942  
943 =head1 NOTES
944  
945 The ACH record format is officially documented in the NACHA I<Operating
946 Rules & Guidelines> publication, which is not freely available. It can
947 be purchased at: https://www.nacha.org/achrules
948  
949 ACH file structure:
950  
951 File Header
952 Batch Header
953 Entries
954 Batch Control
955 Batch Header
956 Entries
957 Batch Control
958 File Control
959  
960 =head1 LIMITATIONS
961  
962 Only certain types of ACH transactions are supported (see the detail
963 record format above).
964  
965 =head1 AUTHOR
966  
967 Tim Keefer <tkeefer@gmail.com>
968  
969 =head1 CONTRIBUTORS
970  
971 =over 4
972  
973 =item *
974  
975 Cameron Baustian <camerb@cpan.org>
976  
977 =item *
978  
979 Steven N. Severinghaus <sns-perl@severinghaus.org>
980  
981 =back
982  
983 =head1 COPYRIGHT
984  
985 Tim Keefer, Cameron Baustian
986  
987 =cut
988  
989 1;