ea9d584025032280e4989f56a4c0ce082d33cb09
[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 #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
26 );
27 my %holiday = (
28   2012 => {
29              7 => { map {$_=>1}  2 }, #canada day
30              8 => { map {$_=>1}  6 }, #First Monday of August Civic Holiday
31              9 => { map {$_=>1}  3 }, #labour day
32             10 => { map {$_=>1}  8 }, #thanksgiving
33           },
34   2013 => {  2 => { map {$_=>1} 18 }, #family day
35              3 => { map {$_=>1} 29 }, #good friday
36              4 => { map {$_=>1}  1 }, #easter monday
37              5 => { map {$_=>1} 20 }, #victoria day
38              7 => { map {$_=>1}  1 }, #canada day
39              8 => { map {$_=>1}  5 }, #First Monday of August Civic Holiday
40              9 => { map {$_=>1}  2 }, #labour day
41             10 => { map {$_=>1} 14 }, #thanksgiving
42           },
43   2014 => {  2 => { map {$_=>1} 17 }, #family day
44              4 => { map {$_=>1} 18 }, #good friday
45              4 => { map {$_=>1} 21 }, #easter monday
46              5 => { map {$_=>1} 19 }, #victoria day
47              7 => { map {$_=>1}  1 }, #canada day
48              8 => { map {$_=>1}  4 }, #First Monday of August Civic Holiday
49              9 => { map {$_=>1}  1 }, #labour day
50             10 => { map {$_=>1} 13 }, #thanksgiving
51           },
52   2015 => {  2 => { map {$_=>1} 16 }, #family day
53              4 => { map {$_=>1}  3 }, #good friday
54              4 => { map {$_=>1}  6 }, #easter monday
55              5 => { map {$_=>1} 18 }, #victoria day
56              7 => { map {$_=>1}  1 }, #canada day
57              8 => { map {$_=>1}  3 }, #First Monday of August Civic Holiday
58              9 => { map {$_=>1}  7 }, #labour day
59             10 => { map {$_=>1} 12 }, #thanksgiving
60           },
61 );
62
63 %export_info = (
64
65   init => sub {
66     my $conf = shift;
67     my @config = $conf->config('batchconfig-eft_canada'); 
68     # SFTP login, password, trans code, delay time
69     my $process_delay;
70     ($trans_code, $process_delay) = @config[2,3];
71     $process_delay ||= 1; # days
72
73     my $pt = time + ($process_delay * 86400);
74     my @lt = localtime($pt);
75     while (    $lt[6] == 0 #Sunday
76             || $lt[6] == 6 #Saturday
77             || $holiday_yearly{ $lt[4]+1 }{ $lt[3] }
78             || $holiday{ $lt[5]+1900 }{ $lt[4]+1 }{ $lt[3] }
79           )
80     {
81       $pt += 86400;
82       @lt = localtime($pt);
83     }
84
85     $process_date = time2str('%D', $pt);
86   },
87
88   delimiter => '', # avoid blank lines for header/footer
89
90   # EFT Upload Specification for .CSV Files, Rev. 2.0
91   # not a true CSV format--strings aren't quoted, so be careful
92   row => sub {
93     my ($cust_pay_batch, $pay_batch) = @_;
94     my @fields;
95     # company + empty or first + last
96     my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
97     if ( $company ) {
98       push @fields, $company, ''
99     }
100     else {
101       push @fields, map { sprintf('%.64s', $_) } 
102         $cust_pay_batch->first, $cust_pay_batch->last;
103     }
104     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
105     my($bankno, $branch);
106     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
107       ($bankno, $branch) = ( $1, $2 );
108     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
109       ($branch, $bankno) = ( $1, $2 );
110     } else {
111       die "invalid branch/routing number '$aba'\n";
112     }
113     push @fields, sprintf('%05s', $branch),
114                   sprintf('%03s', $bankno),
115                   sprintf('%012s', $account),
116                   sprintf('%.02f', $cust_pay_batch->amount);
117     # DB = debit
118     push @fields, 'DB', $trans_code, $process_date;
119     push @fields, $cust_pay_batch->paybatchnum; # reference
120     # strip illegal characters that might occur in customer name
121     s/[,|']//g foreach @fields; # better substitution for these?
122     return join(',', @fields) . "\n";
123   },
124
125 );
126
127 1;