import torrus 1.0.9
[freeside.git] / FS / FS / pay_batch / PAP.pm
1 package FS::pay_batch::PAP;
2
3 use strict;
4 use vars qw(@ISA %import_info %export_info $name);
5 use Time::Local 'timelocal';
6 use FS::Conf;
7
8 my $conf;
9 my ($origid, $datacenter, $typecode, $shortname, $longname, $mybank, $myacct);
10
11 $name = 'PAP';
12
13 %import_info = (
14   'filetype'    => 'fixed',
15   'formatre'    => '^(.).{19}(.{4})(.{3})(.{10})(.{6})(.{9})(.{12}).{110}(.{19}).{71}$',
16   'fields'      => [
17     'recordtype',
18     'batchnum',
19     'datacenter',
20     'paid',
21     '_date',
22     'bank',
23     'payinfo',
24     'paybatchnum',
25   ],
26   'hook'        => sub {
27       my $hash = shift;
28       $hash->{'paid'} = sprintf("%.2f", $hash->{'paid'} / 100 );
29       my $tmpdate = timelocal( 0,0,1,1,0,substr($hash->{'_date'}, 0, 3)+2000);
30       $tmpdate += 86400*(substr($hash->{'_date'}, 3, 3)-1) ;
31       $hash->{'_date'} = $tmpdate;
32       $hash->{'payinfo'} = $hash->{'payinfo'} . '@' . $hash->{'bank'};
33   },
34   'approved'    => sub { 1 },
35   'declined'    => sub { 0 },
36 # Why does pay_batch.pm have approved_condition and declined_condition?
37 # It doesn't even try to handle the case of neither condition being met.
38   'end_hook'    => sub {
39       my( $hash, $total) = @_;
40       $total = sprintf("%.2f", $total);
41       my $batch_total = $hash->{'datacenter'}.$hash->{'paid'}.
42                         substr($hash->{'_date'},0,1);          # YUCK!
43       $batch_total = sprintf("%.2f", $batch_total / 100 );
44       return "Our total $total does not match bank total $batch_total!"
45         if $total != $batch_total;
46       '';
47   },
48   'end_condition' => sub {
49       my $hash = shift;
50       $hash->{recordtype} eq 'W';
51   },
52 );
53
54 %export_info = (
55   init => sub {
56     $conf = shift;
57     ($origid,
58      $datacenter,
59      $typecode, 
60      $shortname, 
61      $longname, 
62      $mybank, 
63      $myacct) = $conf->config("batchconfig-PAP");
64   },
65   header => sub { 
66     my $pay_batch = shift;
67     sprintf( "H%10sD%3s%06u%-15s%09u%-12s%04u%19s\n",
68       $origid,
69       $typecode,
70       cdate($pay_batch->download),
71       $shortname,
72       $mybank,
73       $myacct,
74       $pay_batch->batchnum,
75       "" )
76   },
77   row => sub {
78     my ($cust_pay_batch, $pay_batch) = @_;
79     my ($account, $aba) = split('@', $cust_pay_batch->payinfo);
80     sprintf( "D%-23s%06u%-19s%09u%-12s%010.0f\n",
81       $cust_pay_batch->payname,
82       cdate($pay_batch->download),
83       $cust_pay_batch->paybatchnum,
84       $aba,
85       $account,
86       $cust_pay_batch->amount*100 );
87   },
88   footer => sub {
89     my ($pay_batch, $batchcount, $batchtotal) = @_;
90     sprintf( "T%08u%014.0f%57s\n",
91       $batchcount,
92       $batchtotal*100,
93       "" );
94   },
95 );
96
97 sub cdate {
98   my (@date) = localtime(shift);
99   sprintf("%02d%02d%02d", $date[3], $date[4] + 1, $date[5] % 100);
100 }
101
102 1;
103