b7a9e198eefda057e3ee6e451d9bc3833a3efb67
[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 ($business_trans_code, $personal_trans_code, $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   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
36           },
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
45           },
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
54           },
55 );
56
57 %export_info = (
58
59   init => sub {
60     my $conf = shift;
61     my $agentnum = shift;
62     my @config;
63     if ( $conf->exists('batch-spoolagent') ) {
64       @config = $conf->config('batchconfig-eft_canada', $agentnum);
65     } else {
66       @config = $conf->config('batchconfig-eft_canada');
67     }
68     # SFTP login, password, trans code, delay time
69     ($business_trans_code) = $config[2];
70     ($personal_trans_code) = $config[3];
71
72     $process_date = time2str('%D', process_date($conf, $agentnum));
73   },
74
75   delimiter => '', # avoid blank lines for header/footer
76
77   # EFT Upload Specification for .CSV Files, Rev. 2.0
78   # not a true CSV format--strings aren't quoted, so be careful
79   row => sub {
80     my ($cust_pay_batch, $pay_batch) = @_;
81     my @fields;
82     # company + empty or first + last
83     my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
84     if ( $company ) {
85       push @fields, 'Business';
86       push @fields, $company, '';
87       $trans_code = $business_trans_code;
88     }
89     else {
90       push @fields, 'Personal';
91       push @fields, map { sprintf('%.64s', $_) } 
92         $cust_pay_batch->first, $cust_pay_batch->last;
93         $trans_code = $personal_trans_code;
94     }
95     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
96     my($bankno, $branch);
97     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
98       ($bankno, $branch) = ( $1, $2 );
99     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
100       ($branch, $bankno) = ( $1, $2 );
101     } else {
102       die "invalid branch/routing number '$aba'\n";
103     }
104     push @fields, sprintf('%05s', $branch),
105                   sprintf('%03s', $bankno),
106                   $account,
107                   sprintf('%.02f', $cust_pay_batch->amount);
108     # DB = debit
109     push @fields, 'DB', $trans_code, $process_date;
110     push @fields, $cust_pay_batch->paybatchnum; # reference
111     # strip illegal characters that might occur in customer name
112     s/[,|']//g foreach @fields; # better substitution for these?
113     return join(',', @fields) . "\n";
114   },
115
116 );
117
118 sub download_note { # is a class method
119   my $class = shift;
120   my $pay_batch = shift;
121   my $conf = FS::Conf->new;
122   my $agentnum = $pay_batch->agentnum;
123   my $tomorrow = (localtime(time))[2] >= 10;
124   my $process_date = process_date($conf, $agentnum);
125   my $upload_date = $process_date - 86400;
126   my $date_format = $conf->config('date_format') || '%D';
127
128   my $note = '';
129   if ( $process_date - time < 86400*2 ) {
130     $note = 'Upload this file before 11:00 AM '. 
131             ($tomorrow ? 'tomorrow' : 'today') .
132             ' (' . time2str($date_format, $upload_date) . '). ';
133   } else {
134     $note = 'Upload this file before 11:00 AM on '.
135       time2str($date_format, $upload_date) . '. ';
136   }
137   $note .= 'Payments will be processed on '.
138     time2str($date_format, $process_date) . '.';
139
140   $note;
141 }
142
143 sub process_date {
144   my ($conf, $agentnum) = @_;
145   my @config;
146   if ( $conf->exists('batch-spoolagent') ) {
147     @config = $conf->config('batchconfig-eft_canada', $agentnum);
148   } else {
149     @config = $conf->config('batchconfig-eft_canada');
150   }
151
152   my $process_delay = $config[4] || 1;
153
154   if ( (localtime(time))[2] >= 10 and $process_delay == 1 ) {
155     # If downloading the batch after 10:00 local time, it likely won't make
156     # the cutoff for next-day turnaround, and EFT will reject it.
157     $process_delay++;
158   }
159
160   my $pt = time + ($process_delay * 86400);
161   my @lt = localtime($pt);
162   while (    $lt[6] == 0 #Sunday
163           || $lt[6] == 6 #Saturday
164           || $holiday_yearly{ $lt[4]+1 }{ $lt[3] }
165           || $holiday{ $lt[5]+1900 }{ $lt[4]+1 }{ $lt[3] }
166         )
167   {
168     $pt += 86400;
169     @lt = localtime($pt);
170   }
171
172   $pt;
173 }
174
175 1;