NG auth: autocreate records for external users, RT#21563
[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
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($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.                 #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                #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     # "Total of all positions 4-11 on each 6 record"
123     $entry_hash += substr($aba,0,8); 
124
125     my $cust_main = $cust_pay_batch->cust_main;
126     my $cust_identifier = substr($cust_main->display_custnum. (' 'x15), 0, 15);
127
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' );
131
132     my $cust_name = substr($cust_main->name. (' 'x22), 0, 22);
133
134     #non-PPD transactions?  future
135
136     warn "building PPD Record\n" if $DEBUG;
137
138     ###
139     # PPD Entry Detail Record
140     ###
141
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"
152                                       # (00000?)
153   },
154
155   'footer' => sub {
156     my( $pay_batch, $batchcount, $batchtotal ) = @_;
157
158     #Only use the final 10 positions in the entry
159     $entry_hash = substr( '00'.$entry_hash, -10); 
160
161     $conf->config('batchconfig-nacha-destination') =~ /^\s*(\d{9})\s*$/
162       or die 'illegal NACHA Destination';
163     my $dest = $1;
164
165     $conf->config('batchconfig-nacha-origin') =~ /^\s*(\d{10})\s*$/
166       or die 'illegal NACHA Origin';
167     my $origin = $1;
168
169     my $batchnum = substr( ('0'x7). $pay_batch->batchnum, -7);
170
171     warn "building Batch & File Control Records\n" if $DEBUG;
172
173     ###
174     # Batch Control Record
175     ###
176
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
181     $entry_hash.
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")
189
190     ###
191     # File Control Record
192     ###
193
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
198     $entry_hash.
199     sprintf('%012d', $batchtotal * 100). #Debit total
200     '000000000000'.                      #Credit total
201     ( ' 'x39 )                           #Reserved / blank
202
203   },
204
205 );
206
207 1;
208