This commit was generated by cvs2svn to compensate for changes in r8690,
[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
8 my $conf;
9 my ($client_num, $shortname, $longname, $trans_code, $i);
10
11 $name = 'RBC';
12 # Royal Bank of Canada ACH Direct Payments Service
13
14 %import_info = (
15   'filetype'    => 'fixed',
16   'formatre'    => 
17   '^(.).{18}(.{4}).{15}(.{19}).{6}(.{30}).{17}(.{9})(.{18}).{6}(.{14}).{23}(.).{9}$',
18   'fields' => [ qw(
19     recordtype
20     batchnum
21     paybatchnum
22     custname
23     bank
24     payinfo
25     paid
26     status
27     ) ],
28   'hook' => sub {
29       my $hash = shift;
30       $hash->{'paid'} = sprintf("%.df", $hash->{'paid'} / 100 );
31       $hash->{'_date'} = time;
32       $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
33   },
34   'approved'    => sub { 
35       my $hash = shift;
36       $hash->{'status'} eq ' '
37   },
38   'declined'    => sub {
39       my $hash = shift;
40       grep { $hash->{'status'} eq $_ } ('E', 'R', 'U', 'T');
41   },
42   'end_hook'    => sub {
43       my( $hash, $total, $line ) = @_;
44       $total = sprintf("%.2f", $total);
45       my $batch_total = sprintf("%.2f", substr($line, 140, 18) / 100);
46       return "Our total $total does not match bank total $batch_total!"
47         if $total != $batch_total;
48       '';
49   },
50   'end_condition' => sub {
51       my $hash = shift;
52       $hash->{recordtype} == '3'; # Account Trailer Record
53   },
54 );
55
56 %export_info = (
57   init => sub {
58     $conf = shift;
59     ($client_num,
60      $shortname,
61      $longname,
62      $trans_code, 
63      ) = $conf->config("batchconfig-RBC");
64     $i = 1;
65   },
66   header => sub { 
67     my $pay_batch = shift;
68     '000001'.
69     'A'.
70     'HDR'.
71     sprintf("%10s", $client_num).
72     sprintf("%-30s", $longname).
73     sprintf("%04u", $pay_batch->batchnum).
74     time2str("%Y%j", $pay_batch->download).
75     'CAD'.
76     '1'.
77     ' ' x 87  # filler/reserved fields
78     ;
79   },
80   row => sub {
81     my ($cust_pay_batch, $pay_batch) = @_;
82     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
83     $i++;
84     sprintf("%06u", $i).
85     'D'.
86     sprintf("%3s",$trans_code).
87     sprintf("%10s",$client_num).
88     ' '.
89     sprintf("%-19s", $cust_pay_batch->paybatchnum).
90     '00'.
91     sprintf("%09u", $aba).
92     sprintf("%-18s", $account).
93     ' '.
94     sprintf("%010u",$cust_pay_batch->amount*100).
95     '      '.
96     time2str("%Y%j", $pay_batch->download).
97     sprintf("%-30s", $cust_pay_batch->cust_main->first . ' ' .
98                      $cust_pay_batch->cust_main->last).
99     'E'. # English
100     ' '.
101     sprintf("%-15s", $shortname).
102     'CAD'.
103     ' '.
104     'CAN'.
105     '    '.
106     'N' # no customer optional information follows
107     ;
108 # Note: IAT Address Information and Remittance records are not 
109 # supported. This means you probably can't process payments 
110 # destined to U.S. bank accounts.  If you need this feature, contact 
111 # Freeside Internet Services.
112   },
113   footer => sub {
114     my ($pay_batch, $batchcount, $batchtotal) = @_;
115     sprintf("%06u", $i + 1).
116     'Z'.
117     'TRL'.
118     sprintf("%10s", $client_num).
119     ' ' x 20 .
120     sprintf("%06u", $batchcount).
121     sprintf("%014u", $batchtotal*100).
122     '00' .
123     '000000' . # total number of customer information records
124     ' ' x 84
125     ;
126   },
127 );
128
129 1;
130