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 ($business_trans_code, $personal_trans_code, $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 2015 => { 2 => { map {$_=>1} 16 }, #family day
29 4 => { map {$_=>1} 3 }, #good friday
30 4 => { map {$_=>1} 6 }, #easter monday
31 5 => { map {$_=>1} 18 }, #victoria day
32 7 => { map {$_=>1} 1 }, #canada day
33 8 => { map {$_=>1} 3 }, #First Monday of August Civic Holiday
34 9 => { map {$_=>1} 7 }, #labour day
35 10 => { map {$_=>1} 12 }, #thanksgiving
37 2016 => { 2 => { map {$_=>1} 15 }, #family day
38 3 => { map {$_=>1} 25 }, #good friday
39 3 => { map {$_=>1} 28 }, #easter monday
40 5 => { map {$_=>1} 23 }, #victoria day
41 7 => { map {$_=>1} 1 }, #canada day
42 8 => { map {$_=>1} 1 }, #First Monday of August Civic Holiday
43 9 => { map {$_=>1} 5 }, #labour day
44 10 => { map {$_=>1} 10 }, #thanksgiving
54 if ( $conf->exists('batch-spoolagent') ) {
55 @config = $conf->config('batchconfig-eft_canada', $agentnum);
57 @config = $conf->config('batchconfig-eft_canada');
59 # SFTP login, password, trans code, delay time
60 ($business_trans_code) = $config[2];
61 ($personal_trans_code) = $config[3];
63 $process_date = time2str('%D', process_date($conf, $agentnum));
66 delimiter => '', # avoid blank lines for header/footer
68 # EFT Upload Specification for .CSV Files, Rev. 2.0
69 # not a true CSV format--strings aren't quoted, so be careful
71 my ($cust_pay_batch, $pay_batch) = @_;
73 # company + empty or first + last
74 my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
76 push @fields, 'Business';
77 push @fields, $company, '';
78 $trans_code = $business_trans_code;
81 push @fields, 'Personal';
82 push @fields, map { sprintf('%.64s', $_) }
83 $cust_pay_batch->first, $cust_pay_batch->last;
84 $trans_code = $personal_trans_code;
86 my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
88 if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
89 ($bankno, $branch) = ( $1, $2 );
90 } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
91 ($branch, $bankno) = ( $1, $2 );
93 die "invalid branch/routing number '$aba'\n";
95 push @fields, sprintf('%05s', $branch),
96 sprintf('%03s', $bankno),
98 sprintf('%.02f', $cust_pay_batch->amount);
100 push @fields, 'DB', $trans_code, $process_date;
101 push @fields, $cust_pay_batch->paybatchnum; # reference
102 # strip illegal characters that might occur in customer name
103 s/[,|']//g foreach @fields; # better substitution for these?
104 return join(',', @fields) . "\n";
109 sub download_note { # is a class method
111 my $pay_batch = shift;
112 my $conf = FS::Conf->new;
113 my $agentnum = $pay_batch->agentnum;
114 my $tomorrow = (localtime(time))[2] >= 10;
115 my $process_date = process_date($conf, $agentnum);
116 my $upload_date = $process_date - 86400;
117 my $date_format = $conf->config('date_format') || '%D';
120 if ( $process_date - time < 86400*2 ) {
121 $note = 'Upload this file before 11:00 AM '.
122 ($tomorrow ? 'tomorrow' : 'today') .
123 ' (' . time2str($date_format, $upload_date) . '). ';
125 $note = 'Upload this file before 11:00 AM on '.
126 time2str($date_format, $upload_date) . '. ';
128 $note .= 'Payments will be processed on '.
129 time2str($date_format, $process_date) . '.';
135 my ($conf, $agentnum) = @_;
137 if ( $conf->exists('batch-spoolagent') ) {
138 @config = $conf->config('batchconfig-eft_canada', $agentnum);
140 @config = $conf->config('batchconfig-eft_canada');
143 my $process_delay = $config[4] || 1;
145 if ( (localtime(time))[2] >= 10 and $process_delay == 1 ) {
146 # If downloading the batch after 10:00 local time, it likely won't make
147 # the cutoff for next-day turnaround, and EFT will reject it.
151 my $pt = time + ($process_delay * 86400);
152 my @lt = localtime($pt);
153 while ( $lt[6] == 0 #Sunday
154 || $lt[6] == 6 #Saturday
155 || $holiday_yearly{ $lt[4]+1 }{ $lt[3] }
156 || $holiday{ $lt[5]+1900 }{ $lt[4]+1 }{ $lt[3] }
160 @lt = localtime($pt);