manual batch approval and TD EFT fixes, RT#10545
[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   init => sub {
57     $conf = shift;
58     @opt{
59       'origid',
60       'datacenter',
61       'shortname',
62       'longname',
63       'retbranch',
64       'retacct',
65       'cpacode',
66     } = $conf->config("batchconfig-td_eft1464");
67     $opt{'origid'} = sprintf('%-10s', $opt{'origid'});
68     $opt{'shortname'} = sprintf('%-15s', $opt{'shortname'});
69     $opt{'longname'} = sprintf('%-30s', $opt{'longname'});
70     $opt{'retbranch'} = '0004'.sprintf('%5s',$opt{'retbranch'});
71     $opt{'retacct'} = sprintf('%-11s', $opt{'retacct'}). ' ';
72     $i = 1;
73   },
74   header => sub { 
75     my $pay_batch = shift;
76     my @cust_pay_batch = @{(shift)};
77     my $time = $pay_batch->download || time;
78     my $now = sprintf("%03u%03u", 
79       (localtime(time))[5] % 100,#year since 1900
80       (localtime(time))[7]+1);#day of year
81
82     # Request settlement the next day
83     my $duedate = time+86400;
84     $opt{'due'} = sprintf("%03u%03u",
85       (localtime($duedate))[5] % 100,
86       (localtime($duedate))[7]+1);
87
88     $opt{'fcn'} = 
89       sprintf('%04u', ($pay_batch->batchnum % 9999)+1), # file creation number
90     join('',
91       'A', #record type
92       sprintf('%09u', 1), #record number
93       $opt{'origid'},
94       $opt{'fcn'},
95       $now,
96       $opt{'datacenter'},
97       ' ' x 1429, #filler
98       "\r"
99     );
100   },
101   row => sub {
102     my ($cust_pay_batch, $pay_batch) = @_;
103     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
104     $i++;
105     # The 1464 byte format supports up to 5 payments per line,
106     # but we're only going to send 1.
107     my $control = join('',
108       'D',                  # for 'debit'
109       sprintf("%09u", $i),  #record number
110       $opt{'origid'},
111       $opt{'fcn'},
112     );
113     my $payment = join('',
114       $opt{'cpacode'} || 437, # CPA code, defaults to "Internet access"
115       sprintf('%010.0f', $cust_pay_batch->amount*100),
116       $opt{'due'}, #due date...? XXX
117       sprintf('%09u', $aba),
118       sprintf('%-12s', $account),
119       ' ' x 22,
120       '0' x 3,
121       $opt{'shortname'},
122       sprintf('%-30s', 
123         join(' ',
124           $cust_pay_batch->first, $cust_pay_batch->last)
125       ),
126       $opt{'longname'},
127       $opt{'origid'},
128       sprintf('%-19s', $cust_pay_batch->paybatchnum), # originator reference num
129       $opt{'retbranch'},
130       $opt{'retacct'}, 
131       ' ' x 15,
132       ' ' x 22,
133       ' ' x 2,
134       '0' x 11,
135     );
136     return sprintf('%-1464s',$control . $payment) . "\r";
137   },
138   footer => sub {
139     my ($pay_batch, $batchcount, $batchtotal) = @_;
140     join('',
141       'Z',
142       sprintf('%09u', $batchcount + 2),
143       $opt{'origid'}, 
144       $opt{'fcn'},
145       sprintf('%014.0f', $batchtotal*100), # total of debit txns
146       sprintf('%08u', $batchcount), # number of debit txns
147       '0' x 14, # total of credit txns
148       '0' x 8, # total of credit txns
149       ' ' x 1396,
150       "\r"
151     )
152   },
153 );
154
155 1;
156