1 package FS::pay_batch::eft_canada;
4 use vars qw(@ISA %import_info %export_info $name);
5 use FS::Record 'qsearch';
7 use FS::cust_pay_batch;
8 use Date::Format 'time2str';
9 use Time::Local 'timelocal';
16 %import_info = ( filetype => 'NONE' ); # see FS/bin/freeside-eftca-download
18 my ($trans_code, $process_date);
20 #ref http://gocanada.about.com/od/canadatravelplanner/a/canada_holidays.htm
21 my %holiday_yearly = (
22 1 => { map {$_=>1} 1 }, #new year's
23 11 => { map {$_=>1} 11 }, #remembrance day
24 12 => { map {$_=>1} 25 }, #christmas
25 12 => { map {$_=>1} 26 }, #boxing day
28 2013 => { 2 => { map {$_=>1} 18 }, #family day
29 3 => { map {$_=>1} 29 }, #good friday
30 4 => { map {$_=>1} 1 }, #easter monday
31 5 => { map {$_=>1} 20 }, #victoria day
32 7 => { map {$_=>1} 1 }, #canada day
33 8 => { map {$_=>1} 5 }, #First Monday of August Civic Holiday
34 9 => { map {$_=>1} 2 }, #labour day
35 10 => { map {$_=>1} 14 }, #thanksgiving
37 2014 => { 2 => { map {$_=>1} 17 }, #family day
38 4 => { map {$_=>1} 18 }, #good friday
39 4 => { map {$_=>1} 21 }, #easter monday
40 5 => { map {$_=>1} 19 }, #victoria day
41 7 => { map {$_=>1} 1 }, #canada day
42 8 => { map {$_=>1} 4 }, #First Monday of August Civic Holiday
43 9 => { map {$_=>1} 1 }, #labour day
44 10 => { map {$_=>1} 13 }, #thanksgiving
46 2015 => { 2 => { map {$_=>1} 16 }, #family day
47 4 => { map {$_=>1} 3 }, #good friday
48 4 => { map {$_=>1} 6 }, #easter monday
49 5 => { map {$_=>1} 18 }, #victoria day
50 7 => { map {$_=>1} 1 }, #canada day
51 8 => { map {$_=>1} 3 }, #First Monday of August Civic Holiday
52 9 => { map {$_=>1} 7 }, #labour day
53 10 => { map {$_=>1} 12 }, #thanksgiving
61 my @config = $conf->config('batchconfig-eft_canada');
62 # SFTP login, password, trans code, delay time
64 ($trans_code, $process_delay) = @config[2,3];
65 $process_delay ||= 1; # days
67 my $pt = time + ($process_delay * 86400);
68 my @lt = localtime($pt);
69 while ( $lt[6] == 0 #Sunday
70 || $lt[6] == 6 #Saturday
71 || $holiday_yearly{ $lt[4]+1 }{ $lt[3] }
72 || $holiday{ $lt[5]+1900 }{ $lt[4]+1 }{ $lt[3] }
79 $process_date = time2str('%D', $pt);
82 delimiter => '', # avoid blank lines for header/footer
84 # EFT Upload Specification for .CSV Files, Rev. 2.0
85 # not a true CSV format--strings aren't quoted, so be careful
87 my ($cust_pay_batch, $pay_batch) = @_;
89 # company + empty or first + last
90 my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
92 push @fields, $company, ''
95 push @fields, map { sprintf('%.64s', $_) }
96 $cust_pay_batch->first, $cust_pay_batch->last;
98 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
100 if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
101 ($bankno, $branch) = ( $1, $2 );
102 } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
103 ($branch, $bankno) = ( $1, $2 );
105 die "invalid branch/routing number '$aba'\n";
107 push @fields, sprintf('%05s', $branch),
108 sprintf('%03s', $bankno),
110 sprintf('%.02f', $cust_pay_batch->amount);
112 push @fields, 'DB', $trans_code, $process_date;
113 push @fields, $cust_pay_batch->paybatchnum; # reference
114 # strip illegal characters that might occur in customer name
115 s/[,|']//g foreach @fields; # better substitution for these?
116 return join(',', @fields) . "\n";