fix 'Can't call method "setup" on an undefined value' error when using into rates...
[freeside.git] / FS / FS / pay_batch / eft_canada.pm
1 package FS::pay_batch::eft_canada;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use FS::Record 'qsearch';
6 use FS::Conf;
7 use FS::cust_pay_batch;
8 use Date::Format 'time2str';
9 use Time::Local 'timelocal';
10
11 my $conf;
12 my $origid;
13
14 $name = 'eft_canada';
15
16 %import_info = ( filetype  => 'NONE' ); # see FS/bin/freeside-eftca-download
17
18 my ($trans_code, $process_date);
19
20 %export_info = (
21   init => sub {
22     my $conf = shift;
23     my @config = $conf->config('batchconfig-eft_canada'); 
24     # SFTP login, password, trans code, delay time
25     my $process_delay;
26     ($trans_code, $process_delay) = @config[2,3];
27     $process_delay ||= 1; # days
28     $process_date = time2str('%D', time + ($process_delay * 86400));
29   },
30   delimiter => '', # avoid blank lines for header/footer
31   # EFT Upload Specification for .CSV Files, Rev. 2.0
32   # not a true CSV format--strings aren't quoted, so be careful
33   row => sub {
34     my ($cust_pay_batch, $pay_batch) = @_;
35     my @fields;
36     # company + empty or first + last
37     my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
38     if ( $company ) {
39       push @fields, $company, ''
40     }
41     else {
42       push @fields, map { sprintf('%.64s', $_) } 
43         $cust_pay_batch->first, $cust_pay_batch->last;
44     }
45     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
46     my($bankno, $branch);
47     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
48       ($bankno, $branch) = ( $1, $2 );
49     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
50       ($branch, $bankno) = ( $1, $2 );
51     } else {
52       die "invalid branch/routing number '$aba'\n";
53     }
54     push @fields, sprintf('%05s', $branch),
55                   sprintf('%03s', $bankno),
56                   sprintf('%012s', $account),
57                   sprintf('%.02f', $cust_pay_batch->amount);
58     # DB = debit
59     push @fields, 'DB', $trans_code, $process_date;
60     push @fields, $cust_pay_batch->paybatchnum; # reference
61     # strip illegal characters that might occur in customer name
62     s/[,|']//g foreach @fields; # better substitution for these?
63     return join(',', @fields) . "\n";
64   },
65
66 );
67
68 1;