fix TeleAPI import (what kind of crack was Christopher smoking that he couldn't fix...
[freeside.git] / FS / FS / cdr / aapt.pm
1 package FS::cdr::aapt;
2
3 use strict;
4 use base qw( FS::cdr );
5 use vars qw ( %info );
6 use FS::cdr qw(_cdr_date_parser_maker);
7
8 my %CURRENCY = ( #Table 2.1.3
9   1 => 'AUD',
10   2 => 'USD',
11   3 => 'AUD',
12 );
13
14 my %UNIT_SCALE = ( #Table 2.1.4
15   100 => 1,     # seconds
16   101 => 0.1,   # tenths
17   120 => 60,    # minutes
18   130 => 3600,  # hours
19   #(irrelevant, because we don't yet support these usage types, but still)
20   200 => 1,     # bytes
21   201 => 2**10, # KB
22   202 => 2**20, # MB
23   203 => 2**30, # GB
24   401 => 2**10 * 1000, # "decimal MB"
25   402 => 2**20 * 1000, # "decimal GB"
26   451 => 1,     # Kbps--conveniently the same as our base unit
27   452 => 1000,  # Mbps (decimal)
28 );
29
30 %info = (
31   'name'          => 'AAPT CTOP',
32   'weight'        => 600,
33   'header'        => 1,
34   'type'          => 'fixedlength',
35   'row_callback'  => sub { $DB::single = 1; }, #XXX
36   'parser_opt'    => { trim => 1 },
37   'fixedlength_format' => [qw(
38     record_type:6:1:6
39     transaction_id:20R:7:26
40     product_id:6R:27:32
41     usage_type:6R:33:38
42     id_type:6R:39:44
43     id_value:48R:45:92
44     trans_time:14:93:106
45     sec_time:14:107:120
46     target:24R:121:144
47     origin:24R:145:168
48     rated_units:10R:169:178
49     rated_price:18R:179:196
50     jurisdiction:18R:197:214
51     fnn:18R:215:232
52     foreign_amount:18R:233:250
53     currency:6R:251:256
54     recipient:10R:257:266
55     completion:3R:267:269
56     record_id:22R:270:291
57     raw_units:10R:292:301
58     raw_unittype:6R:302:307
59     rated_unittype:6R:308:313
60     base_amount:18R:314:331
61     second_units:10R:332:341
62     third_units:10R:342:351
63     special1:255:352:606
64     special2:96:607:702
65     service_type:6:703:708
66     sec_id_type:6:709:714
67     sec_id_value:48:715:762
68     unused:238:763:1000
69   )],
70   'import_fields' => [
71     sub {                   # record type
72       my ($cdr, $data, $conf, $param) = @_;
73       $param->{skiprow} = 1 if $data ne 'PWTDET'; # skip non-detail records
74     },
75     '',                     # transaction ID
76     '',                     # product ID (CPRD)
77     'calltypenum',          # usage ID (CUSG)
78     sub {                   # ID type
79       my ($cdr, $data, $conf, $param) = @_;
80       if ($data !~ /^\s*(1|50)$/) {
81         warn "AAPT: service ID type is not telephone number.\n";
82         $param->{skiprow} = 1;
83       }
84     },
85     'charged_party',        # ID value (phone number, if ID type = 1)
86     _cdr_date_parser_maker('startdate'),  # trans date/time
87     '',                     # secondary date (unused?)
88     'dst',                  # Target (B-party)
89     'src',                  # Origin (A-party)
90     'billsec',              # Rated units (may need unit scaling)
91     sub {                   # Amount charged
92       my ($cdr, $data) = @_;
93       $cdr->set('upstream_price', sprintf('%.5f', $data/100));
94     },
95     'dcontext',             # Jurisdiction code; we use dcontext for this
96     '',                     # Full National Number (unused?)
97     '',                     # "Foreign Amount"
98     sub {                   # Currency
99       my ($cdr, $data) = @_;
100       $cdr->set('upstream_currency', $CURRENCY{$data});
101     },
102     '',                     # Reseller account number
103     '',                     # Completion status
104     'uniqueid',             # CTOP Record ID
105     'duration',             # Raw units
106     sub {                   # Raw unit type
107       my ($cdr, $data) = @_;
108       if (exists($UNIT_SCALE{$data})) {
109         $cdr->set('duration',
110           sprintf('%.0f', $cdr->get('duration') * $UNIT_SCALE{$data})
111         );
112       }
113     },
114     sub {                   # Rated unit type
115       my ($cdr, $data) = @_;
116       if (exists($UNIT_SCALE{$data})) {
117         $cdr->set('billsec',
118           sprintf('%.0f', $cdr->get('billsec') * $UNIT_SCALE{$data})
119         );
120       }
121     },
122     # trailing fields we don't care about
123   ], #import_fields
124 );
125
126 1;