2016 holidays for EFT canada, RT#18117
[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   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
36           },
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
45           },
46 );
47
48 %export_info = (
49
50   init => sub {
51     my $conf = shift;
52     my $agentnum = shift;
53     my @config;
54     if ( $conf->exists('batch-spoolagent') ) {
55       @config = $conf->config('batchconfig-eft_canada', $agentnum);
56     } else {
57       @config = $conf->config('batchconfig-eft_canada');
58     }
59     # SFTP login, password, trans code, delay time
60     ($business_trans_code) = $config[2];
61     ($personal_trans_code) = $config[3];
62
63     $process_date = time2str('%D', process_date($conf, $agentnum));
64   },
65
66   delimiter => '', # avoid blank lines for header/footer
67
68   # EFT Upload Specification for .CSV Files, Rev. 2.0
69   # not a true CSV format--strings aren't quoted, so be careful
70   row => sub {
71     my ($cust_pay_batch, $pay_batch) = @_;
72     my @fields;
73     # company + empty or first + last
74     my $company = sprintf('%.64s', $cust_pay_batch->cust_main->company);
75     if ( $company ) {
76       push @fields, 'Business';
77       push @fields, $company, '';
78       $trans_code = $business_trans_code;
79     }
80     else {
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;
85     }
86     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
87     my($bankno, $branch);
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 );
92     } else {
93       die "invalid branch/routing number '$aba'\n";
94     }
95     push @fields, sprintf('%05s', $branch),
96                   sprintf('%03s', $bankno),
97                   $account,
98                   sprintf('%.02f', $cust_pay_batch->amount);
99     # DB = debit
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";
105   },
106
107 );
108
109 sub download_note { # is a class method
110   my $class = shift;
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';
118
119   my $note = '';
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) . '). ';
124   } else {
125     $note = 'Upload this file before 11:00 AM on '.
126       time2str($date_format, $upload_date) . '. ';
127   }
128   $note .= 'Payments will be processed on '.
129     time2str($date_format, $process_date) . '.';
130
131   $note;
132 }
133
134 sub process_date {
135   my ($conf, $agentnum) = @_;
136   my @config;
137   if ( $conf->exists('batch-spoolagent') ) {
138     @config = $conf->config('batchconfig-eft_canada', $agentnum);
139   } else {
140     @config = $conf->config('batchconfig-eft_canada');
141   }
142
143   my $process_delay = $config[4] || 1;
144
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.
148     $process_delay++;
149   }
150
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] }
157         )
158   {
159     $pt += 86400;
160     @lt = localtime($pt);
161   }
162
163   $pt;
164 }
165
166 1;