Merge branch 'master' of https://github.com/jgoodman/Freeside
[freeside.git] / FS / FS / pay_batch / nacha.pm
1 package FS::pay_batch::nacha;
2
3 use strict;
4 use vars qw( %import_info %export_info $name $conf $entry_hash $DEBUG );
5 use Date::Format;
6 #use Time::Local 'timelocal';
7 #use FS::Conf;
8
9 $name = 'NACHA';
10 my $i;
11 $DEBUG = 0;
12
13 %import_info = (
14   #XXX stub finish me
15   'filetype' => 'CSV',
16   'fields' => [
17   ],
18   'hook' => sub {
19     my $hash = shift;
20   },
21   'approved' => sub { 1 },
22   'declined' => sub { 0 },
23 );
24
25 %export_info = (
26
27   #optional
28   init => sub {
29     $conf = shift;
30   },
31
32   delimiter => '',
33
34
35   header => sub {
36     my( $pay_batch, $cust_pay_batch_arrayref ) = @_;
37
38     $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
39       or die 'illegal NACHA Destination';
40     my $dest = $1;
41
42     my $dest_name = $conf->config('batchconfig-nacha-destination_name');
43     $dest_name = substr( $dest_name. (' 'x23), 0, 23);
44
45     $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
46       or die 'illegal NACHA Origin';
47     my $origin = $1;
48
49     my $company = $conf->config('company_name', $pay_batch->agentnum);
50     $company = substr(uc($company). (' 'x23), 0, 23);
51
52     my $now = time;
53
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);
57
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);
61
62     $entry_hash = 0;
63
64     warn "building File & Batch Header Records\n" if $DEBUG;
65
66     ##
67     # File Header Record
68     ##
69
70     '1'.                      #Record Type Code
71     '01'.                     #Priority 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
79     '1'.                      #Format code
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)
83
84     ###
85     # Batch Header Record
86     ###
87
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)
94     'PPD'. #others?
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
101
102     #6 char "Descriptive date" printed on customer statements
103     #XXX now? or use a separate post date?
104     time2str('%y%m%d', $now).
105
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).
109
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")
114
115   },
116
117   'row' => sub {
118     my( $cust_pay_batch, $pay_batch, $batchcount, $batchtotal ) = @_;
119
120     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
121
122     $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
123       or die 'illegal NACHA Destination';
124     my $dest = $1;
125
126     # "Total of all positions 4-11 on each 6 record"
127     $entry_hash += substr($aba,0,8); 
128
129     my $cust_main = $cust_pay_batch->cust_main;
130     my $cust_identifier = substr($cust_main->display_custnum. (' 'x15), 0, 15);
131
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' );
135
136     my $cust_name = substr($cust_main->name. (' 'x22), 0, 22);
137     $i++;
138     my $tracenum = $dest. substr(('0'x7). $i, -6);
139     #non-PPD transactions?  future
140
141     warn "building PPD Record\n" if $DEBUG;
142
143     ###
144     # PPD Entry Detail Record
145     ###
146
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('%010d', $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"
157      $tracenum.
158     "\n"                              # (00000?)
159   },
160
161   'footer' => sub {
162     my( $pay_batch, $batchcount, $batchtotal ) = @_;
163
164     #Only use the final 10 positions in the entry
165     $entry_hash = substr( '00'.$entry_hash, -10); 
166
167     $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
168       or die 'illegal NACHA Destination';
169     my $dest = $1;
170
171     $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
172       or die 'illegal NACHA Origin';
173     my $origin = $1;
174
175     my $batchnum = substr( ('0'x7). $pay_batch->batchnum, -7);
176
177     warn "building Batch & File Control Records\n" if $DEBUG;
178
179     ###
180     # Batch Control Record
181     ###
182
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
187     $entry_hash.
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")
195
196     ###
197     # File Control Record
198     ###
199
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
204     $entry_hash.
205     sprintf('%012.0f', $batchtotal * 100). #Debit total
206     '000000000000'.                      #Credit total
207     ( ' 'x39 )                           #Reserved / blank
208
209   },
210
211 );
212
213 1;
214