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. #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 #Batch Number ("number batches sequentially")
118 my( $cust_pay_batch, $pay_batch, $batchcount, $batchtotal ) = @_;
120 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
122 # "Total of all positions 4-11 on each 6 record"
123 $entry_hash += substr($aba,0,8);
125 my $cust_main = $cust_pay_batch->cust_main;
126 my $cust_identifier = substr($cust_main->display_custnum. (' 'x15), 0, 15);
128 #XXX paytype should actually be in the batch, but this will do for now
129 #27 checking debit, 37 savings debit
130 my $transaction_code = ( $cust_main->paytype =~ /savings/i ? '37' : '27' );
132 my $cust_name = substr($cust_main->name. (' 'x22), 0, 22);
134 #non-PPD transactions? future
136 warn "building PPD Record\n" if $DEBUG;
139 # PPD Entry Detail Record
142 '6'. #Record Type Code
143 $transaction_code. #Transaction Code
144 $aba. #Receiving DFI Identification, check digit
145 substr($account.(' 'x17), 0, 17). #DFI Account number (Left justify)
146 sprintf('%010d', $cust_pay_batch->amount * 100). #Amount
147 $cust_identifier. #Individual Identification Number, 15 char
148 $cust_name. #Individual name (22-char)
149 ' '. #2 char "company internal use if desired"
150 '0'. #Addenda Record Indicator
151 (' 'x15) #15 digit "bank will assign trace number"
156 my( $pay_batch, $batchcount, $batchtotal ) = @_;
158 #Only use the final 10 positions in the entry
159 $entry_hash = substr( '00'.$entry_hash, -10);
161 $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
162 or die 'illegal NACHA Destination';
165 $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
166 or die 'illegal NACHA Origin';
169 my $batchnum = substr( ('0'x7). $pay_batch->batchnum, -7);
171 warn "building Batch & File Control Records\n" if $DEBUG;
174 # Batch Control Record
177 '8'. #Record Type Code
178 '225'. #Service Class Code (220 credits only,
179 # 200 mixed debits&credits)
180 sprintf('%06d', $batchcount). #Entry / Addenda Count
182 sprintf('%012d', $batchtotal * 100). #Debit total
183 '000000000000'. #Credit total
184 $origin. #Company Identification (Immediate Origin)
185 (' 'x19). #Message Authentication Code (19 char blank)
186 (' 'x6). #Federal Reserve Use (6 char blank)
187 substr($dest, 0, 8). #Originating Financial Institution
188 $batchnum. #Batch Number ("number batches sequentially")
191 # File Control Record
194 '9'. #Record Type Code
195 '000001'. #Batch Counter (# of batch header recs)
196 sprintf('%06d', $batchcount + 4). #num of physical blocks on the file..?
197 sprintf('%08d', $batchcount). #total # of entry detail and addenda
199 sprintf('%012d', $batchtotal * 100). #Debit total
200 '000000000000'. #Credit total
201 ( ' 'x39 ) #Reserved / blank