Ticket #31847 CIBC batch format
[freeside.git] / FS / FS / pay_batch / CIBC.pm
1 package FS::pay_batch::CIBC;
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 ($origid, $datacenter, $transcode, $shortname, $mybank, $myacct);
10
11 $name = 'CIBC';
12
13 %import_info = (
14   'filetype'    => 'CSV',
15   'fields'      => [],
16   'hook'        => sub { die "Can't import CIBC" },
17   'approved'    => sub { 1 },
18   'declined'    => sub { 0 },
19 );
20
21 %export_info = (
22   init => sub {
23     $conf = shift;
24     ($origid,
25      $datacenter,
26      $transcode, 
27      $shortname, 
28      $mybank, 
29      $myacct) = $conf->config("batchconfig-CIBC");
30   },
31   header => sub { 
32     my $pay_batch = shift;
33     sprintf( "1%2s%05u%-5s%010u%6s%04u%1s%04u%5u%-12u%2s%-15s%1s%3s%4s \n",  #80
34       '',
35       substr(0,5, $origid),
36       '',
37       $origid,
38       time2str('%y%m%d', $pay_batch->download),
39       $pay_batch->batchnum,
40       ' ',
41       '0010',
42       $mybank,
43       $myacct,
44       '',
45       $shortname,
46       ' ',
47       'CAD',
48       '', ) .
49     sprintf( "5%46s%03u%-10s%6s%14s", #80
50       '',
51       $transcode,
52       '           ',
53       time2str('%y%m%d', $pay_batch->download),
54       '               ');
55   },
56   row => sub {
57     my ($cust_pay_batch, $pay_batch) = @_;
58     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
59     my($bankno, $branch);
60     if ( $aba =~ /^0(\d{3})(\d{5})$/ ) { # standard format for Canadian bank ID
61       ($bankno, $branch) = ( $1, $2 );
62     } elsif ( $aba =~ /^(\d{5})\.(\d{3})$/ ) { #how we store branches
63       ($branch, $bankno) = ( $1, $2 );
64     } else {
65       die "invalid branch/routing number '$aba'\n";
66     }
67     sprintf( "6%1s%1s%04u%05u%-12u%5u%10s%-13s%-22s%6s ", #80
68       'D',
69       '',
70       $bankno,
71       $branch,
72       $account,
73       '',
74       $cust_pay_batch->amount * 100,
75       $cust_pay_batch->paybatchnum,
76       $cust_pay_batch->payname,
77       '     ',
78       );
79   },
80   footer => sub {
81     my ($pay_batch, $batchcount, $batchtotal) = @_;
82     sprintf( "7%03u%06f%010s%20s%012s%28s \n", $transcode, $batchcount,'0','',$batchtotal*100,''). #80
83     sprintf( "9%06s%06s%67s", 1, $batchcount,''); #80
84   },
85 );
86
87 1;