RT#37465: RBC PAD error when calculating totals
[freeside.git] / FS / FS / pay_batch / RBC.pm
1 package FS::pay_batch::RBC;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Date::Format 'time2str';
6 use FS::Conf;
7 use Encode 'encode';
8
9 my $conf;
10 my ($client_num, $shortname, $longname, $trans_code, $testmode, $i, $declined, $totaloffset);
11
12 $name = 'RBC';
13 # Royal Bank of Canada ACH Direct Payments Service
14
15 # Meaning of initial characters in records:
16 # 0 - header row, skipped by begin_condition
17 # 1 - Debit Detail Record (only when subtype is 0)
18 # 2 - Credit Detail Record, we die with a parse error (shouldn't appear in freeside-generated batches)
19 # 3 - Account Trailer Record (appears after Returned items, we skip)
20 # 4 - Client Trailer Record, indicates end of batch in end_condition
21 #
22 # Subtypes (27th char) indicate different kinds of Debit/Credit records
23 # 0 - Credit/Debit Detail Record
24 # 3 - Error Message Record
25 # 4 - Foreign Currency Information Records
26 # We skip all subtypes except 0
27 #
28 # additional info available at https://www.rbcroyalbank.com/ach/cid-213166.html
29 %import_info = (
30   'filetype'    => 'fixed',
31   #this only really applies to Debit Detail, but we otherwise only need first char
32   'formatre'    => 
33   '^(.).{18}(.{4}).{3}(.).{11}(.{19}).{6}(.{30}).{17}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}\r?$',
34   'fields' => [ qw(
35     recordtype
36     batchnum
37     subtype
38     paybatchnum
39     custname
40     bank
41     payinfo
42     paid
43     status
44     ) ],
45   'hook' => sub {
46       my $hash = shift;
47       $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
48       $hash->{'_date'} = time;
49       $hash->{'payinfo'} =~ s/^(\S+).*/$1/; # these often have trailing spaces
50       $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
51   },
52   'approved'    => sub { 
53       my $hash = shift;
54       $hash->{'status'} eq ' '
55   },
56   'declined'    => sub {
57       my $hash = shift;
58       my $status = $hash->{'status'};
59       my $message = '';
60       if ($status eq 'E') {
61         $message = 'Reversed payment';
62       } elsif ($status eq 'R') {
63         $message = 'Rejected payment';
64       } elsif ($status eq 'U') {
65         $message = 'Returned payment';
66       } elsif ($status eq 'T') {
67         $message = 'Error';
68       } else {
69         return 0;
70       }
71       $hash->{'error_message'} = $message;
72       $declined->{$hash->{'paybatchnum'}} = 1;
73       return 1;
74   },
75   'begin_condition' => sub {
76       my $hash = shift;
77       # Debit Detail Record
78       if ($hash->{recordtype} eq '1') {
79         $declined = {};
80         $totaloffset = 0;
81         return 1;
82       # Credit Detail Record, will immediately trigger end condition & error
83       } elsif ($hash->{recordtype} eq '2') { 
84         return 1;
85       } else {
86         return 0;
87       }
88   },
89   'end_hook'    => sub {
90       my( $hash, $total, $line ) = @_;
91       return "Can't process Credit Detail Record, aborting import"
92         if ($hash->{'recordtype'} eq '2');
93       $total += $totaloffset;
94       $total = sprintf("%.2f", $total);
95       # We assume here that this is an 'All Records' or 'Input Records' report.
96       my $batch_total = sprintf("%.2f", substr($line, 59, 18) / 100);
97       return "Our total $total does not match bank total $batch_total!"
98         if $total != $batch_total;
99       return '';
100   },
101   'end_condition' => sub {
102       my $hash = shift;
103       return ($hash->{recordtype} eq '4')  # Client Trailer Record
104           || ($hash->{recordtype} eq '2'); # Credit Detail Record, will throw error in end_hook
105   },
106   'skip_condition' => sub {
107       my $hash = shift;
108       #we already declined it this run, no takebacks
109       if ($declined->{$hash->{'paybatchnum'}}) {
110         #file counts this as part of total, but we skip
111         $totaloffset += sprintf("%.2f", $hash->{'paid'} / 100 )
112           if $hash->{'status'} eq ' '; #false laziness with 'approved' above
113         return 1;
114       }
115       #skipping W for now (maybe it should be declined?)
116       if ($hash->{'status'} eq 'W') {
117         #file counts this as part of total, but we skip
118         $totaloffset += sprintf("%.2f", $hash->{'paid'} / 100 );
119         return 1;
120       }
121       return 
122         ($hash->{'recordtype'} eq '3') || #Account Trailer Record, concludes returned items
123         ($hash->{'subtype'} ne '0'); #error messages, etc, too late to apply to previous entry
124   },
125 );
126
127 %export_info = (
128   init => sub {
129     $conf = shift;
130     ($client_num,
131      $shortname,
132      $longname,
133      $trans_code, 
134      $testmode
135      ) = $conf->config("batchconfig-RBC");
136     $testmode = '' unless $testmode eq 'TEST';
137     $i = 1;
138   },
139   header => sub { 
140     my $pay_batch = shift;
141     my $mode = $testmode ? 'TEST' : 'PROD';
142     my $filenum = $testmode ? 'TEST' : sprintf("%04u", $pay_batch->batchnum);
143     '$$AAPASTD0152['.$mode.'[NL$$'."\n".
144     '000001'.
145     'A'.
146     'HDR'.
147     sprintf("%10s", $client_num).
148     sprintf("%-30s", $longname).
149     $filenum.
150     time2str("%Y%j", $pay_batch->download).
151     'CAD'.
152     '1'.
153     ' ' x 87  # filler/reserved fields
154     ;
155   },
156   row => sub {
157     my ($cust_pay_batch, $pay_batch) = @_;
158     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
159     my($bankno, $branch);
160     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
161       ($bankno, $branch) = ( $1, $2 );
162     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
163       ($branch, $bankno) = ( $1, $2 );
164     } else {
165       die "invalid branch/routing number '$aba'\n";
166     }
167
168     $i++;
169     sprintf("%06u", $i).
170     'D'.
171     sprintf("%3s",$trans_code).
172     sprintf("%10s",$client_num).
173     ' '.
174     sprintf("%-19s", $cust_pay_batch->paybatchnum).
175     '00'.
176     sprintf("%04s", $bankno).
177     sprintf("%05s", $branch).
178     sprintf("%-18s", $account).
179     ' '.
180     sprintf("%010.0f",$cust_pay_batch->amount*100).
181     '      '.
182     time2str("%Y%j", time + 86400).
183     sprintf("%-30.30s", encode('utf8', $cust_pay_batch->cust_main->first . ' ' .
184                      $cust_pay_batch->cust_main->last)).
185     'E'. # English
186     ' '.
187     sprintf("%-15s", $shortname).
188     'CAD'.
189     ' '.
190     'CAN'.
191     '    '.
192     'N' # no customer optional information follows
193     ;
194 # Note: IAT Address Information and Remittance records are not 
195 # supported. This means you probably can't process payments 
196 # destined to U.S. bank accounts.  If you need this feature, contact 
197 # Freeside Internet Services.
198   },
199   footer => sub {
200     my ($pay_batch, $batchcount, $batchtotal) = @_;
201     sprintf("%06u", $i + 1).
202     'Z'.
203     'TRL'.
204     sprintf("%10s", $client_num).
205     '0' x 20 .
206     sprintf("%06u", $batchcount).
207     sprintf("%014.0f", $batchtotal*100).
208     '00' .
209     '000000' . # total number of customer information records
210     ' ' x 84
211     ;
212   },
213 );
214
215 1;
216