1 package FS::pay_batch::nacha;
4 use vars qw( %import_info %export_info $name $conf $entry_hash $DEBUG );
6 #use Time::Local 'timelocal';
21 'approved' => sub { 1 },
22 'declined' => sub { 0 },
36 my( $pay_batch, $cust_pay_batch_arrayref ) = @_;
38 $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
39 or die 'illegal NACHA Destination';
42 my $dest_name = $conf->config('batchconfig-nacha-destination_name');
43 $dest_name = substr( $dest_name. (' 'x23), 0, 23);
45 $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
46 or die 'illegal NACHA Origin';
49 my $company = $conf->config('company_name', $pay_batch->agentnum);
50 $company = substr(uc($company). (' 'x23), 0, 23);
54 #haha don't want to break after a quarter million years of a batch a day
55 #or 54 years for 5000 agent-virtualized hosted companies batching daily
56 my $refcode = substr( (' 'x8). $pay_batch->batchnum, -8);
58 #or only 25,000 years or 5.4 for 5000 companies :)
59 #though they would probably want them numbered per company
60 my $batchnum = substr( ('0'x7). $pay_batch->batchnum, -7);
64 warn "building File & Batch Header Records\n" if $DEBUG;
70 '1'. #Record Type Code
72 ' '. $dest. #Immediate Destination / 9-digit transit routing #
73 $origin. #Immediate Origin / 10 digit company number
74 time2str('%y%m%d', $now). #File Creation Date
75 time2str('%H%M', $now). #File Creation Time
76 'A'. #XXX file ID modifier, mult. files in transit? [A-Z0-9]
77 '094'. #94 character records
78 '10'. #Blocking Factor
80 $dest_name. #Immediate Destination Name / 23 char bank name
81 $company. #Immediate Origin Name / 23 char company name
82 $refcode. "\n". #Reference Code (internal/optional)
88 '5'. #Record Type Code
89 '225'. #Service Class Code (220 credits only,
90 # 200 mixed debits & credits)
91 substr($company, 0, 16). #on cust. statements
92 (' 'x20 ). #20 char "company internal use if desired"
93 $origin. #Company Identification (Immediate Origin)
95 #PPD "Prearranged Payments and Deposit entries" for consumer items
96 #CCD (Cash Concentration and Disbursement)
97 #CTX (Corporate Trade Exchange)
98 #TEL (Telephone initiated entires)
99 #WEB (Authorization received via the Internet)
100 'InterntSvc'. #XXX from conf 10 char txn desc, printed on cust. statements
102 #6 char "Descriptive date" printed on customer statements
103 #XXX now? or use a separate post date?
104 time2str('%y%m%d', $now).
106 #6 char date transactions are to be posted
107 #XXX now? or do we need a future banking day date like eft_canada trainwreck
108 time2str('%y%m%d', $now).
110 (' 'x3). #Settlement Date / Reserved
111 '1'. #Originator Status Code
112 substr($dest, 0, 8). #Originating Financial Institution
113 $batchnum. "\n" #Batch Number ("number batches sequentially")
118 my( $cust_pay_batch, $pay_batch, $batchcount, $batchtotal ) = @_;
120 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
122 $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
123 or die 'illegal NACHA Destination';
126 # "Total of all positions 4-11 on each 6 record"
127 $entry_hash += substr($aba,0,8);
129 my $cust_main = $cust_pay_batch->cust_main;
130 my $cust_identifier = substr($cust_main->display_custnum. (' 'x15), 0, 15);
132 #XXX paytype should actually be in the batch, but this will do for now
133 #27 checking debit, 37 savings debit
134 my $transaction_code = ( $cust_main->paytype =~ /savings/i ? '37' : '27' );
136 my $cust_name = substr($cust_main->name. (' 'x22), 0, 22);
138 my $tracenum = $dest. substr(('0'x7). $i, -6);
139 #non-PPD transactions? future
141 warn "building PPD Record\n" if $DEBUG;
144 # PPD Entry Detail Record
147 '6'. #Record Type Code
148 $transaction_code. #Transaction Code
149 $aba. #Receiving DFI Identification, check digit
150 substr($account.(' 'x17), 0, 17). #DFI Account number (Left justify)
151 sprintf('%010.0f', $cust_pay_batch->amount * 100). #Amount
152 $cust_identifier. #Individual Identification Number, 15 char
153 $cust_name. #Individual name (22-char)
154 ' '. #2 char "company internal use if desired"
155 '0'. #Addenda Record Indicator
156 # (' 'x15). #15 digit "bank will assign trace number"
162 my( $pay_batch, $batchcount, $batchtotal ) = @_;
164 #Only use the final 10 positions in the entry
165 $entry_hash = substr( '00'.$entry_hash, -10);
167 $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
168 or die 'illegal NACHA Destination';
171 $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
172 or die 'illegal NACHA Origin';
175 my $batchnum = substr( ('0'x7). $pay_batch->batchnum, -7);
177 warn "building Batch & File Control Records\n" if $DEBUG;
180 # Batch Control Record
183 '8'. #Record Type Code
184 '225'. #Service Class Code (220 credits only,
185 # 200 mixed debits&credits)
186 sprintf('%06d', $batchcount). #Entry / Addenda Count
188 sprintf('%012.0f', $batchtotal * 100). #Debit total
189 '000000000000'. #Credit total
190 $origin. #Company Identification (Immediate Origin)
191 (' 'x19). #Message Authentication Code (19 char blank)
192 (' 'x6). #Federal Reserve Use (6 char blank)
193 substr($dest, 0, 8). #Originating Financial Institution
194 $batchnum. "\n". #Batch Number ("number batches sequentially")
197 # File Control Record
200 '9'. #Record Type Code
201 '000001'. #Batch Counter (# of batch header recs)
202 sprintf('%06d', $batchcount + 4). #num of physical blocks on the file..?
203 sprintf('%08d', $batchcount). #total # of entry detail and addenda
205 sprintf('%012.0f', $batchtotal * 100). #Debit total
206 '000000000000'. #Credit total
207 ( ' 'x39 ) #Reserved / blank