3c60035cea46b567a4f25b437adee1ea176f7ddc
[freeside.git] / FS / FS / pay_batch / td_eft1464.pm
1 package FS::pay_batch::td_eft1464;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
6 use FS::Conf;
7 use FS::Record qw(qsearch);
8
9 =head1 NAME
10
11 td_eft1464 - TD Commercial Banking EFT1464 format
12
13 =head1 CONFIGURATION
14
15 The Freeside option 'batchconfig-td_eft1464' must be set 
16 with the following values on separate lines:
17
18 =over 4
19
20 =item Originator ID
21
22 =item TD Datacenter Location
23
24 00400 - Vancouver
25 00410 - Montreal
26 00420 - Toronto
27 00430 - Halifax
28 00470 - Winnipeg
29 00490 - Calgary
30
31 =item Short Name
32
33 =item Long Name
34
35 =item Returned Payment Branch (5 digits)
36
37 =item Returned Payment Account
38
39 =item Transaction Type Code - defaults to "437" (Internet access)
40
41 =back
42
43 =cut
44
45 my $conf;
46 my %opt;
47 my $i;
48
49 $name = 'td_eft1464';
50 # TD Bank EFT 1464 Byte format
51
52 %import_info = ( filetype => 'NONE' ); 
53 # just to suppress warning; importing this format is a fatal error
54
55 %export_info = (
56   delimiter => '',
57   init => sub {
58     $conf = shift;
59     @opt{
60       'origid',
61       'datacenter',
62       'shortname',
63       'longname',
64       'retbranch',
65       'retacct',
66       'cpacode',
67     } = $conf->config("batchconfig-td_eft1464");
68     $opt{'origid'} = sprintf('%-10.10s', $opt{'origid'});
69     $opt{'shortname'} = sprintf('%-15.15s', $opt{'shortname'});
70     $opt{'longname'} = sprintf('%-30.30s', $opt{'longname'});
71     $opt{'retbranch'} = '0004'.sprintf('%5.5s',$opt{'retbranch'});
72     $opt{'retacct'} = sprintf('%-11.11s', $opt{'retacct'}). ' ';
73     $i = 1;
74   },
75   header => sub { 
76     my $pay_batch = shift;
77     my @cust_pay_batch = @{(shift)};
78     my $time = $pay_batch->download || time;
79     my $now = sprintf("%03u%03u", 
80       (localtime(time))[5] % 100,#year since 1900
81       (localtime(time))[7]+1);#day of year
82
83     # Request settlement the next day
84     my $duedate = time+86400;
85     $opt{'due'} = sprintf("%03u%03u",
86       (localtime($duedate))[5] % 100,
87       (localtime($duedate))[7]+1);
88
89     $opt{'fcn'} = 
90       sprintf('%04u', ($pay_batch->batchnum % 9999)+1), # file creation number
91     join('',
92       'A', #record type
93       sprintf('%09u', 1), #record number
94       $opt{'origid'},
95       $opt{'fcn'},
96       $now,
97       $opt{'datacenter'},
98       ' ' x 1429, #filler
99     );
100   },
101   row => sub {
102     my ($cust_pay_batch, $pay_batch) = @_;
103     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
104     if ( $aba =~ /^(\d+)\.(\d+)$/ ) {  #branch.route
105       $aba = $2.$1; #routebranch
106     }
107     $i++;
108     # The 1464 byte format supports up to 5 payments per line,
109     # but we're only going to send 1.
110
111     ## set to D for debit by default, then override to what cust_pay_batch has as payments may not have paycode.
112     my $debitorcredit = 'D';
113     $debitorcredit = $cust_pay_batch->paycode unless !$cust_pay_batch->paycode;
114
115     my $control = join('',
116       $debitorcredit,       # D for 'debit' or C for Credit
117       sprintf("%09u", $i),  #record number
118       $opt{'origid'},
119       $opt{'fcn'},
120     );
121     my $payment = join('',
122       $opt{'cpacode'} || 437, # CPA code, defaults to "Internet access"
123       sprintf('%010.0f', $cust_pay_batch->amount*100),
124       $opt{'due'}, #due date...? XXX
125       sprintf('%09u', $aba),
126       sprintf('%-12.12s', $account),
127       '0' x 22,
128       '0' x 3,
129       $opt{'shortname'},
130       sprintf('%-30.30s', 
131         join(' ',
132           $cust_pay_batch->first, $cust_pay_batch->last)
133       ),
134       $opt{'longname'},
135       $opt{'origid'},
136       sprintf('%-19.19s', $cust_pay_batch->paybatchnum), # originator reference num
137       $opt{'retbranch'},
138       $opt{'retacct'}, 
139       ' ' x 15,
140       ' ' x 22,
141       ' ' x 2,
142       '0' x 11,
143     );
144     return sprintf('%-1464s',$control . $payment) ;
145   },
146   footer => sub {
147     my ($pay_batch, $batchcount, $batchtotal) = @_;
148     join('',
149       'Z',
150       sprintf('%09u', $batchcount + 2),
151       $opt{'origid'}, 
152       $opt{'fcn'},
153       sprintf('%014.0f', $batchtotal*100), # total of debit txns
154       sprintf('%08u', $batchcount), # number of debit txns
155       '0' x 14, # total of credit txns
156       '0' x 8, # total of credit txns
157       ' ' x 1396,
158     )
159   },
160 );
161
162 sub _upgrade_gateway {
163   my $conf = FS::Conf->new;
164   my @batchconfig = $conf->config('batchconfig-td_eft1464');
165   my %options;
166   @options{ qw(originator datacentre short_name long_name return_branch 
167                return_account cpa_code) } = @batchconfig;
168   ( 'TD_EFT', %options );
169 }
170
171 1;
172